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;
61
63
64/* Archive modes */
65typedef enum ArchiveMode
66{
67 ARCHIVE_MODE_OFF = 0, /* disabled */
68 ARCHIVE_MODE_ON, /* enabled while server is running normally */
69 ARCHIVE_MODE_ALWAYS, /* enabled always (even during recovery) */
72
73/* WAL levels */
80
81/* Compression algorithms for WAL */
89
90/* Recovery states */
91typedef enum RecoveryState
92{
93 RECOVERY_STATE_CRASH = 0, /* crash recovery */
94 RECOVERY_STATE_ARCHIVE, /* archive recovery */
95 RECOVERY_STATE_DONE, /* currently in production */
97
98extern PGDLLIMPORT int wal_level;
100
101/* Is WAL archiving enabled (always or only while server is running normally)? */
102#define XLogArchivingActive() \
103 (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode > ARCHIVE_MODE_OFF)
104/* Is WAL archiving enabled always (even during recovery)? */
105#define XLogArchivingAlways() \
106 (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode == ARCHIVE_MODE_ALWAYS)
107
108/*
109 * Is WAL-logging necessary for archival or log-shipping, or can we skip
110 * WAL-logging if we fsync() the data before committing instead?
111 */
112#define XLogIsNeeded() (wal_level >= WAL_LEVEL_REPLICA)
113
114/*
115 * Is a full-page image needed for hint bit updates?
116 *
117 * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,
118 * we have to protect them against torn page writes. When you only set
119 * individual bits on a page, it's still consistent no matter what combination
120 * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log
121 * them if forced by wal_log_hints=on.
122 */
123#define XLogHintBitIsNeeded() (wal_log_hints || DataChecksumsNeedWrite())
124
125/* Do we need to WAL-log information required only for Hot Standby and logical replication? */
126#define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_REPLICA)
127
128/*
129 * Do we need to WAL-log information required only for logical replication?
130 *
131 * When XLogLogicalInfoActive() returns true, it enables logical-decoding-related
132 * WAL logging as if wal_level were set to 'logical', even if it's actually set
133 * to 'replica'. Note that XLogLogicalInfo is a process-local cache and can
134 * change until an XID is assigned to the transaction. In other words, it
135 * ensures that the same result is returned within an XID-assigned transaction.
136 */
137#define XLogLogicalInfoActive() \
138 (wal_level >= WAL_LEVEL_LOGICAL || XLogLogicalInfo)
139
140#ifdef WAL_DEBUG
141extern PGDLLIMPORT bool XLOG_DEBUG;
142#endif
143
144/*
145 * OR-able request flag bits for checkpoints. The "cause" bits are used only
146 * for logging purposes. Note: the flags must be defined so that it's
147 * sensible to OR together request flags arising from different requestors.
148 */
149
150/* These directly affect the behavior of CreateCheckPoint and subsidiaries */
151#define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
152#define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint, but
153 * issued at end of WAL recovery */
154#define CHECKPOINT_FAST 0x0004 /* Do it without delays */
155#define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
156#define CHECKPOINT_FLUSH_UNLOGGED 0x0010 /* Flush unlogged tables */
157/* These are important to RequestCheckpoint */
158#define CHECKPOINT_WAIT 0x0020 /* Wait for completion */
159#define CHECKPOINT_REQUESTED 0x0040 /* Checkpoint request has been made */
160/* These indicate the cause of a checkpoint request */
161#define CHECKPOINT_CAUSE_XLOG 0x0080 /* XLOG consumption */
162#define CHECKPOINT_CAUSE_TIME 0x0100 /* Elapsed time */
163
164/*
165 * Flag bits for the record being inserted, set using XLogSetRecordFlags().
166 */
167#define XLOG_INCLUDE_ORIGIN 0x01 /* include the replication origin */
168#define XLOG_MARK_UNIMPORTANT 0x02 /* record not important for durability */
169
170
171/* Checkpoint statistics */
172typedef struct CheckpointStatsData
174 TimestampTz ckpt_start_t; /* start of checkpoint */
175 TimestampTz ckpt_write_t; /* start of flushing buffers */
176 TimestampTz ckpt_sync_t; /* start of fsyncs */
177 TimestampTz ckpt_sync_end_t; /* end of fsyncs */
178 TimestampTz ckpt_end_t; /* end of checkpoint */
180 int ckpt_bufs_written; /* # of buffers written */
181 int ckpt_slru_written; /* # of SLRU buffers written */
183 int ckpt_segs_added; /* # of new xlog segments created */
184 int ckpt_segs_removed; /* # of xlog segments deleted */
185 int ckpt_segs_recycled; /* # of xlog segments recycled */
187 int ckpt_sync_rels; /* # of relations synced */
188 uint64 ckpt_longest_sync; /* Longest sync for one relation */
189 uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
190 * times, which is not necessarily the
191 * same as the total elapsed time for the
192 * entire sync phase. */
194
196
197/*
198 * GetWALAvailability return codes
199 */
200typedef enum WALAvailability
202 WALAVAIL_INVALID_LSN, /* parameter error */
203 WALAVAIL_RESERVED, /* WAL segment is within max_wal_size */
204 WALAVAIL_EXTENDED, /* WAL segment is reserved by a slot or
205 * wal_keep_size */
206 WALAVAIL_UNRESERVED, /* no longer reserved, but not removed yet */
207 WALAVAIL_REMOVED, /* WAL segment has been removed */
209
210struct XLogRecData;
211struct XLogReaderState;
212
215 uint8 flags,
216 int num_fpi,
218 bool topxid_included);
219extern void XLogFlush(XLogRecPtr record);
220extern bool XLogBackgroundFlush(void);
221extern bool XLogNeedsFlush(XLogRecPtr record);
223extern int XLogFileOpen(XLogSegNo segno, TimeLineID tli);
224
225extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
228extern void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN);
231
232extern void xlog_redo(struct XLogReaderState *record);
233extern void xlog2_redo(struct XLogReaderState *record);
234extern void xlog_desc(StringInfo buf, struct XLogReaderState *record);
235extern void xlog2_desc(StringInfo buf, struct XLogReaderState *record);
236extern const char *xlog_identify(uint8 info);
237extern const char *xlog2_identify(uint8 info);
238
239extern void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli);
240
241extern bool RecoveryInProgress(void);
243extern bool XLogInsertAllowed(void);
246extern XLogRecPtr GetXLogWriteRecPtr(void);
247
248extern uint64 GetSystemIdentifier(void);
249extern char *GetMockAuthenticationNonce(void);
250extern bool DataChecksumsNeedWrite(void);
251extern bool DataChecksumsNeedVerify(void);
252extern bool DataChecksumsInProgressOn(void);
253extern void SetDataChecksumsOnInProgress(void);
254extern void SetDataChecksumsOn(void);
255extern void SetDataChecksumsOff(void);
256extern const char *show_data_checksums(void);
257extern const char *get_checksum_state_string(uint32 state);
258extern void InitLocalDataChecksumState(void);
259extern void SetLocalDataChecksumState(uint32 data_checksum_version);
260extern bool GetDefaultCharSignedness(void);
262extern void BootStrapXLOG(uint32 data_checksum_version);
263extern void InitializeWalConsistencyChecking(void);
264extern void LocalProcessControlFile(bool reset);
266extern void StartupXLOG(void);
267extern void ShutdownXLOG(int code, Datum arg);
268extern bool CreateCheckPoint(int flags);
269extern bool CreateRestartPoint(int flags);
271extern void XLogPutNextOid(Oid nextOid);
272extern XLogRecPtr XLogRestorePoint(const char *rpName);
273extern XLogRecPtr XLogAssignLSN(void);
274extern void UpdateFullPageWrites(void);
276extern XLogRecPtr GetRedoRecPtr(void);
277extern XLogRecPtr GetInsertRecPtr(void);
282
283extern void SetWalWriterSleeping(bool sleeping);
284
285extern void WakeupCheckpointer(void);
286
287extern Size WALReadFromBuffers(char *dstbuf, XLogRecPtr startptr, Size count,
288 TimeLineID tli);
289
290/*
291 * Routines used by xlogrecovery.c to call back into xlog.c during recovery.
292 */
297extern void SetInstallXLogFileSegmentActive(void);
298extern bool IsInstallXLogFileSegmentActive(void);
299extern void ResetInstallXLogFileSegmentActive(void);
300extern void XLogShutdownWalRcv(void);
301
302/*
303 * Routines to start, stop, and get status of a base backup.
304 */
305
306/*
307 * Session-level status of base backups
308 *
309 * This is used in parallel with the shared memory status to control parallel
310 * execution of base backup functions for a given session, be it a backend
311 * dedicated to replication or a normal backend connected to a database. The
312 * update of the session-level status happens at the same time as the shared
313 * memory counters to keep a consistent global and local state of the backups
314 * running.
321
322extern void do_pg_backup_start(const char *backupidstr, bool fast,
323 List **tablespaces, BackupState *state,
326extern void do_pg_abort_backup(int code, Datum arg);
329
330/* File path names (all relative to $PGDATA) */
331#define RECOVERY_SIGNAL_FILE "recovery.signal"
332#define STANDBY_SIGNAL_FILE "standby.signal"
333#define BACKUP_LABEL_FILE "backup_label"
334#define BACKUP_LABEL_OLD "backup_label.old"
336#define TABLESPACE_MAP "tablespace_map"
337#define TABLESPACE_MAP_OLD "tablespace_map.old"
338
339/* files to signal promotion to primary */
340#define PROMOTE_SIGNAL_FILE "promote"
341
342#endif /* XLOG_H */
#define PGDLLIMPORT
Definition c.h:1421
uint8_t uint8
Definition c.h:622
uint64_t uint64
Definition c.h:625
uint32_t uint32
Definition c.h:624
size_t Size
Definition c.h:689
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:188
uint64 ckpt_longest_sync
Definition xlog.h:187
TimestampTz ckpt_start_t
Definition xlog.h:173
TimestampTz ckpt_end_t
Definition xlog.h:177
int ckpt_segs_removed
Definition xlog.h:183
TimestampTz ckpt_write_t
Definition xlog.h:174
TimestampTz ckpt_sync_end_t
Definition xlog.h:176
TimestampTz ckpt_sync_t
Definition xlog.h:175
int ckpt_bufs_written
Definition xlog.h:179
int ckpt_segs_recycled
Definition xlog.h:184
int ckpt_slru_written
Definition xlog.h:180
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:3435
uint64 GetSystemIdentifier(void)
Definition xlog.c:4647
void UpdateFullPageWrites(void)
Definition xlog.c:8755
bool RecoveryInProgress(void)
Definition xlog.c:6830
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition xlog.c:6963
TimeLineID GetWALInsertionTimeLine(void)
Definition xlog.c:7016
PGDLLIMPORT bool log_checkpoints
Definition xlog.c:136
void do_pg_abort_backup(int code, Datum arg)
Definition xlog.c:10067
XLogSegNo XLogGetLastRemovedSegno(void)
Definition xlog.c:3813
void SetLocalDataChecksumState(uint32 data_checksum_version)
Definition xlog.c:4969
void xlog_desc(StringInfo buf, struct XLogReaderState *record)
Definition xlogdesc.c:93
Size WALReadFromBuffers(char *dstbuf, XLogRecPtr startptr, Size count, TimeLineID tli)
Definition xlog.c:1789
void SetDataChecksumsOff(void)
Definition xlog.c:4858
XLogRecPtr GetRedoRecPtr(void)
Definition xlog.c:6933
void SetInstallXLogFileSegmentActive(void)
Definition xlog.c:10172
void StartupXLOG(void)
Definition xlog.c:5846
bool IsInstallXLogFileSegmentActive(void)
Definition xlog.c:10189
PGDLLIMPORT XLogRecPtr XactLastRecEnd
Definition xlog.c:261
void BootStrapXLOG(uint32 data_checksum_version)
Definition xlog.c:5454
ArchiveMode
Definition xlog.h:66
@ ARCHIVE_MODE_ALWAYS
Definition xlog.h:69
@ ARCHIVE_MODE_OFF
Definition xlog.h:67
@ ARCHIVE_MODE_ON
Definition xlog.h:68
bool CreateRestartPoint(int flags)
Definition xlog.c:8129
PGDLLIMPORT int wal_sync_method
Definition xlog.c:137
PGDLLIMPORT bool EnableHotStandby
Definition xlog.c:128
XLogRecPtr GetInsertRecPtr(void)
Definition xlog.c:6978
XLogRecPtr XLogGetReplicationSlotMinimumLSN(void)
Definition xlog.c:2700
SessionBackupState get_backup_status(void)
Definition xlog.c:9774
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition xlog.c:3782
WALAvailability
Definition xlog.h:200
@ WALAVAIL_REMOVED
Definition xlog.h:206
@ WALAVAIL_RESERVED
Definition xlog.h:202
@ WALAVAIL_UNRESERVED
Definition xlog.h:205
@ WALAVAIL_EXTENDED
Definition xlog.h:203
@ WALAVAIL_INVALID_LSN
Definition xlog.h:201
PGDLLIMPORT CheckpointStatsData CheckpointStats
Definition xlog.c:216
WALAvailability GetWALAvailability(XLogRecPtr targetLSN)
Definition xlog.c:8414
PGDLLIMPORT int wal_retrieve_retry_interval
Definition xlog.c:141
void ShutdownXLOG(int code, Datum arg)
Definition xlog.c:7098
RecoveryState GetRecoveryState(void)
Definition xlog.c:6866
const char * xlog2_identify(uint8 info)
Definition xlogdesc.c:284
const char * xlog_identify(uint8 info)
Definition xlogdesc.c:224
PGDLLIMPORT int wal_compression
Definition xlog.c:131
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition xlog.c:2687
PGDLLIMPORT int CommitSiblings
Definition xlog.c:140
void SwitchIntoArchiveRecovery(XLogRecPtr EndRecPtr, TimeLineID replayTLI)
Definition xlog.c:6705
bool DataChecksumsNeedVerify(void)
Definition xlog.c:4706
WalCompression
Definition xlog.h:83
@ WAL_COMPRESSION_NONE
Definition xlog.h:84
@ WAL_COMPRESSION_LZ4
Definition xlog.h:86
@ WAL_COMPRESSION_PGLZ
Definition xlog.h:85
@ WAL_COMPRESSION_ZSTD
Definition xlog.h:87
void SetDataChecksumsOn(void)
Definition xlog.c:4786
bool GetDefaultCharSignedness(void)
Definition xlog.c:4991
PGDLLIMPORT int wal_decode_buffer_size
Definition xlog.c:143
XLogRecPtr GetXLogInsertRecPtr(void)
Definition xlog.c:10108
void SetWalWriterSleeping(bool sleeping)
Definition xlog.c:10204
void WakeupCheckpointer(void)
void SetDataChecksumsOnInProgress(void)
Definition xlog.c:4722
int XLogFileOpen(XLogSegNo segno, TimeLineID tli)
Definition xlog.c:3673
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition xlog.c:6995
WalLevel GetActiveWalLevelOnStandby(void)
Definition xlog.c:5284
PGDLLIMPORT int CheckPointSegments
Definition xlog.c:163
void xlog_redo(struct XLogReaderState *record)
Definition xlog.c:8824
void InitializeWalConsistencyChecking(void)
Definition xlog.c:5188
PGDLLIMPORT bool fullPageWrites
Definition xlog.c:129
void RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
Definition xlog.c:3995
XLogRecPtr GetLastImportantRecPtr(void)
Definition xlog.c:7052
PGDLLIMPORT XLogRecPtr ProcLastRecPtr
Definition xlog.c:260
PGDLLIMPORT bool XLogLogicalInfo
Definition logicalctl.c:115
PGDLLIMPORT char * wal_consistency_checking_string
Definition xlog.c:132
SessionBackupState
Definition xlog.h:316
@ SESSION_BACKUP_RUNNING
Definition xlog.h:318
@ SESSION_BACKUP_NONE
Definition xlog.h:317
XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi, uint64 fpi_bytes, bool topxid_included)
Definition xlog.c:784
bool XLogNeedsFlush(XLogRecPtr record)
Definition xlog.c:3163
PGDLLIMPORT int wal_segment_size
Definition xlog.c:150
void register_persistent_abort_backup_handler(void)
Definition xlog.c:10094
void ReachedEndOfBackup(XLogRecPtr EndRecPtr, TimeLineID tli)
Definition xlog.c:6743
void LocalProcessControlFile(bool reset)
Definition xlog.c:5269
XLogSegNo XLogGetOldestSegno(TimeLineID tli)
Definition xlog.c:3829
PGDLLIMPORT int XLogArchiveMode
Definition xlog.c:126
XLogRecPtr GetXLogWriteRecPtr(void)
Definition xlog.c:10140
PGDLLIMPORT char * XLogArchiveCommand
Definition xlog.c:127
void ResetInstallXLogFileSegmentActive(void)
Definition xlog.c:10181
PGDLLIMPORT int wal_keep_size_mb
Definition xlog.c:123
void xlog2_redo(struct XLogReaderState *record)
Definition xlog.c:9256
bool XLogBackgroundFlush(void)
Definition xlog.c:3006
char * GetMockAuthenticationNonce(void)
Definition xlog.c:4657
PGDLLIMPORT bool * wal_consistency_checking
Definition xlog.c:133
const char * show_data_checksums(void)
Definition xlog.c:4978
PGDLLIMPORT int max_slot_wal_keep_size_mb
Definition xlog.c:142
bool DataChecksumsInProgressOn(void)
Definition xlog.c:4686
bool XLogInsertAllowed(void)
Definition xlog.c:6885
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition xlog.c:9471
PGDLLIMPORT bool wal_init_zero
Definition xlog.c:134
void xlog2_desc(StringInfo buf, struct XLogReaderState *record)
Definition xlogdesc.c:78
const char * get_checksum_state_string(uint32 state)
Definition xlogdesc.c:59
PGDLLIMPORT int min_wal_size_mb
Definition xlog.c:122
PGDLLIMPORT int CommitDelay
Definition xlog.c:139
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition xlog.c:8625
PGDLLIMPORT int XLOGbuffers
Definition xlog.c:124
TimeLineID GetWALInsertionTimeLineIfSet(void)
Definition xlog.c:7032
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition xlog.c:9793
PGDLLIMPORT bool wal_recycle
Definition xlog.c:135
WalLevel
Definition xlog.h:75
@ WAL_LEVEL_REPLICA
Definition xlog.h:77
@ WAL_LEVEL_LOGICAL
Definition xlog.h:78
@ WAL_LEVEL_MINIMAL
Definition xlog.h:76
bool CreateCheckPoint(int flags)
Definition xlog.c:7395
PGDLLIMPORT int data_checksums
Definition xlog.c:683
bool DataChecksumsNeedWrite(void)
Definition xlog.c:4678
XLogRecPtr GetFakeLSNForUnloggedRel(void)
Definition xlog.c:5006
PGDLLIMPORT int XLogArchiveTimeout
Definition xlog.c:125
XLogRecPtr GetXLogInsertEndRecPtr(void)
Definition xlog.c:10124
void XLogPutNextOid(Oid nextOid)
Definition xlog.c:8570
void XLogFlush(XLogRecPtr record)
Definition xlog.c:2801
PGDLLIMPORT XLogRecPtr XactLastCommitEnd
Definition xlog.c:262
XLogRecPtr XLogAssignLSN(void)
Definition xlog.c:8655
void InitLocalDataChecksumState(void)
Definition xlog.c:4961
PGDLLIMPORT int wal_level
Definition xlog.c:138
PGDLLIMPORT bool wal_log_hints
Definition xlog.c:130
RecoveryState
Definition xlog.h:92
@ RECOVERY_STATE_CRASH
Definition xlog.h:93
@ RECOVERY_STATE_DONE
Definition xlog.h:95
@ RECOVERY_STATE_ARCHIVE
Definition xlog.h:94
void XLogShutdownWalRcv(void)
Definition xlog.c:10162
void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
Definition xlog.c:9374
PGDLLIMPORT int max_wal_size_mb
Definition xlog.c:121
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition xlog.c:2630
PGDLLIMPORT bool track_wal_io_timing
Definition xlog.c:144
bool XLogCheckpointNeeded(XLogSegNo new_segno)
Definition xlog.c:2301
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