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-2019, 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/rmgr.h"
15 #include "access/xlogdefs.h"
16 #include "access/xloginsert.h"
17 #include "access/xlogreader.h"
18 #include "datatype/timestamp.h"
19 #include "lib/stringinfo.h"
20 #include "nodes/pg_list.h"
21 #include "storage/fd.h"
22 
23 
24 /* Sync methods */
25 #define SYNC_METHOD_FSYNC 0
26 #define SYNC_METHOD_FDATASYNC 1
27 #define SYNC_METHOD_OPEN 2 /* for O_SYNC */
28 #define SYNC_METHOD_FSYNC_WRITETHROUGH 3
29 #define SYNC_METHOD_OPEN_DSYNC 4 /* for O_DSYNC */
30 extern int sync_method;
31 
32 extern PGDLLIMPORT TimeLineID ThisTimeLineID; /* current TLI */
33 
34 /*
35  * Prior to 8.4, all activity during recovery was carried out by the startup
36  * process. This local variable continues to be used in many parts of the
37  * code to indicate actions taken by RecoveryManagers. Other processes that
38  * potentially perform work during recovery should check RecoveryInProgress().
39  * See XLogCtl notes in xlog.c.
40  */
41 extern bool InRecovery;
42 
43 /*
44  * Like InRecovery, standbyState is only valid in the startup process.
45  * In all other processes it will have the value STANDBY_DISABLED (so
46  * InHotStandby will read as false).
47  *
48  * In DISABLED state, we're performing crash recovery or hot standby was
49  * disabled in postgresql.conf.
50  *
51  * In INITIALIZED state, we've run InitRecoveryTransactionEnvironment, but
52  * we haven't yet processed a RUNNING_XACTS or shutdown-checkpoint WAL record
53  * to initialize our master-transaction tracking system.
54  *
55  * When the transaction tracking is initialized, we enter the SNAPSHOT_PENDING
56  * state. The tracked information might still be incomplete, so we can't allow
57  * connections yet, but redo functions must update the in-memory state when
58  * appropriate.
59  *
60  * In SNAPSHOT_READY mode, we have full knowledge of transactions that are
61  * (or were) running in the master at the current WAL location. Snapshots
62  * can be taken, and read-only queries can be run.
63  */
64 typedef enum
65 {
71 
73 
74 #define InHotStandby (standbyState >= STANDBY_SNAPSHOT_PENDING)
75 
76 /*
77  * Recovery target type.
78  * Only set during a Point in Time recovery, not when in standby mode.
79  */
80 typedef enum
81 {
89 
90 /*
91  * Recovery target TimeLine goal
92  */
93 typedef enum
94 {
99 
103 
104 extern bool reachedConsistency;
105 
106 /* these variables are GUC parameters related to XLOG */
107 extern int wal_segment_size;
108 extern int min_wal_size_mb;
109 extern int max_wal_size_mb;
110 extern int wal_keep_segments;
111 extern int XLOGbuffers;
112 extern int XLogArchiveTimeout;
113 extern int wal_retrieve_retry_interval;
114 extern char *XLogArchiveCommand;
115 extern bool EnableHotStandby;
116 extern bool fullPageWrites;
117 extern bool wal_log_hints;
118 extern bool wal_compression;
119 extern bool wal_init_zero;
120 extern bool wal_recycle;
121 extern bool *wal_consistency_checking;
123 extern bool log_checkpoints;
124 extern char *recoveryRestoreCommand;
125 extern char *recoveryEndCommand;
126 extern char *archiveCleanupCommand;
127 extern bool recoveryTargetInclusive;
128 extern int recoveryTargetAction;
129 extern int recovery_min_apply_delay;
130 extern char *PrimaryConnInfo;
131 extern char *PrimarySlotName;
132 
133 /* indirectly set via GUC system */
135 extern char *recovery_target_time_string;
136 extern const char *recoveryTargetName;
139 extern char *PromoteTriggerFile;
143 
144 extern int CheckPointSegments;
145 
146 /* option set locally in startup process only when signal files exist */
147 extern bool StandbyModeRequested;
148 extern bool StandbyMode;
149 
150 /* Archive modes */
151 typedef enum ArchiveMode
152 {
153  ARCHIVE_MODE_OFF = 0, /* disabled */
154  ARCHIVE_MODE_ON, /* enabled while server is running normally */
155  ARCHIVE_MODE_ALWAYS /* enabled always (even during recovery) */
156 } ArchiveMode;
157 extern int XLogArchiveMode;
158 
159 /* WAL levels */
160 typedef enum WalLevel
161 {
165 } WalLevel;
166 
167 extern PGDLLIMPORT int wal_level;
168 
169 /* Is WAL archiving enabled (always or only while server is running normally)? */
170 #define XLogArchivingActive() \
171  (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode > ARCHIVE_MODE_OFF)
172 /* Is WAL archiving enabled always (even during recovery)? */
173 #define XLogArchivingAlways() \
174  (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode == ARCHIVE_MODE_ALWAYS)
175 #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
176 
177 /*
178  * Is WAL-logging necessary for archival or log-shipping, or can we skip
179  * WAL-logging if we fsync() the data before committing instead?
180  */
181 #define XLogIsNeeded() (wal_level >= WAL_LEVEL_REPLICA)
182 
183 /*
184  * Is a full-page image needed for hint bit updates?
185  *
186  * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,
187  * we have to protect them against torn page writes. When you only set
188  * individual bits on a page, it's still consistent no matter what combination
189  * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log
190  * them if forced by wal_log_hints=on.
191  */
192 #define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints)
193 
194 /* Do we need to WAL-log information required only for Hot Standby and logical replication? */
195 #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_REPLICA)
196 
197 /* Do we need to WAL-log information required only for logical replication? */
198 #define XLogLogicalInfoActive() (wal_level >= WAL_LEVEL_LOGICAL)
199 
200 #ifdef WAL_DEBUG
201 extern bool XLOG_DEBUG;
202 #endif
203 
204 /*
205  * OR-able request flag bits for checkpoints. The "cause" bits are used only
206  * for logging purposes. Note: the flags must be defined so that it's
207  * sensible to OR together request flags arising from different requestors.
208  */
209 
210 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
211 #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
212 #define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint, but
213  * issued at end of WAL recovery */
214 #define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */
215 #define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
216 #define CHECKPOINT_FLUSH_ALL 0x0010 /* Flush all pages, including those
217  * belonging to unlogged tables */
218 /* These are important to RequestCheckpoint */
219 #define CHECKPOINT_WAIT 0x0020 /* Wait for completion */
220 #define CHECKPOINT_REQUESTED 0x0040 /* Checkpoint request has been made */
221 /* These indicate the cause of a checkpoint request */
222 #define CHECKPOINT_CAUSE_XLOG 0x0080 /* XLOG consumption */
223 #define CHECKPOINT_CAUSE_TIME 0x0100 /* Elapsed time */
224 
225 /*
226  * Flag bits for the record being inserted, set using XLogSetRecordFlags().
227  */
228 #define XLOG_INCLUDE_ORIGIN 0x01 /* include the replication origin */
229 #define XLOG_MARK_UNIMPORTANT 0x02 /* record not important for durability */
230 
231 
232 /* Checkpoint statistics */
233 typedef struct CheckpointStatsData
234 {
235  TimestampTz ckpt_start_t; /* start of checkpoint */
236  TimestampTz ckpt_write_t; /* start of flushing buffers */
237  TimestampTz ckpt_sync_t; /* start of fsyncs */
238  TimestampTz ckpt_sync_end_t; /* end of fsyncs */
239  TimestampTz ckpt_end_t; /* end of checkpoint */
240 
241  int ckpt_bufs_written; /* # of buffers written */
242 
243  int ckpt_segs_added; /* # of new xlog segments created */
244  int ckpt_segs_removed; /* # of xlog segments deleted */
245  int ckpt_segs_recycled; /* # of xlog segments recycled */
246 
247  int ckpt_sync_rels; /* # of relations synced */
248  uint64 ckpt_longest_sync; /* Longest sync for one relation */
249  uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
250  * times, which is not necessarily the
251  * same as the total elapsed time for the
252  * entire sync phase. */
254 
256 
257 struct XLogRecData;
258 
259 extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata,
260  XLogRecPtr fpw_lsn,
261  uint8 flags);
262 extern void XLogFlush(XLogRecPtr RecPtr);
263 extern bool XLogBackgroundFlush(void);
264 extern bool XLogNeedsFlush(XLogRecPtr RecPtr);
265 extern int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock);
266 extern int XLogFileOpen(XLogSegNo segno);
267 
268 extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
270 extern void XLogSetAsyncXactLSN(XLogRecPtr record);
272 
273 extern void xlog_redo(XLogReaderState *record);
274 extern void xlog_desc(StringInfo buf, XLogReaderState *record);
275 extern const char *xlog_identify(uint8 info);
276 
277 extern void issue_xlog_fsync(int fd, XLogSegNo segno);
278 
279 extern bool RecoveryInProgress(void);
280 extern bool HotStandbyActive(void);
281 extern bool HotStandbyActiveInReplay(void);
282 extern bool XLogInsertAllowed(void);
283 extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
284 extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
285 extern XLogRecPtr GetXLogInsertRecPtr(void);
286 extern XLogRecPtr GetXLogWriteRecPtr(void);
287 extern bool RecoveryIsPaused(void);
288 extern void SetRecoveryPause(bool recoveryPause);
289 extern TimestampTz GetLatestXTime(void);
291 extern char *XLogFileNameP(TimeLineID tli, XLogSegNo segno);
292 
293 extern void UpdateControlFile(void);
294 extern uint64 GetSystemIdentifier(void);
295 extern char *GetMockAuthenticationNonce(void);
296 extern bool DataChecksumsEnabled(void);
298 extern Size XLOGShmemSize(void);
299 extern void XLOGShmemInit(void);
300 extern void BootStrapXLOG(void);
301 extern void LocalProcessControlFile(bool reset);
302 extern void StartupXLOG(void);
303 extern void ShutdownXLOG(int code, Datum arg);
304 extern void InitXLOGAccess(void);
305 extern void CreateCheckPoint(int flags);
306 extern bool CreateRestartPoint(int flags);
307 extern void XLogPutNextOid(Oid nextOid);
308 extern XLogRecPtr XLogRestorePoint(const char *rpName);
309 extern void UpdateFullPageWrites(void);
310 extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p);
311 extern XLogRecPtr GetRedoRecPtr(void);
312 extern XLogRecPtr GetInsertRecPtr(void);
313 extern XLogRecPtr GetFlushRecPtr(void);
315 extern void RemovePromoteSignalFiles(void);
316 
317 extern bool CheckPromoteSignal(void);
318 extern void WakeupRecovery(void);
319 extern void SetWalWriterSleeping(bool sleeping);
320 
321 extern void XLogRequestWalReceiverReply(void);
322 
323 extern void assign_max_wal_size(int newval, void *extra);
324 extern void assign_checkpoint_completion_target(double newval, void *extra);
325 
326 /*
327  * Routines to start, stop, and get status of a base backup.
328  */
329 
330 /*
331  * Session-level status of base backups
332  *
333  * This is used in parallel with the shared memory status to control parallel
334  * execution of base backup functions for a given session, be it a backend
335  * dedicated to replication or a normal backend connected to a database. The
336  * update of the session-level status happens at the same time as the shared
337  * memory counters to keep a consistent global and local state of the backups
338  * running.
339  */
340 typedef enum SessionBackupState
341 {
346 
347 extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast,
348  TimeLineID *starttli_p, StringInfo labelfile,
349  List **tablespaces, StringInfo tblspcmapfile, bool infotbssize,
350  bool needtblspcmapfile);
351 extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive,
352  TimeLineID *stoptli_p);
353 extern void do_pg_abort_backup(void);
355 
356 /* File path names (all relative to $PGDATA) */
357 #define RECOVERY_SIGNAL_FILE "recovery.signal"
358 #define STANDBY_SIGNAL_FILE "standby.signal"
359 #define BACKUP_LABEL_FILE "backup_label"
360 #define BACKUP_LABEL_OLD "backup_label.old"
361 
362 #define TABLESPACE_MAP "tablespace_map"
363 #define TABLESPACE_MAP_OLD "tablespace_map.old"
364 
365 /* files to signal promotion to primary */
366 #define PROMOTE_SIGNAL_FILE "promote"
367 #define FALLBACK_PROMOTE_SIGNAL_FILE "fallback_promote"
368 
369 #endif /* XLOG_H */
void CreateCheckPoint(int flags)
Definition: xlog.c:8510
int wal_keep_segments
Definition: xlog.c:88
struct CheckpointStatsData CheckpointStatsData
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:11165
uint64 ckpt_agg_sync_time
Definition: xlog.h:249
TimeLineID recoveryTargetTLI
Definition: xlog.c:332
const char * recoveryTargetName
Definition: xlog.c:277
TimestampTz ckpt_sync_end_t
Definition: xlog.h:238
bool CreateRestartPoint(int flags)
Definition: xlog.c:9050
int ckpt_sync_rels
Definition: xlog.h:247
bool XLogInsertAllowed(void)
Definition: xlog.c:7993
bool recoveryTargetInclusive
Definition: xlog.c:272
char * recovery_target_time_string
Definition: xlog.c:275
int min_wal_size_mb
Definition: xlog.c:87
bool XLogNeedsFlush(XLogRecPtr RecPtr)
Definition: xlog.c:3119
uint32 TimeLineID
Definition: xlogdefs.h:52
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8171
bool DataChecksumsEnabled(void)
Definition: xlog.c:4822
void WakeupRecovery(void)
Definition: xlog.c:12225
uint32 TransactionId
Definition: c.h:507
void XLogFlush(XLogRecPtr RecPtr)
Definition: xlog.c:2798
void xlog_desc(StringInfo buf, XLogReaderState *record)
Definition: xlogdesc.c:37
bool HotStandbyActive(void)
Definition: xlog.c:7954
int64 TimestampTz
Definition: timestamp.h:39
int XLogFileOpen(XLogSegNo segno)
Definition: xlog.c:3597
void reset(void)
Definition: sql-declare.c:577
int recoveryTargetAction
Definition: xlog.c:273
TimestampTz ckpt_start_t
Definition: xlog.h:235
XLogRecPtr XactLastRecEnd
Definition: xlog.c:352
RecoveryTargetTimeLineGoal
Definition: xlog.h:93
bool wal_log_hints
Definition: xlog.c:95
void LocalProcessControlFile(bool reset)
Definition: xlog.c:4922
unsigned char uint8
Definition: c.h:356
char * GetMockAuthenticationNonce(void)
Definition: xlog.c:4812
int ckpt_segs_recycled
Definition: xlog.h:245
bool fullPageWrites
Definition: xlog.c:94
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3877
int max_wal_size_mb
Definition: xlog.c:86
char * wal_consistency_checking_string
Definition: xlog.c:97
unsigned int Oid
Definition: postgres_ext.h:31
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:5942
bool CheckPromoteSignal(void)
Definition: xlog.c:12209
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11130
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void UpdateFullPageWrites(void)
Definition: xlog.c:9479
char * recoveryRestoreCommand
Definition: xlog.c:268
RecoveryTargetTimeLineGoal recoveryTargetTimeLineGoal
Definition: xlog.c:330
bool XLogBackgroundFlush(void)
Definition: xlog.c:2987
#define PGDLLIMPORT
Definition: c.h:1243
void xlog_redo(XLogReaderState *record)
Definition: xlog.c:9591
char * XLogFileNameP(TimeLineID tli, XLogSegNo segno)
Definition: xlog.c:10130
bool wal_init_zero
Definition: xlog.c:99
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition: xlog.c:2677
void XLogSetAsyncXactLSN(XLogRecPtr record)
Definition: xlog.c:2636
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition: xlog.c:3846
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:8214
bool reachedConsistency
Definition: xlog.c:844
bool wal_compression
Definition: xlog.c:96
void assign_checkpoint_completion_target(double newval, void *extra)
Definition: xlog.c:2291
bool * wal_consistency_checking
Definition: xlog.c:98
void UpdateControlFile(void)
Definition: xlog.c:4793
int XLogArchiveTimeout
Definition: xlog.c:90
int XLogArchiveMode
Definition: xlog.c:91
const char * xlog_identify(uint8 info)
Definition: xlogdesc.c:146
char * recoveryEndCommand
Definition: xlog.c:269
void InitXLOGAccess(void)
Definition: xlog.c:8145
HotStandbyState standbyState
Definition: xlog.c:203
SessionBackupState
Definition: xlog.h:340
char * PrimaryConnInfo
Definition: xlog.c:284
PGDLLIMPORT int wal_level
Definition: xlog.c:103
bool EnableHotStandby
Definition: xlog.c:93
static char * buf
Definition: pg_test_fsync.c:68
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:11149
int XLOGbuffers
Definition: xlog.c:89
XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags)
Definition: xlog.c:972
uint64 XLogSegNo
Definition: xlogdefs.h:41
RecoveryTargetType recoveryTarget
Definition: xlog.c:271
char * PromoteTriggerFile
Definition: xlog.c:286
RecoveryTargetType
Definition: xlog.h:80
void RemovePromoteSignalFiles(void)
Definition: xlog.c:12198
int ckpt_segs_removed
Definition: xlog.h:244
bool log_checkpoints
Definition: xlog.c:101
bool StandbyMode
Definition: xlog.c:289
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:8230
int recovery_min_apply_delay
Definition: xlog.c:279
int ckpt_bufs_written
Definition: xlog.h:241
void StartupXLOG(void)
Definition: xlog.c:6195
int wal_segment_size
Definition: xlog.c:112
bool HotStandbyActiveInReplay(void)
Definition: xlog.c:7979
int CheckPointSegments
Definition: xlog.c:125
uintptr_t Datum
Definition: postgres.h:367
bool StandbyModeRequested
Definition: xlog.c:283
SessionBackupState get_backup_status(void)
Definition: xlog.c:10676
void issue_xlog_fsync(int fd, XLogSegNo segno)
Definition: xlog.c:10085
XLogRecPtr ProcLastRecPtr
Definition: xlog.c:351
bool InRecovery
Definition: xlog.c:200
TimestampTz GetCurrentChunkReplayStartTime(void)
Definition: xlog.c:6094
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:8248
int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock)
Definition: xlog.c:3205
int sync_method
Definition: xlog.c:102
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
Definition: xlog.c:10697
PGDLLIMPORT XLogRecPtr XactLastCommitEnd
Definition: xlog.c:353
void SetWalWriterSleeping(bool sleeping)
Definition: xlog.c:12234
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:8294
XLogRecPtr GetFakeLSNForUnloggedRel(void)
Definition: xlog.c:4838
void do_pg_abort_backup(void)
Definition: xlog.c:11102
void assign_max_wal_size(int newval, void *extra)
Definition: xlog.c:2284
ArchiveMode
Definition: xlog.h:151
size_t Size
Definition: c.h:466
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6064
TimestampTz ckpt_write_t
Definition: xlog.h:236
#define newval
void XLogRequestWalReceiverReply(void)
Definition: xlog.c:12245
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition: xlog.c:9395
Size XLOGShmemSize(void)
Definition: xlog.c:4933
bool RecoveryInProgress(void)
Definition: xlog.c:7898
TransactionId recoveryTargetXid
Definition: xlog.c:274
TimestampTz ckpt_sync_t
Definition: xlog.h:237
XLogRecPtr recoveryTargetLSN
Definition: xlog.c:278
int ckpt_segs_added
Definition: xlog.h:243
void XLOGShmemInit(void)
Definition: xlog.c:4974
XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p, StringInfo labelfile, List **tablespaces, StringInfo tblspcmapfile, bool infotbssize, bool needtblspcmapfile)
Definition: xlog.c:10178
bool RecoveryIsPaused(void)
Definition: xlog.c:5930
char * archiveCleanupCommand
Definition: xlog.c:270
bool wal_recycle
Definition: xlog.c:100
void * arg
CheckpointStatsData CheckpointStats
Definition: xlog.c:181
PGDLLIMPORT TimeLineID ThisTimeLineID
Definition: xlog.c:187
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:8199
int wal_retrieve_retry_interval
Definition: xlog.c:106
void BootStrapXLOG(void)
Definition: xlog.c:5087
void XLogPutNextOid(Oid nextOid)
Definition: xlog.c:9340
Definition: pg_list.h:50
void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream)
Definition: xlog.c:6110
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4802
TimeLineID recoveryTargetTLIRequested
Definition: xlog.c:331
TimestampTz ckpt_end_t
Definition: xlog.h:239
HotStandbyState
Definition: xlog.h:64
WalLevel
Definition: xlog.h:160
uint64 ckpt_longest_sync
Definition: xlog.h:248
char * XLogArchiveCommand
Definition: xlog.c:92
char * PrimarySlotName
Definition: xlog.c:285