]> granicus.if.org Git - postgresql/blob - src/include/access/xlog.h
41a8d84dade4a667df4a8fffd5ef4904f22f3983
[postgresql] / src / include / access / xlog.h
1 /*
2  * xlog.h
3  *
4  * PostgreSQL transaction log manager
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $Id: xlog.h,v 1.23 2001/03/22 04:00:32 momjian Exp $
10  */
11 #ifndef XLOG_H
12 #define XLOG_H
13
14 #include "access/rmgr.h"
15 #include "access/transam.h"
16 #include "access/xlogdefs.h"
17 #include "utils/pg_crc.h"
18
19
20 /*
21  * Header for each record in XLOG
22  *
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
26  * really:
27  *
28  * SizeOfXLogRecord + xl_len + n_backup_blocks * (sizeof(BkpBlock) + BLCKSZ)
29  *
30  * rounded up to a MAXALIGN boundary (so that all xlog records start on
31  * MAXALIGN boundaries).
32  */
33 typedef struct XLogRecord
34 {
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 */
42
43         /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
44
45 } XLogRecord;
46
47 #define SizeOfXLogRecord        MAXALIGN(sizeof(XLogRecord))
48 #define MAXLOGRECSZ                     65535           /* the most that'll fit in xl_len */
49
50 #define XLogRecGetData(record)  ((char*) (record) + SizeOfXLogRecord)
51
52 /*
53  * XLOG uses only low 4 bits of xl_info.  High 4 bits may be used by rmgr.
54  */
55 #define XLR_INFO_MASK                   0x0F
56
57 /*
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.
62  */
63 #define XLR_BKP_BLOCK_MASK              0x0C    /* all info bits used for bkp
64                                                                                  * blocks */
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 */
69
70 /*
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.
73  */
74 #define XLOG_NO_TRAN                    XLR_INFO_MASK
75
76 /*
77  * Header info for a backup block appended to an XLOG record.
78  *
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.
82  */
83 typedef struct BkpBlock
84 {
85         crc64           crc;
86         RelFileNode node;
87         BlockNumber block;
88 } BkpBlock;
89
90 /*
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.)
95  *
96  * Note that xl_rem_len includes backup-block data, unlike xl_len in the
97  * initial header.
98  */
99 typedef struct XLogContRecord
100 {
101         uint32          xl_rem_len;             /* total len of remaining data for record */
102
103         /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
104
105 }                       XLogContRecord;
106
107 #define SizeOfXLogContRecord    MAXALIGN(sizeof(XLogContRecord))
108
109 /*
110  * Each page of XLOG file has a header like this:
111  */
112 #define XLOG_PAGE_MAGIC 0xD058  /* can be used as WAL version indicator */
113
114 typedef struct XLogPageHeaderData
115 {
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;
120
121 #define SizeOfXLogPHD   MAXALIGN(sizeof(XLogPageHeaderData))
122
123 typedef XLogPageHeaderData *XLogPageHeader;
124
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
129
130 /*
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.
134  */
135 #define XLogSegSize             ((uint32) (16*1024*1024))
136 #define XLogSegsPerFile (((uint32) 0xffffffff) / XLogSegSize)
137 #define XLogFileSize    (XLogSegsPerFile * XLogSegSize)
138
139 /*
140  * Method table for resource managers.
141  *
142  * RmgrTable[] is indexed by RmgrId values (see rmgr.h).
143  */
144 typedef struct RmgrData
145 {
146         char       *rm_name;
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);
150 } RmgrData;
151
152 extern RmgrData RmgrTable[];
153
154 /*--------------------
155  * List of these structs is used to pass data to XLogInsert().
156  *
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  *--------------------
167  */
168 typedef struct XLogRecData
169 {
170         Buffer          buffer;                 /* buffer associated with this data */
171         char       *data;
172         uint32          len;
173         struct XLogRecData *next;
174 } XLogRecData;
175
176 extern StartUpID ThisStartUpID; /* current SUI */
177 extern bool InRecovery;
178 extern XLogRecPtr MyLastRecPtr;
179
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[];
187
188
189 extern XLogRecPtr XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata);
190 extern void XLogFlush(XLogRecPtr RecPtr);
191
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);
195
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);
208
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 ...
211  */
212 extern XLogRecPtr GetUndoRecPtr(void);
213
214 extern bool check_xlog_sync_method(const char *method);
215 extern void assign_xlog_sync_method(const char *method);
216
217 #endif   /* XLOG_H */