OSDN Git Service

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