xlog_internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * xlog_internal.h
  3. *
  4. * PostgreSQL transaction 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_receivexlog to deal with WAL files.
  13. *
  14. * Portions Copyright (c) 1996-2016, 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 0xD093 /* 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.
  42. *
  43. * Note that xl_rem_len includes backup-block data; that is, it tracks
  44. * xl_tot_len not xl_len in the initial header. Also note that the
  45. * continuation data isn't necessarily aligned.
  46. */
  47. uint32 xlp_rem_len; /* total len of remaining data for record */
  48. } XLogPageHeaderData;
  49. #define SizeOfXLogShortPHD MAXALIGN(sizeof(XLogPageHeaderData))
  50. typedef XLogPageHeaderData *XLogPageHeader;
  51. /*
  52. * When the XLP_LONG_HEADER flag is set, we store additional fields in the
  53. * page header. (This is ordinarily done just in the first page of an
  54. * XLOG file.) The additional fields serve to identify the file accurately.
  55. */
  56. typedef struct XLogLongPageHeaderData
  57. {
  58. XLogPageHeaderData std; /* standard header fields */
  59. uint64 xlp_sysid; /* system identifier from pg_control */
  60. uint32 xlp_seg_size; /* just as a cross-check */
  61. uint32 xlp_xlog_blcksz; /* just as a cross-check */
  62. } XLogLongPageHeaderData;
  63. #define SizeOfXLogLongPHD MAXALIGN(sizeof(XLogLongPageHeaderData))
  64. typedef XLogLongPageHeaderData *XLogLongPageHeader;
  65. /* When record crosses page boundary, set this flag in new page's header */
  66. #define XLP_FIRST_IS_CONTRECORD 0x0001
  67. /* This flag indicates a "long" page header */
  68. #define XLP_LONG_HEADER 0x0002
  69. /* This flag indicates backup blocks starting in this page are optional */
  70. #define XLP_BKP_REMOVABLE 0x0004
  71. /* All defined flag bits in xlp_info (used for validity checking of header) */
  72. #define XLP_ALL_FLAGS 0x0007
  73. #define XLogPageHeaderSize(hdr) \
  74. (((hdr)->xlp_info & XLP_LONG_HEADER) ? SizeOfXLogLongPHD : SizeOfXLogShortPHD)
  75. /*
  76. * The XLOG is split into WAL segments (physical files) of the size indicated
  77. * by XLOG_SEG_SIZE.
  78. */
  79. #define XLogSegSize ((uint32) XLOG_SEG_SIZE)
  80. #define XLogSegmentsPerXLogId (UINT64CONST(0x100000000) / XLOG_SEG_SIZE)
  81. #define XLogSegNoOffsetToRecPtr(segno, offset, dest) \
  82. (dest) = (segno) * XLOG_SEG_SIZE + (offset)
  83. /*
  84. * Compute a segment number from an XLogRecPtr.
  85. *
  86. * For XLByteToSeg, do the computation at face value. For XLByteToPrevSeg,
  87. * a boundary byte is taken to be in the previous segment. This is suitable
  88. * for deciding which segment to write given a pointer to a record end,
  89. * for example.
  90. */
  91. #define XLByteToSeg(xlrp, logSegNo) \
  92. logSegNo = (xlrp) / XLogSegSize
  93. #define XLByteToPrevSeg(xlrp, logSegNo) \
  94. logSegNo = ((xlrp) - 1) / XLogSegSize
  95. /*
  96. * Is an XLogRecPtr within a particular XLOG segment?
  97. *
  98. * For XLByteInSeg, do the computation at face value. For XLByteInPrevSeg,
  99. * a boundary byte is taken to be in the previous segment.
  100. */
  101. #define XLByteInSeg(xlrp, logSegNo) \
  102. (((xlrp) / XLogSegSize) == (logSegNo))
  103. #define XLByteInPrevSeg(xlrp, logSegNo) \
  104. ((((xlrp) - 1) / XLogSegSize) == (logSegNo))
  105. /* Check if an XLogRecPtr value is in a plausible range */
  106. #define XRecOffIsValid(xlrp) \
  107. ((xlrp) % XLOG_BLCKSZ >= SizeOfXLogShortPHD)
  108. /*
  109. * The XLog directory and control file (relative to $PGDATA)
  110. */
  111. #define XLOGDIR "pg_xlog"
  112. #define XLOG_CONTROL_FILE "global/pg_control"
  113. /*
  114. * These macros encapsulate knowledge about the exact layout of XLog file
  115. * names, timeline history file names, and archive-status file names.
  116. */
  117. #define MAXFNAMELEN 64
  118. /* Length of XLog file name */
  119. #define XLOG_FNAME_LEN 24
  120. #define XLogFileName(fname, tli, logSegNo) \
  121. snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, \
  122. (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
  123. (uint32) ((logSegNo) % XLogSegmentsPerXLogId))
  124. #define XLogFileNameById(fname, tli, log, seg) \
  125. snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, log, seg)
  126. #define IsXLogFileName(fname) \
  127. (strlen(fname) == XLOG_FNAME_LEN && \
  128. strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN)
  129. /*
  130. * XLOG segment with .partial suffix. Used by pg_receivexlog and at end of
  131. * archive recovery, when we want to archive a WAL segment but it might not
  132. * be complete yet.
  133. */
  134. #define IsPartialXLogFileName(fname) \
  135. (strlen(fname) == XLOG_FNAME_LEN + strlen(".partial") && \
  136. strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
  137. strcmp((fname) + XLOG_FNAME_LEN, ".partial") == 0)
  138. #define XLogFromFileName(fname, tli, logSegNo) \
  139. do { \
  140. uint32 log; \
  141. uint32 seg; \
  142. sscanf(fname, "%08X%08X%08X", tli, &log, &seg); \
  143. *logSegNo = (uint64) log * XLogSegmentsPerXLogId + seg; \
  144. } while (0)
  145. #define XLogFilePath(path, tli, logSegNo) \
  146. snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X", tli, \
  147. (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
  148. (uint32) ((logSegNo) % XLogSegmentsPerXLogId))
  149. #define TLHistoryFileName(fname, tli) \
  150. snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
  151. #define IsTLHistoryFileName(fname) \
  152. (strlen(fname) == 8 + strlen(".history") && \
  153. strspn(fname, "0123456789ABCDEF") == 8 && \
  154. strcmp((fname) + 8, ".history") == 0)
  155. #define TLHistoryFilePath(path, tli) \
  156. snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
  157. #define StatusFilePath(path, xlog, suffix) \
  158. snprintf(path, MAXPGPATH, XLOGDIR "/archive_status/%s%s", xlog, suffix)
  159. #define BackupHistoryFileName(fname, tli, logSegNo, offset) \
  160. snprintf(fname, MAXFNAMELEN, "%08X%08X%08X.%08X.backup", tli, \
  161. (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
  162. (uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)
  163. #define IsBackupHistoryFileName(fname) \
  164. (strlen(fname) > XLOG_FNAME_LEN && \
  165. strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
  166. strcmp((fname) + strlen(fname) - strlen(".backup"), ".backup") == 0)
  167. #define BackupHistoryFilePath(path, tli, logSegNo, offset) \
  168. snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X.%08X.backup", tli, \
  169. (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
  170. (uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)
  171. /*
  172. * Information logged when we detect a change in one of the parameters
  173. * important for Hot Standby.
  174. */
  175. typedef struct xl_parameter_change
  176. {
  177. int MaxConnections;
  178. int max_worker_processes;
  179. int max_prepared_xacts;
  180. int max_locks_per_xact;
  181. int wal_level;
  182. bool wal_log_hints;
  183. bool track_commit_timestamp;
  184. } xl_parameter_change;
  185. /* logs restore point */
  186. typedef struct xl_restore_point
  187. {
  188. TimestampTz rp_time;
  189. char rp_name[MAXFNAMELEN];
  190. } xl_restore_point;
  191. /* End of recovery mark, when we don't do an END_OF_RECOVERY checkpoint */
  192. typedef struct xl_end_of_recovery
  193. {
  194. TimestampTz end_time;
  195. TimeLineID ThisTimeLineID; /* new TLI */
  196. TimeLineID PrevTimeLineID; /* previous TLI we forked off from */
  197. } xl_end_of_recovery;
  198. /*
  199. * The functions in xloginsert.c construct a chain of XLogRecData structs
  200. * to represent the final WAL record.
  201. */
  202. typedef struct XLogRecData
  203. {
  204. struct XLogRecData *next; /* next struct in chain, or NULL */
  205. char *data; /* start of rmgr data to include */
  206. uint32 len; /* length of rmgr data to include */
  207. } XLogRecData;
  208. /*
  209. * Recovery target action.
  210. */
  211. typedef enum
  212. {
  213. RECOVERY_TARGET_ACTION_PAUSE,
  214. RECOVERY_TARGET_ACTION_PROMOTE,
  215. RECOVERY_TARGET_ACTION_SHUTDOWN
  216. } RecoveryTargetAction;
  217. /*
  218. * Method table for resource managers.
  219. *
  220. * This struct must be kept in sync with the PG_RMGR definition in
  221. * rmgr.c.
  222. *
  223. * rm_identify must return a name for the record based on xl_info (without
  224. * reference to the rmid). For example, XLOG_BTREE_VACUUM would be named
  225. * "VACUUM". rm_desc can then be called to obtain additional detail for the
  226. * record, if available (e.g. the last block).
  227. *
  228. * RmgrTable[] is indexed by RmgrId values (see rmgrlist.h).
  229. */
  230. typedef struct RmgrData
  231. {
  232. const char *rm_name;
  233. void (*rm_redo) (XLogReaderState *record);
  234. void (*rm_desc) (StringInfo buf, XLogReaderState *record);
  235. const char *(*rm_identify) (uint8 info);
  236. void (*rm_startup) (void);
  237. void (*rm_cleanup) (void);
  238. } RmgrData;
  239. extern const RmgrData RmgrTable[];
  240. /*
  241. * Exported to support xlog switching from checkpointer
  242. */
  243. extern pg_time_t GetLastSegSwitchTime(void);
  244. extern XLogRecPtr RequestXLogSwitch(void);
  245. extern void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli);
  246. /*
  247. * Exported for the functions in timeline.c and xlogarchive.c. Only valid
  248. * in the startup process.
  249. */
  250. extern bool ArchiveRecoveryRequested;
  251. extern bool InArchiveRecovery;
  252. extern bool StandbyMode;
  253. extern char *recoveryRestoreCommand;
  254. /*
  255. * Prototypes for functions in xlogarchive.c
  256. */
  257. extern bool RestoreArchivedFile(char *path, const char *xlogfname,
  258. const char *recovername, off_t expectedSize,
  259. bool cleanupEnabled);
  260. extern void ExecuteRecoveryCommand(char *command, char *commandName,
  261. bool failOnerror);
  262. extern void KeepFileRestoredFromArchive(char *path, char *xlogfname);
  263. extern void XLogArchiveNotify(const char *xlog);
  264. extern void XLogArchiveNotifySeg(XLogSegNo segno);
  265. extern void XLogArchiveForceDone(const char *xlog);
  266. extern bool XLogArchiveCheckDone(const char *xlog);
  267. extern bool XLogArchiveIsBusy(const char *xlog);
  268. extern bool XLogArchiveIsReady(const char *xlog);
  269. extern bool XLogArchiveIsReadyOrDone(const char *xlog);
  270. extern void XLogArchiveCleanup(const char *xlog);
  271. #endif /* XLOG_INTERNAL_H */