PostgreSQL Source Code  git master
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-2024, 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"
16 #include "datatype/timestamp.h"
17 #include "lib/stringinfo.h"
18 #include "nodes/pg_list.h"
19 
20 
21 /* Sync methods */
23 {
26  WAL_SYNC_METHOD_OPEN, /* for O_SYNC */
28  WAL_SYNC_METHOD_OPEN_DSYNC /* for O_DSYNC */
29 };
30 extern PGDLLIMPORT int wal_sync_method;
31 
35 
36 /* these variables are GUC parameters related to XLOG */
37 extern PGDLLIMPORT int wal_segment_size;
38 extern PGDLLIMPORT int min_wal_size_mb;
39 extern PGDLLIMPORT int max_wal_size_mb;
40 extern PGDLLIMPORT int wal_keep_size_mb;
42 extern PGDLLIMPORT int XLOGbuffers;
45 extern PGDLLIMPORT char *XLogArchiveCommand;
46 extern PGDLLIMPORT bool EnableHotStandby;
47 extern PGDLLIMPORT bool fullPageWrites;
48 extern PGDLLIMPORT bool wal_log_hints;
49 extern PGDLLIMPORT int wal_compression;
50 extern PGDLLIMPORT bool wal_init_zero;
51 extern PGDLLIMPORT bool wal_recycle;
54 extern PGDLLIMPORT bool log_checkpoints;
55 extern PGDLLIMPORT int CommitDelay;
56 extern PGDLLIMPORT int CommitSiblings;
59 
61 
62 /* Archive modes */
63 typedef enum ArchiveMode
64 {
65  ARCHIVE_MODE_OFF = 0, /* disabled */
66  ARCHIVE_MODE_ON, /* enabled while server is running normally */
67  ARCHIVE_MODE_ALWAYS, /* enabled always (even during recovery) */
69 extern PGDLLIMPORT int XLogArchiveMode;
70 
71 /* WAL levels */
72 typedef enum WalLevel
73 {
78 
79 /* Compression algorithms for WAL */
80 typedef enum WalCompression
81 {
87 
88 /* Recovery states */
89 typedef enum RecoveryState
90 {
91  RECOVERY_STATE_CRASH = 0, /* crash recovery */
92  RECOVERY_STATE_ARCHIVE, /* archive recovery */
93  RECOVERY_STATE_DONE, /* currently in production */
95 
96 extern PGDLLIMPORT int wal_level;
97 
98 /* Is WAL archiving enabled (always or only while server is running normally)? */
99 #define XLogArchivingActive() \
100  (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode > ARCHIVE_MODE_OFF)
101 /* Is WAL archiving enabled always (even during recovery)? */
102 #define XLogArchivingAlways() \
103  (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode == ARCHIVE_MODE_ALWAYS)
104 
105 /*
106  * Is WAL-logging necessary for archival or log-shipping, or can we skip
107  * WAL-logging if we fsync() the data before committing instead?
108  */
109 #define XLogIsNeeded() (wal_level >= WAL_LEVEL_REPLICA)
110 
111 /*
112  * Is a full-page image needed for hint bit updates?
113  *
114  * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,
115  * we have to protect them against torn page writes. When you only set
116  * individual bits on a page, it's still consistent no matter what combination
117  * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log
118  * them if forced by wal_log_hints=on.
119  */
120 #define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints)
121 
122 /* Do we need to WAL-log information required only for Hot Standby and logical replication? */
123 #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_REPLICA)
124 
125 /* Do we need to WAL-log information required only for logical replication? */
126 #define XLogLogicalInfoActive() (wal_level >= WAL_LEVEL_LOGICAL)
127 
128 #ifdef WAL_DEBUG
129 extern PGDLLIMPORT bool XLOG_DEBUG;
130 #endif
131 
132 /*
133  * OR-able request flag bits for checkpoints. The "cause" bits are used only
134  * for logging purposes. Note: the flags must be defined so that it's
135  * sensible to OR together request flags arising from different requestors.
136  */
137 
138 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
139 #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
140 #define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint, but
141  * issued at end of WAL recovery */
142 #define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */
143 #define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
144 #define CHECKPOINT_FLUSH_ALL 0x0010 /* Flush all pages, including those
145  * belonging to unlogged tables */
146 /* These are important to RequestCheckpoint */
147 #define CHECKPOINT_WAIT 0x0020 /* Wait for completion */
148 #define CHECKPOINT_REQUESTED 0x0040 /* Checkpoint request has been made */
149 /* These indicate the cause of a checkpoint request */
150 #define CHECKPOINT_CAUSE_XLOG 0x0080 /* XLOG consumption */
151 #define CHECKPOINT_CAUSE_TIME 0x0100 /* Elapsed time */
152 
153 /*
154  * Flag bits for the record being inserted, set using XLogSetRecordFlags().
155  */
156 #define XLOG_INCLUDE_ORIGIN 0x01 /* include the replication origin */
157 #define XLOG_MARK_UNIMPORTANT 0x02 /* record not important for durability */
158 
159 
160 /* Checkpoint statistics */
161 typedef struct CheckpointStatsData
162 {
163  TimestampTz ckpt_start_t; /* start of checkpoint */
164  TimestampTz ckpt_write_t; /* start of flushing buffers */
165  TimestampTz ckpt_sync_t; /* start of fsyncs */
166  TimestampTz ckpt_sync_end_t; /* end of fsyncs */
167  TimestampTz ckpt_end_t; /* end of checkpoint */
168 
169  int ckpt_bufs_written; /* # of buffers written */
170 
171  int ckpt_segs_added; /* # of new xlog segments created */
172  int ckpt_segs_removed; /* # of xlog segments deleted */
173  int ckpt_segs_recycled; /* # of xlog segments recycled */
174 
175  int ckpt_sync_rels; /* # of relations synced */
176  uint64 ckpt_longest_sync; /* Longest sync for one relation */
177  uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
178  * times, which is not necessarily the
179  * same as the total elapsed time for the
180  * entire sync phase. */
182 
184 
185 /*
186  * GetWALAvailability return codes
187  */
188 typedef enum WALAvailability
189 {
190  WALAVAIL_INVALID_LSN, /* parameter error */
191  WALAVAIL_RESERVED, /* WAL segment is within max_wal_size */
192  WALAVAIL_EXTENDED, /* WAL segment is reserved by a slot or
193  * wal_keep_size */
194  WALAVAIL_UNRESERVED, /* no longer reserved, but not removed yet */
195  WALAVAIL_REMOVED, /* WAL segment has been removed */
197 
198 struct XLogRecData;
199 struct XLogReaderState;
200 
201 extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata,
202  XLogRecPtr fpw_lsn,
203  uint8 flags,
204  int num_fpi,
205  bool topxid_included);
206 extern void XLogFlush(XLogRecPtr record);
207 extern bool XLogBackgroundFlush(void);
208 extern bool XLogNeedsFlush(XLogRecPtr record);
209 extern int XLogFileInit(XLogSegNo logsegno, TimeLineID logtli);
210 extern int XLogFileOpen(XLogSegNo segno, TimeLineID tli);
211 
212 extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
215 extern void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN);
217 
218 extern void xlog_redo(struct XLogReaderState *record);
219 extern void xlog_desc(StringInfo buf, struct XLogReaderState *record);
220 extern const char *xlog_identify(uint8 info);
221 
222 extern void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli);
223 
224 extern bool RecoveryInProgress(void);
225 extern RecoveryState GetRecoveryState(void);
226 extern bool XLogInsertAllowed(void);
227 extern XLogRecPtr GetXLogInsertRecPtr(void);
228 extern XLogRecPtr GetXLogWriteRecPtr(void);
229 
230 extern uint64 GetSystemIdentifier(void);
231 extern char *GetMockAuthenticationNonce(void);
232 extern bool DataChecksumsEnabled(void);
234 extern Size XLOGShmemSize(void);
235 extern void XLOGShmemInit(void);
236 extern void BootStrapXLOG(uint32 data_checksum_version);
237 extern void InitializeWalConsistencyChecking(void);
238 extern void LocalProcessControlFile(bool reset);
240 extern void StartupXLOG(void);
241 extern void ShutdownXLOG(int code, Datum arg);
242 extern void CreateCheckPoint(int flags);
243 extern bool CreateRestartPoint(int flags);
245 extern void XLogPutNextOid(Oid nextOid);
246 extern XLogRecPtr XLogRestorePoint(const char *rpName);
247 extern void UpdateFullPageWrites(void);
248 extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p);
249 extern XLogRecPtr GetRedoRecPtr(void);
250 extern XLogRecPtr GetInsertRecPtr(void);
251 extern XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI);
255 
256 extern void SetWalWriterSleeping(bool sleeping);
257 
258 extern Size WALReadFromBuffers(char *dstbuf, XLogRecPtr startptr, Size count,
259  TimeLineID tli);
260 
261 /*
262  * Routines used by xlogrecovery.c to call back into xlog.c during recovery.
263  */
264 extern void RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI);
265 extern bool XLogCheckpointNeeded(XLogSegNo new_segno);
268 extern void SetInstallXLogFileSegmentActive(void);
269 extern bool IsInstallXLogFileSegmentActive(void);
270 extern void XLogShutdownWalRcv(void);
271 
272 /*
273  * Routines to start, stop, and get status of a base backup.
274  */
275 
276 /*
277  * Session-level status of base backups
278  *
279  * This is used in parallel with the shared memory status to control parallel
280  * execution of base backup functions for a given session, be it a backend
281  * dedicated to replication or a normal backend connected to a database. The
282  * update of the session-level status happens at the same time as the shared
283  * memory counters to keep a consistent global and local state of the backups
284  * running.
285  */
286 typedef enum SessionBackupState
287 {
291 
292 extern void do_pg_backup_start(const char *backupidstr, bool fast,
293  List **tablespaces, BackupState *state,
294  StringInfo tblspcmapfile);
295 extern void do_pg_backup_stop(BackupState *state, bool waitforarchive);
296 extern void do_pg_abort_backup(int code, Datum arg);
299 
300 /* File path names (all relative to $PGDATA) */
301 #define RECOVERY_SIGNAL_FILE "recovery.signal"
302 #define STANDBY_SIGNAL_FILE "standby.signal"
303 #define BACKUP_LABEL_FILE "backup_label"
304 #define BACKUP_LABEL_OLD "backup_label.old"
305 
306 #define TABLESPACE_MAP "tablespace_map"
307 #define TABLESPACE_MAP_OLD "tablespace_map.old"
308 
309 /* files to signal promotion to primary */
310 #define PROMOTE_SIGNAL_FILE "promote"
311 
312 #endif /* XLOG_H */
unsigned int uint32
Definition: c.h:506
#define PGDLLIMPORT
Definition: c.h:1316
unsigned char uint8
Definition: c.h:504
size_t Size
Definition: c.h:605
int64 TimestampTz
Definition: timestamp.h:39
void * arg
static char * buf
Definition: pg_test_fsync.c:73
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void reset(void)
Definition: sql-declare.c:600
uint64 ckpt_agg_sync_time
Definition: xlog.h:175
uint64 ckpt_longest_sync
Definition: xlog.h:174
TimestampTz ckpt_start_t
Definition: xlog.h:161
TimestampTz ckpt_end_t
Definition: xlog.h:165
int ckpt_segs_removed
Definition: xlog.h:170
TimestampTz ckpt_write_t
Definition: xlog.h:162
int ckpt_segs_added
Definition: xlog.h:169
TimestampTz ckpt_sync_end_t
Definition: xlog.h:164
TimestampTz ckpt_sync_t
Definition: xlog.h:163
int ckpt_bufs_written
Definition: xlog.h:167
int ckpt_segs_recycled
Definition: xlog.h:171
int ckpt_sync_rels
Definition: xlog.h:173
Definition: pg_list.h:54
DecodedXLogRecord * record
Definition: xlogreader.h:236
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
Definition: regguts.h:323
int XLogFileInit(XLogSegNo logsegno, TimeLineID logtli)
Definition: xlog.c:3373
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4561
void UpdateFullPageWrites(void)
Definition: xlog.c:8158
bool RecoveryInProgress(void)
Definition: xlog.c:6333
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:6466
TimeLineID GetWALInsertionTimeLine(void)
Definition: xlog.c:6519
PGDLLIMPORT bool log_checkpoints
Definition: xlog.c:128
void do_pg_abort_backup(int code, Datum arg)
Definition: xlog.c:9386
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3751
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:1747
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6436
void SetInstallXLogFileSegmentActive(void)
Definition: xlog.c:9476
XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi, bool topxid_included)
Definition: xlog.c:747
void StartupXLOG(void)
Definition: xlog.c:5422
bool IsInstallXLogFileSegmentActive(void)
Definition: xlog.c:9484
PGDLLIMPORT XLogRecPtr XactLastRecEnd
Definition: xlog.c:253
void BootStrapXLOG(uint32 data_checksum_version)
Definition: xlog.c:5026
ArchiveMode
Definition: xlog.h:64
@ ARCHIVE_MODE_ALWAYS
Definition: xlog.h:67
@ ARCHIVE_MODE_OFF
Definition: xlog.h:65
@ ARCHIVE_MODE_ON
Definition: xlog.h:66
bool CreateRestartPoint(int flags)
Definition: xlog.c:7583
PGDLLIMPORT int wal_sync_method
Definition: xlog.c:129
PGDLLIMPORT bool EnableHotStandby
Definition: xlog.c:120
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:6481
SessionBackupState get_backup_status(void)
Definition: xlog.c:9093
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition: xlog.c:3720
WALAvailability
Definition: xlog.h:187
@ WALAVAIL_REMOVED
Definition: xlog.h:193
@ WALAVAIL_RESERVED
Definition: xlog.h:189
@ WALAVAIL_UNRESERVED
Definition: xlog.h:192
@ WALAVAIL_EXTENDED
Definition: xlog.h:190
@ WALAVAIL_INVALID_LSN
Definition: xlog.h:188
PGDLLIMPORT CheckpointStatsData CheckpointStats
Definition: xlog.c:208
WALAvailability GetWALAvailability(XLogRecPtr targetLSN)
Definition: xlog.c:7861
PGDLLIMPORT int wal_retrieve_retry_interval
Definition: xlog.c:133
void XLOGShmemInit(void)
Definition: xlog.c:4911
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:6601
bool DataChecksumsEnabled(void)
Definition: xlog.c:4581
RecoveryState GetRecoveryState(void)
Definition: xlog.c:6369
char * GetMockAuthenticationNonce(void)
Definition: xlog.c:4571
PGDLLIMPORT int wal_compression
Definition: xlog.c:123
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition: xlog.c:2681
PGDLLIMPORT int CommitSiblings
Definition: xlog.c:132
void SwitchIntoArchiveRecovery(XLogRecPtr EndRecPtr, TimeLineID replayTLI)
Definition: xlog.c:6208
WalCompression
Definition: xlog.h:81
@ WAL_COMPRESSION_NONE
Definition: xlog.h:82
@ WAL_COMPRESSION_LZ4
Definition: xlog.h:84
@ WAL_COMPRESSION_PGLZ
Definition: xlog.h:83
@ WAL_COMPRESSION_ZSTD
Definition: xlog.h:85
PGDLLIMPORT int wal_decode_buffer_size
Definition: xlog.c:135
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:9427
Size XLOGShmemSize(void)
Definition: xlog.c:4861
void SetWalWriterSleeping(bool sleeping)
Definition: xlog.c:9499
struct CheckpointStatsData CheckpointStatsData
int XLogFileOpen(XLogSegNo segno, TimeLineID tli)
Definition: xlog.c:3611
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition: xlog.c:6498
WalLevel GetActiveWalLevelOnStandby(void)
Definition: xlog.c:4852
PGDLLIMPORT int CheckPointSegments
Definition: xlog.c:155
void xlog_redo(struct XLogReaderState *record)
Definition: xlog.c:8227
void InitializeWalConsistencyChecking(void)
Definition: xlog.c:4777
PGDLLIMPORT bool fullPageWrites
Definition: xlog.c:121
void RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
Definition: xlog.c:3933
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:6555
PGDLLIMPORT XLogRecPtr ProcLastRecPtr
Definition: xlog.c:252
PGDLLIMPORT char * wal_consistency_checking_string
Definition: xlog.c:124
SessionBackupState
Definition: xlog.h:285
@ SESSION_BACKUP_RUNNING
Definition: xlog.h:287
@ SESSION_BACKUP_NONE
Definition: xlog.h:286
bool XLogNeedsFlush(XLogRecPtr record)
Definition: xlog.c:3126
PGDLLIMPORT int wal_segment_size
Definition: xlog.c:142
void register_persistent_abort_backup_handler(void)
Definition: xlog.c:9413
void ReachedEndOfBackup(XLogRecPtr EndRecPtr, TimeLineID tli)
Definition: xlog.c:6246
void LocalProcessControlFile(bool reset)
Definition: xlog.c:4839
XLogSegNo XLogGetOldestSegno(TimeLineID tli)
Definition: xlog.c:3767
PGDLLIMPORT int XLogArchiveMode
Definition: xlog.c:118
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:9443
PGDLLIMPORT char * XLogArchiveCommand
Definition: xlog.c:119
PGDLLIMPORT int wal_keep_size_mb
Definition: xlog.c:115
bool XLogBackgroundFlush(void)
Definition: xlog.c:2983
PGDLLIMPORT bool * wal_consistency_checking
Definition: xlog.c:125
PGDLLIMPORT int max_slot_wal_keep_size_mb
Definition: xlog.c:134
bool XLogInsertAllowed(void)
Definition: xlog.c:6388
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition: xlog.c:8784
PGDLLIMPORT bool wal_init_zero
Definition: xlog.c:126
PGDLLIMPORT int min_wal_size_mb
Definition: xlog.c:114
PGDLLIMPORT int CommitDelay
Definition: xlog.c:131
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition: xlog.c:8070
const char * xlog_identify(uint8 info)
Definition: xlogdesc.c:173
PGDLLIMPORT int XLOGbuffers
Definition: xlog.c:116
TimeLineID GetWALInsertionTimeLineIfSet(void)
Definition: xlog.c:6535
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition: xlog.c:9112
PGDLLIMPORT bool wal_recycle
Definition: xlog.c:127
WalLevel
Definition: xlog.h:73
@ WAL_LEVEL_REPLICA
Definition: xlog.h:75
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:76
@ WAL_LEVEL_MINIMAL
Definition: xlog.h:74
XLogRecPtr GetFakeLSNForUnloggedRel(void)
Definition: xlog.c:4597
PGDLLIMPORT int XLogArchiveTimeout
Definition: xlog.c:117
void XLogPutNextOid(Oid nextOid)
Definition: xlog.c:8015
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2795
PGDLLIMPORT XLogRecPtr XactLastCommitEnd
Definition: xlog.c:254
PGDLLIMPORT int wal_level
Definition: xlog.c:130
PGDLLIMPORT bool wal_log_hints
Definition: xlog.c:122
RecoveryState
Definition: xlog.h:90
@ RECOVERY_STATE_CRASH
Definition: xlog.h:91
@ RECOVERY_STATE_DONE
Definition: xlog.h:93
@ RECOVERY_STATE_ARCHIVE
Definition: xlog.h:92
void XLogShutdownWalRcv(void)
Definition: xlog.c:9465
void CreateCheckPoint(int flags)
Definition: xlog.c:6883
void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
Definition: xlog.c:8675
PGDLLIMPORT int max_wal_size_mb
Definition: xlog.c:113
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2630
PGDLLIMPORT bool track_wal_io_timing
Definition: xlog.c:136
bool XLogCheckpointNeeded(XLogSegNo new_segno)
Definition: xlog.c:2289
WalSyncMethod
Definition: xlog.h:23
@ WAL_SYNC_METHOD_OPEN
Definition: xlog.h:26
@ WAL_SYNC_METHOD_FDATASYNC
Definition: xlog.h:25
@ WAL_SYNC_METHOD_FSYNC_WRITETHROUGH
Definition: xlog.h:27
@ WAL_SYNC_METHOD_OPEN_DSYNC
Definition: xlog.h:28
@ WAL_SYNC_METHOD_FSYNC
Definition: xlog.h:24
uint64 XLogRecPtr
Definition: xlogdefs.h:21
uint32 TimeLineID
Definition: xlogdefs.h:59
uint64 XLogSegNo
Definition: xlogdefs.h:48