4 * PostgreSQL transaction log internal declarations
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.
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.
14 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
15 * Portions Copyright (c) 1994, Regents of the University of California
17 * src/include/access/xlog_internal.h
19 #ifndef XLOG_INTERNAL_H
20 #define XLOG_INTERNAL_H
22 #include "access/xlogdefs.h"
23 #include "datatype/timestamp.h"
24 #include "lib/stringinfo.h"
26 #include "storage/block.h"
27 #include "storage/relfilenode.h"
31 * Header info for a backup block appended to an XLOG record.
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.
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
44 typedef struct BkpBlock
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" */
52 /* ACTUAL BLOCK DATA FOLLOWS AT END OF STRUCT */
56 * Each page of XLOG file has a header like this:
58 #define XLOG_PAGE_MAGIC 0xD075 /* can be used as WAL version indicator */
60 typedef struct XLogPageHeaderData
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 */
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.
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.
76 uint32 xlp_rem_len; /* total len of remaining data for record */
79 #define SizeOfXLogShortPHD MAXALIGN(sizeof(XLogPageHeaderData))
81 typedef XLogPageHeaderData *XLogPageHeader;
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.
88 typedef struct XLogLongPageHeaderData
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;
96 #define SizeOfXLogLongPHD MAXALIGN(sizeof(XLogLongPageHeaderData))
98 typedef XLogLongPageHeaderData *XLogLongPageHeader;
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
109 #define XLogPageHeaderSize(hdr) \
110 (((hdr)->xlp_info & XLP_LONG_HEADER) ? SizeOfXLogLongPHD : SizeOfXLogShortPHD)
113 * The XLOG is split into WAL segments (physical files) of the size indicated
116 #define XLogSegSize ((uint32) XLOG_SEG_SIZE)
117 #define XLogSegmentsPerXLogId (UINT64CONST(0x100000000) / XLOG_SEG_SIZE)
119 #define XLogSegNoOffsetToRecPtr(segno, offset, dest) \
120 (dest) = (segno) * XLOG_SEG_SIZE + (offset)
123 * Compute ID and segment from an XLogRecPtr.
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,
130 #define XLByteToSeg(xlrp, logSegNo) \
131 logSegNo = (xlrp) / XLogSegSize
133 #define XLByteToPrevSeg(xlrp, logSegNo) \
134 logSegNo = ((xlrp) - 1) / XLogSegSize
137 * Is an XLogRecPtr within a particular XLOG segment?
139 * For XLByteInSeg, do the computation at face value. For XLByteInPrevSeg,
140 * a boundary byte is taken to be in the previous segment.
142 #define XLByteInSeg(xlrp, logSegNo) \
143 (((xlrp) / XLogSegSize) == (logSegNo))
145 #define XLByteInPrevSeg(xlrp, logSegNo) \
146 ((((xlrp) - 1) / XLogSegSize) == (logSegNo))
148 /* Check if an XLogRecPtr value is in a plausible range */
149 #define XRecOffIsValid(xlrp) \
150 ((xlrp) % XLOG_BLCKSZ >= SizeOfXLogShortPHD)
153 * The XLog directory and control file (relative to $PGDATA)
155 #define XLOGDIR "pg_xlog"
156 #define XLOG_CONTROL_FILE "global/pg_control"
159 * These macros encapsulate knowledge about the exact layout of XLog file
160 * names, timeline history file names, and archive-status file names.
162 #define MAXFNAMELEN 64
164 #define XLogFileName(fname, tli, logSegNo) \
165 snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, \
166 (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
167 (uint32) ((logSegNo) % XLogSegmentsPerXLogId))
169 #define XLogFromFileName(fname, tli, logSegNo) \
173 sscanf(fname, "%08X%08X%08X", tli, &log, &seg); \
174 *logSegNo = (uint64) log * XLogSegmentsPerXLogId + seg; \
177 #define XLogFilePath(path, tli, logSegNo) \
178 snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X", tli, \
179 (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
180 (uint32) ((logSegNo) % XLogSegmentsPerXLogId))
182 #define TLHistoryFileName(fname, tli) \
183 snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
185 #define TLHistoryFilePath(path, tli) \
186 snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
188 #define StatusFilePath(path, xlog, suffix) \
189 snprintf(path, MAXPGPATH, XLOGDIR "/archive_status/%s%s", xlog, suffix)
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)
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)
202 * Information logged when we detect a change in one of the parameters
203 * important for Hot Standby.
205 typedef struct xl_parameter_change
208 int max_prepared_xacts;
209 int max_locks_per_xact;
211 } xl_parameter_change;
213 /* logs restore point */
214 typedef struct xl_restore_point
217 char rp_name[MAXFNAMELEN];
222 * XLogRecord is defined in xlog.h, but we avoid #including that to keep
223 * this file includable in stand-alone programs.
228 * Method table for resource managers.
230 * RmgrTable[] is indexed by RmgrId values (see rmgr.h).
232 typedef struct RmgrData
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);
242 extern const RmgrData RmgrTable[];
245 * Exported to support xlog switching from checkpointer
247 extern pg_time_t GetLastSegSwitchTime(void);
248 extern XLogRecPtr RequestXLogSwitch(void);
250 extern void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli);
253 * Exported for the functions in timeline.c and xlogarchive.c. Only valid
254 * in the startup process.
256 extern bool InArchiveRecovery;
257 extern bool StandbyMode;
258 extern char *recoveryRestoreCommand;
261 * Prototypes for functions in xlogarchive.c
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,
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);
275 #endif /* XLOG_INTERNAL_H */