gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

338 líneas
11KB

  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-2019, 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 0xD101 /* 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. /* wal_segment_size can range from 1MB to 1GB */
  76. #define WalSegMinSize 1024 * 1024
  77. #define WalSegMaxSize 1024 * 1024 * 1024
  78. /* default number of min and max wal segments */
  79. #define DEFAULT_MIN_WAL_SEGS 5
  80. #define DEFAULT_MAX_WAL_SEGS 64
  81. /* check that the given size is a valid wal_segment_size */
  82. #define IsPowerOf2(x) (x > 0 && ((x) & ((x)-1)) == 0)
  83. #define IsValidWalSegSize(size) \
  84. (IsPowerOf2(size) && \
  85. ((size) >= WalSegMinSize && (size) <= WalSegMaxSize))
  86. #define XLogSegmentsPerXLogId(wal_segsz_bytes) \
  87. (UINT64CONST(0x100000000) / (wal_segsz_bytes))
  88. #define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest) \
  89. (dest) = (segno) * (wal_segsz_bytes) + (offset)
  90. #define XLogSegmentOffset(xlogptr, wal_segsz_bytes) \
  91. ((xlogptr) & ((wal_segsz_bytes) - 1))
  92. /*
  93. * Compute a segment number from an XLogRecPtr.
  94. *
  95. * For XLByteToSeg, do the computation at face value. For XLByteToPrevSeg,
  96. * a boundary byte is taken to be in the previous segment. This is suitable
  97. * for deciding which segment to write given a pointer to a record end,
  98. * for example.
  99. */
  100. #define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes) \
  101. logSegNo = (xlrp) / (wal_segsz_bytes)
  102. #define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
  103. logSegNo = ((xlrp) - 1) / (wal_segsz_bytes)
  104. /*
  105. * Is an XLogRecPtr within a particular XLOG segment?
  106. *
  107. * For XLByteInSeg, do the computation at face value. For XLByteInPrevSeg,
  108. * a boundary byte is taken to be in the previous segment.
  109. */
  110. #define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes) \
  111. (((xlrp) / (wal_segsz_bytes)) == (logSegNo))
  112. #define XLByteInPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
  113. ((((xlrp) - 1) / (wal_segsz_bytes)) == (logSegNo))
  114. /* Check if an XLogRecPtr value is in a plausible range */
  115. #define XRecOffIsValid(xlrp) \
  116. ((xlrp) % XLOG_BLCKSZ >= SizeOfXLogShortPHD)
  117. /*
  118. * The XLog directory and control file (relative to $PGDATA)
  119. */
  120. #define XLOGDIR "pg_wal"
  121. #define XLOG_CONTROL_FILE "global/pg_control"
  122. /*
  123. * These macros encapsulate knowledge about the exact layout of XLog file
  124. * names, timeline history file names, and archive-status file names.
  125. */
  126. #define MAXFNAMELEN 64
  127. /* Length of XLog file name */
  128. #define XLOG_FNAME_LEN 24
  129. #define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes) \
  130. snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, \
  131. (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  132. (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)))
  133. #define XLogFileNameById(fname, tli, log, seg) \
  134. snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, log, seg)
  135. #define IsXLogFileName(fname) \
  136. (strlen(fname) == XLOG_FNAME_LEN && \
  137. strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN)
  138. /*
  139. * XLOG segment with .partial suffix. Used by pg_receivewal and at end of
  140. * archive recovery, when we want to archive a WAL segment but it might not
  141. * be complete yet.
  142. */
  143. #define IsPartialXLogFileName(fname) \
  144. (strlen(fname) == XLOG_FNAME_LEN + strlen(".partial") && \
  145. strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
  146. strcmp((fname) + XLOG_FNAME_LEN, ".partial") == 0)
  147. #define XLogFromFileName(fname, tli, logSegNo, wal_segsz_bytes) \
  148. do { \
  149. uint32 log; \
  150. uint32 seg; \
  151. sscanf(fname, "%08X%08X%08X", tli, &log, &seg); \
  152. *logSegNo = (uint64) log * XLogSegmentsPerXLogId(wal_segsz_bytes) + seg; \
  153. } while (0)
  154. #define XLogFilePath(path, tli, logSegNo, wal_segsz_bytes) \
  155. snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X", tli, \
  156. (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  157. (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)))
  158. #define TLHistoryFileName(fname, tli) \
  159. snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
  160. #define IsTLHistoryFileName(fname) \
  161. (strlen(fname) == 8 + strlen(".history") && \
  162. strspn(fname, "0123456789ABCDEF") == 8 && \
  163. strcmp((fname) + 8, ".history") == 0)
  164. #define TLHistoryFilePath(path, tli) \
  165. snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
  166. #define StatusFilePath(path, xlog, suffix) \
  167. snprintf(path, MAXPGPATH, XLOGDIR "/archive_status/%s%s", xlog, suffix)
  168. #define BackupHistoryFileName(fname, tli, logSegNo, startpoint, wal_segsz_bytes) \
  169. snprintf(fname, MAXFNAMELEN, "%08X%08X%08X.%08X.backup", tli, \
  170. (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  171. (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)), \
  172. (uint32) (XLogSegmentOffset(startpoint, wal_segsz_bytes)))
  173. #define IsBackupHistoryFileName(fname) \
  174. (strlen(fname) > XLOG_FNAME_LEN && \
  175. strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
  176. strcmp((fname) + strlen(fname) - strlen(".backup"), ".backup") == 0)
  177. #define BackupHistoryFilePath(path, tli, logSegNo, startpoint, wal_segsz_bytes) \
  178. snprintf(path, MAXPGPATH, XLOGDIR "/%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. /*
  183. * Information logged when we detect a change in one of the parameters
  184. * important for Hot Standby.
  185. */
  186. typedef struct xl_parameter_change
  187. {
  188. int MaxConnections;
  189. int max_worker_processes;
  190. int max_wal_senders;
  191. int max_prepared_xacts;
  192. int max_locks_per_xact;
  193. int wal_level;
  194. bool wal_log_hints;
  195. bool track_commit_timestamp;
  196. } xl_parameter_change;
  197. /* logs restore point */
  198. typedef struct xl_restore_point
  199. {
  200. TimestampTz rp_time;
  201. char rp_name[MAXFNAMELEN];
  202. } xl_restore_point;
  203. /* End of recovery mark, when we don't do an END_OF_RECOVERY checkpoint */
  204. typedef struct xl_end_of_recovery
  205. {
  206. TimestampTz end_time;
  207. TimeLineID ThisTimeLineID; /* new TLI */
  208. TimeLineID PrevTimeLineID; /* previous TLI we forked off from */
  209. } xl_end_of_recovery;
  210. /*
  211. * The functions in xloginsert.c construct a chain of XLogRecData structs
  212. * to represent the final WAL record.
  213. */
  214. typedef struct XLogRecData
  215. {
  216. struct XLogRecData *next; /* next struct in chain, or NULL */
  217. char *data; /* start of rmgr data to include */
  218. uint32 len; /* length of rmgr data to include */
  219. } XLogRecData;
  220. /*
  221. * Recovery target action.
  222. */
  223. typedef enum
  224. {
  225. RECOVERY_TARGET_ACTION_PAUSE,
  226. RECOVERY_TARGET_ACTION_PROMOTE,
  227. RECOVERY_TARGET_ACTION_SHUTDOWN
  228. } RecoveryTargetAction;
  229. /*
  230. * Method table for resource managers.
  231. *
  232. * This struct must be kept in sync with the PG_RMGR definition in
  233. * rmgr.c.
  234. *
  235. * rm_identify must return a name for the record based on xl_info (without
  236. * reference to the rmid). For example, XLOG_BTREE_VACUUM would be named
  237. * "VACUUM". rm_desc can then be called to obtain additional detail for the
  238. * record, if available (e.g. the last block).
  239. *
  240. * rm_mask takes as input a page modified by the resource manager and masks
  241. * out bits that shouldn't be flagged by wal_consistency_checking.
  242. *
  243. * RmgrTable[] is indexed by RmgrId values (see rmgrlist.h).
  244. */
  245. typedef struct RmgrData
  246. {
  247. const char *rm_name;
  248. void (*rm_redo) (XLogReaderState *record);
  249. void (*rm_desc) (StringInfo buf, XLogReaderState *record);
  250. const char *(*rm_identify) (uint8 info);
  251. void (*rm_startup) (void);
  252. void (*rm_cleanup) (void);
  253. void (*rm_mask) (char *pagedata, BlockNumber blkno);
  254. } RmgrData;
  255. extern const RmgrData RmgrTable[];
  256. /*
  257. * Exported to support xlog switching from checkpointer
  258. */
  259. extern pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN);
  260. extern XLogRecPtr RequestXLogSwitch(bool mark_unimportant);
  261. extern void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli);
  262. /*
  263. * Exported for the functions in timeline.c and xlogarchive.c. Only valid
  264. * in the startup process.
  265. */
  266. extern bool ArchiveRecoveryRequested;
  267. extern bool InArchiveRecovery;
  268. extern bool StandbyMode;
  269. extern char *recoveryRestoreCommand;
  270. /*
  271. * Prototypes for functions in xlogarchive.c
  272. */
  273. extern bool RestoreArchivedFile(char *path, const char *xlogfname,
  274. const char *recovername, off_t expectedSize,
  275. bool cleanupEnabled);
  276. extern void ExecuteRecoveryCommand(const char *command, const char *commandName,
  277. bool failOnerror);
  278. extern void KeepFileRestoredFromArchive(const char *path, const char *xlogfname);
  279. extern void XLogArchiveNotify(const char *xlog);
  280. extern void XLogArchiveNotifySeg(XLogSegNo segno);
  281. extern void XLogArchiveForceDone(const char *xlog);
  282. extern bool XLogArchiveCheckDone(const char *xlog);
  283. extern bool XLogArchiveIsBusy(const char *xlog);
  284. extern bool XLogArchiveIsReady(const char *xlog);
  285. extern bool XLogArchiveIsReadyOrDone(const char *xlog);
  286. extern void XLogArchiveCleanup(const char *xlog);
  287. #endif /* XLOG_INTERNAL_H */
上海开阖软件有限公司 沪ICP备12045867号-1