Header And Logo

PostgreSQL
| The world's most advanced open source database.

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xlog.h
Go to the documentation of this file.
1 /*
2  * xlog.h
3  *
4  * PostgreSQL transaction log manager
5  *
6  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/access/xlog.h
10  */
11 #ifndef XLOG_H
12 #define XLOG_H
13 
14 #include "access/rmgr.h"
15 #include "access/xlogdefs.h"
16 #include "datatype/timestamp.h"
17 #include "lib/stringinfo.h"
18 #include "storage/buf.h"
19 #include "utils/pg_crc.h"
20 
21 /*
22  * The overall layout of an XLOG record is:
23  * Fixed-size header (XLogRecord struct)
24  * rmgr-specific data
25  * BkpBlock
26  * backup block data
27  * BkpBlock
28  * backup block data
29  * ...
30  *
31  * where there can be zero to four backup blocks (as signaled by xl_info flag
32  * bits). XLogRecord structs always start on MAXALIGN boundaries in the WAL
33  * files, and we round up SizeOfXLogRecord so that the rmgr data is also
34  * guaranteed to begin on a MAXALIGN boundary. However, no padding is added
35  * to align BkpBlock structs or backup block data.
36  *
37  * NOTE: xl_len counts only the rmgr data, not the XLogRecord header,
38  * and also not any backup blocks. xl_tot_len counts everything. Neither
39  * length field is rounded up to an alignment boundary.
40  */
41 typedef struct XLogRecord
42 {
43  uint32 xl_tot_len; /* total len of entire record */
44  TransactionId xl_xid; /* xact id */
45  uint32 xl_len; /* total len of rmgr data */
46  uint8 xl_info; /* flag bits, see below */
47  RmgrId xl_rmid; /* resource manager for this record */
48  /* 2 bytes of padding here, initialize to zero */
49  XLogRecPtr xl_prev; /* ptr to previous record in log */
50  pg_crc32 xl_crc; /* CRC for this record */
51 
52  /* If MAXALIGN==8, there are 4 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 4 disk blocks per XLOG
70  * record.
71  */
72 #define XLR_BKP_BLOCK_MASK 0x0F /* all info bits used for bkp blocks */
73 #define XLR_MAX_BKP_BLOCKS 4
74 #define XLR_BKP_BLOCK(iblk) (0x08 >> (iblk)) /* iblk in 0..3 */
75 
76 /* Sync methods */
77 #define SYNC_METHOD_FSYNC 0
78 #define SYNC_METHOD_FDATASYNC 1
79 #define SYNC_METHOD_OPEN 2 /* for O_SYNC */
80 #define SYNC_METHOD_FSYNC_WRITETHROUGH 3
81 #define SYNC_METHOD_OPEN_DSYNC 4 /* for O_DSYNC */
82 extern int sync_method;
83 
84 /*
85  * The rmgr data to be written by XLogInsert() is defined by a chain of
86  * one or more XLogRecData structs. (Multiple structs would be used when
87  * parts of the source data aren't physically adjacent in memory, or when
88  * multiple associated buffers need to be specified.)
89  *
90  * If buffer is valid then XLOG will check if buffer must be backed up
91  * (ie, whether this is first change of that page since last checkpoint).
92  * If so, the whole page contents are attached to the XLOG record, and XLOG
93  * sets XLR_BKP_BLOCK(N) bit in xl_info. Note that the buffer must be pinned
94  * and exclusive-locked by the caller, so that it won't change under us.
95  * NB: when the buffer is backed up, we DO NOT insert the data pointed to by
96  * this XLogRecData struct into the XLOG record, since we assume it's present
97  * in the buffer. Therefore, rmgr redo routines MUST pay attention to
98  * XLR_BKP_BLOCK(N) to know what is actually stored in the XLOG record.
99  * The N'th XLR_BKP_BLOCK bit corresponds to the N'th distinct buffer
100  * value (ignoring InvalidBuffer) appearing in the rdata chain.
101  *
102  * When buffer is valid, caller must set buffer_std to indicate whether the
103  * page uses standard pd_lower/pd_upper header fields. If this is true, then
104  * XLOG is allowed to omit the free space between pd_lower and pd_upper from
105  * the backed-up page image. Note that even when buffer_std is false, the
106  * page MUST have an LSN field as its first eight bytes!
107  *
108  * Note: data can be NULL to indicate no rmgr data associated with this chain
109  * entry. This can be sensible (ie, not a wasted entry) if buffer is valid.
110  * The implication is that the buffer has been changed by the operation being
111  * logged, and so may need to be backed up, but the change can be redone using
112  * only information already present elsewhere in the XLOG entry.
113  */
114 typedef struct XLogRecData
115 {
116  char *data; /* start of rmgr data to include */
117  uint32 len; /* length of rmgr data to include */
118  Buffer buffer; /* buffer associated with data, if any */
119  bool buffer_std; /* buffer has standard pd_lower/pd_upper */
120  struct XLogRecData *next; /* next struct in chain, or NULL */
121 } XLogRecData;
122 
123 extern PGDLLIMPORT TimeLineID ThisTimeLineID; /* current TLI */
124 
125 /*
126  * Prior to 8.4, all activity during recovery was carried out by the startup
127  * process. This local variable continues to be used in many parts of the
128  * code to indicate actions taken by RecoveryManagers. Other processes that
129  * potentially perform work during recovery should check RecoveryInProgress().
130  * See XLogCtl notes in xlog.c.
131  */
132 extern bool InRecovery;
133 
134 /*
135  * Like InRecovery, standbyState is only valid in the startup process.
136  * In all other processes it will have the value STANDBY_DISABLED (so
137  * InHotStandby will read as FALSE).
138  *
139  * In DISABLED state, we're performing crash recovery or hot standby was
140  * disabled in postgresql.conf.
141  *
142  * In INITIALIZED state, we've run InitRecoveryTransactionEnvironment, but
143  * we haven't yet processed a RUNNING_XACTS or shutdown-checkpoint WAL record
144  * to initialize our master-transaction tracking system.
145  *
146  * When the transaction tracking is initialized, we enter the SNAPSHOT_PENDING
147  * state. The tracked information might still be incomplete, so we can't allow
148  * connections yet, but redo functions must update the in-memory state when
149  * appropriate.
150  *
151  * In SNAPSHOT_READY mode, we have full knowledge of transactions that are
152  * (or were) running in the master at the current WAL location. Snapshots
153  * can be taken, and read-only queries can be run.
154  */
155 typedef enum
156 {
162 
164 
165 #define InHotStandby (standbyState >= STANDBY_SNAPSHOT_PENDING)
166 
167 /*
168  * Recovery target type.
169  * Only set during a Point in Time recovery, not when standby_mode = on
170  */
171 typedef enum
172 {
178 
180 
181 extern bool reachedConsistency;
182 
183 /* these variables are GUC parameters related to XLOG */
184 extern int CheckPointSegments;
185 extern int wal_keep_segments;
186 extern int XLOGbuffers;
187 extern int XLogArchiveTimeout;
188 extern bool XLogArchiveMode;
189 extern char *XLogArchiveCommand;
190 extern bool EnableHotStandby;
191 extern bool fullPageWrites;
192 extern bool log_checkpoints;
193 
194 /* WAL levels */
195 typedef enum WalLevel
196 {
200 } WalLevel;
201 extern int wal_level;
202 
203 #define XLogArchivingActive() (XLogArchiveMode && wal_level >= WAL_LEVEL_ARCHIVE)
204 #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
205 
206 /*
207  * Is WAL-logging necessary for archival or log-shipping, or can we skip
208  * WAL-logging if we fsync() the data before committing instead?
209  */
210 #define XLogIsNeeded() (wal_level >= WAL_LEVEL_ARCHIVE)
211 
212 /* Do we need to WAL-log information required only for Hot Standby? */
213 #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_HOT_STANDBY)
214 
215 #ifdef WAL_DEBUG
216 extern bool XLOG_DEBUG;
217 #endif
218 
219 /*
220  * OR-able request flag bits for checkpoints. The "cause" bits are used only
221  * for logging purposes. Note: the flags must be defined so that it's
222  * sensible to OR together request flags arising from different requestors.
223  */
224 
225 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
226 #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
227 #define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint,
228  * but issued at end of WAL
229  * recovery */
230 #define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */
231 #define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
232 /* These are important to RequestCheckpoint */
233 #define CHECKPOINT_WAIT 0x0010 /* Wait for completion */
234 /* These indicate the cause of a checkpoint request */
235 #define CHECKPOINT_CAUSE_XLOG 0x0020 /* XLOG consumption */
236 #define CHECKPOINT_CAUSE_TIME 0x0040 /* Elapsed time */
238 /* Checkpoint statistics */
239 typedef struct CheckpointStatsData
240 {
241  TimestampTz ckpt_start_t; /* start of checkpoint */
242  TimestampTz ckpt_write_t; /* start of flushing buffers */
243  TimestampTz ckpt_sync_t; /* start of fsyncs */
244  TimestampTz ckpt_sync_end_t; /* end of fsyncs */
245  TimestampTz ckpt_end_t; /* end of checkpoint */
246 
247  int ckpt_bufs_written; /* # of buffers written */
249  int ckpt_segs_added; /* # of new xlog segments created */
250  int ckpt_segs_removed; /* # of xlog segments deleted */
251  int ckpt_segs_recycled; /* # of xlog segments recycled */
253  int ckpt_sync_rels; /* # of relations synced */
254  uint64 ckpt_longest_sync; /* Longest sync for one relation */
255  uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
256  * times, which is not necessarily the
257  * same as the total elapsed time for
258  * the entire sync phase. */
260 
262 
263 extern XLogRecPtr XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata);
264 extern void XLogFlush(XLogRecPtr RecPtr);
265 extern bool XLogBackgroundFlush(void);
266 extern bool XLogNeedsFlush(XLogRecPtr RecPtr);
267 extern int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock);
268 extern int XLogFileOpen(XLogSegNo segno);
269 
271 
272 extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
273 extern void XLogSetAsyncXactLSN(XLogRecPtr record);
274 
275 extern Buffer RestoreBackupBlock(XLogRecPtr lsn, XLogRecord *record,
276  int block_index,
277  bool get_cleanup_lock, bool keep_buffer);
278 
279 extern void xlog_redo(XLogRecPtr lsn, XLogRecord *record);
280 extern void xlog_desc(StringInfo buf, uint8 xl_info, char *rec);
281 
282 extern void issue_xlog_fsync(int fd, XLogSegNo segno);
283 
284 extern bool RecoveryInProgress(void);
285 extern bool HotStandbyActive(void);
286 extern bool XLogInsertAllowed(void);
287 extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
288 extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
289 extern XLogRecPtr GetXLogInsertRecPtr(void);
290 extern XLogRecPtr GetXLogWriteRecPtr(void);
291 extern bool RecoveryIsPaused(void);
292 extern void SetRecoveryPause(bool recoveryPause);
293 extern TimestampTz GetLatestXTime(void);
295 extern char *XLogFileNameP(TimeLineID tli, XLogSegNo segno);
296 
297 extern void UpdateControlFile(void);
298 extern uint64 GetSystemIdentifier(void);
299 extern bool DataChecksumsEnabled(void);
301 extern Size XLOGShmemSize(void);
302 extern void XLOGShmemInit(void);
303 extern void BootStrapXLOG(void);
304 extern void StartupXLOG(void);
305 extern void ShutdownXLOG(int code, Datum arg);
306 extern void InitXLOGAccess(void);
307 extern void CreateCheckPoint(int flags);
308 extern bool CreateRestartPoint(int flags);
309 extern void XLogPutNextOid(Oid nextOid);
310 extern XLogRecPtr XLogRestorePoint(const char *rpName);
311 extern void UpdateFullPageWrites(void);
312 extern XLogRecPtr GetRedoRecPtr(void);
313 extern XLogRecPtr GetInsertRecPtr(void);
314 extern XLogRecPtr GetFlushRecPtr(void);
315 extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
316 
317 extern bool CheckPromoteSignal(void);
318 extern void WakeupRecovery(void);
319 extern void SetWalWriterSleeping(bool sleeping);
320 
321 /*
322  * Starting/stopping a base backup
323  */
324 extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast,
325  TimeLineID *starttli_p, char **labelfile);
326 extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive,
327  TimeLineID *stoptli_p);
328 extern void do_pg_abort_backup(void);
330 /* File path names (all relative to $PGDATA) */
331 #define BACKUP_LABEL_FILE "backup_label"
332 #define BACKUP_LABEL_OLD "backup_label.old"
333 
334 #endif /* XLOG_H */