Minor improvements in backup and recovery:
[PostgreSQL.git] / src / include / access / xlog.h
blob6ec97c0886739ddb6d28c99f280b73db9b99bdc6
1 /*
2 * xlog.h
4 * PostgreSQL transaction log manager
6 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL$
11 #ifndef XLOG_H
12 #define XLOG_H
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"
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 * ...
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.
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.
42 typedef struct XLogRecord
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 */
52 /* Depending on MAXALIGN, there are either 2 or 6 wasted bytes here */
54 /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
56 } XLogRecord;
58 #define SizeOfXLogRecord MAXALIGN(sizeof(XLogRecord))
60 #define XLogRecGetData(record) ((char*) (record) + SizeOfXLogRecord)
63 * XLOG uses only low 4 bits of xl_info. High 4 bits may be used by rmgr.
65 #define XLR_INFO_MASK 0x0F
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.
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 */
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.
86 #define XLR_BKP_REMOVABLE 0x01
88 /* Sync methods */
89 #define SYNC_METHOD_FSYNC 0
90 #define SYNC_METHOD_FDATASYNC 1
91 #define SYNC_METHOD_OPEN 2 /* for O_SYNC and O_DSYNC */
92 #define SYNC_METHOD_FSYNC_WRITETHROUGH 3
93 extern int sync_method;
96 * The rmgr data to be written by XLogInsert() is defined by a chain of
97 * one or more XLogRecData structs. (Multiple structs would be used when
98 * parts of the source data aren't physically adjacent in memory, or when
99 * multiple associated buffers need to be specified.)
101 * If buffer is valid then XLOG will check if buffer must be backed up
102 * (ie, whether this is first change of that page since last checkpoint).
103 * If so, the whole page contents are attached to the XLOG record, and XLOG
104 * sets XLR_BKP_BLOCK_X bit in xl_info. Note that the buffer must be pinned
105 * and exclusive-locked by the caller, so that it won't change under us.
106 * NB: when the buffer is backed up, we DO NOT insert the data pointed to by
107 * this XLogRecData struct into the XLOG record, since we assume it's present
108 * in the buffer. Therefore, rmgr redo routines MUST pay attention to
109 * XLR_BKP_BLOCK_X to know what is actually stored in the XLOG record.
110 * The i'th XLR_BKP_BLOCK bit corresponds to the i'th distinct buffer
111 * value (ignoring InvalidBuffer) appearing in the rdata chain.
113 * When buffer is valid, caller must set buffer_std to indicate whether the
114 * page uses standard pd_lower/pd_upper header fields. If this is true, then
115 * XLOG is allowed to omit the free space between pd_lower and pd_upper from
116 * the backed-up page image. Note that even when buffer_std is false, the
117 * page MUST have an LSN field as its first eight bytes!
119 * Note: data can be NULL to indicate no rmgr data associated with this chain
120 * entry. This can be sensible (ie, not a wasted entry) if buffer is valid.
121 * The implication is that the buffer has been changed by the operation being
122 * logged, and so may need to be backed up, but the change can be redone using
123 * only information already present elsewhere in the XLOG entry.
125 typedef struct XLogRecData
127 char *data; /* start of rmgr data to include */
128 uint32 len; /* length of rmgr data to include */
129 Buffer buffer; /* buffer associated with data, if any */
130 bool buffer_std; /* buffer has standard pd_lower/pd_upper */
131 struct XLogRecData *next; /* next struct in chain, or NULL */
132 } XLogRecData;
134 extern TimeLineID ThisTimeLineID; /* current TLI */
135 extern bool InRecovery;
136 extern XLogRecPtr XactLastRecEnd;
138 /* these variables are GUC parameters related to XLOG */
139 extern int CheckPointSegments;
140 extern int XLOGbuffers;
141 extern bool XLogArchiveMode;
142 extern char *XLogArchiveCommand;
143 extern int XLogArchiveTimeout;
144 extern char *XLOG_sync_method;
145 extern const char XLOG_sync_method_default[];
146 extern bool log_checkpoints;
148 #define XLogArchivingActive() (XLogArchiveMode)
149 #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
151 #ifdef WAL_DEBUG
152 extern bool XLOG_DEBUG;
153 #endif
156 * OR-able request flag bits for checkpoints. The "cause" bits are used only
157 * for logging purposes. Note: the flags must be defined so that it's
158 * sensible to OR together request flags arising from different requestors.
161 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
162 #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
163 #define CHECKPOINT_IMMEDIATE 0x0002 /* Do it without delays */
164 #define CHECKPOINT_FORCE 0x0004 /* Force even if no activity */
165 /* These are important to RequestCheckpoint */
166 #define CHECKPOINT_WAIT 0x0008 /* Wait for completion */
167 /* These indicate the cause of a checkpoint request */
168 #define CHECKPOINT_CAUSE_XLOG 0x0010 /* XLOG consumption */
169 #define CHECKPOINT_CAUSE_TIME 0x0020 /* Elapsed time */
171 /* Checkpoint statistics */
172 typedef struct CheckpointStatsData
174 TimestampTz ckpt_start_t; /* start of checkpoint */
175 TimestampTz ckpt_write_t; /* start of flushing buffers */
176 TimestampTz ckpt_sync_t; /* start of fsyncs */
177 TimestampTz ckpt_sync_end_t; /* end of fsyncs */
178 TimestampTz ckpt_end_t; /* end of checkpoint */
180 int ckpt_bufs_written; /* # of buffers written */
182 int ckpt_segs_added; /* # of new xlog segments created */
183 int ckpt_segs_removed; /* # of xlog segments deleted */
184 int ckpt_segs_recycled; /* # of xlog segments recycled */
185 } CheckpointStatsData;
187 extern CheckpointStatsData CheckpointStats;
190 extern XLogRecPtr XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata);
191 extern void XLogFlush(XLogRecPtr RecPtr);
192 extern void XLogBackgroundFlush(void);
193 extern void XLogAsyncCommitFlush(void);
194 extern bool XLogNeedsFlush(XLogRecPtr RecPtr);
196 extern void XLogSetAsyncCommitLSN(XLogRecPtr record);
198 extern void xlog_redo(XLogRecPtr lsn, XLogRecord *record);
199 extern void xlog_desc(StringInfo buf, uint8 xl_info, char *rec);
201 extern void UpdateControlFile(void);
202 extern Size XLOGShmemSize(void);
203 extern void XLOGShmemInit(void);
204 extern void BootStrapXLOG(void);
205 extern void StartupXLOG(void);
206 extern void ShutdownXLOG(int code, Datum arg);
207 extern void InitXLOGAccess(void);
208 extern void CreateCheckPoint(int flags);
209 extern void XLogPutNextOid(Oid nextOid);
210 extern XLogRecPtr GetRedoRecPtr(void);
211 extern XLogRecPtr GetInsertRecPtr(void);
212 extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
214 #endif /* XLOG_H */