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.23 2001/03/22 04:00:32 momjian Exp $
14 #include "access/rmgr.h"
15 #include "access/transam.h"
16 #include "access/xlogdefs.h"
17 #include "utils/pg_crc.h"
21 * Header for each record in XLOG
23 * NOTE: xl_len counts only the rmgr data, not the XLogRecord header,
24 * and also not any backup blocks appended to the record (which are signaled
25 * by xl_info flag bits). The total space needed for an XLOG record is
28 * SizeOfXLogRecord + xl_len + n_backup_blocks * (sizeof(BkpBlock) + BLCKSZ)
30 * rounded up to a MAXALIGN boundary (so that all xlog records start on
31 * MAXALIGN boundaries).
33 typedef struct XLogRecord
35 crc64 xl_crc; /* CRC for this record */
36 XLogRecPtr xl_prev; /* ptr to previous record in log */
37 XLogRecPtr xl_xact_prev; /* ptr to previous record of this xact */
38 TransactionId xl_xid; /* xact id */
39 uint16 xl_len; /* total len of rmgr data */
40 uint8 xl_info; /* flag bits, see below */
41 RmgrId xl_rmid; /* resource manager for this record */
43 /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
47 #define SizeOfXLogRecord MAXALIGN(sizeof(XLogRecord))
48 #define MAXLOGRECSZ 65535 /* the most that'll fit in xl_len */
50 #define XLogRecGetData(record) ((char*) (record) + SizeOfXLogRecord)
53 * XLOG uses only low 4 bits of xl_info. High 4 bits may be used by rmgr.
55 #define XLR_INFO_MASK 0x0F
58 * We support backup of up to 2 disk blocks per XLOG record (could support
59 * more if we cared to dedicate more xl_info bits for this purpose; currently
60 * do not need more than 2 anyway). If we backed up any disk blocks then we
61 * use flag bits in xl_info to signal it.
63 #define XLR_BKP_BLOCK_MASK 0x0C /* all info bits used for bkp
65 #define XLR_MAX_BKP_BLOCKS 2
66 #define XLR_SET_BKP_BLOCK(iblk) (0x08 >> (iblk))
67 #define XLR_BKP_BLOCK_1 XLR_SET_BKP_BLOCK(0) /* 0x08 */
68 #define XLR_BKP_BLOCK_2 XLR_SET_BKP_BLOCK(1) /* 0x04 */
71 * Sometimes we log records which are out of transaction control.
72 * Rmgr may "or" XLOG_NO_TRAN into info passed to XLogInsert to indicate this.
74 #define XLOG_NO_TRAN XLR_INFO_MASK
77 * Header info for a backup block appended to an XLOG record.
79 * Note that the backup block has its own CRC, and is not covered by
80 * the CRC of the XLOG record proper. Also note that we don't attempt
81 * to align either the BkpBlock struct or the block's data.
83 typedef struct BkpBlock
91 * When there is not enough space on current page for whole record, we
92 * continue on the next page with continuation record. (However, the
93 * XLogRecord header will never be split across pages; if there's less than
94 * SizeOfXLogRecord space left at the end of a page, we just waste it.)
96 * Note that xl_rem_len includes backup-block data, unlike xl_len in the
99 typedef struct XLogContRecord
101 uint32 xl_rem_len; /* total len of remaining data for record */
103 /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
107 #define SizeOfXLogContRecord MAXALIGN(sizeof(XLogContRecord))
110 * Each page of XLOG file has a header like this:
112 #define XLOG_PAGE_MAGIC 0xD058 /* can be used as WAL version indicator */
114 typedef struct XLogPageHeaderData
116 uint16 xlp_magic; /* magic value for correctness checks */
117 uint16 xlp_info; /* flag bits, see below */
118 StartUpID xlp_sui; /* StartUpID of first record on page */
119 } XLogPageHeaderData;
121 #define SizeOfXLogPHD MAXALIGN(sizeof(XLogPageHeaderData))
123 typedef XLogPageHeaderData *XLogPageHeader;
125 /* When record crosses page boundary, set this flag in new page's header */
126 #define XLP_FIRST_IS_CONTRECORD 0x0001
127 /* All defined flag bits in xlp_info (used for validity checking of header) */
128 #define XLP_ALL_FLAGS 0x0001
131 * We break each logical log file (xlogid value) into 16Mb segments.
132 * One possible segment at the end of each log file is wasted, to ensure
133 * that we don't have problems representing last-byte-position-plus-1.
135 #define XLogSegSize ((uint32) (16*1024*1024))
136 #define XLogSegsPerFile (((uint32) 0xffffffff) / XLogSegSize)
137 #define XLogFileSize (XLogSegsPerFile * XLogSegSize)
140 * Method table for resource managers.
142 * RmgrTable[] is indexed by RmgrId values (see rmgr.h).
144 typedef struct RmgrData
147 void (*rm_redo) (XLogRecPtr lsn, XLogRecord *rptr);
148 void (*rm_undo) (XLogRecPtr lsn, XLogRecord *rptr);
149 void (*rm_desc) (char *buf, uint8 xl_info, char *rec);
152 extern RmgrData RmgrTable[];
154 /*--------------------
155 * List of these structs is used to pass data to XLogInsert().
157 * If buffer is valid then XLOG will check if buffer must be backed up
158 * (ie, whether this is first change of that page since last checkpoint).
159 * If so, the whole page contents are attached to the XLOG record, and XLOG
160 * sets XLR_BKP_BLOCK_X bit in xl_info. Note that the buffer must be pinned
161 * and locked while this is going on, so that it won't change under us.
162 * NB: when this happens, we do not bother to insert the associated data into
163 * the XLOG record, since we assume it's present in the buffer. Therefore,
164 * rmgr redo routines MUST pay attention to XLR_BKP_BLOCK_X to know what
165 * is actually stored in the XLOG record.
166 *--------------------
168 typedef struct XLogRecData
170 Buffer buffer; /* buffer associated with this data */
173 struct XLogRecData *next;
176 extern StartUpID ThisStartUpID; /* current SUI */
177 extern bool InRecovery;
178 extern XLogRecPtr MyLastRecPtr;
180 /* these variables are GUC parameters related to XLOG */
181 extern int CheckPointSegments;
182 extern int XLOGbuffers;
183 extern int XLOGfiles;
184 extern int XLOG_DEBUG;
185 extern char *XLOG_sync_method;
186 extern const char XLOG_sync_method_default[];
189 extern XLogRecPtr XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata);
190 extern void XLogFlush(XLogRecPtr RecPtr);
192 extern void xlog_redo(XLogRecPtr lsn, XLogRecord *record);
193 extern void xlog_undo(XLogRecPtr lsn, XLogRecord *record);
194 extern void xlog_desc(char *buf, uint8 xl_info, char *rec);
196 extern void UpdateControlFile(void);
197 extern int XLOGShmemSize(void);
198 extern void XLOGShmemInit(void);
199 extern void XLOGPathInit(void);
200 extern void BootStrapXLOG(void);
201 extern void StartupXLOG(void);
202 extern void ShutdownXLOG(void);
203 extern void CreateCheckPoint(bool shutdown);
204 extern void SetThisStartUpID(void);
205 extern void XLogPutNextOid(Oid nextOid);
206 extern void SetRedoRecPtr(void);
207 extern void GetRedoRecPtr(void);
209 /* in storage/ipc/sinval.c, but don't want to declare in sinval.h because
210 * we'd have to include xlog.h into that ...
212 extern XLogRecPtr GetUndoRecPtr(void);
214 extern bool check_xlog_sync_method(const char *method);
215 extern void assign_xlog_sync_method(const char *method);