pg_control.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*-------------------------------------------------------------------------
  2. *
  3. * pg_control.h
  4. * The system control file "pg_control" is not a heap relation.
  5. * However, we define it here so that the format is documented.
  6. *
  7. *
  8. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  9. * Portions Copyright (c) 1994, Regents of the University of California
  10. *
  11. * src/include/catalog/pg_control.h
  12. *
  13. *-------------------------------------------------------------------------
  14. */
  15. #ifndef PG_CONTROL_H
  16. #define PG_CONTROL_H
  17. #include "access/transam.h"
  18. #include "access/xlogdefs.h"
  19. #include "pgtime.h" /* for pg_time_t */
  20. #include "port/pg_crc32c.h"
  21. /* Version identifier for this pg_control format */
  22. #define PG_CONTROL_VERSION 1300
  23. /* Nonce key length, see below */
  24. #define MOCK_AUTH_NONCE_LEN 32
  25. /*
  26. * Body of CheckPoint XLOG records. This is declared here because we keep
  27. * a copy of the latest one in pg_control for possible disaster recovery.
  28. * Changing this struct requires a PG_CONTROL_VERSION bump.
  29. */
  30. typedef struct CheckPoint
  31. {
  32. XLogRecPtr redo; /* next RecPtr available when we began to
  33. * create CheckPoint (i.e. REDO start point) */
  34. TimeLineID ThisTimeLineID; /* current TLI */
  35. TimeLineID PrevTimeLineID; /* previous TLI, if this record begins a new
  36. * timeline (equals ThisTimeLineID otherwise) */
  37. bool fullPageWrites; /* current full_page_writes */
  38. FullTransactionId nextXid; /* next free transaction ID */
  39. Oid nextOid; /* next free OID */
  40. MultiXactId nextMulti; /* next free MultiXactId */
  41. MultiXactOffset nextMultiOffset; /* next free MultiXact offset */
  42. TransactionId oldestXid; /* cluster-wide minimum datfrozenxid */
  43. Oid oldestXidDB; /* database with minimum datfrozenxid */
  44. MultiXactId oldestMulti; /* cluster-wide minimum datminmxid */
  45. Oid oldestMultiDB; /* database with minimum datminmxid */
  46. pg_time_t time; /* time stamp of checkpoint */
  47. TransactionId oldestCommitTsXid; /* oldest Xid with valid commit
  48. * timestamp */
  49. TransactionId newestCommitTsXid; /* newest Xid with valid commit
  50. * timestamp */
  51. /*
  52. * Oldest XID still running. This is only needed to initialize hot standby
  53. * mode from an online checkpoint, so we only bother calculating this for
  54. * online checkpoints and only when wal_level is replica. Otherwise it's
  55. * set to InvalidTransactionId.
  56. */
  57. TransactionId oldestActiveXid;
  58. } CheckPoint;
  59. /* XLOG info values for XLOG rmgr */
  60. #define XLOG_CHECKPOINT_SHUTDOWN 0x00
  61. #define XLOG_CHECKPOINT_ONLINE 0x10
  62. #define XLOG_NOOP 0x20
  63. #define XLOG_NEXTOID 0x30
  64. #define XLOG_SWITCH 0x40
  65. #define XLOG_BACKUP_END 0x50
  66. #define XLOG_PARAMETER_CHANGE 0x60
  67. #define XLOG_RESTORE_POINT 0x70
  68. #define XLOG_FPW_CHANGE 0x80
  69. #define XLOG_END_OF_RECOVERY 0x90
  70. #define XLOG_FPI_FOR_HINT 0xA0
  71. #define XLOG_FPI 0xB0
  72. /* 0xC0 is used in Postgres 9.5-11 */
  73. #define XLOG_OVERWRITE_CONTRECORD 0xD0
  74. /*
  75. * System status indicator. Note this is stored in pg_control; if you change
  76. * it, you must bump PG_CONTROL_VERSION
  77. */
  78. typedef enum DBState
  79. {
  80. DB_STARTUP = 0,
  81. DB_SHUTDOWNED,
  82. DB_SHUTDOWNED_IN_RECOVERY,
  83. DB_SHUTDOWNING,
  84. DB_IN_CRASH_RECOVERY,
  85. DB_IN_ARCHIVE_RECOVERY,
  86. DB_IN_PRODUCTION
  87. } DBState;
  88. /*
  89. * Contents of pg_control.
  90. */
  91. typedef struct ControlFileData
  92. {
  93. /*
  94. * Unique system identifier --- to ensure we match up xlog files with the
  95. * installation that produced them.
  96. */
  97. uint64 system_identifier;
  98. /*
  99. * Version identifier information. Keep these fields at the same offset,
  100. * especially pg_control_version; they won't be real useful if they move
  101. * around. (For historical reasons they must be 8 bytes into the file
  102. * rather than immediately at the front.)
  103. *
  104. * pg_control_version identifies the format of pg_control itself.
  105. * catalog_version_no identifies the format of the system catalogs.
  106. *
  107. * There are additional version identifiers in individual files; for
  108. * example, WAL logs contain per-page magic numbers that can serve as
  109. * version cues for the WAL log.
  110. */
  111. uint32 pg_control_version; /* PG_CONTROL_VERSION */
  112. uint32 catalog_version_no; /* see catversion.h */
  113. /*
  114. * System status data
  115. */
  116. DBState state; /* see enum above */
  117. pg_time_t time; /* time stamp of last pg_control update */
  118. XLogRecPtr checkPoint; /* last check point record ptr */
  119. CheckPoint checkPointCopy; /* copy of last check point record */
  120. XLogRecPtr unloggedLSN; /* current fake LSN value, for unlogged rels */
  121. /*
  122. * These two values determine the minimum point we must recover up to
  123. * before starting up:
  124. *
  125. * minRecoveryPoint is updated to the latest replayed LSN whenever we
  126. * flush a data change during archive recovery. That guards against
  127. * starting archive recovery, aborting it, and restarting with an earlier
  128. * stop location. If we've already flushed data changes from WAL record X
  129. * to disk, we mustn't start up until we reach X again. Zero when not
  130. * doing archive recovery.
  131. *
  132. * backupStartPoint is the redo pointer of the backup start checkpoint, if
  133. * we are recovering from an online backup and haven't reached the end of
  134. * backup yet. It is reset to zero when the end of backup is reached, and
  135. * we mustn't start up before that. A boolean would suffice otherwise, but
  136. * we use the redo pointer as a cross-check when we see an end-of-backup
  137. * record, to make sure the end-of-backup record corresponds the base
  138. * backup we're recovering from.
  139. *
  140. * backupEndPoint is the backup end location, if we are recovering from an
  141. * online backup which was taken from the standby and haven't reached the
  142. * end of backup yet. It is initialized to the minimum recovery point in
  143. * pg_control which was backed up last. It is reset to zero when the end
  144. * of backup is reached, and we mustn't start up before that.
  145. *
  146. * If backupEndRequired is true, we know for sure that we're restoring
  147. * from a backup, and must see a backup-end record before we can safely
  148. * start up.
  149. */
  150. XLogRecPtr minRecoveryPoint;
  151. TimeLineID minRecoveryPointTLI;
  152. XLogRecPtr backupStartPoint;
  153. XLogRecPtr backupEndPoint;
  154. bool backupEndRequired;
  155. /*
  156. * Parameter settings that determine if the WAL can be used for archival
  157. * or hot standby.
  158. */
  159. int wal_level;
  160. bool wal_log_hints;
  161. int MaxConnections;
  162. int max_worker_processes;
  163. int max_wal_senders;
  164. int max_prepared_xacts;
  165. int max_locks_per_xact;
  166. bool track_commit_timestamp;
  167. /*
  168. * This data is used to check for hardware-architecture compatibility of
  169. * the database and the backend executable. We need not check endianness
  170. * explicitly, since the pg_control version will surely look wrong to a
  171. * machine of different endianness, but we do need to worry about MAXALIGN
  172. * and floating-point format. (Note: storage layout nominally also
  173. * depends on SHORTALIGN and INTALIGN, but in practice these are the same
  174. * on all architectures of interest.)
  175. *
  176. * Testing just one double value is not a very bulletproof test for
  177. * floating-point compatibility, but it will catch most cases.
  178. */
  179. uint32 maxAlign; /* alignment requirement for tuples */
  180. double floatFormat; /* constant 1234567.0 */
  181. #define FLOATFORMAT_VALUE 1234567.0
  182. /*
  183. * This data is used to make sure that configuration of this database is
  184. * compatible with the backend executable.
  185. */
  186. uint32 blcksz; /* data block size for this DB */
  187. uint32 relseg_size; /* blocks per segment of large relation */
  188. uint32 xlog_blcksz; /* block size within WAL files */
  189. uint32 xlog_seg_size; /* size of each WAL segment */
  190. uint32 nameDataLen; /* catalog name field width */
  191. uint32 indexMaxKeys; /* max number of columns in an index */
  192. uint32 toast_max_chunk_size; /* chunk size in TOAST tables */
  193. uint32 loblksize; /* chunk size in pg_largeobject */
  194. bool float8ByVal; /* float8, int8, etc pass-by-value? */
  195. /* Are data pages protected by checksums? Zero if no checksum version */
  196. uint32 data_checksum_version;
  197. /*
  198. * Random nonce, used in authentication requests that need to proceed
  199. * based on values that are cluster-unique, like a SASL exchange that
  200. * failed at an early stage.
  201. */
  202. char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN];
  203. /* CRC of all above ... MUST BE LAST! */
  204. pg_crc32c crc;
  205. } ControlFileData;
  206. /*
  207. * Maximum safe value of sizeof(ControlFileData). For reliability's sake,
  208. * it's critical that pg_control updates be atomic writes. That generally
  209. * means the active data can't be more than one disk sector, which is 512
  210. * bytes on common hardware. Be very careful about raising this limit.
  211. */
  212. #define PG_CONTROL_MAX_SAFE_SIZE 512
  213. /*
  214. * Physical size of the pg_control file. Note that this is considerably
  215. * bigger than the actually used size (ie, sizeof(ControlFileData)).
  216. * The idea is to keep the physical size constant independent of format
  217. * changes, so that ReadControlFile will deliver a suitable wrong-version
  218. * message instead of a read error if it's looking at an incompatible file.
  219. */
  220. #define PG_CONTROL_FILE_SIZE 8192
  221. #endif /* PG_CONTROL_H */