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);
229
230extern void xlog_redo(struct XLogReaderState *record);
231extern void xlog_desc(StringInfo buf, struct XLogReaderState *record);
232extern const char *xlog_identify(uint8 info);
233
234extern void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli);
235
236extern bool RecoveryInProgress(void);
238extern bool XLogInsertAllowed(void);
240extern XLogRecPtr GetXLogWriteRecPtr(void);
241
242extern uint64 GetSystemIdentifier(void);
243extern char *GetMockAuthenticationNonce(void);
244extern bool DataChecksumsEnabled(void);
245extern bool GetDefaultCharSignedness(void);
247extern Size XLOGShmemSize(void);
248extern void XLOGShmemInit(void);
249extern void BootStrapXLOG(uint32 data_checksum_version);
250extern void InitializeWalConsistencyChecking(void);
251extern void LocalProcessControlFile(bool reset);
253extern void StartupXLOG(void);
254extern void ShutdownXLOG(int code, Datum arg);
255extern bool CreateCheckPoint(int flags);
256extern bool CreateRestartPoint(int flags);
258extern void XLogPutNextOid(Oid nextOid);
259extern XLogRecPtr XLogRestorePoint(const char *rpName);
260extern void UpdateFullPageWrites(void);
262extern XLogRecPtr GetRedoRecPtr(void);
263extern XLogRecPtr GetInsertRecPtr(void);
268
269extern void SetWalWriterSleeping(bool sleeping);
270
271extern void WakeupCheckpointer(void);
272
273extern Size WALReadFromBuffers(char *dstbuf, XLogRecPtr startptr, Size count,
274 TimeLineID tli);
275
276/*
277 * Routines used by xlogrecovery.c to call back into xlog.c during recovery.
278 */
283extern void SetInstallXLogFileSegmentActive(void);
284extern bool IsInstallXLogFileSegmentActive(void);
285extern void ResetInstallXLogFileSegmentActive(void);
286extern void XLogShutdownWalRcv(void);
287
288/*
289 * Routines to start, stop, and get status of a base backup.
290 */
291
292/*
293 * Session-level status of base backups
294 *
295 * This is used in parallel with the shared memory status to control parallel
296 * execution of base backup functions for a given session, be it a backend
297 * dedicated to replication or a normal backend connected to a database. The
298 * update of the session-level status happens at the same time as the shared
299 * memory counters to keep a consistent global and local state of the backups
300 * running.
307
308extern void do_pg_backup_start(const char *backupidstr, bool fast,
309 List **tablespaces, BackupState *state,
312extern void do_pg_abort_backup(int code, Datum arg);
315
316/* File path names (all relative to $PGDATA) */
317#define RECOVERY_SIGNAL_FILE "recovery.signal"
318#define STANDBY_SIGNAL_FILE "standby.signal"
319#define BACKUP_LABEL_FILE "backup_label"
320#define BACKUP_LABEL_OLD "backup_label.old"
322#define TABLESPACE_MAP "tablespace_map"
323#define TABLESPACE_MAP_OLD "tablespace_map.old"
324
325/* files to signal promotion to primary */
326#define PROMOTE_SIGNAL_FILE "promote"
327
328#endif /* XLOG_H */
#define PGDLLIMPORT
Definition c.h:1328
uint8_t uint8
Definition c.h:554
uint64_t uint64
Definition c.h:557
uint32_t uint32
Definition c.h:556
size_t Size
Definition c.h:629
int64 TimestampTz
Definition timestamp.h:39
void * arg
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:3418
uint64 GetSystemIdentifier(void)
Definition xlog.c:4628
void UpdateFullPageWrites(void)
Definition xlog.c:8299
bool RecoveryInProgress(void)
Definition xlog.c:6461
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition xlog.c:6594
TimeLineID GetWALInsertionTimeLine(void)
Definition xlog.c:6647
PGDLLIMPORT bool log_checkpoints
Definition xlog.c:132
void do_pg_abort_backup(int code, Datum arg)
Definition xlog.c:9555
XLogSegNo XLogGetLastRemovedSegno(void)
Definition xlog.c:3796
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:6564
void SetInstallXLogFileSegmentActive(void)
Definition xlog.c:9644
void StartupXLOG(void)
Definition xlog.c:5518
bool IsInstallXLogFileSegmentActive(void)
Definition xlog.c:9661
PGDLLIMPORT XLogRecPtr XactLastRecEnd
Definition xlog.c:257
void BootStrapXLOG(uint32 data_checksum_version)
Definition xlog.c:5127
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:7722
PGDLLIMPORT int wal_sync_method
Definition xlog.c:133
PGDLLIMPORT bool EnableHotStandby
Definition xlog.c:124
XLogRecPtr GetInsertRecPtr(void)
Definition xlog.c:6609
SessionBackupState get_backup_status(void)
Definition xlog.c:9262
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition xlog.c:3765
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:212
WALAvailability GetWALAvailability(XLogRecPtr targetLSN)
Definition xlog.c:8000
PGDLLIMPORT int wal_retrieve_retry_interval
Definition xlog.c:137
void XLOGShmemInit(void)
Definition xlog.c:5012
void ShutdownXLOG(int code, Datum arg)
Definition xlog.c:6729
bool DataChecksumsEnabled(void)
Definition xlog.c:4648
RecoveryState GetRecoveryState(void)
Definition xlog.c:6497
const char * xlog_identify(uint8 info)
Definition xlogdesc.c:181
PGDLLIMPORT int wal_compression
Definition xlog.c:127
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition xlog.c:2670
PGDLLIMPORT int CommitSiblings
Definition xlog.c:136
void SwitchIntoArchiveRecovery(XLogRecPtr EndRecPtr, TimeLineID replayTLI)
Definition xlog.c:6336
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:4662
PGDLLIMPORT int wal_decode_buffer_size
Definition xlog.c:139
XLogRecPtr GetXLogInsertRecPtr(void)
Definition xlog.c:9596
Size XLOGShmemSize(void)
Definition xlog.c:4962
void SetWalWriterSleeping(bool sleeping)
Definition xlog.c:9676
void WakeupCheckpointer(void)
int XLogFileOpen(XLogSegNo segno, TimeLineID tli)
Definition xlog.c:3656
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition xlog.c:6626
WalLevel GetActiveWalLevelOnStandby(void)
Definition xlog.c:4953
PGDLLIMPORT int CheckPointSegments
Definition xlog.c:159
void xlog_redo(struct XLogReaderState *record)
Definition xlog.c:8368
void InitializeWalConsistencyChecking(void)
Definition xlog.c:4859
PGDLLIMPORT bool fullPageWrites
Definition xlog.c:125
void RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
Definition xlog.c:3978
XLogRecPtr GetLastImportantRecPtr(void)
Definition xlog.c:6683
PGDLLIMPORT XLogRecPtr ProcLastRecPtr
Definition xlog.c:256
PGDLLIMPORT bool XLogLogicalInfo
Definition logicalctl.c:107
PGDLLIMPORT char * wal_consistency_checking_string
Definition xlog.c:128
SessionBackupState
Definition xlog.h:302
@ SESSION_BACKUP_RUNNING
Definition xlog.h:304
@ SESSION_BACKUP_NONE
Definition xlog.h:303
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:3146
PGDLLIMPORT int wal_segment_size
Definition xlog.c:146
void register_persistent_abort_backup_handler(void)
Definition xlog.c:9582
void ReachedEndOfBackup(XLogRecPtr EndRecPtr, TimeLineID tli)
Definition xlog.c:6374
void LocalProcessControlFile(bool reset)
Definition xlog.c:4940
XLogSegNo XLogGetOldestSegno(TimeLineID tli)
Definition xlog.c:3812
PGDLLIMPORT int XLogArchiveMode
Definition xlog.c:122
XLogRecPtr GetXLogWriteRecPtr(void)
Definition xlog.c:9612
PGDLLIMPORT char * XLogArchiveCommand
Definition xlog.c:123
void ResetInstallXLogFileSegmentActive(void)
Definition xlog.c:9653
PGDLLIMPORT int wal_keep_size_mb
Definition xlog.c:119
bool XLogBackgroundFlush(void)
Definition xlog.c:2989
char * GetMockAuthenticationNonce(void)
Definition xlog.c:4638
PGDLLIMPORT bool * wal_consistency_checking
Definition xlog.c:129
PGDLLIMPORT int max_slot_wal_keep_size_mb
Definition xlog.c:138
bool XLogInsertAllowed(void)
Definition xlog.c:6516
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition xlog.c:8953
PGDLLIMPORT bool wal_init_zero
Definition xlog.c:130
PGDLLIMPORT int min_wal_size_mb
Definition xlog.c:118
PGDLLIMPORT int CommitDelay
Definition xlog.c:135
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition xlog.c:8211
PGDLLIMPORT int XLOGbuffers
Definition xlog.c:120
TimeLineID GetWALInsertionTimeLineIfSet(void)
Definition xlog.c:6663
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition xlog.c:9281
PGDLLIMPORT bool wal_recycle
Definition xlog.c:131
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:7016
XLogRecPtr GetFakeLSNForUnloggedRel(void)
Definition xlog.c:4677
PGDLLIMPORT int XLogArchiveTimeout
Definition xlog.c:121
void XLogPutNextOid(Oid nextOid)
Definition xlog.c:8156
void XLogFlush(XLogRecPtr record)
Definition xlog.c:2784
PGDLLIMPORT XLogRecPtr XactLastCommitEnd
Definition xlog.c:258
PGDLLIMPORT int wal_level
Definition xlog.c:134
PGDLLIMPORT bool wal_log_hints
Definition xlog.c:126
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:9634
void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
Definition xlog.c:8856
PGDLLIMPORT int max_wal_size_mb
Definition xlog.c:117
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition xlog.c:2613
PGDLLIMPORT bool track_wal_io_timing
Definition xlog.c:140
bool XLogCheckpointNeeded(XLogSegNo new_segno)
Definition xlog.c:2284
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