PostgreSQL Source Code git master
Loading...
Searching...
No Matches
xlog.h
Go to the documentation of this file.
1/*
2 * xlog.h
3 *
4 * PostgreSQL write-ahead log manager
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * src/include/access/xlog.h
10 */
11#ifndef XLOG_H
12#define XLOG_H
13
14#include "access/xlogbackup.h"
15#include "access/xlogdefs.h"
17#include "datatype/timestamp.h"
18#include "lib/stringinfo.h"
19#include "nodes/pg_list.h"
20
21
22/* Sync methods */
32
36
37/* these variables are GUC parameters related to XLOG */
43extern PGDLLIMPORT int XLOGbuffers;
48extern PGDLLIMPORT bool fullPageWrites;
49extern PGDLLIMPORT bool wal_log_hints;
51extern PGDLLIMPORT bool wal_init_zero;
52extern PGDLLIMPORT bool wal_recycle;
56extern PGDLLIMPORT int CommitDelay;
60
62
63/* Archive modes */
64typedef enum ArchiveMode
65{
66 ARCHIVE_MODE_OFF = 0, /* disabled */
67 ARCHIVE_MODE_ON, /* enabled while server is running normally */
68 ARCHIVE_MODE_ALWAYS, /* enabled always (even during recovery) */
71
72/* WAL levels */
79
80/* Compression algorithms for WAL */
88
89/* Recovery states */
90typedef enum RecoveryState
91{
92 RECOVERY_STATE_CRASH = 0, /* crash recovery */
93 RECOVERY_STATE_ARCHIVE, /* archive recovery */
94 RECOVERY_STATE_DONE, /* currently in production */
96
97extern PGDLLIMPORT int wal_level;
99
100/* Is WAL archiving enabled (always or only while server is running normally)? */
101#define XLogArchivingActive() \
102 (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode > ARCHIVE_MODE_OFF)
103/* Is WAL archiving enabled always (even during recovery)? */
104#define XLogArchivingAlways() \
105 (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode == ARCHIVE_MODE_ALWAYS)
106
107/*
108 * Is WAL-logging necessary for archival or log-shipping, or can we skip
109 * WAL-logging if we fsync() the data before committing instead?
110 */
111#define XLogIsNeeded() (wal_level >= WAL_LEVEL_REPLICA)
112
113/*
114 * Is a full-page image needed for hint bit updates?
115 *
116 * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,
117 * we have to protect them against torn page writes. When you only set
118 * individual bits on a page, it's still consistent no matter what combination
119 * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log
120 * them if forced by wal_log_hints=on.
121 */
122#define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints)
123
124/* Do we need to WAL-log information required only for Hot Standby and logical replication? */
125#define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_REPLICA)
126
127/*
128 * Do we need to WAL-log information required only for logical replication?
129 *
130 * When XLogLogicalInfoActive() returns true, it enables logical-decoding-related
131 * WAL logging as if wal_level were set to 'logical', even if it's actually set
132 * to 'replica'. Note that XLogLogicalInfo is a process-local cache and can
133 * change until an XID is assigned to the transaction. In other words, it
134 * ensures that the same result is returned within an XID-assigned transaction.
135 */
136#define XLogLogicalInfoActive() \
137 (wal_level >= WAL_LEVEL_LOGICAL || XLogLogicalInfo)
138
139#ifdef WAL_DEBUG
140extern PGDLLIMPORT bool XLOG_DEBUG;
141#endif
142
143/*
144 * OR-able request flag bits for checkpoints. The "cause" bits are used only
145 * for logging purposes. Note: the flags must be defined so that it's
146 * sensible to OR together request flags arising from different requestors.
147 */
148
149/* These directly affect the behavior of CreateCheckPoint and subsidiaries */
150#define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
151#define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint, but
152 * issued at end of WAL recovery */
153#define CHECKPOINT_FAST 0x0004 /* Do it without delays */
154#define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
155#define CHECKPOINT_FLUSH_UNLOGGED 0x0010 /* Flush unlogged tables */
156/* These are important to RequestCheckpoint */
157#define CHECKPOINT_WAIT 0x0020 /* Wait for completion */
158#define CHECKPOINT_REQUESTED 0x0040 /* Checkpoint request has been made */
159/* These indicate the cause of a checkpoint request */
160#define CHECKPOINT_CAUSE_XLOG 0x0080 /* XLOG consumption */
161#define CHECKPOINT_CAUSE_TIME 0x0100 /* Elapsed time */
162
163/*
164 * Flag bits for the record being inserted, set using XLogSetRecordFlags().
165 */
166#define XLOG_INCLUDE_ORIGIN 0x01 /* include the replication origin */
167#define XLOG_MARK_UNIMPORTANT 0x02 /* record not important for durability */
168
169
170/* Checkpoint statistics */
171typedef struct CheckpointStatsData
173 TimestampTz ckpt_start_t; /* start of checkpoint */
174 TimestampTz ckpt_write_t; /* start of flushing buffers */
175 TimestampTz ckpt_sync_t; /* start of fsyncs */
176 TimestampTz ckpt_sync_end_t; /* end of fsyncs */
177 TimestampTz ckpt_end_t; /* end of checkpoint */
179 int ckpt_bufs_written; /* # of buffers written */
180 int ckpt_slru_written; /* # of SLRU buffers written */
182 int ckpt_segs_added; /* # of new xlog segments created */
183 int ckpt_segs_removed; /* # of xlog segments deleted */
184 int ckpt_segs_recycled; /* # of xlog segments recycled */
186 int ckpt_sync_rels; /* # of relations synced */
187 uint64 ckpt_longest_sync; /* Longest sync for one relation */
188 uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
189 * times, which is not necessarily the
190 * same as the total elapsed time for the
191 * entire sync phase. */
193
195
196/*
197 * GetWALAvailability return codes
198 */
199typedef enum WALAvailability
201 WALAVAIL_INVALID_LSN, /* parameter error */
202 WALAVAIL_RESERVED, /* WAL segment is within max_wal_size */
203 WALAVAIL_EXTENDED, /* WAL segment is reserved by a slot or
204 * wal_keep_size */
205 WALAVAIL_UNRESERVED, /* no longer reserved, but not removed yet */
206 WALAVAIL_REMOVED, /* WAL segment has been removed */
208
209struct XLogRecData;
210struct XLogReaderState;
211
214 uint8 flags,
215 int num_fpi,
217 bool topxid_included);
218extern void XLogFlush(XLogRecPtr record);
219extern bool XLogBackgroundFlush(void);
220extern bool XLogNeedsFlush(XLogRecPtr record);
222extern int XLogFileOpen(XLogSegNo segno, TimeLineID tli);
223
224extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
227extern void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN);
230
231extern void xlog_redo(struct XLogReaderState *record);
232extern void xlog_desc(StringInfo buf, struct XLogReaderState *record);
233extern const char *xlog_identify(uint8 info);
234
235extern void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli);
236
237extern bool RecoveryInProgress(void);
239extern bool XLogInsertAllowed(void);
242extern XLogRecPtr GetXLogWriteRecPtr(void);
243
244extern uint64 GetSystemIdentifier(void);
245extern char *GetMockAuthenticationNonce(void);
246extern bool DataChecksumsEnabled(void);
247extern bool GetDefaultCharSignedness(void);
249extern Size XLOGShmemSize(void);
250extern void XLOGShmemInit(void);
251extern void BootStrapXLOG(uint32 data_checksum_version);
252extern void InitializeWalConsistencyChecking(void);
253extern void LocalProcessControlFile(bool reset);
255extern void StartupXLOG(void);
256extern void ShutdownXLOG(int code, Datum arg);
257extern bool CreateCheckPoint(int flags);
258extern bool CreateRestartPoint(int flags);
260extern void XLogPutNextOid(Oid nextOid);
261extern XLogRecPtr XLogRestorePoint(const char *rpName);
262extern XLogRecPtr XLogAssignLSN(void);
263extern void UpdateFullPageWrites(void);
265extern XLogRecPtr GetRedoRecPtr(void);
266extern XLogRecPtr GetInsertRecPtr(void);
271
272extern void SetWalWriterSleeping(bool sleeping);
273
274extern void WakeupCheckpointer(void);
275
276extern Size WALReadFromBuffers(char *dstbuf, XLogRecPtr startptr, Size count,
277 TimeLineID tli);
278
279/*
280 * Routines used by xlogrecovery.c to call back into xlog.c during recovery.
281 */
286extern void SetInstallXLogFileSegmentActive(void);
287extern bool IsInstallXLogFileSegmentActive(void);
288extern void ResetInstallXLogFileSegmentActive(void);
289extern void XLogShutdownWalRcv(void);
290
291/*
292 * Routines to start, stop, and get status of a base backup.
293 */
294
295/*
296 * Session-level status of base backups
297 *
298 * This is used in parallel with the shared memory status to control parallel
299 * execution of base backup functions for a given session, be it a backend
300 * dedicated to replication or a normal backend connected to a database. The
301 * update of the session-level status happens at the same time as the shared
302 * memory counters to keep a consistent global and local state of the backups
303 * running.
310
311extern void do_pg_backup_start(const char *backupidstr, bool fast,
312 List **tablespaces, BackupState *state,
315extern void do_pg_abort_backup(int code, Datum arg);
318
319/* File path names (all relative to $PGDATA) */
320#define RECOVERY_SIGNAL_FILE "recovery.signal"
321#define STANDBY_SIGNAL_FILE "standby.signal"
322#define BACKUP_LABEL_FILE "backup_label"
323#define BACKUP_LABEL_OLD "backup_label.old"
325#define TABLESPACE_MAP "tablespace_map"
326#define TABLESPACE_MAP_OLD "tablespace_map.old"
327
328/* files to signal promotion to primary */
329#define PROMOTE_SIGNAL_FILE "promote"
330
331#endif /* XLOG_H */
#define PGDLLIMPORT
Definition c.h:1423
uint8_t uint8
Definition c.h:616
uint64_t uint64
Definition c.h:619
uint32_t uint32
Definition c.h:618
size_t Size
Definition c.h:691
int64 TimestampTz
Definition timestamp.h:39
Datum arg
Definition elog.c:1322
static char buf[DEFAULT_XLOG_SEG_SIZE]
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fd(const char *x, int i)
static int fb(int x)
void reset(void)
uint64 ckpt_agg_sync_time
Definition xlog.h:187
uint64 ckpt_longest_sync
Definition xlog.h:186
TimestampTz ckpt_start_t
Definition xlog.h:172
TimestampTz ckpt_end_t
Definition xlog.h:176
int ckpt_segs_removed
Definition xlog.h:182
TimestampTz ckpt_write_t
Definition xlog.h:173
TimestampTz ckpt_sync_end_t
Definition xlog.h:175
TimestampTz ckpt_sync_t
Definition xlog.h:174
int ckpt_bufs_written
Definition xlog.h:178
int ckpt_segs_recycled
Definition xlog.h:183
int ckpt_slru_written
Definition xlog.h:179
Definition pg_list.h:54
DecodedXLogRecord * record
Definition xlogreader.h:235
XLogRecPtr EndRecPtr
Definition xlogreader.h:206
int XLogFileInit(XLogSegNo logsegno, TimeLineID logtli)
Definition xlog.c:3401
uint64 GetSystemIdentifier(void)
Definition xlog.c:4611
void UpdateFullPageWrites(void)
Definition xlog.c:8319
bool RecoveryInProgress(void)
Definition xlog.c:6444
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition xlog.c:6577
TimeLineID GetWALInsertionTimeLine(void)
Definition xlog.c:6630
PGDLLIMPORT bool log_checkpoints
Definition xlog.c:133
void do_pg_abort_backup(int code, Datum arg)
Definition xlog.c:9573
XLogSegNo XLogGetLastRemovedSegno(void)
Definition xlog.c:3779
void xlog_desc(StringInfo buf, struct XLogReaderState *record)
Definition xlogdesc.c:58
Size WALReadFromBuffers(char *dstbuf, XLogRecPtr startptr, Size count, TimeLineID tli)
Definition xlog.c:1755
XLogRecPtr GetRedoRecPtr(void)
Definition xlog.c:6547
void SetInstallXLogFileSegmentActive(void)
Definition xlog.c:9678
void StartupXLOG(void)
Definition xlog.c:5501
bool IsInstallXLogFileSegmentActive(void)
Definition xlog.c:9695
PGDLLIMPORT XLogRecPtr XactLastRecEnd
Definition xlog.c:258
void BootStrapXLOG(uint32 data_checksum_version)
Definition xlog.c:5110
ArchiveMode
Definition xlog.h:65
@ ARCHIVE_MODE_ALWAYS
Definition xlog.h:68
@ ARCHIVE_MODE_OFF
Definition xlog.h:66
@ ARCHIVE_MODE_ON
Definition xlog.h:67
bool CreateRestartPoint(int flags)
Definition xlog.c:7715
PGDLLIMPORT int wal_sync_method
Definition xlog.c:134
PGDLLIMPORT bool EnableHotStandby
Definition xlog.c:125
XLogRecPtr GetInsertRecPtr(void)
Definition xlog.c:6592
XLogRecPtr XLogGetReplicationSlotMinimumLSN(void)
Definition xlog.c:2666
SessionBackupState get_backup_status(void)
Definition xlog.c:9280
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition xlog.c:3748
WALAvailability
Definition xlog.h:199
@ WALAVAIL_REMOVED
Definition xlog.h:205
@ WALAVAIL_RESERVED
Definition xlog.h:201
@ WALAVAIL_UNRESERVED
Definition xlog.h:204
@ WALAVAIL_EXTENDED
Definition xlog.h:202
@ WALAVAIL_INVALID_LSN
Definition xlog.h:200
PGDLLIMPORT CheckpointStatsData CheckpointStats
Definition xlog.c:213
WALAvailability GetWALAvailability(XLogRecPtr targetLSN)
Definition xlog.c:7996
PGDLLIMPORT int wal_retrieve_retry_interval
Definition xlog.c:138
void XLOGShmemInit(void)
Definition xlog.c:4995
void ShutdownXLOG(int code, Datum arg)
Definition xlog.c:6712
bool DataChecksumsEnabled(void)
Definition xlog.c:4631
RecoveryState GetRecoveryState(void)
Definition xlog.c:6480
const char * xlog_identify(uint8 info)
Definition xlogdesc.c:185
PGDLLIMPORT int wal_compression
Definition xlog.c:128
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition xlog.c:2653
PGDLLIMPORT int CommitSiblings
Definition xlog.c:137
void SwitchIntoArchiveRecovery(XLogRecPtr EndRecPtr, TimeLineID replayTLI)
Definition xlog.c:6319
WalCompression
Definition xlog.h:82
@ WAL_COMPRESSION_NONE
Definition xlog.h:83
@ WAL_COMPRESSION_LZ4
Definition xlog.h:85
@ WAL_COMPRESSION_PGLZ
Definition xlog.h:84
@ WAL_COMPRESSION_ZSTD
Definition xlog.h:86
bool GetDefaultCharSignedness(void)
Definition xlog.c:4645
PGDLLIMPORT int wal_decode_buffer_size
Definition xlog.c:140
XLogRecPtr GetXLogInsertRecPtr(void)
Definition xlog.c:9614
Size XLOGShmemSize(void)
Definition xlog.c:4945
void SetWalWriterSleeping(bool sleeping)
Definition xlog.c:9710
void WakeupCheckpointer(void)
int XLogFileOpen(XLogSegNo segno, TimeLineID tli)
Definition xlog.c:3639
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition xlog.c:6609
WalLevel GetActiveWalLevelOnStandby(void)
Definition xlog.c:4936
PGDLLIMPORT int CheckPointSegments
Definition xlog.c:160
void xlog_redo(struct XLogReaderState *record)
Definition xlog.c:8388
void InitializeWalConsistencyChecking(void)
Definition xlog.c:4842
PGDLLIMPORT bool fullPageWrites
Definition xlog.c:126
void RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
Definition xlog.c:3961
XLogRecPtr GetLastImportantRecPtr(void)
Definition xlog.c:6666
PGDLLIMPORT XLogRecPtr ProcLastRecPtr
Definition xlog.c:257
PGDLLIMPORT bool XLogLogicalInfo
Definition logicalctl.c:108
PGDLLIMPORT char * wal_consistency_checking_string
Definition xlog.c:129
SessionBackupState
Definition xlog.h:305
@ SESSION_BACKUP_RUNNING
Definition xlog.h:307
@ SESSION_BACKUP_NONE
Definition xlog.h:306
XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi, uint64 fpi_bytes, bool topxid_included)
Definition xlog.c:750
bool XLogNeedsFlush(XLogRecPtr record)
Definition xlog.c:3129
PGDLLIMPORT int wal_segment_size
Definition xlog.c:147
void register_persistent_abort_backup_handler(void)
Definition xlog.c:9600
void ReachedEndOfBackup(XLogRecPtr EndRecPtr, TimeLineID tli)
Definition xlog.c:6357
void LocalProcessControlFile(bool reset)
Definition xlog.c:4923
XLogSegNo XLogGetOldestSegno(TimeLineID tli)
Definition xlog.c:3795
PGDLLIMPORT int XLogArchiveMode
Definition xlog.c:123
XLogRecPtr GetXLogWriteRecPtr(void)
Definition xlog.c:9646
PGDLLIMPORT char * XLogArchiveCommand
Definition xlog.c:124
void ResetInstallXLogFileSegmentActive(void)
Definition xlog.c:9687
PGDLLIMPORT int wal_keep_size_mb
Definition xlog.c:120
bool XLogBackgroundFlush(void)
Definition xlog.c:2972
char * GetMockAuthenticationNonce(void)
Definition xlog.c:4621
PGDLLIMPORT bool * wal_consistency_checking
Definition xlog.c:130
PGDLLIMPORT int max_slot_wal_keep_size_mb
Definition xlog.c:139
bool XLogInsertAllowed(void)
Definition xlog.c:6499
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition xlog.c:8977
PGDLLIMPORT bool wal_init_zero
Definition xlog.c:131
PGDLLIMPORT int min_wal_size_mb
Definition xlog.c:119
PGDLLIMPORT int CommitDelay
Definition xlog.c:136
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition xlog.c:8207
PGDLLIMPORT int XLOGbuffers
Definition xlog.c:121
TimeLineID GetWALInsertionTimeLineIfSet(void)
Definition xlog.c:6646
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition xlog.c:9299
PGDLLIMPORT bool wal_recycle
Definition xlog.c:132
WalLevel
Definition xlog.h:74
@ WAL_LEVEL_REPLICA
Definition xlog.h:76
@ WAL_LEVEL_LOGICAL
Definition xlog.h:77
@ WAL_LEVEL_MINIMAL
Definition xlog.h:75
bool CreateCheckPoint(int flags)
Definition xlog.c:7009
XLogRecPtr GetFakeLSNForUnloggedRel(void)
Definition xlog.c:4660
PGDLLIMPORT int XLogArchiveTimeout
Definition xlog.c:122
XLogRecPtr GetXLogInsertEndRecPtr(void)
Definition xlog.c:9630
void XLogPutNextOid(Oid nextOid)
Definition xlog.c:8152
void XLogFlush(XLogRecPtr record)
Definition xlog.c:2767
PGDLLIMPORT XLogRecPtr XactLastCommitEnd
Definition xlog.c:259
XLogRecPtr XLogAssignLSN(void)
Definition xlog.c:8237
PGDLLIMPORT int wal_level
Definition xlog.c:135
PGDLLIMPORT bool wal_log_hints
Definition xlog.c:127
RecoveryState
Definition xlog.h:91
@ RECOVERY_STATE_CRASH
Definition xlog.h:92
@ RECOVERY_STATE_DONE
Definition xlog.h:94
@ RECOVERY_STATE_ARCHIVE
Definition xlog.h:93
void XLogShutdownWalRcv(void)
Definition xlog.c:9668
void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
Definition xlog.c:8880
PGDLLIMPORT int max_wal_size_mb
Definition xlog.c:118
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition xlog.c:2596
PGDLLIMPORT bool track_wal_io_timing
Definition xlog.c:141
bool XLogCheckpointNeeded(XLogSegNo new_segno)
Definition xlog.c:2267
WalSyncMethod
Definition xlog.h:24
@ WAL_SYNC_METHOD_OPEN
Definition xlog.h:27
@ WAL_SYNC_METHOD_FDATASYNC
Definition xlog.h:26
@ WAL_SYNC_METHOD_FSYNC_WRITETHROUGH
Definition xlog.h:28
@ WAL_SYNC_METHOD_OPEN_DSYNC
Definition xlog.h:29
@ WAL_SYNC_METHOD_FSYNC
Definition xlog.h:25
uint64 XLogRecPtr
Definition xlogdefs.h:21
uint32 TimeLineID
Definition xlogdefs.h:63
uint64 XLogSegNo
Definition xlogdefs.h:52