]> granicus.if.org Git - postgresql/blob - src/include/access/xlog.h
Make the server track an 'XID epoch', that is, maintain higher-order bits
[postgresql] / src / include / access / xlog.h
1 /*
2  * xlog.h
3  *
4  * PostgreSQL transaction log manager
5  *
6  * Portions Copyright (c) 1996-2006, 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.74 2006/08/21 16:16:31 tgl 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/pg_crc.h"
19
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 three 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         pg_crc32        xl_crc;                 /* CRC for this record */
44         XLogRecPtr      xl_prev;                /* ptr to previous record in log */
45         TransactionId xl_xid;           /* xact id */
46         uint32          xl_tot_len;             /* total len of entire record */
47         uint32          xl_len;                 /* total len of rmgr data */
48         uint8           xl_info;                /* flag bits, see below */
49         RmgrId          xl_rmid;                /* resource manager for this record */
50
51         /* Depending on MAXALIGN, there are either 2 or 6 wasted bytes here */
52
53         /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
54
55 } XLogRecord;
56
57 #define SizeOfXLogRecord        MAXALIGN(sizeof(XLogRecord))
58
59 #define XLogRecGetData(record)  ((char*) (record) + SizeOfXLogRecord)
60
61 /*
62  * XLOG uses only low 4 bits of xl_info.  High 4 bits may be used by rmgr.
63  */
64 #define XLR_INFO_MASK                   0x0F
65
66 /*
67  * If we backed up any disk blocks with the XLOG record, we use flag bits in
68  * xl_info to signal it.  We support backup of up to 3 disk blocks per XLOG
69  * record.      (Could support 4 if we cared to dedicate all the xl_info bits for
70  * this purpose; currently bit 0 of xl_info is unused and available.)
71  */
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 */
78
79 /*
80  * Sometimes we log records which are out of transaction control.
81  * Rmgr may "or" XLOG_NO_TRAN into info passed to XLogInsert to indicate this.
82  */
83 #define XLOG_NO_TRAN                    XLR_INFO_MASK
84
85 /* Sync methods */
86 #define SYNC_METHOD_FSYNC               0
87 #define SYNC_METHOD_FDATASYNC   1
88 #define SYNC_METHOD_OPEN                2               /* for O_SYNC and O_DSYNC */
89 #define SYNC_METHOD_FSYNC_WRITETHROUGH  3
90 extern int      sync_method;
91
92 /*
93  * The rmgr data to be written by XLogInsert() is defined by a chain of
94  * one or more XLogRecData structs.  (Multiple structs would be used when
95  * parts of the source data aren't physically adjacent in memory, or when
96  * multiple associated buffers need to be specified.)
97  *
98  * If buffer is valid then XLOG will check if buffer must be backed up
99  * (ie, whether this is first change of that page since last checkpoint).
100  * If so, the whole page contents are attached to the XLOG record, and XLOG
101  * sets XLR_BKP_BLOCK_X bit in xl_info.  Note that the buffer must be pinned
102  * and exclusive-locked by the caller, so that it won't change under us.
103  * NB: when the buffer is backed up, we DO NOT insert the data pointed to by
104  * this XLogRecData struct into the XLOG record, since we assume it's present
105  * in the buffer.  Therefore, rmgr redo routines MUST pay attention to
106  * XLR_BKP_BLOCK_X to know what is actually stored in the XLOG record.
107  * The i'th XLR_BKP_BLOCK bit corresponds to the i'th distinct buffer
108  * value (ignoring InvalidBuffer) appearing in the rdata chain.
109  *
110  * When buffer is valid, caller must set buffer_std to indicate whether the
111  * page uses standard pd_lower/pd_upper header fields.  If this is true, then
112  * XLOG is allowed to omit the free space between pd_lower and pd_upper from
113  * the backed-up page image.  Note that even when buffer_std is false, the
114  * page MUST have an LSN field as its first eight bytes!
115  *
116  * Note: data can be NULL to indicate no rmgr data associated with this chain
117  * entry.  This can be sensible (ie, not a wasted entry) if buffer is valid.
118  * The implication is that the buffer has been changed by the operation being
119  * logged, and so may need to be backed up, but the change can be redone using
120  * only information already present elsewhere in the XLOG entry.
121  */
122 typedef struct XLogRecData
123 {
124         char       *data;                       /* start of rmgr data to include */
125         uint32          len;                    /* length of rmgr data to include */
126         Buffer          buffer;                 /* buffer associated with data, if any */
127         bool            buffer_std;             /* buffer has standard pd_lower/pd_upper */
128         struct XLogRecData *next;       /* next struct in chain, or NULL */
129 } XLogRecData;
130
131 extern TimeLineID ThisTimeLineID;               /* current TLI */
132 extern bool InRecovery;
133 extern XLogRecPtr MyLastRecPtr;
134 extern bool MyXactMadeXLogEntry;
135 extern bool MyXactMadeTempRelUpdate;
136 extern XLogRecPtr ProcLastRecEnd;
137
138 /* these variables are GUC parameters related to XLOG */
139 extern int      CheckPointSegments;
140 extern int      XLOGbuffers;
141 extern char *XLogArchiveCommand;
142 extern int      XLogArchiveTimeout;
143 extern char *XLOG_sync_method;
144 extern const char XLOG_sync_method_default[];
145
146 #define XLogArchivingActive()   (XLogArchiveCommand[0] != '\0')
147
148 #ifdef WAL_DEBUG
149 extern bool XLOG_DEBUG;
150 #endif
151
152 extern XLogRecPtr XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata);
153 extern void XLogFlush(XLogRecPtr RecPtr);
154
155 extern void xlog_redo(XLogRecPtr lsn, XLogRecord *record);
156 extern void xlog_desc(StringInfo buf, uint8 xl_info, char *rec);
157
158 extern void UpdateControlFile(void);
159 extern Size XLOGShmemSize(void);
160 extern void XLOGShmemInit(void);
161 extern void BootStrapXLOG(void);
162 extern void StartupXLOG(void);
163 extern void ShutdownXLOG(int code, Datum arg);
164 extern void InitXLOGAccess(void);
165 extern void CreateCheckPoint(bool shutdown, bool force);
166 extern void XLogPutNextOid(Oid nextOid);
167 extern XLogRecPtr GetRedoRecPtr(void);
168 extern TransactionId GetRecentNextXid(void);
169 extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
170
171 #endif   /* XLOG_H */