codekingpro/portable-devtools
114k
1/*2 * xlog.h3 *4 * PostgreSQL write-ahead log manager5 *6 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group7 * Portions Copyright (c) 1994, Regents of the University of California8 *9 * src/include/access/xlog.h10 */11#ifndef XLOG_H12#define XLOG_H13 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 */22#define SYNC_METHOD_FSYNC 023#define SYNC_METHOD_FDATASYNC 124#define SYNC_METHOD_OPEN 2 /* for O_SYNC */25#define SYNC_METHOD_FSYNC_WRITETHROUGH 326#define SYNC_METHOD_OPEN_DSYNC 4 /* for O_DSYNC */27extern PGDLLIMPORT int sync_method;28 29extern PGDLLIMPORT XLogRecPtr ProcLastRecPtr;30extern PGDLLIMPORT XLogRecPtr XactLastRecEnd;31extern PGDLLIMPORT XLogRecPtr XactLastCommitEnd;32 33/* these variables are GUC parameters related to XLOG */34extern PGDLLIMPORT int wal_segment_size;35extern PGDLLIMPORT int min_wal_size_mb;36extern PGDLLIMPORT int max_wal_size_mb;37extern PGDLLIMPORT int wal_keep_size_mb;38extern PGDLLIMPORT int max_slot_wal_keep_size_mb;39extern PGDLLIMPORT int XLOGbuffers;40extern PGDLLIMPORT int XLogArchiveTimeout;41extern PGDLLIMPORT int wal_retrieve_retry_interval;42extern PGDLLIMPORT char *XLogArchiveCommand;43extern PGDLLIMPORT bool EnableHotStandby;44extern PGDLLIMPORT bool fullPageWrites;45extern PGDLLIMPORT bool wal_log_hints;46extern PGDLLIMPORT int wal_compression;47extern PGDLLIMPORT bool wal_init_zero;48extern PGDLLIMPORT bool wal_recycle;49extern PGDLLIMPORT bool *wal_consistency_checking;50extern PGDLLIMPORT char *wal_consistency_checking_string;51extern PGDLLIMPORT bool log_checkpoints;52extern PGDLLIMPORT bool track_wal_io_timing;53extern PGDLLIMPORT int wal_decode_buffer_size;54 55extern PGDLLIMPORT int CheckPointSegments;56 57/* Archive modes */58typedef enum ArchiveMode59{60 ARCHIVE_MODE_OFF = 0, /* disabled */61 ARCHIVE_MODE_ON, /* enabled while server is running normally */62 ARCHIVE_MODE_ALWAYS /* enabled always (even during recovery) */63} ArchiveMode;64extern PGDLLIMPORT int XLogArchiveMode;65 66/* WAL levels */67typedef enum WalLevel68{69 WAL_LEVEL_MINIMAL = 0,70 WAL_LEVEL_REPLICA,71 WAL_LEVEL_LOGICAL72} WalLevel;73 74/* Compression algorithms for WAL */75typedef enum WalCompression76{77 WAL_COMPRESSION_NONE = 0,78 WAL_COMPRESSION_PGLZ,79 WAL_COMPRESSION_LZ4,80 WAL_COMPRESSION_ZSTD81} WalCompression;82 83/* Recovery states */84typedef enum RecoveryState85{86 RECOVERY_STATE_CRASH = 0, /* crash recovery */87 RECOVERY_STATE_ARCHIVE, /* archive recovery */88 RECOVERY_STATE_DONE /* currently in production */89} RecoveryState;90 91extern PGDLLIMPORT int wal_level;92 93/* Is WAL archiving enabled (always or only while server is running normally)? */94#define XLogArchivingActive() \95 (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode > ARCHIVE_MODE_OFF)96/* Is WAL archiving enabled always (even during recovery)? */97#define XLogArchivingAlways() \98 (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode == ARCHIVE_MODE_ALWAYS)99 100/*101 * Is WAL-logging necessary for archival or log-shipping, or can we skip102 * WAL-logging if we fsync() the data before committing instead?103 */104#define XLogIsNeeded() (wal_level >= WAL_LEVEL_REPLICA)105 106/*107 * Is a full-page image needed for hint bit updates?108 *109 * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,110 * we have to protect them against torn page writes. When you only set111 * individual bits on a page, it's still consistent no matter what combination112 * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log113 * them if forced by wal_log_hints=on.114 */115#define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints)116 117/* Do we need to WAL-log information required only for Hot Standby and logical replication? */118#define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_REPLICA)119 120/* Do we need to WAL-log information required only for logical replication? */121#define XLogLogicalInfoActive() (wal_level >= WAL_LEVEL_LOGICAL)122 123#ifdef WAL_DEBUG124extern PGDLLIMPORT bool XLOG_DEBUG;125#endif126 127/*128 * OR-able request flag bits for checkpoints. The "cause" bits are used only129 * for logging purposes. Note: the flags must be defined so that it's130 * sensible to OR together request flags arising from different requestors.131 */132 133/* These directly affect the behavior of CreateCheckPoint and subsidiaries */134#define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */135#define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint, but136 * issued at end of WAL recovery */137#define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */138#define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */139#define CHECKPOINT_FLUSH_ALL 0x0010 /* Flush all pages, including those140 * belonging to unlogged tables */141/* These are important to RequestCheckpoint */142#define CHECKPOINT_WAIT 0x0020 /* Wait for completion */143#define CHECKPOINT_REQUESTED 0x0040 /* Checkpoint request has been made */144/* These indicate the cause of a checkpoint request */145#define CHECKPOINT_CAUSE_XLOG 0x0080 /* XLOG consumption */146#define CHECKPOINT_CAUSE_TIME 0x0100 /* Elapsed time */147 148/*149 * Flag bits for the record being inserted, set using XLogSetRecordFlags().150 */151#define XLOG_INCLUDE_ORIGIN 0x01 /* include the replication origin */152#define XLOG_MARK_UNIMPORTANT 0x02 /* record not important for durability */153 154 155/* Checkpoint statistics */156typedef struct CheckpointStatsData157{158 TimestampTz ckpt_start_t; /* start of checkpoint */159 TimestampTz ckpt_write_t; /* start of flushing buffers */160 TimestampTz ckpt_sync_t; /* start of fsyncs */161 TimestampTz ckpt_sync_end_t; /* end of fsyncs */162 TimestampTz ckpt_end_t; /* end of checkpoint */163 164 int ckpt_bufs_written; /* # of buffers written */165 166 int ckpt_segs_added; /* # of new xlog segments created */167 int ckpt_segs_removed; /* # of xlog segments deleted */168 int ckpt_segs_recycled; /* # of xlog segments recycled */169 170 int ckpt_sync_rels; /* # of relations synced */171 uint64 ckpt_longest_sync; /* Longest sync for one relation */172 uint64 ckpt_agg_sync_time; /* The sum of all the individual sync173 * times, which is not necessarily the174 * same as the total elapsed time for the175 * entire sync phase. */176} CheckpointStatsData;177 178extern PGDLLIMPORT CheckpointStatsData CheckpointStats;179 180/*181 * GetWALAvailability return codes182 */183typedef enum WALAvailability184{185 WALAVAIL_INVALID_LSN, /* parameter error */186 WALAVAIL_RESERVED, /* WAL segment is within max_wal_size */187 WALAVAIL_EXTENDED, /* WAL segment is reserved by a slot or188 * wal_keep_size */189 WALAVAIL_UNRESERVED, /* no longer reserved, but not removed yet */190 WALAVAIL_REMOVED /* WAL segment has been removed */191} WALAvailability;192 193struct XLogRecData;194struct XLogReaderState;195 196extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata,197 XLogRecPtr fpw_lsn,198 uint8 flags,199 int num_fpi,200 bool topxid_included);201extern void XLogFlush(XLogRecPtr record);202extern bool XLogBackgroundFlush(void);203extern bool XLogNeedsFlush(XLogRecPtr record);204extern int XLogFileInit(XLogSegNo logsegno, TimeLineID logtli);205extern int XLogFileOpen(XLogSegNo segno, TimeLineID tli);206 207extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);208extern XLogSegNo XLogGetLastRemovedSegno(void);209extern void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN);210extern void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn);211 212extern void xlog_redo(struct XLogReaderState *record);213extern void xlog_desc(StringInfo buf, struct XLogReaderState *record);214extern const char *xlog_identify(uint8 info);215 216extern void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli);217 218extern bool RecoveryInProgress(void);219extern RecoveryState GetRecoveryState(void);220extern bool XLogInsertAllowed(void);221extern XLogRecPtr GetXLogInsertRecPtr(void);222extern XLogRecPtr GetXLogWriteRecPtr(void);223 224extern uint64 GetSystemIdentifier(void);225extern char *GetMockAuthenticationNonce(void);226extern bool DataChecksumsEnabled(void);227extern XLogRecPtr GetFakeLSNForUnloggedRel(void);228extern Size XLOGShmemSize(void);229extern void XLOGShmemInit(void);230extern void BootStrapXLOG(void);231extern void InitializeWalConsistencyChecking(void);232extern void LocalProcessControlFile(bool reset);233extern WalLevel GetActiveWalLevelOnStandby(void);234extern void StartupXLOG(void);235extern void ShutdownXLOG(int code, Datum arg);236extern void CreateCheckPoint(int flags);237extern bool CreateRestartPoint(int flags);238extern WALAvailability GetWALAvailability(XLogRecPtr targetLSN);239extern void XLogPutNextOid(Oid nextOid);240extern XLogRecPtr XLogRestorePoint(const char *rpName);241extern void UpdateFullPageWrites(void);242extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p);243extern XLogRecPtr GetRedoRecPtr(void);244extern XLogRecPtr GetInsertRecPtr(void);245extern XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI);246extern TimeLineID GetWALInsertionTimeLine(void);247extern XLogRecPtr GetLastImportantRecPtr(void);248 249extern void SetWalWriterSleeping(bool sleeping);250 251/*252 * Routines used by xlogrecovery.c to call back into xlog.c during recovery.253 */254extern void RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI);255extern bool XLogCheckpointNeeded(XLogSegNo new_segno);256extern void SwitchIntoArchiveRecovery(XLogRecPtr EndRecPtr, TimeLineID replayTLI);257extern void ReachedEndOfBackup(XLogRecPtr EndRecPtr, TimeLineID tli);258extern void SetInstallXLogFileSegmentActive(void);259extern bool IsInstallXLogFileSegmentActive(void);260extern void XLogShutdownWalRcv(void);261 262/*263 * Routines to start, stop, and get status of a base backup.264 */265 266/*267 * Session-level status of base backups268 *269 * This is used in parallel with the shared memory status to control parallel270 * execution of base backup functions for a given session, be it a backend271 * dedicated to replication or a normal backend connected to a database. The272 * update of the session-level status happens at the same time as the shared273 * memory counters to keep a consistent global and local state of the backups274 * running.275 */276typedef enum SessionBackupState277{278 SESSION_BACKUP_NONE,279 SESSION_BACKUP_RUNNING,280} SessionBackupState;281 282extern void do_pg_backup_start(const char *backupidstr, bool fast,283 List **tablespaces, BackupState *state,284 StringInfo tblspcmapfile);285extern void do_pg_backup_stop(BackupState *state, bool waitforarchive);286extern void do_pg_abort_backup(int code, Datum arg);287extern void register_persistent_abort_backup_handler(void);288extern SessionBackupState get_backup_status(void);289 290/* File path names (all relative to $PGDATA) */291#define RECOVERY_SIGNAL_FILE "recovery.signal"292#define STANDBY_SIGNAL_FILE "standby.signal"293#define BACKUP_LABEL_FILE "backup_label"294#define BACKUP_LABEL_OLD "backup_label.old"295 296#define TABLESPACE_MAP "tablespace_map"297#define TABLESPACE_MAP_OLD "tablespace_map.old"298 299/* files to signal promotion to primary */300#define PROMOTE_SIGNAL_FILE "promote"301 302#endif /* XLOG_H */303 