xlog_internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * xlog_internal.h
  3. *
  4. * PostgreSQL write-ahead log internal declarations
  5. *
  6. * NOTE: this file is intended to contain declarations useful for
  7. * manipulating the XLOG files directly, but it is not supposed to be
  8. * needed by rmgr routines (redo support for individual record types).
  9. * So the XLogRecord typedef and associated stuff appear in xlogrecord.h.
  10. *
  11. * Note: This file must be includable in both frontend and backend contexts,
  12. * to allow stand-alone tools like pg_receivewal to deal with WAL files.
  13. *
  14. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  15. * Portions Copyright (c) 1994, Regents of the University of California
  16. *
  17. * src/include/access/xlog_internal.h
  18. */
  19. #ifndef XLOG_INTERNAL_H
  20. #define XLOG_INTERNAL_H
  21. #include "access/xlogdefs.h"
  22. #include "access/xlogreader.h"
  23. #include "datatype/timestamp.h"
  24. #include "lib/stringinfo.h"
  25. #include "pgtime.h"
  26. #include "storage/block.h"
  27. #include "storage/relfilenode.h"
  28. /*
  29. * Each page of XLOG file has a header like this:
  30. */
  31. #define XLOG_PAGE_MAGIC 0xD110 /* can be used as WAL version indicator */
  32. typedef struct XLogPageHeaderData
  33. {
  34. uint16 xlp_magic; /* magic value for correctness checks */
  35. uint16 xlp_info; /* flag bits, see below */
  36. TimeLineID xlp_tli; /* TimeLineID of first record on page */
  37. XLogRecPtr xlp_pageaddr; /* XLOG address of this page */
  38. /*
  39. * When there is not enough space on current page for whole record, we
  40. * continue on the next page. xlp_rem_len is the number of bytes
  41. * remaining from a previous page; it tracks xl_tot_len in the initial
  42. * header. Note that the continuation data isn't necessarily aligned.
  43. */
  44. uint32 xlp_rem_len; /* total len of remaining data for record */
  45. } XLogPageHeaderData;
  46. #define SizeOfXLogShortPHD MAXALIGN(sizeof(XLogPageHeaderData))
  47. typedef XLogPageHeaderData *XLogPageHeader;
  48. /*
  49. * When the XLP_LONG_HEADER flag is set, we store additional fields in the
  50. * page header. (This is ordinarily done just in the first page of an
  51. * XLOG file.) The additional fields serve to identify the file accurately.
  52. */
  53. typedef struct XLogLongPageHeaderData
  54. {
  55. XLogPageHeaderData std; /* standard header fields */
  56. uint64 xlp_sysid; /* system identifier from pg_control */
  57. uint32 xlp_seg_size; /* just as a cross-check */
  58. uint32 xlp_xlog_blcksz; /* just as a cross-check */
  59. } XLogLongPageHeaderData;
  60. #define SizeOfXLogLongPHD MAXALIGN(sizeof(XLogLongPageHeaderData))
  61. typedef XLogLongPageHeaderData *XLogLongPageHeader;
  62. /* When record crosses page boundary, set this flag in new page's header */
  63. #define XLP_FIRST_IS_CONTRECORD 0x0001
  64. /* This flag indicates a "long" page header */
  65. #define XLP_LONG_HEADER 0x0002
  66. /* This flag indicates backup blocks starting in this page are optional */
  67. #define XLP_BKP_REMOVABLE 0x0004
  68. /* Replaces a missing contrecord; see CreateOverwriteContrecordRecord */
  69. #define XLP_FIRST_IS_OVERWRITE_CONTRECORD 0x0008
  70. /* All defined flag bits in xlp_info (used for validity checking of header) */
  71. #define XLP_ALL_FLAGS 0x000F
  72. #define XLogPageHeaderSize(hdr) \
  73. (((hdr)->xlp_info & XLP_LONG_HEADER) ? SizeOfXLogLongPHD : SizeOfXLogShortPHD)
  74. /* wal_segment_size can range from 1MB to 1GB */
  75. #define WalSegMinSize 1024 * 1024
  76. #define WalSegMaxSize 1024 * 1024 * 1024
  77. /* default number of min and max wal segments */
  78. #define DEFAULT_MIN_WAL_SEGS 5
  79. #define DEFAULT_MAX_WAL_SEGS 64
  80. /* check that the given size is a valid wal_segment_size */
  81. #define IsPowerOf2(x) (x > 0 && ((x) & ((x)-1)) == 0)
  82. #define IsValidWalSegSize(size) \
  83. (IsPowerOf2(size) && \
  84. ((size) >= WalSegMinSize && (size) <= WalSegMaxSize))
  85. #define XLogSegmentsPerXLogId(wal_segsz_bytes) \
  86. (UINT64CONST(0x100000000) / (wal_segsz_bytes))
  87. #define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest) \
  88. (dest) = (segno) * (wal_segsz_bytes) + (offset)
  89. #define XLogSegmentOffset(xlogptr, wal_segsz_bytes) \
  90. ((xlogptr) & ((wal_segsz_bytes) - 1))
  91. /*
  92. * Compute a segment number from an XLogRecPtr.
  93. *
  94. * For XLByteToSeg, do the computation at face value. For XLByteToPrevSeg,
  95. * a boundary byte is taken to be in the previous segment. This is suitable
  96. * for deciding which segment to write given a pointer to a record end,
  97. * for example.
  98. */
  99. #define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes) \
  100. logSegNo = (xlrp) / (wal_segsz_bytes)
  101. #define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
  102. logSegNo = ((xlrp) - 1) / (wal_segsz_bytes)
  103. /*
  104. * Convert values of GUCs measured in megabytes to equiv. segment count.
  105. * Rounds down.
  106. */
  107. #define XLogMBVarToSegs(mbvar, wal_segsz_bytes) \
  108. ((mbvar) / ((wal_segsz_bytes) / (1024 * 1024)))
  109. /*
  110. * Is an XLogRecPtr within a particular XLOG segment?
  111. *
  112. * For XLByteInSeg, do the computation at face value. For XLByteInPrevSeg,
  113. * a boundary byte is taken to be in the previous segment.
  114. */
  115. #define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes) \
  116. (((xlrp) / (wal_segsz_bytes)) == (logSegNo))
  117. #define XLByteInPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
  118. ((((xlrp) - 1) / (wal_segsz_bytes)) == (logSegNo))
  119. /* Check if an XLogRecPtr value is in a plausible range */
  120. #define XRecOffIsValid(xlrp) \
  121. ((xlrp) % XLOG_BLCKSZ >= SizeOfXLogShortPHD)
  122. /*
  123. * The XLog directory and control file (relative to $PGDATA)
  124. */
  125. #define XLOGDIR "pg_wal"
  126. #define XLOG_CONTROL_FILE "global/pg_control"
  127. /*
  128. * These macros encapsulate knowledge about the exact layout of XLog file
  129. * names, timeline history file names, and archive-status file names.
  130. */
  131. #define MAXFNAMELEN 64
  132. /* Length of XLog file name */
  133. #define XLOG_FNAME_LEN 24
  134. /*
  135. * Generate a WAL segment file name. Do not use this macro in a helper
  136. * function allocating the result generated.
  137. */
  138. #define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes) \
  139. snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, \
  140. (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  141. (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)))
  142. #define XLogFileNameById(fname, tli, log, seg) \
  143. snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, log, seg)
  144. #define IsXLogFileName(fname) \
  145. (strlen(fname) == XLOG_FNAME_LEN && \
  146. strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN)
  147. /*
  148. * XLOG segment with .partial suffix. Used by pg_receivewal and at end of
  149. * archive recovery, when we want to archive a WAL segment but it might not
  150. * be complete yet.
  151. */
  152. #define IsPartialXLogFileName(fname) \
  153. (strlen(fname) == XLOG_FNAME_LEN + strlen(".partial") && \
  154. strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
  155. strcmp((fname) + XLOG_FNAME_LEN, ".partial") == 0)
  156. #define XLogFromFileName(fname, tli, logSegNo, wal_segsz_bytes) \
  157. do { \
  158. uint32 log; \
  159. uint32 seg; \
  160. sscanf(fname, "%08X%08X%08X", tli, &log, &seg); \
  161. *logSegNo = (uint64) log * XLogSegmentsPerXLogId(wal_segsz_bytes) + seg; \
  162. } while (0)
  163. #define XLogFilePath(path, tli, logSegNo, wal_segsz_bytes) \
  164. snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X", tli, \
  165. (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  166. (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)))
  167. #define TLHistoryFileName(fname, tli) \
  168. snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
  169. #define IsTLHistoryFileName(fname) \
  170. (strlen(fname) == 8 + strlen(".history") && \
  171. strspn(fname, "0123456789ABCDEF") == 8 && \
  172. strcmp((fname) + 8, ".history") == 0)
  173. #define TLHistoryFilePath(path, tli) \
  174. snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
  175. #define StatusFilePath(path, xlog, suffix) \
  176. snprintf(path, MAXPGPATH, XLOGDIR "/archive_status/%s%s", xlog, suffix)
  177. #define BackupHistoryFileName(fname, tli, logSegNo, startpoint, wal_segsz_bytes) \
  178. snprintf(fname, MAXFNAMELEN, "%08X%08X%08X.%08X.backup", tli, \
  179. (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  180. (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  181. (uint32) (XLogSegmentOffset(startpoint, wal_segsz_bytes)))
  182. #define IsBackupHistoryFileName(fname) \
  183. (strlen(fname) > XLOG_FNAME_LEN && \
  184. strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
  185. strcmp((fname) + strlen(fname) - strlen(".backup"), ".backup") == 0)
  186. #define BackupHistoryFilePath(path, tli, logSegNo, startpoint, wal_segsz_bytes) \
  187. snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X.%08X.backup", tli, \
  188. (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  189. (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  190. (uint32) (XLogSegmentOffset((startpoint), wal_segsz_bytes)))
  191. /*
  192. * Information logged when we detect a change in one of the parameters
  193. * important for Hot Standby.
  194. */
  195. typedef struct xl_parameter_change
  196. {
  197. int MaxConnections;
  198. int max_worker_processes;
  199. int max_wal_senders;
  200. int max_prepared_xacts;
  201. int max_locks_per_xact;
  202. int wal_level;
  203. bool wal_log_hints;
  204. bool track_commit_timestamp;
  205. } xl_parameter_change;
  206. /* logs restore point */
  207. typedef struct xl_restore_point
  208. {
  209. TimestampTz rp_time;
  210. char rp_name[MAXFNAMELEN];
  211. } xl_restore_point;
  212. /* Overwrite of prior contrecord */
  213. typedef struct xl_overwrite_contrecord
  214. {
  215. XLogRecPtr overwritten_lsn;
  216. TimestampTz overwrite_time;
  217. } xl_overwrite_contrecord;
  218. /* End of recovery mark, when we don't do an END_OF_RECOVERY checkpoint */
  219. typedef struct xl_end_of_recovery
  220. {
  221. TimestampTz end_time;
  222. TimeLineID ThisTimeLineID; /* new TLI */
  223. TimeLineID PrevTimeLineID; /* previous TLI we forked off from */
  224. } xl_end_of_recovery;
  225. /*
  226. * The functions in xloginsert.c construct a chain of XLogRecData structs
  227. * to represent the final WAL record.
  228. */
  229. typedef struct XLogRecData
  230. {
  231. struct XLogRecData *next; /* next struct in chain, or NULL */
  232. char *data; /* start of rmgr data to include */
  233. uint32 len; /* length of rmgr data to include */
  234. } XLogRecData;
  235. /*
  236. * Recovery target action.
  237. */
  238. typedef enum
  239. {
  240. RECOVERY_TARGET_ACTION_PAUSE,
  241. RECOVERY_TARGET_ACTION_PROMOTE,
  242. RECOVERY_TARGET_ACTION_SHUTDOWN
  243. } RecoveryTargetAction;
  244. struct LogicalDecodingContext;
  245. struct XLogRecordBuffer;
  246. /*
  247. * Method table for resource managers.
  248. *
  249. * This struct must be kept in sync with the PG_RMGR definition in
  250. * rmgr.c.
  251. *
  252. * rm_identify must return a name for the record based on xl_info (without
  253. * reference to the rmid). For example, XLOG_BTREE_VACUUM would be named
  254. * "VACUUM". rm_desc can then be called to obtain additional detail for the
  255. * record, if available (e.g. the last block).
  256. *
  257. * rm_mask takes as input a page modified by the resource manager and masks
  258. * out bits that shouldn't be flagged by wal_consistency_checking.
  259. *
  260. * RmgrTable[] is indexed by RmgrId values (see rmgrlist.h). If rm_name is
  261. * NULL, the corresponding RmgrTable entry is considered invalid.
  262. */
  263. typedef struct RmgrData
  264. {
  265. const char *rm_name;
  266. void (*rm_redo) (XLogReaderState *record);
  267. void (*rm_desc) (StringInfo buf, XLogReaderState *record);
  268. const char *(*rm_identify) (uint8 info);
  269. void (*rm_startup) (void);
  270. void (*rm_cleanup) (void);
  271. void (*rm_mask) (char *pagedata, BlockNumber blkno);
  272. void (*rm_decode) (struct LogicalDecodingContext *ctx,
  273. struct XLogRecordBuffer *buf);
  274. } RmgrData;
  275. extern PGDLLIMPORT RmgrData RmgrTable[];
  276. extern void RmgrStartup(void);
  277. extern void RmgrCleanup(void);
  278. extern void RmgrNotFound(RmgrId rmid);
  279. extern void RegisterCustomRmgr(RmgrId rmid, RmgrData *rmgr);
  280. #ifndef FRONTEND
  281. static inline bool
  282. RmgrIdExists(RmgrId rmid)
  283. {
  284. return RmgrTable[rmid].rm_name != NULL;
  285. }
  286. static inline RmgrData
  287. GetRmgr(RmgrId rmid)
  288. {
  289. if (unlikely(!RmgrIdExists(rmid)))
  290. RmgrNotFound(rmid);
  291. return RmgrTable[rmid];
  292. }
  293. #endif
  294. /*
  295. * Exported to support xlog switching from checkpointer
  296. */
  297. extern pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN);
  298. extern XLogRecPtr RequestXLogSwitch(bool mark_unimportant);
  299. extern void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli);
  300. extern void XLogRecGetBlockRefInfo(XLogReaderState *record, bool pretty,
  301. bool detailed_format, StringInfo buf,
  302. uint32 *fpi_len);
  303. /*
  304. * Exported for the functions in timeline.c and xlogarchive.c. Only valid
  305. * in the startup process.
  306. */
  307. extern PGDLLIMPORT bool ArchiveRecoveryRequested;
  308. extern PGDLLIMPORT bool InArchiveRecovery;
  309. extern PGDLLIMPORT bool StandbyMode;
  310. extern PGDLLIMPORT char *recoveryRestoreCommand;
  311. #endif /* XLOG_INTERNAL_H */