]> granicus.if.org Git - postgresql/blob - src/include/access/xlog.h
9252a8d324a10968a9f01d6cf362f4a043623c30
[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.29 2002/03/15 19:20:36 tgl 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 "storage/bufmgr.h"
18 #include "utils/pg_crc.h"
19
20
21 /*
22  * Header for each record in XLOG
23  *
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
27  * really:
28  *
29  * SizeOfXLogRecord + xl_len + n_backup_blocks * (sizeof(BkpBlock) + BLCKSZ)
30  *
31  * rounded up to a MAXALIGN boundary (so that all xlog records start on
32  * MAXALIGN boundaries).
33  */
34 typedef struct XLogRecord
35 {
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 */
43
44         /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
45
46 } XLogRecord;
47
48 #define SizeOfXLogRecord        MAXALIGN(sizeof(XLogRecord))
49 #define MAXLOGRECSZ                     65535           /* the most that'll fit in xl_len */
50
51 #define XLogRecGetData(record)  ((char*) (record) + SizeOfXLogRecord)
52
53 /*
54  * XLOG uses only low 4 bits of xl_info.  High 4 bits may be used by rmgr.
55  */
56 #define XLR_INFO_MASK                   0x0F
57
58 /*
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.
63  */
64 #define XLR_BKP_BLOCK_MASK              0x0C    /* all info bits used for bkp
65                                                                                  * blocks */
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 */
70
71 /*
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.
74  */
75 #define XLOG_NO_TRAN                    XLR_INFO_MASK
76
77 /*
78  * Header info for a backup block appended to an XLOG record.
79  *
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.
83  */
84 typedef struct BkpBlock
85 {
86         crc64           crc;
87         RelFileNode node;
88         BlockNumber block;
89 } BkpBlock;
90
91 /*
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.)
96  *
97  * Note that xl_rem_len includes backup-block data, unlike xl_len in the
98  * initial header.
99  */
100 typedef struct XLogContRecord
101 {
102         uint32          xl_rem_len;             /* total len of remaining data for record */
103
104         /* ACTUAL LOG DATA FOLLOWS AT END OF STRUCT */
105
106 } XLogContRecord;
107
108 #define SizeOfXLogContRecord    MAXALIGN(sizeof(XLogContRecord))
109
110 /*
111  * Each page of XLOG file has a header like this:
112  */
113 #define XLOG_PAGE_MAGIC 0xD059  /* can be used as WAL version indicator */
114
115 typedef struct XLogPageHeaderData
116 {
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;
122
123 #define SizeOfXLogPHD   MAXALIGN(sizeof(XLogPageHeaderData))
124
125 typedef XLogPageHeaderData *XLogPageHeader;
126
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
131
132 /*
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.
136  */
137 #define XLogSegSize             ((uint32) (16*1024*1024))
138 #define XLogSegsPerFile (((uint32) 0xffffffff) / XLogSegSize)
139 #define XLogFileSize    (XLogSegsPerFile * XLogSegSize)
140
141 /*
142  * Method table for resource managers.
143  *
144  * RmgrTable[] is indexed by RmgrId values (see rmgr.h).
145  */
146 typedef struct RmgrData
147 {
148         char       *rm_name;
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);
152 } RmgrData;
153
154 extern RmgrData RmgrTable[];
155
156 /*--------------------
157  * List of these structs is used to pass data to XLogInsert().
158  *
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  *--------------------
169  */
170 typedef struct XLogRecData
171 {
172         Buffer          buffer;                 /* buffer associated with this data */
173         char       *data;
174         uint32          len;
175         struct XLogRecData *next;
176 } XLogRecData;
177
178 extern StartUpID ThisStartUpID; /* current SUI */
179 extern bool InRecovery;
180 extern XLogRecPtr MyLastRecPtr;
181 extern bool MyXactMadeXLogEntry;
182 extern XLogRecPtr ProcLastRecEnd;
183
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[];
191
192
193 extern XLogRecPtr XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata);
194 extern void XLogFlush(XLogRecPtr RecPtr);
195
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);
199
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);
213
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 ...
216  */
217 extern XLogRecPtr GetUndoRecPtr(void);
218
219 extern bool check_xlog_sync_method(const char *method);
220 extern void assign_xlog_sync_method(const char *method);
221
222 #endif   /* XLOG_H */