OSDN Git Service

Merge branch 'pgrex90-base' into pgrex90
[pg-rex/syncrep.git] / src / include / access / xlog.h
1 /*
2  * xlog.h
3  *
4  * PostgreSQL transaction log manager
5  *
6  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.117 2010/09/15 10:35:05 heikki Exp $
10  */
11 #ifndef XLOG_H
12 #define XLOG_H
13
14 #include "access/rmgr.h"
15 #include "access/xlogdefs.h"
16 #include "lib/stringinfo.h"
17 #include "storage/buf.h"
18 #include "utils/pg_crc.h"
19 #include "utils/timestamp.h"
20
21
22 /*
23  * The overall layout of an XLOG record is:
24  *              Fixed-size header (XLogRecord struct)
25  *              rmgr-specific data
26  *              BkpBlock
27  *              backup block data
28  *              BkpBlock
29  *              backup block data
30  *              ...
31  *
32  * where there can be zero to three backup blocks (as signaled by xl_info flag
33  * bits).  XLogRecord structs always start on MAXALIGN boundaries in the WAL
34  * files, and we round up SizeOfXLogRecord so that the rmgr data is also
35  * guaranteed to begin on a MAXALIGN boundary.  However, no padding is added
36  * to align BkpBlock structs or backup block data.
37  *
38  * NOTE: xl_len counts only the rmgr data, not the XLogRecord header,
39  * and also not any backup blocks.      xl_tot_len counts everything.  Neither
40  * length field is rounded up to an alignment boundary.
41  */
42 typedef struct XLogRecord
43 {
44         pg_crc32        xl_crc;                 /* CRC for this record */
45         XLogRecPtr      xl_prev;                /* ptr to previous record in log */
46         TransactionId xl_xid;           /* xact id */
47         uint32          xl_tot_len;             /* total len of entire record */
48         uint32          xl_len;                 /* total len of rmgr data */
49         uint8           xl_info;                /* flag bits, see below */
50         RmgrId          xl_rmid;                /* resource manager for this record */
51
52         /* Depending on MAXALIGN, there are either 2 or 6 wasted bytes here */
53
54         /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
55
56 } XLogRecord;
57
58 #define SizeOfXLogRecord        MAXALIGN(sizeof(XLogRecord))
59
60 #define XLogRecGetData(record)  ((char*) (record) + SizeOfXLogRecord)
61
62 /*
63  * XLOG uses only low 4 bits of xl_info.  High 4 bits may be used by rmgr.
64  */
65 #define XLR_INFO_MASK                   0x0F
66
67 /*
68  * If we backed up any disk blocks with the XLOG record, we use flag bits in
69  * xl_info to signal it.  We support backup of up to 3 disk blocks per XLOG
70  * record.
71  */
72 #define XLR_BKP_BLOCK_MASK              0x0E    /* all info bits used for bkp blocks */
73 #define XLR_MAX_BKP_BLOCKS              3
74 #define XLR_SET_BKP_BLOCK(iblk) (0x08 >> (iblk))
75 #define XLR_BKP_BLOCK_1                 XLR_SET_BKP_BLOCK(0)    /* 0x08 */
76 #define XLR_BKP_BLOCK_2                 XLR_SET_BKP_BLOCK(1)    /* 0x04 */
77 #define XLR_BKP_BLOCK_3                 XLR_SET_BKP_BLOCK(2)    /* 0x02 */
78
79 /*
80  * Bit 0 of xl_info is set if the backed-up blocks could safely be removed
81  * from a compressed version of XLOG (that is, they are backed up only to
82  * prevent partial-page-write problems, and not to ensure consistency of PITR
83  * recovery).  The compression algorithm would need to extract data from the
84  * blocks to create an equivalent non-full-page XLOG record.
85  */
86 #define XLR_BKP_REMOVABLE               0x01
87
88 /* Sync methods */
89 #define SYNC_METHOD_FSYNC               0
90 #define SYNC_METHOD_FDATASYNC   1
91 #define SYNC_METHOD_OPEN                2               /* for O_SYNC */
92 #define SYNC_METHOD_FSYNC_WRITETHROUGH  3
93 #define SYNC_METHOD_OPEN_DSYNC  4               /* for O_DSYNC */
94 extern int      sync_method;
95
96 /*
97  * The rmgr data to be written by XLogInsert() is defined by a chain of
98  * one or more XLogRecData structs.  (Multiple structs would be used when
99  * parts of the source data aren't physically adjacent in memory, or when
100  * multiple associated buffers need to be specified.)
101  *
102  * If buffer is valid then XLOG will check if buffer must be backed up
103  * (ie, whether this is first change of that page since last checkpoint).
104  * If so, the whole page contents are attached to the XLOG record, and XLOG
105  * sets XLR_BKP_BLOCK_X bit in xl_info.  Note that the buffer must be pinned
106  * and exclusive-locked by the caller, so that it won't change under us.
107  * NB: when the buffer is backed up, we DO NOT insert the data pointed to by
108  * this XLogRecData struct into the XLOG record, since we assume it's present
109  * in the buffer.  Therefore, rmgr redo routines MUST pay attention to
110  * XLR_BKP_BLOCK_X to know what is actually stored in the XLOG record.
111  * The i'th XLR_BKP_BLOCK bit corresponds to the i'th distinct buffer
112  * value (ignoring InvalidBuffer) appearing in the rdata chain.
113  *
114  * When buffer is valid, caller must set buffer_std to indicate whether the
115  * page uses standard pd_lower/pd_upper header fields.  If this is true, then
116  * XLOG is allowed to omit the free space between pd_lower and pd_upper from
117  * the backed-up page image.  Note that even when buffer_std is false, the
118  * page MUST have an LSN field as its first eight bytes!
119  *
120  * Note: data can be NULL to indicate no rmgr data associated with this chain
121  * entry.  This can be sensible (ie, not a wasted entry) if buffer is valid.
122  * The implication is that the buffer has been changed by the operation being
123  * logged, and so may need to be backed up, but the change can be redone using
124  * only information already present elsewhere in the XLOG entry.
125  */
126 typedef struct XLogRecData
127 {
128         char       *data;                       /* start of rmgr data to include */
129         uint32          len;                    /* length of rmgr data to include */
130         Buffer          buffer;                 /* buffer associated with data, if any */
131         bool            buffer_std;             /* buffer has standard pd_lower/pd_upper */
132         struct XLogRecData *next;       /* next struct in chain, or NULL */
133 } XLogRecData;
134
135 extern PGDLLIMPORT TimeLineID ThisTimeLineID;   /* current TLI */
136
137 /*
138  * Prior to 8.4, all activity during recovery was carried out by the startup
139  * process. This local variable continues to be used in many parts of the
140  * code to indicate actions taken by RecoveryManagers. Other processes that
141  * potentially perform work during recovery should check RecoveryInProgress().
142  * See XLogCtl notes in xlog.c.
143  */
144 extern bool InRecovery;
145
146 /*
147  * Like InRecovery, standbyState is only valid in the startup process.
148  * In all other processes it will have the value STANDBY_DISABLED (so
149  * InHotStandby will read as FALSE).
150  *
151  * In DISABLED state, we're performing crash recovery or hot standby was
152  * disabled in postgresql.conf.
153  *
154  * In INITIALIZED state, we've run InitRecoveryTransactionEnvironment, but
155  * we haven't yet processed a RUNNING_XACTS or shutdown-checkpoint WAL record
156  * to initialize our master-transaction tracking system.
157  *
158  * When the transaction tracking is initialized, we enter the SNAPSHOT_PENDING
159  * state. The tracked information might still be incomplete, so we can't allow
160  * connections yet, but redo functions must update the in-memory state when
161  * appropriate.
162  *
163  * In SNAPSHOT_READY mode, we have full knowledge of transactions that are
164  * (or were) running in the master at the current WAL location. Snapshots
165  * can be taken, and read-only queries can be run.
166  */
167 typedef enum
168 {
169         STANDBY_DISABLED,
170         STANDBY_INITIALIZED,
171         STANDBY_SNAPSHOT_PENDING,
172         STANDBY_SNAPSHOT_READY
173 } HotStandbyState;
174
175 extern HotStandbyState standbyState;
176
177 #define InHotStandby (standbyState >= STANDBY_SNAPSHOT_PENDING)
178
179 /*
180  * Recovery target type.
181  * Only set during a Point in Time recovery, not when standby_mode = on
182  */
183 typedef enum
184 {
185         RECOVERY_TARGET_UNSET,
186         RECOVERY_TARGET_XID,
187         RECOVERY_TARGET_TIME
188 } RecoveryTargetType;
189
190 extern XLogRecPtr XactLastRecEnd;
191
192 /*
193  * Replication mode. This is used to identify how long transaction
194  * commit should wait for replication.
195  *
196  * REPLICATION_MODE_ASYNC doesn't make transaction commit wait for
197  * replication, i.e., asynchronous replication.
198  *
199  * REPLICATION_MODE_RECV makes transaction commit wait for XLOG
200  * records to be received on the standby.
201  *
202  * REPLICATION_MODE_FSYNC makes transaction commit wait for XLOG
203  * records to be received and fsync'd on the standby.
204  *
205  * REPLICATION_MODE_APPLY makes transaction commit wait for XLOG
206  * records to be received, fsync'd and applied on the standby.
207  */
208 typedef enum ReplicationMode
209 {
210         InvalidReplicationMode = -1,
211         REPLICATION_MODE_ASYNC = 0,
212         REPLICATION_MODE_RECV,
213         REPLICATION_MODE_FSYNC,
214         REPLICATION_MODE_APPLY
215
216         /*
217          * NOTE: if you add a new mode, change MAXREPLICATIONMODE below
218          * and update the ReplicationModeNames array in xlog.c
219          */
220 } ReplicationMode;
221
222 #define MAXREPLICATIONMODE              REPLICATION_MODE_APPLY
223
224 extern const char *ReplicationModeNames[];
225 extern ReplicationMode  rplMode;
226
227 /* these variables are GUC parameters related to XLOG */
228 extern int      CheckPointSegments;
229 extern int      wal_keep_segments;
230 extern int      XLOGbuffers;
231 extern int      XLogArchiveTimeout;
232 extern bool XLogArchiveMode;
233 extern char *XLogArchiveCommand;
234 extern bool EnableHotStandby;
235 extern bool log_checkpoints;
236
237 /* WAL levels */
238 typedef enum WalLevel
239 {
240         WAL_LEVEL_MINIMAL = 0,
241         WAL_LEVEL_ARCHIVE,
242         WAL_LEVEL_HOT_STANDBY
243 } WalLevel;
244 extern int      wal_level;
245
246 #define XLogArchivingActive()   (XLogArchiveMode && wal_level >= WAL_LEVEL_ARCHIVE)
247 #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
248
249 /*
250  * Is WAL-logging necessary for archival or log-shipping, or can we skip
251  * WAL-logging if we fsync() the data before committing instead?
252  */
253 #define XLogIsNeeded() (wal_level >= WAL_LEVEL_ARCHIVE)
254
255 /* Do we need to WAL-log information required only for Hot Standby? */
256 #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_HOT_STANDBY)
257
258 #ifdef WAL_DEBUG
259 extern bool XLOG_DEBUG;
260 #endif
261
262 /*
263  * OR-able request flag bits for checkpoints.  The "cause" bits are used only
264  * for logging purposes.  Note: the flags must be defined so that it's
265  * sensible to OR together request flags arising from different requestors.
266  */
267
268 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
269 #define CHECKPOINT_IS_SHUTDOWN  0x0001  /* Checkpoint is for shutdown */
270 #define CHECKPOINT_END_OF_RECOVERY      0x0002          /* Like shutdown checkpoint,
271                                                                                                  * but issued at end of WAL
272                                                                                                  * recovery */
273 #define CHECKPOINT_IMMEDIATE    0x0004  /* Do it without delays */
274 #define CHECKPOINT_FORCE                0x0008  /* Force even if no activity */
275 /* These are important to RequestCheckpoint */
276 #define CHECKPOINT_WAIT                 0x0010  /* Wait for completion */
277 /* These indicate the cause of a checkpoint request */
278 #define CHECKPOINT_CAUSE_XLOG   0x0020  /* XLOG consumption */
279 #define CHECKPOINT_CAUSE_TIME   0x0040  /* Elapsed time */
280
281 /* Checkpoint statistics */
282 typedef struct CheckpointStatsData
283 {
284         TimestampTz ckpt_start_t;       /* start of checkpoint */
285         TimestampTz ckpt_write_t;       /* start of flushing buffers */
286         TimestampTz ckpt_sync_t;        /* start of fsyncs */
287         TimestampTz ckpt_sync_end_t;    /* end of fsyncs */
288         TimestampTz ckpt_end_t;         /* end of checkpoint */
289
290         int                     ckpt_bufs_written;              /* # of buffers written */
291
292         int                     ckpt_segs_added;        /* # of new xlog segments created */
293         int                     ckpt_segs_removed;              /* # of xlog segments deleted */
294         int                     ckpt_segs_recycled;             /* # of xlog segments recycled */
295 } CheckpointStatsData;
296
297 extern CheckpointStatsData CheckpointStats;
298
299 extern XLogRecPtr XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata);
300 extern void XLogFlush(XLogRecPtr RecPtr);
301 extern void XLogBackgroundFlush(void);
302 extern bool XLogNeedsFlush(XLogRecPtr RecPtr);
303 extern int XLogFileInit(uint32 log, uint32 seg,
304                          bool *use_existent, bool use_lock);
305 extern int      XLogFileOpen(uint32 log, uint32 seg);
306
307
308 extern void XLogGetLastRemoved(uint32 *log, uint32 *seg);
309 extern void XLogSetAsyncXactLSN(XLogRecPtr record);
310
311 extern void RestoreBkpBlocks(XLogRecPtr lsn, XLogRecord *record, bool cleanup);
312
313 extern void xlog_redo(XLogRecPtr lsn, XLogRecord *record);
314 extern void xlog_desc(StringInfo buf, uint8 xl_info, char *rec);
315
316 extern void issue_xlog_fsync(int fd, uint32 log, uint32 seg);
317
318 extern bool RecoveryInProgress(void);
319 extern bool XLogInsertAllowed(void);
320 extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
321
322 extern void UpdateControlFile(void);
323 extern uint64 GetSystemIdentifier(void);
324 extern Size XLOGShmemSize(void);
325 extern void XLOGShmemInit(void);
326 extern void BootStrapXLOG(void);
327 extern void StartupXLOG(void);
328 extern void ShutdownXLOG(int code, Datum arg);
329 extern void InitXLOGAccess(void);
330 extern void CreateCheckPoint(int flags);
331 extern bool CreateRestartPoint(int flags);
332 extern void XLogPutNextOid(Oid nextOid);
333 extern XLogRecPtr GetRedoRecPtr(void);
334 extern XLogRecPtr GetInsertRecPtr(void);
335 extern XLogRecPtr GetFlushRecPtr(void);
336 extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
337 extern TimeLineID GetRecoveryTargetTLI(void);
338
339 extern void HandleStartupProcInterrupts(void);
340 extern void StartupProcessMain(void);
341 extern void WakeupRecovery(void);
342
343 extern ReplicationMode ReplicationModeNameGetValue(char *name);
344
345 #endif   /* XLOG_H */