xlog.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * xlog.h
  3. *
  4. * PostgreSQL transaction log manager
  5. *
  6. * Portions Copyright (c) 1996-2016, 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. #include "access/rmgr.h"
  14. #include "access/xlogdefs.h"
  15. #include "access/xloginsert.h"
  16. #include "access/xlogreader.h"
  17. #include "datatype/timestamp.h"
  18. #include "lib/stringinfo.h"
  19. #include "nodes/pg_list.h"
  20. #include "storage/fd.h"
  21. /* Sync methods */
  22. #define SYNC_METHOD_FSYNC 0
  23. #define SYNC_METHOD_FDATASYNC 1
  24. #define SYNC_METHOD_OPEN 2 /* for O_SYNC */
  25. #define SYNC_METHOD_FSYNC_WRITETHROUGH 3
  26. #define SYNC_METHOD_OPEN_DSYNC 4 /* for O_DSYNC */
  27. extern int sync_method;
  28. extern PGDLLIMPORT TimeLineID ThisTimeLineID; /* current TLI */
  29. /*
  30. * Prior to 8.4, all activity during recovery was carried out by the startup
  31. * process. This local variable continues to be used in many parts of the
  32. * code to indicate actions taken by RecoveryManagers. Other processes that
  33. * potentially perform work during recovery should check RecoveryInProgress().
  34. * See XLogCtl notes in xlog.c.
  35. */
  36. extern bool InRecovery;
  37. /*
  38. * Like InRecovery, standbyState is only valid in the startup process.
  39. * In all other processes it will have the value STANDBY_DISABLED (so
  40. * InHotStandby will read as FALSE).
  41. *
  42. * In DISABLED state, we're performing crash recovery or hot standby was
  43. * disabled in postgresql.conf.
  44. *
  45. * In INITIALIZED state, we've run InitRecoveryTransactionEnvironment, but
  46. * we haven't yet processed a RUNNING_XACTS or shutdown-checkpoint WAL record
  47. * to initialize our master-transaction tracking system.
  48. *
  49. * When the transaction tracking is initialized, we enter the SNAPSHOT_PENDING
  50. * state. The tracked information might still be incomplete, so we can't allow
  51. * connections yet, but redo functions must update the in-memory state when
  52. * appropriate.
  53. *
  54. * In SNAPSHOT_READY mode, we have full knowledge of transactions that are
  55. * (or were) running in the master at the current WAL location. Snapshots
  56. * can be taken, and read-only queries can be run.
  57. */
  58. typedef enum
  59. {
  60. STANDBY_DISABLED,
  61. STANDBY_INITIALIZED,
  62. STANDBY_SNAPSHOT_PENDING,
  63. STANDBY_SNAPSHOT_READY
  64. } HotStandbyState;
  65. extern HotStandbyState standbyState;
  66. #define InHotStandby (standbyState >= STANDBY_SNAPSHOT_PENDING)
  67. /*
  68. * Recovery target type.
  69. * Only set during a Point in Time recovery, not when standby_mode = on
  70. */
  71. typedef enum
  72. {
  73. RECOVERY_TARGET_UNSET,
  74. RECOVERY_TARGET_XID,
  75. RECOVERY_TARGET_TIME,
  76. RECOVERY_TARGET_NAME,
  77. RECOVERY_TARGET_IMMEDIATE
  78. } RecoveryTargetType;
  79. extern XLogRecPtr ProcLastRecPtr;
  80. extern XLogRecPtr XactLastRecEnd;
  81. extern PGDLLIMPORT XLogRecPtr XactLastCommitEnd;
  82. extern bool reachedConsistency;
  83. /* these variables are GUC parameters related to XLOG */
  84. extern int min_wal_size;
  85. extern int max_wal_size;
  86. extern int wal_keep_segments;
  87. extern int XLOGbuffers;
  88. extern int XLogArchiveTimeout;
  89. extern int wal_retrieve_retry_interval;
  90. extern char *XLogArchiveCommand;
  91. extern bool EnableHotStandby;
  92. extern bool fullPageWrites;
  93. extern bool wal_log_hints;
  94. extern bool wal_compression;
  95. extern bool log_checkpoints;
  96. extern int CheckPointSegments;
  97. /* Archive modes */
  98. typedef enum ArchiveMode
  99. {
  100. ARCHIVE_MODE_OFF = 0, /* disabled */
  101. ARCHIVE_MODE_ON, /* enabled while server is running normally */
  102. ARCHIVE_MODE_ALWAYS /* enabled always (even during recovery) */
  103. } ArchiveMode;
  104. extern int XLogArchiveMode;
  105. /* WAL levels */
  106. typedef enum WalLevel
  107. {
  108. WAL_LEVEL_MINIMAL = 0,
  109. WAL_LEVEL_REPLICA,
  110. WAL_LEVEL_LOGICAL
  111. } WalLevel;
  112. extern PGDLLIMPORT int wal_level;
  113. /* Is WAL archiving enabled (always or only while server is running normally)? */
  114. #define XLogArchivingActive() \
  115. (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode > ARCHIVE_MODE_OFF)
  116. /* Is WAL archiving enabled always (even during recovery)? */
  117. #define XLogArchivingAlways() \
  118. (AssertMacro(XLogArchiveMode == ARCHIVE_MODE_OFF || wal_level >= WAL_LEVEL_REPLICA), XLogArchiveMode == ARCHIVE_MODE_ALWAYS)
  119. #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
  120. /*
  121. * Is WAL-logging necessary for archival or log-shipping, or can we skip
  122. * WAL-logging if we fsync() the data before committing instead?
  123. */
  124. #define XLogIsNeeded() (wal_level >= WAL_LEVEL_REPLICA)
  125. /*
  126. * Is a full-page image needed for hint bit updates?
  127. *
  128. * Normally, we don't WAL-log hint bit updates, but if checksums are enabled,
  129. * we have to protect them against torn page writes. When you only set
  130. * individual bits on a page, it's still consistent no matter what combination
  131. * of the bits make it to disk, but the checksum wouldn't match. Also WAL-log
  132. * them if forced by wal_log_hints=on.
  133. */
  134. #define XLogHintBitIsNeeded() (DataChecksumsEnabled() || wal_log_hints)
  135. /* Do we need to WAL-log information required only for Hot Standby and logical replication? */
  136. #define XLogStandbyInfoActive() (wal_level >= WAL_LEVEL_REPLICA)
  137. /* Do we need to WAL-log information required only for logical replication? */
  138. #define XLogLogicalInfoActive() (wal_level >= WAL_LEVEL_LOGICAL)
  139. #ifdef WAL_DEBUG
  140. extern bool XLOG_DEBUG;
  141. #endif
  142. /*
  143. * OR-able request flag bits for checkpoints. The "cause" bits are used only
  144. * for logging purposes. Note: the flags must be defined so that it's
  145. * sensible to OR together request flags arising from different requestors.
  146. */
  147. /* These directly affect the behavior of CreateCheckPoint and subsidiaries */
  148. #define CHECKPOINT_IS_SHUTDOWN 0x0001 /* Checkpoint is for shutdown */
  149. #define CHECKPOINT_END_OF_RECOVERY 0x0002 /* Like shutdown checkpoint,
  150. * but issued at end of WAL
  151. * recovery */
  152. #define CHECKPOINT_IMMEDIATE 0x0004 /* Do it without delays */
  153. #define CHECKPOINT_FORCE 0x0008 /* Force even if no activity */
  154. #define CHECKPOINT_FLUSH_ALL 0x0010 /* Flush all pages, including those
  155. * belonging to unlogged tables */
  156. /* These are important to RequestCheckpoint */
  157. #define CHECKPOINT_WAIT 0x0020 /* Wait for completion */
  158. /* These indicate the cause of a checkpoint request */
  159. #define CHECKPOINT_CAUSE_XLOG 0x0040 /* XLOG consumption */
  160. #define CHECKPOINT_CAUSE_TIME 0x0080 /* Elapsed time */
  161. /* Checkpoint statistics */
  162. typedef struct CheckpointStatsData
  163. {
  164. TimestampTz ckpt_start_t; /* start of checkpoint */
  165. TimestampTz ckpt_write_t; /* start of flushing buffers */
  166. TimestampTz ckpt_sync_t; /* start of fsyncs */
  167. TimestampTz ckpt_sync_end_t; /* end of fsyncs */
  168. TimestampTz ckpt_end_t; /* end of checkpoint */
  169. int ckpt_bufs_written; /* # of buffers written */
  170. int ckpt_segs_added; /* # of new xlog segments created */
  171. int ckpt_segs_removed; /* # of xlog segments deleted */
  172. int ckpt_segs_recycled; /* # of xlog segments recycled */
  173. int ckpt_sync_rels; /* # of relations synced */
  174. uint64 ckpt_longest_sync; /* Longest sync for one relation */
  175. uint64 ckpt_agg_sync_time; /* The sum of all the individual sync
  176. * times, which is not necessarily the
  177. * same as the total elapsed time for
  178. * the entire sync phase. */
  179. } CheckpointStatsData;
  180. extern CheckpointStatsData CheckpointStats;
  181. struct XLogRecData;
  182. extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata, XLogRecPtr fpw_lsn);
  183. extern void XLogFlush(XLogRecPtr RecPtr);
  184. extern bool XLogBackgroundFlush(void);
  185. extern bool XLogNeedsFlush(XLogRecPtr RecPtr);
  186. extern int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock);
  187. extern int XLogFileOpen(XLogSegNo segno);
  188. extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
  189. extern XLogSegNo XLogGetLastRemovedSegno(void);
  190. extern void XLogSetAsyncXactLSN(XLogRecPtr record);
  191. extern void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn);
  192. extern void xlog_redo(XLogReaderState *record);
  193. extern void xlog_desc(StringInfo buf, XLogReaderState *record);
  194. extern const char *xlog_identify(uint8 info);
  195. extern void issue_xlog_fsync(int fd, XLogSegNo segno);
  196. extern bool RecoveryInProgress(void);
  197. extern bool HotStandbyActive(void);
  198. extern bool HotStandbyActiveInReplay(void);
  199. extern bool XLogInsertAllowed(void);
  200. extern void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream);
  201. extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
  202. extern XLogRecPtr GetXLogInsertRecPtr(void);
  203. extern XLogRecPtr GetXLogWriteRecPtr(void);
  204. extern bool RecoveryIsPaused(void);
  205. extern void SetRecoveryPause(bool recoveryPause);
  206. extern TimestampTz GetLatestXTime(void);
  207. extern TimestampTz GetCurrentChunkReplayStartTime(void);
  208. extern char *XLogFileNameP(TimeLineID tli, XLogSegNo segno);
  209. extern void UpdateControlFile(void);
  210. extern uint64 GetSystemIdentifier(void);
  211. extern bool DataChecksumsEnabled(void);
  212. extern XLogRecPtr GetFakeLSNForUnloggedRel(void);
  213. extern Size XLOGShmemSize(void);
  214. extern void XLOGShmemInit(void);
  215. extern void BootStrapXLOG(void);
  216. extern void StartupXLOG(void);
  217. extern void ShutdownXLOG(int code, Datum arg);
  218. extern void InitXLOGAccess(void);
  219. extern void CreateCheckPoint(int flags);
  220. extern bool CreateRestartPoint(int flags);
  221. extern void XLogPutNextOid(Oid nextOid);
  222. extern XLogRecPtr XLogRestorePoint(const char *rpName);
  223. extern void UpdateFullPageWrites(void);
  224. extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p);
  225. extern XLogRecPtr GetRedoRecPtr(void);
  226. extern XLogRecPtr GetInsertRecPtr(void);
  227. extern XLogRecPtr GetFlushRecPtr(void);
  228. extern void GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch);
  229. extern void RemovePromoteSignalFiles(void);
  230. extern bool CheckPromoteSignal(void);
  231. extern void WakeupRecovery(void);
  232. extern void SetWalWriterSleeping(bool sleeping);
  233. extern void XLogRequestWalReceiverReply(void);
  234. extern void assign_max_wal_size(int newval, void *extra);
  235. extern void assign_checkpoint_completion_target(double newval, void *extra);
  236. /*
  237. * Routines to start, stop, and get status of a base backup.
  238. */
  239. /*
  240. * Session-level status of base backups
  241. *
  242. * This is used in parallel with the shared memory status to control parallel
  243. * execution of base backup functions for a given session, be it a backend
  244. * dedicated to replication or a normal backend connected to a database. The
  245. * update of the session-level status happens at the same time as the shared
  246. * memory counters to keep a consistent global and local state of the backups
  247. * running.
  248. */
  249. typedef enum SessionBackupState
  250. {
  251. SESSION_BACKUP_NONE,
  252. SESSION_BACKUP_EXCLUSIVE,
  253. SESSION_BACKUP_NON_EXCLUSIVE
  254. } SessionBackupState;
  255. extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast,
  256. TimeLineID *starttli_p, StringInfo labelfile, DIR *tblspcdir,
  257. List **tablespaces, StringInfo tblspcmapfile, bool infotbssize,
  258. bool needtblspcmapfile);
  259. extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive,
  260. TimeLineID *stoptli_p);
  261. extern void do_pg_abort_backup(void);
  262. extern SessionBackupState get_backup_status(void);
  263. /* File path names (all relative to $PGDATA) */
  264. #define BACKUP_LABEL_FILE "backup_label"
  265. #define BACKUP_LABEL_OLD "backup_label.old"
  266. #define TABLESPACE_MAP "tablespace_map"
  267. #define TABLESPACE_MAP_OLD "tablespace_map.old"
  268. #endif /* XLOG_H */