]> granicus.if.org Git - postgresql/blob - src/include/access/xlog_internal.h
Update copyrights for 2013
[postgresql] / src / include / access / xlog_internal.h
1 /*
2  * xlog_internal.h
3  *
4  * PostgreSQL transaction log internal declarations
5  *
6  * NOTE: this file is intended to contain declarations useful for
7  * manipulating the XLOG files directly, but it is not supposed to be
8  * needed by rmgr routines (redo support for individual record types).
9  * So the XLogRecord typedef and associated stuff appear in xlog.h.
10  *
11  * Note: This file must be includable in both frontend and backend contexts,
12  * to allow stand-alone tools like pg_receivexlog to deal with WAL files.
13  *
14  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
15  * Portions Copyright (c) 1994, Regents of the University of California
16  *
17  * src/include/access/xlog_internal.h
18  */
19 #ifndef XLOG_INTERNAL_H
20 #define XLOG_INTERNAL_H
21
22 #include "access/xlogdefs.h"
23 #include "datatype/timestamp.h"
24 #include "lib/stringinfo.h"
25 #include "pgtime.h"
26 #include "storage/block.h"
27 #include "storage/relfilenode.h"
28
29
30 /*
31  * Header info for a backup block appended to an XLOG record.
32  *
33  * As a trivial form of data compression, the XLOG code is aware that
34  * PG data pages usually contain an unused "hole" in the middle, which
35  * contains only zero bytes.  If hole_length > 0 then we have removed
36  * such a "hole" from the stored data (and it's not counted in the
37  * XLOG record's CRC, either).  Hence, the amount of block data actually
38  * present following the BkpBlock struct is BLCKSZ - hole_length bytes.
39  *
40  * Note that we don't attempt to align either the BkpBlock struct or the
41  * block's data.  So, the struct must be copied to aligned local storage
42  * before use.
43  */
44 typedef struct BkpBlock
45 {
46         RelFileNode node;                       /* relation containing block */
47         ForkNumber      fork;                   /* fork within the relation */
48         BlockNumber block;                      /* block number */
49         uint16          hole_offset;    /* number of bytes before "hole" */
50         uint16          hole_length;    /* number of bytes in "hole" */
51
52         /* ACTUAL BLOCK DATA FOLLOWS AT END OF STRUCT */
53 } BkpBlock;
54
55 /*
56  * Each page of XLOG file has a header like this:
57  */
58 #define XLOG_PAGE_MAGIC 0xD075  /* can be used as WAL version indicator */
59
60 typedef struct XLogPageHeaderData
61 {
62         uint16          xlp_magic;              /* magic value for correctness checks */
63         uint16          xlp_info;               /* flag bits, see below */
64         TimeLineID      xlp_tli;                /* TimeLineID of first record on page */
65         XLogRecPtr      xlp_pageaddr;   /* XLOG address of this page */
66
67         /*
68          * When there is not enough space on current page for whole record, we
69          * continue on the next page.  xlp_rem_len is the number of bytes
70          * remaining from a previous page.
71          *
72          * Note that xl_rem_len includes backup-block data; that is, it tracks
73          * xl_tot_len not xl_len in the initial header.  Also note that the
74          * continuation data isn't necessarily aligned.
75          */
76         uint32          xlp_rem_len;    /* total len of remaining data for record */
77 } XLogPageHeaderData;
78
79 #define SizeOfXLogShortPHD      MAXALIGN(sizeof(XLogPageHeaderData))
80
81 typedef XLogPageHeaderData *XLogPageHeader;
82
83 /*
84  * When the XLP_LONG_HEADER flag is set, we store additional fields in the
85  * page header.  (This is ordinarily done just in the first page of an
86  * XLOG file.)  The additional fields serve to identify the file accurately.
87  */
88 typedef struct XLogLongPageHeaderData
89 {
90         XLogPageHeaderData std;         /* standard header fields */
91         uint64          xlp_sysid;              /* system identifier from pg_control */
92         uint32          xlp_seg_size;   /* just as a cross-check */
93         uint32          xlp_xlog_blcksz;        /* just as a cross-check */
94 } XLogLongPageHeaderData;
95
96 #define SizeOfXLogLongPHD       MAXALIGN(sizeof(XLogLongPageHeaderData))
97
98 typedef XLogLongPageHeaderData *XLogLongPageHeader;
99
100 /* When record crosses page boundary, set this flag in new page's header */
101 #define XLP_FIRST_IS_CONTRECORD         0x0001
102 /* This flag indicates a "long" page header */
103 #define XLP_LONG_HEADER                         0x0002
104 /* This flag indicates backup blocks starting in this page are optional */
105 #define XLP_BKP_REMOVABLE                       0x0004
106 /* All defined flag bits in xlp_info (used for validity checking of header) */
107 #define XLP_ALL_FLAGS                           0x0007
108
109 #define XLogPageHeaderSize(hdr)         \
110         (((hdr)->xlp_info & XLP_LONG_HEADER) ? SizeOfXLogLongPHD : SizeOfXLogShortPHD)
111
112 /*
113  * The XLOG is split into WAL segments (physical files) of the size indicated
114  * by XLOG_SEG_SIZE.
115  */
116 #define XLogSegSize             ((uint32) XLOG_SEG_SIZE)
117 #define XLogSegmentsPerXLogId   (UINT64CONST(0x100000000) / XLOG_SEG_SIZE)
118
119 #define XLogSegNoOffsetToRecPtr(segno, offset, dest) \
120                 (dest) = (segno) * XLOG_SEG_SIZE + (offset)
121
122 /*
123  * Compute ID and segment from an XLogRecPtr.
124  *
125  * For XLByteToSeg, do the computation at face value.  For XLByteToPrevSeg,
126  * a boundary byte is taken to be in the previous segment.      This is suitable
127  * for deciding which segment to write given a pointer to a record end,
128  * for example.
129  */
130 #define XLByteToSeg(xlrp, logSegNo)     \
131         logSegNo = (xlrp) / XLogSegSize
132
133 #define XLByteToPrevSeg(xlrp, logSegNo) \
134         logSegNo = ((xlrp) - 1) / XLogSegSize
135
136 /*
137  * Is an XLogRecPtr within a particular XLOG segment?
138  *
139  * For XLByteInSeg, do the computation at face value.  For XLByteInPrevSeg,
140  * a boundary byte is taken to be in the previous segment.
141  */
142 #define XLByteInSeg(xlrp, logSegNo)     \
143         (((xlrp) / XLogSegSize) == (logSegNo))
144
145 #define XLByteInPrevSeg(xlrp, logSegNo) \
146         ((((xlrp) - 1) / XLogSegSize) == (logSegNo))
147
148 /* Check if an XLogRecPtr value is in a plausible range */
149 #define XRecOffIsValid(xlrp) \
150                 ((xlrp) % XLOG_BLCKSZ >= SizeOfXLogShortPHD)
151
152 /*
153  * The XLog directory and control file (relative to $PGDATA)
154  */
155 #define XLOGDIR                         "pg_xlog"
156 #define XLOG_CONTROL_FILE       "global/pg_control"
157
158 /*
159  * These macros encapsulate knowledge about the exact layout of XLog file
160  * names, timeline history file names, and archive-status file names.
161  */
162 #define MAXFNAMELEN             64
163
164 #define XLogFileName(fname, tli, logSegNo)      \
165         snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli,               \
166                          (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
167                          (uint32) ((logSegNo) % XLogSegmentsPerXLogId))
168
169 #define XLogFromFileName(fname, tli, logSegNo)  \
170         do {                                                                                            \
171                 uint32 log;                                                                             \
172                 uint32 seg;                                                                             \
173                 sscanf(fname, "%08X%08X%08X", tli, &log, &seg); \
174                 *logSegNo = (uint64) log * XLogSegmentsPerXLogId + seg; \
175         } while (0)
176
177 #define XLogFilePath(path, tli, logSegNo)       \
178         snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X", tli,                         \
179                          (uint32) ((logSegNo) / XLogSegmentsPerXLogId),                         \
180                          (uint32) ((logSegNo) % XLogSegmentsPerXLogId))
181
182 #define TLHistoryFileName(fname, tli)   \
183         snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
184
185 #define TLHistoryFilePath(path, tli)    \
186         snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
187
188 #define StatusFilePath(path, xlog, suffix)      \
189         snprintf(path, MAXPGPATH, XLOGDIR "/archive_status/%s%s", xlog, suffix)
190
191 #define BackupHistoryFileName(fname, tli, logSegNo, offset) \
192         snprintf(fname, MAXFNAMELEN, "%08X%08X%08X.%08X.backup", tli, \
193                          (uint32) ((logSegNo) / XLogSegmentsPerXLogId),           \
194                          (uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)
195
196 #define BackupHistoryFilePath(path, tli, logSegNo, offset)      \
197         snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X.%08X.backup", tli, \
198                          (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
199                          (uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)
200
201 /*
202  * Information logged when we detect a change in one of the parameters
203  * important for Hot Standby.
204  */
205 typedef struct xl_parameter_change
206 {
207         int                     MaxConnections;
208         int                     max_prepared_xacts;
209         int                     max_locks_per_xact;
210         int                     wal_level;
211 } xl_parameter_change;
212
213 /* logs restore point */
214 typedef struct xl_restore_point
215 {
216         TimestampTz rp_time;
217         char            rp_name[MAXFNAMELEN];
218 } xl_restore_point;
219
220
221 /*
222  * XLogRecord is defined in xlog.h, but we avoid #including that to keep
223  * this file includable in stand-alone programs.
224  */
225 struct XLogRecord;
226
227 /*
228  * Method table for resource managers.
229  *
230  * RmgrTable[] is indexed by RmgrId values (see rmgr.h).
231  */
232 typedef struct RmgrData
233 {
234         const char *rm_name;
235         void            (*rm_redo) (XLogRecPtr lsn, struct XLogRecord *rptr);
236         void            (*rm_desc) (StringInfo buf, uint8 xl_info, char *rec);
237         void            (*rm_startup) (void);
238         void            (*rm_cleanup) (void);
239         bool            (*rm_safe_restartpoint) (void);
240 } RmgrData;
241
242 extern const RmgrData RmgrTable[];
243
244 /*
245  * Exported to support xlog switching from checkpointer
246  */
247 extern pg_time_t GetLastSegSwitchTime(void);
248 extern XLogRecPtr RequestXLogSwitch(void);
249
250 extern void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli);
251
252 /*
253  * Exported for the functions in timeline.c and xlogarchive.c.  Only valid
254  * in the startup process.
255  */
256 extern bool InArchiveRecovery;
257 extern bool StandbyMode;
258 extern char *recoveryRestoreCommand;
259
260 /*
261  * Prototypes for functions in xlogarchive.c
262  */
263 extern bool RestoreArchivedFile(char *path, const char *xlogfname,
264                                         const char *recovername, off_t expectedSize,
265                                         bool cleanupEnabled);
266 extern void ExecuteRecoveryCommand(char *command, char *commandName,
267                                            bool failOnerror);
268 extern void KeepFileRestoredFromArchive(char  *path, char *xlogfname);
269 extern void XLogArchiveNotify(const char *xlog);
270 extern void XLogArchiveNotifySeg(XLogSegNo segno);
271 extern bool XLogArchiveCheckDone(const char *xlog);
272 extern bool XLogArchiveIsBusy(const char *xlog);
273 extern void XLogArchiveCleanup(const char *xlog);
274
275 #endif   /* XLOG_INTERNAL_H */