4 * PostgreSQL write-ahead 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 xlogrecord.h.
11 * Note: This file must be includable in both frontend and backend contexts,
12 * to allow stand-alone tools like pg_receivewal to deal with WAL files.
14 * Portions Copyright (c) 1996-2025, 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
32 * Each page of XLOG file has a header like this:
34 #define XLOG_PAGE_MAGIC 0xD118 /* can be used as WAL version indicator */
44 * When there is not enough space on current page for whole record, we
45 * continue on the next page. xlp_rem_len is the number of bytes
46 * remaining from a previous page; it tracks xl_tot_len in the initial
47 * header. Note that the continuation data isn't necessarily aligned.
52 #define SizeOfXLogShortPHD MAXALIGN(sizeof(XLogPageHeaderData))
57 * When the XLP_LONG_HEADER flag is set, we store additional fields in the
58 * page header. (This is ordinarily done just in the first page of an
59 * XLOG file.) The additional fields serve to identify the file accurately.
69 #define SizeOfXLogLongPHD MAXALIGN(sizeof(XLogLongPageHeaderData))
73/* When record crosses page boundary, set this flag in new page's header */
74 #define XLP_FIRST_IS_CONTRECORD 0x0001
75/* This flag indicates a "long" page header */
76 #define XLP_LONG_HEADER 0x0002
77/* This flag indicates backup blocks starting in this page are optional */
78 #define XLP_BKP_REMOVABLE 0x0004
79/* Replaces a missing contrecord; see CreateOverwriteContrecordRecord */
80 #define XLP_FIRST_IS_OVERWRITE_CONTRECORD 0x0008
81/* All defined flag bits in xlp_info (used for validity checking of header) */
82 #define XLP_ALL_FLAGS 0x000F
84 #define XLogPageHeaderSize(hdr) \
85 (((hdr)->xlp_info & XLP_LONG_HEADER) ? SizeOfXLogLongPHD : SizeOfXLogShortPHD)
87/* wal_segment_size can range from 1MB to 1GB */
88 #define WalSegMinSize 1024 * 1024
89 #define WalSegMaxSize 1024 * 1024 * 1024
90/* default number of min and max wal segments */
91 #define DEFAULT_MIN_WAL_SEGS 5
92 #define DEFAULT_MAX_WAL_SEGS 64
94/* check that the given size is a valid wal_segment_size */
95 #define IsPowerOf2(x) (x > 0 && ((x) & ((x)-1)) == 0)
96 #define IsValidWalSegSize(size) \
97 (IsPowerOf2(size) && \
98 ((size) >= WalSegMinSize && (size) <= WalSegMaxSize))
100 #define XLogSegmentsPerXLogId(wal_segsz_bytes) \
101 (UINT64CONST(0x100000000) / (wal_segsz_bytes))
103 #define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest) \
104 (dest) = (segno) * (wal_segsz_bytes) + (offset)
106 #define XLogSegmentOffset(xlogptr, wal_segsz_bytes) \
107 ((xlogptr) & ((wal_segsz_bytes) - 1))
110 * Compute a segment number from an XLogRecPtr.
112 * For XLByteToSeg, do the computation at face value. For XLByteToPrevSeg,
113 * a boundary byte is taken to be in the previous segment. This is suitable
114 * for deciding which segment to write given a pointer to a record end,
117 #define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes) \
118 logSegNo = (xlrp) / (wal_segsz_bytes)
120 #define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
121 logSegNo = ((xlrp) - 1) / (wal_segsz_bytes)
124 * Convert values of GUCs measured in megabytes to equiv. segment count.
127 #define XLogMBVarToSegs(mbvar, wal_segsz_bytes) \
128 ((mbvar) / ((wal_segsz_bytes) / (1024 * 1024)))
131 * Is an XLogRecPtr within a particular XLOG segment?
133 * For XLByteInSeg, do the computation at face value. For XLByteInPrevSeg,
134 * a boundary byte is taken to be in the previous segment.
136 #define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes) \
137 (((xlrp) / (wal_segsz_bytes)) == (logSegNo))
139 #define XLByteInPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
140 ((((xlrp) - 1) / (wal_segsz_bytes)) == (logSegNo))
142/* Check if an XLogRecPtr value is in a plausible range */
143 #define XRecOffIsValid(xlrp) \
144 ((xlrp) % XLOG_BLCKSZ >= SizeOfXLogShortPHD)
147 * The XLog directory and control file (relative to $PGDATA)
149 #define XLOGDIR "pg_wal"
150 #define XLOG_CONTROL_FILE "global/pg_control"
153 * These macros encapsulate knowledge about the exact layout of XLog file
154 * names, timeline history file names, and archive-status file names.
156 #define MAXFNAMELEN 64
158/* Length of XLog file name */
159 #define XLOG_FNAME_LEN 24
162 * Generate a WAL segment file name. Do not use this function in a helper
163 * function allocating the result generated.
187 * XLOG segment with .partial suffix. Used by pg_receivewal and at end of
188 * archive recovery, when we want to archive a WAL segment but it might not
205 sscanf(fname,
"%08X%08X%08X", tli, &log, &seg);
226 return (strlen(fname) == 8 + strlen(
".history") &&
227 strspn(fname,
"0123456789ABCDEF") == 8 &&
228 strcmp(fname + 8,
".history") == 0);
257 strcmp(fname + strlen(fname) - strlen(
".backup"),
".backup") == 0);
270 * Information logged when we detect a change in one of the parameters
271 * important for Hot Standby.
285/* logs restore point */
292/* Overwrite of prior contrecord */
299/* End of recovery mark, when we don't do an END_OF_RECOVERY checkpoint */
309 * The functions in xloginsert.c construct a chain of XLogRecData structs
310 * to represent the final WAL record.
315 const void *
data;
/* start of rmgr data to include */
323 * Method table for resource managers.
325 * This struct must be kept in sync with the PG_RMGR definition in
328 * rm_identify must return a name for the record based on xl_info (without
329 * reference to the rmid). For example, XLOG_BTREE_VACUUM would be named
330 * "VACUUM". rm_desc can then be called to obtain additional detail for the
331 * record, if available (e.g. the last block).
333 * rm_mask takes as input a page modified by the resource manager and masks
334 * out bits that shouldn't be flagged by wal_consistency_checking.
336 * RmgrTable[] is indexed by RmgrId values (see rmgrlist.h). If rm_name is
337 * NULL, the corresponding RmgrTable entry is considered invalid.
344 const char *(*rm_identify) (
uint8 info);
375 * Exported to support xlog switching from checkpointer
387 * Exported for the functions in timeline.c and xlogarchive.c. Only valid
388 * in the startup process.
395#endif /* XLOG_INTERNAL_H */
void(* rm_mask)(char *pagedata, BlockNumber blkno)
void(* rm_redo)(XLogReaderState *record)
void(* rm_decode)(struct LogicalDecodingContext *ctx, struct XLogRecordBuffer *buf)
void(* rm_desc)(StringInfo buf, XLogReaderState *record)
struct XLogRecData * next
TimeLineID PrevTimeLineID
TimeLineID ThisTimeLineID
TimestampTz overwrite_time
XLogRecPtr overwritten_lsn
bool track_commit_timestamp
char rp_name[MAXFNAMELEN]
struct xl_overwrite_contrecord xl_overwrite_contrecord
struct XLogLongPageHeaderData XLogLongPageHeaderData
static RmgrData GetRmgr(RmgrId rmid)
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
struct xl_restore_point xl_restore_point
XLogLongPageHeaderData * XLogLongPageHeader
struct XLogRecData XLogRecData
PGDLLIMPORT bool ArchiveRecoveryRequested
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
static bool IsXLogFileName(const char *fname)
static void XLogFromFileName(const char *fname, TimeLineID *tli, XLogSegNo *logSegNo, int wal_segsz_bytes)
void XLogRecGetBlockRefInfo(XLogReaderState *record, bool pretty, bool detailed_format, StringInfo buf, uint32 *fpi_len)
static bool IsTLHistoryFileName(const char *fname)
pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN)
XLogPageHeaderData * XLogPageHeader
static bool IsBackupHistoryFileName(const char *fname)
static void StatusFilePath(char *path, const char *xlog, const char *suffix)
struct xl_parameter_change xl_parameter_change
static void BackupHistoryFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, XLogRecPtr startpoint, int wal_segsz_bytes)
void RmgrNotFound(RmgrId rmid)
static void XLogFilePath(char *path, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
PGDLLIMPORT bool StandbyMode
PGDLLIMPORT bool InArchiveRecovery
static void XLogFileNameById(char *fname, TimeLineID tli, uint32 log, uint32 seg)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli)
static void TLHistoryFilePath(char *path, TimeLineID tli)
static void BackupHistoryFilePath(char *path, TimeLineID tli, XLogSegNo logSegNo, XLogRecPtr startpoint, int wal_segsz_bytes)
PGDLLIMPORT RmgrData RmgrTable[]
struct XLogPageHeaderData XLogPageHeaderData
struct xl_end_of_recovery xl_end_of_recovery
static bool RmgrIdExists(RmgrId rmid)
static void TLHistoryFileName(char *fname, TimeLineID tli)
#define XLogSegmentsPerXLogId(wal_segsz_bytes)
void RegisterCustomRmgr(RmgrId rmid, const RmgrData *rmgr)
PGDLLIMPORT char * recoveryRestoreCommand
static bool IsPartialXLogFileName(const char *fname)