4 * PostgreSQL transaction log manager
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $Id: xlog.h,v 1.30 2002/05/17 01:19:19 tgl Exp $
14 #include "access/rmgr.h"
15 #include "access/transam.h"
16 #include "access/xlogdefs.h"
17 #include "storage/bufmgr.h"
18 #include "utils/pg_crc.h"
22 * Header for each record in XLOG
24 * NOTE: xl_len counts only the rmgr data, not the XLogRecord header,
25 * and also not any backup blocks appended to the record (which are signaled
26 * by xl_info flag bits). The total space needed for an XLOG record is
29 * SizeOfXLogRecord + xl_len + n_backup_blocks * (sizeof(BkpBlock) + BLCKSZ)
31 * rounded up to a MAXALIGN boundary (so that all xlog records start on
32 * MAXALIGN boundaries).
34 typedef struct XLogRecord
36 crc64 xl_crc; /* CRC for this record */
37 XLogRecPtr xl_prev; /* ptr to previous record in log */
38 XLogRecPtr xl_xact_prev; /* ptr to previous record of this xact */
39 TransactionId xl_xid; /* xact id */
40 uint16 xl_len; /* total len of rmgr data */
41 uint8 xl_info; /* flag bits, see below */
42 RmgrId xl_rmid; /* resource manager for this record */
44 /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
48 #define SizeOfXLogRecord MAXALIGN(sizeof(XLogRecord))
49 #define MAXLOGRECSZ 65535 /* the most that'll fit in xl_len */
51 #define XLogRecGetData(record) ((char*) (record) + SizeOfXLogRecord)
54 * XLOG uses only low 4 bits of xl_info. High 4 bits may be used by rmgr.
56 #define XLR_INFO_MASK 0x0F
59 * We support backup of up to 2 disk blocks per XLOG record (could support
60 * more if we cared to dedicate more xl_info bits for this purpose; currently
61 * do not need more than 2 anyway). If we backed up any disk blocks then we
62 * use flag bits in xl_info to signal it.
64 #define XLR_BKP_BLOCK_MASK 0x0C /* all info bits used for bkp
66 #define XLR_MAX_BKP_BLOCKS 2
67 #define XLR_SET_BKP_BLOCK(iblk) (0x08 >> (iblk))
68 #define XLR_BKP_BLOCK_1 XLR_SET_BKP_BLOCK(0) /* 0x08 */
69 #define XLR_BKP_BLOCK_2 XLR_SET_BKP_BLOCK(1) /* 0x04 */
72 * Sometimes we log records which are out of transaction control.
73 * Rmgr may "or" XLOG_NO_TRAN into info passed to XLogInsert to indicate this.
75 #define XLOG_NO_TRAN XLR_INFO_MASK
78 * Header info for a backup block appended to an XLOG record.
80 * Note that the backup block has its own CRC, and is not covered by
81 * the CRC of the XLOG record proper. Also note that we don't attempt
82 * to align either the BkpBlock struct or the block's data.
84 typedef struct BkpBlock
92 * When there is not enough space on current page for whole record, we
93 * continue on the next page with continuation record. (However, the
94 * XLogRecord header will never be split across pages; if there's less than
95 * SizeOfXLogRecord space left at the end of a page, we just waste it.)
97 * Note that xl_rem_len includes backup-block data, unlike xl_len in the
100 typedef struct XLogContRecord
102 uint32 xl_rem_len; /* total len of remaining data for record */
104 /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
108 #define SizeOfXLogContRecord MAXALIGN(sizeof(XLogContRecord))
111 * Each page of XLOG file has a header like this:
113 #define XLOG_PAGE_MAGIC 0xD059 /* can be used as WAL version indicator */
115 typedef struct XLogPageHeaderData
117 uint16 xlp_magic; /* magic value for correctness checks */
118 uint16 xlp_info; /* flag bits, see below */
119 StartUpID xlp_sui; /* StartUpID of first record on page */
120 XLogRecPtr xlp_pageaddr; /* XLOG address of this page */
121 } XLogPageHeaderData;
123 #define SizeOfXLogPHD MAXALIGN(sizeof(XLogPageHeaderData))
125 typedef XLogPageHeaderData *XLogPageHeader;
127 /* When record crosses page boundary, set this flag in new page's header */
128 #define XLP_FIRST_IS_CONTRECORD 0x0001
129 /* All defined flag bits in xlp_info (used for validity checking of header) */
130 #define XLP_ALL_FLAGS 0x0001
133 * We break each logical log file (xlogid value) into 16Mb segments.
134 * One possible segment at the end of each log file is wasted, to ensure
135 * that we don't have problems representing last-byte-position-plus-1.
137 #define XLogSegSize ((uint32) (16*1024*1024))
138 #define XLogSegsPerFile (((uint32) 0xffffffff) / XLogSegSize)
139 #define XLogFileSize (XLogSegsPerFile * XLogSegSize)
142 * Method table for resource managers.
144 * RmgrTable[] is indexed by RmgrId values (see rmgr.h).
146 typedef struct RmgrData
149 void (*rm_redo) (XLogRecPtr lsn, XLogRecord *rptr);
150 void (*rm_undo) (XLogRecPtr lsn, XLogRecord *rptr);
151 void (*rm_desc) (char *buf, uint8 xl_info, char *rec);
154 extern RmgrData RmgrTable[];
156 /*--------------------
157 * List of these structs is used to pass data to XLogInsert().
159 * If buffer is valid then XLOG will check if buffer must be backed up
160 * (ie, whether this is first change of that page since last checkpoint).
161 * If so, the whole page contents are attached to the XLOG record, and XLOG
162 * sets XLR_BKP_BLOCK_X bit in xl_info. Note that the buffer must be pinned
163 * and locked while this is going on, so that it won't change under us.
164 * NB: when this happens, we do not bother to insert the associated data into
165 * the XLOG record, since we assume it's present in the buffer. Therefore,
166 * rmgr redo routines MUST pay attention to XLR_BKP_BLOCK_X to know what
167 * is actually stored in the XLOG record.
168 *--------------------
170 typedef struct XLogRecData
172 Buffer buffer; /* buffer associated with this data */
175 struct XLogRecData *next;
178 extern StartUpID ThisStartUpID; /* current SUI */
179 extern bool InRecovery;
180 extern XLogRecPtr MyLastRecPtr;
181 extern bool MyXactMadeXLogEntry;
182 extern XLogRecPtr ProcLastRecEnd;
184 /* these variables are GUC parameters related to XLOG */
185 extern int CheckPointSegments;
186 extern int XLOGbuffers;
187 extern int XLOGfiles;
188 extern int XLOG_DEBUG;
189 extern char *XLOG_sync_method;
190 extern const char XLOG_sync_method_default[];
193 extern XLogRecPtr XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata);
194 extern void XLogFlush(XLogRecPtr RecPtr);
196 extern void xlog_redo(XLogRecPtr lsn, XLogRecord *record);
197 extern void xlog_undo(XLogRecPtr lsn, XLogRecord *record);
198 extern void xlog_desc(char *buf, uint8 xl_info, char *rec);
200 extern void UpdateControlFile(void);
201 extern int XLOGShmemSize(void);
202 extern void XLOGShmemInit(void);
203 extern void XLOGPathInit(void);
204 extern void BootStrapXLOG(void);
205 extern void StartupXLOG(void);
206 extern void ShutdownXLOG(void);
207 extern void CreateCheckPoint(bool shutdown);
208 extern void SetThisStartUpID(void);
209 extern void XLogPutNextOid(Oid nextOid);
210 extern void SetSavedRedoRecPtr(void);
211 extern void GetSavedRedoRecPtr(void);
212 extern XLogRecPtr GetRedoRecPtr(void);
214 /* in storage/ipc/sinval.c, but don't want to declare in sinval.h because
215 * we'd have to include xlog.h into that ...
217 extern XLogRecPtr GetUndoRecPtr(void);
219 extern const char *assign_xlog_sync_method(const char *method,
220 bool doit, bool interactive);