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 { 66 STANDBY_DISABLED, 67 STANDBY_INITIALIZED, 68 STANDBY_SNAPSHOT_PENDING, 69 STANDBY_SNAPSHOT_READY 70 } HotStandbyState; 71 72 extern HotStandbyState standbyState; 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 { 82 RECOVERY_TARGET_UNSET, 83 RECOVERY_TARGET_XID, 84 RECOVERY_TARGET_TIME, 85 RECOVERY_TARGET_NAME, 86 RECOVERY_TARGET_LSN, 87 RECOVERY_TARGET_IMMEDIATE 88 } RecoveryTargetType; 89 90 /* 91 * Recovery target TimeLine goal 92 */ 93 typedef enum 94 { 95 RECOVERY_TARGET_TIMELINE_CONTROLFILE, 96 RECOVERY_TARGET_TIMELINE_LATEST, 97 RECOVERY_TARGET_TIMELINE_NUMERIC 98 } RecoveryTargetTimeLineGoal; 99 100 extern XLogRecPtr ProcLastRecPtr; 101 extern XLogRecPtr XactLastRecEnd; 102 extern PGDLLIMPORT XLogRecPtr XactLastCommitEnd; 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; 122 extern char *wal_consistency_checking_string; 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 */ 134 extern TransactionId recoveryTargetXid; 135 extern char *recovery_target_time_string; 136 extern const char *recoveryTargetName; 137 extern XLogRecPtr recoveryTargetLSN; 138 extern RecoveryTargetType recoveryTarget; 139 extern char *PromoteTriggerFile; 140 extern RecoveryTargetTimeLineGoal recoveryTargetTimeLineGoal; 141 extern TimeLineID recoveryTargetTLIRequested; 142 extern TimeLineID recoveryTargetTLI; 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 { 162 WAL_LEVEL_MINIMAL = 0, 163 WAL_LEVEL_REPLICA, 164 WAL_LEVEL_LOGICAL 165 } WalLevel; 166 167 /* Recovery states */ 168 typedef enum RecoveryState 169 { 170 RECOVERY_STATE_CRASH = 0, /* crash recovery */ 171 RECOVERY_STATE_ARCHIVE, /* archive recovery */ 172 RECOVERY_STATE_DONE /* currently in production */ 173 } RecoveryState; 174 175 extern PGDLLIMPORT int wal_level; 176 177 /* Is WAL archiving enabled (always or only while server is running normally)? */ 178 #define XLogArchivingActive() \ 179 (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode > ARCHIVE_MODE_OFF) 180 /* Is WAL archiving enabled always (even during recovery)? */ 181 #define XLogArchivingAlways() \ 182 (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode == ARCHIVE_MODE_ALWAYS) 183 #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0') 184 185 /* 186 * Is WAL-logging necessary for archival or log-shipping, or can we skip 187 * WAL-logging if we fsync() the data before committing instead? 188 */ 189 #define XLogIsNeeded() (wal_level >= WAL_LEVEL_REPLICA) 190 191 /* 192 * Is a full-page image needed for hint bit updates? 193 * 194 * Normally, we don't WAL-log hint bit updates, but if checksums are enabled, 195 * we have to protect them against torn page writes. When you only set 196 * individual bits on a page, it's still consistent no matter what combination 197 * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log 198 * them if forced by wal_log_hints=on. 199 */ 200 #define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints) 201 202 /* Do we need to WAL-log information required only for Hot Standby and logical replication? */ 203 #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_REPLICA) 204 205 /* Do we need to WAL-log information required only for logical replication? */ 206 #define XLogLogicalInfoActive() (wal_level >= WAL_LEVEL_LOGICAL) 207 208 #ifdef WAL_DEBUG 209 extern bool XLOG_DEBUG; 210 #endif 211 212 /* 213 * OR-able request flag bits for checkpoints. The "cause" bits are used only 214 * for logging purposes. Note: the flags must be defined so that it's 215 * sensible to OR together request flags arising from different requestors. 216 */ 217 218 /* These directly affect the behavior of CreateCheckPoint and subsidiaries */ 219 #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */ 220 #define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint, but 221 * issued at end of WAL recovery */ 222 #define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */ 223 #define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */ 224 #define CHECKPOINT_FLUSH_ALL 0x0010 /* Flush all pages, including those 225 * belonging to unlogged tables */ 226 /* These are important to RequestCheckpoint */ 227 #define CHECKPOINT_WAIT 0x0020 /* Wait for completion */ 228 #define CHECKPOINT_REQUESTED 0x0040 /* Checkpoint request has been made */ 229 /* These indicate the cause of a checkpoint request */ 230 #define CHECKPOINT_CAUSE_XLOG 0x0080 /* XLOG consumption */ 231 #define CHECKPOINT_CAUSE_TIME 0x0100 /* Elapsed time */ 232 233 /* 234 * Flag bits for the record being inserted, set using XLogSetRecordFlags(). 235 */ 236 #define XLOG_INCLUDE_ORIGIN 0x01 /* include the replication origin */ 237 #define XLOG_MARK_UNIMPORTANT 0x02 /* record not important for durability */ 238 239 240 /* Checkpoint statistics */ 241 typedef struct CheckpointStatsData 242 { 243 TimestampTz ckpt_start_t; /* start of checkpoint */ 244 TimestampTz ckpt_write_t; /* start of flushing buffers */ 245 TimestampTz ckpt_sync_t; /* start of fsyncs */ 246 TimestampTz ckpt_sync_end_t; /* end of fsyncs */ 247 TimestampTz ckpt_end_t; /* end of checkpoint */ 248 249 int ckpt_bufs_written; /* # of buffers written */ 250 251 int ckpt_segs_added; /* # of new xlog segments created */ 252 int ckpt_segs_removed; /* # of xlog segments deleted */ 253 int ckpt_segs_recycled; /* # of xlog segments recycled */ 254 255 int ckpt_sync_rels; /* # of relations synced */ 256 uint64 ckpt_longest_sync; /* Longest sync for one relation */ 257 uint64 ckpt_agg_sync_time; /* The sum of all the individual sync 258 * times, which is not necessarily the 259 * same as the total elapsed time for the 260 * entire sync phase. */ 261 } CheckpointStatsData; 262 263 extern CheckpointStatsData CheckpointStats; 264 265 struct XLogRecData; 266 267 extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, 268 XLogRecPtr fpw_lsn, 269 uint8 flags); 270 extern void XLogFlush(XLogRecPtr RecPtr); 271 extern bool XLogBackgroundFlush(void); 272 extern bool XLogNeedsFlush(XLogRecPtr RecPtr); 273 extern int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock); 274 extern int XLogFileOpen(XLogSegNo segno); 275 276 extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli); 277 extern XLogSegNo XLogGetLastRemovedSegno(void); 278 extern void XLogSetAsyncXactLSN(XLogRecPtr record); 279 extern void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn); 280 281 extern void xlog_redo(XLogReaderState *record); 282 extern void xlog_desc(StringInfo buf, XLogReaderState *record); 283 extern const char *xlog_identify(uint8 info); 284 285 extern void issue_xlog_fsync(int fd, XLogSegNo segno); 286 287 extern bool RecoveryInProgress(void); 288 extern RecoveryState GetRecoveryState(void); 289 extern bool HotStandbyActive(void); 290 extern bool HotStandbyActiveInReplay(void); 291 extern bool XLogInsertAllowed(void); 292 extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream); 293 extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI); 294 extern XLogRecPtr GetXLogInsertRecPtr(void); 295 extern XLogRecPtr GetXLogWriteRecPtr(void); 296 extern bool RecoveryIsPaused(void); 297 extern void SetRecoveryPause(bool recoveryPause); 298 extern TimestampTz GetLatestXTime(void); 299 extern TimestampTz GetCurrentChunkReplayStartTime(void); 300 extern char *XLogFileNameP(TimeLineID tli, XLogSegNo segno); 301 302 extern void UpdateControlFile(void); 303 extern uint64 GetSystemIdentifier(void); 304 extern char *GetMockAuthenticationNonce(void); 305 extern bool DataChecksumsEnabled(void); 306 extern XLogRecPtr GetFakeLSNForUnloggedRel(void); 307 extern Size XLOGShmemSize(void); 308 extern void XLOGShmemInit(void); 309 extern void BootStrapXLOG(void); 310 extern void LocalProcessControlFile(bool reset); 311 extern void StartupXLOG(void); 312 extern void ShutdownXLOG(int code, Datum arg); 313 extern void InitXLOGAccess(void); 314 extern void CreateCheckPoint(int flags); 315 extern bool CreateRestartPoint(int flags); 316 extern void XLogPutNextOid(Oid nextOid); 317 extern XLogRecPtr XLogRestorePoint(const char *rpName); 318 extern void UpdateFullPageWrites(void); 319 extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p); 320 extern XLogRecPtr GetRedoRecPtr(void); 321 extern XLogRecPtr GetInsertRecPtr(void); 322 extern XLogRecPtr GetFlushRecPtr(void); 323 extern XLogRecPtr GetLastImportantRecPtr(void); 324 extern void RemovePromoteSignalFiles(void); 325 326 extern bool CheckPromoteSignal(void); 327 extern void WakeupRecovery(void); 328 extern void SetWalWriterSleeping(bool sleeping); 329 330 extern void XLogRequestWalReceiverReply(void); 331 332 extern void assign_max_wal_size(int newval, void *extra); 333 extern void assign_checkpoint_completion_target(double newval, void *extra); 334 335 /* 336 * Routines to start, stop, and get status of a base backup. 337 */ 338 339 /* 340 * Session-level status of base backups 341 * 342 * This is used in parallel with the shared memory status to control parallel 343 * execution of base backup functions for a given session, be it a backend 344 * dedicated to replication or a normal backend connected to a database. The 345 * update of the session-level status happens at the same time as the shared 346 * memory counters to keep a consistent global and local state of the backups 347 * running. 348 */ 349 typedef enum SessionBackupState 350 { 351 SESSION_BACKUP_NONE, 352 SESSION_BACKUP_EXCLUSIVE, 353 SESSION_BACKUP_NON_EXCLUSIVE 354 } SessionBackupState; 355 356 extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast, 357 TimeLineID *starttli_p, StringInfo labelfile, 358 List **tablespaces, StringInfo tblspcmapfile, bool infotbssize, 359 bool needtblspcmapfile); 360 extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive, 361 TimeLineID *stoptli_p); 362 extern void do_pg_abort_backup(int code, Datum arg); 363 extern void register_persistent_abort_backup_handler(void); 364 extern SessionBackupState get_backup_status(void); 365 366 /* File path names (all relative to $PGDATA) */ 367 #define RECOVERY_SIGNAL_FILE "recovery.signal" 368 #define STANDBY_SIGNAL_FILE "standby.signal" 369 #define BACKUP_LABEL_FILE "backup_label" 370 #define BACKUP_LABEL_OLD "backup_label.old" 371 372 #define TABLESPACE_MAP "tablespace_map" 373 #define TABLESPACE_MAP_OLD "tablespace_map.old" 374 375 /* files to signal promotion to primary */ 376 #define PROMOTE_SIGNAL_FILE "promote" 377 #define FALLBACK_PROMOTE_SIGNAL_FILE "fallback_promote" 378 379 #endif /* XLOG_H */ 380