gooderp18绿色标准版
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

421 Zeilen
13KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * heapam_xlog.h
  4. * POSTGRES heap access XLOG definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/access/heapam_xlog.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef HEAPAM_XLOG_H
  15. #define HEAPAM_XLOG_H
  16. #include "access/htup.h"
  17. #include "access/xlogreader.h"
  18. #include "lib/stringinfo.h"
  19. #include "storage/buf.h"
  20. #include "storage/bufpage.h"
  21. #include "storage/relfilenode.h"
  22. #include "utils/relcache.h"
  23. /*
  24. * WAL record definitions for heapam.c's WAL operations
  25. *
  26. * XLOG allows to store some information in high 4 bits of log
  27. * record xl_info field. We use 3 for opcode and one for init bit.
  28. */
  29. #define XLOG_HEAP_INSERT 0x00
  30. #define XLOG_HEAP_DELETE 0x10
  31. #define XLOG_HEAP_UPDATE 0x20
  32. #define XLOG_HEAP_TRUNCATE 0x30
  33. #define XLOG_HEAP_HOT_UPDATE 0x40
  34. #define XLOG_HEAP_CONFIRM 0x50
  35. #define XLOG_HEAP_LOCK 0x60
  36. #define XLOG_HEAP_INPLACE 0x70
  37. #define XLOG_HEAP_OPMASK 0x70
  38. /*
  39. * When we insert 1st item on new page in INSERT, UPDATE, HOT_UPDATE,
  40. * or MULTI_INSERT, we can (and we do) restore entire page in redo
  41. */
  42. #define XLOG_HEAP_INIT_PAGE 0x80
  43. /*
  44. * We ran out of opcodes, so heapam.c now has a second RmgrId. These opcodes
  45. * are associated with RM_HEAP2_ID, but are not logically different from
  46. * the ones above associated with RM_HEAP_ID. XLOG_HEAP_OPMASK applies to
  47. * these, too.
  48. */
  49. #define XLOG_HEAP2_REWRITE 0x00
  50. #define XLOG_HEAP2_CLEAN 0x10
  51. #define XLOG_HEAP2_FREEZE_PAGE 0x20
  52. #define XLOG_HEAP2_CLEANUP_INFO 0x30
  53. #define XLOG_HEAP2_VISIBLE 0x40
  54. #define XLOG_HEAP2_MULTI_INSERT 0x50
  55. #define XLOG_HEAP2_LOCK_UPDATED 0x60
  56. #define XLOG_HEAP2_NEW_CID 0x70
  57. /*
  58. * xl_heap_insert/xl_heap_multi_insert flag values, 8 bits are available.
  59. */
  60. /* PD_ALL_VISIBLE was cleared */
  61. #define XLH_INSERT_ALL_VISIBLE_CLEARED (1<<0)
  62. #define XLH_INSERT_LAST_IN_MULTI (1<<1)
  63. #define XLH_INSERT_IS_SPECULATIVE (1<<2)
  64. #define XLH_INSERT_CONTAINS_NEW_TUPLE (1<<3)
  65. /*
  66. * xl_heap_update flag values, 8 bits are available.
  67. */
  68. /* PD_ALL_VISIBLE was cleared */
  69. #define XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED (1<<0)
  70. /* PD_ALL_VISIBLE was cleared in the 2nd page */
  71. #define XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED (1<<1)
  72. #define XLH_UPDATE_CONTAINS_OLD_TUPLE (1<<2)
  73. #define XLH_UPDATE_CONTAINS_OLD_KEY (1<<3)
  74. #define XLH_UPDATE_CONTAINS_NEW_TUPLE (1<<4)
  75. #define XLH_UPDATE_PREFIX_FROM_OLD (1<<5)
  76. #define XLH_UPDATE_SUFFIX_FROM_OLD (1<<6)
  77. /* convenience macro for checking whether any form of old tuple was logged */
  78. #define XLH_UPDATE_CONTAINS_OLD \
  79. (XLH_UPDATE_CONTAINS_OLD_TUPLE | XLH_UPDATE_CONTAINS_OLD_KEY)
  80. /*
  81. * xl_heap_delete flag values, 8 bits are available.
  82. */
  83. /* PD_ALL_VISIBLE was cleared */
  84. #define XLH_DELETE_ALL_VISIBLE_CLEARED (1<<0)
  85. #define XLH_DELETE_CONTAINS_OLD_TUPLE (1<<1)
  86. #define XLH_DELETE_CONTAINS_OLD_KEY (1<<2)
  87. #define XLH_DELETE_IS_SUPER (1<<3)
  88. #define XLH_DELETE_IS_PARTITION_MOVE (1<<4)
  89. /* convenience macro for checking whether any form of old tuple was logged */
  90. #define XLH_DELETE_CONTAINS_OLD \
  91. (XLH_DELETE_CONTAINS_OLD_TUPLE | XLH_DELETE_CONTAINS_OLD_KEY)
  92. /* This is what we need to know about delete */
  93. typedef struct xl_heap_delete
  94. {
  95. TransactionId xmax; /* xmax of the deleted tuple */
  96. OffsetNumber offnum; /* deleted tuple's offset */
  97. uint8 infobits_set; /* infomask bits */
  98. uint8 flags;
  99. } xl_heap_delete;
  100. #define SizeOfHeapDelete (offsetof(xl_heap_delete, flags) + sizeof(uint8))
  101. /*
  102. * xl_heap_truncate flag values, 8 bits are available.
  103. */
  104. #define XLH_TRUNCATE_CASCADE (1<<0)
  105. #define XLH_TRUNCATE_RESTART_SEQS (1<<1)
  106. /*
  107. * For truncate we list all truncated relids in an array, followed by all
  108. * sequence relids that need to be restarted, if any.
  109. * All rels are always within the same database, so we just list dbid once.
  110. */
  111. typedef struct xl_heap_truncate
  112. {
  113. Oid dbId;
  114. uint32 nrelids;
  115. uint8 flags;
  116. Oid relids[FLEXIBLE_ARRAY_MEMBER];
  117. } xl_heap_truncate;
  118. #define SizeOfHeapTruncate (offsetof(xl_heap_truncate, relids))
  119. /*
  120. * We don't store the whole fixed part (HeapTupleHeaderData) of an inserted
  121. * or updated tuple in WAL; we can save a few bytes by reconstructing the
  122. * fields that are available elsewhere in the WAL record, or perhaps just
  123. * plain needn't be reconstructed. These are the fields we must store.
  124. * NOTE: t_hoff could be recomputed, but we may as well store it because
  125. * it will come for free due to alignment considerations.
  126. */
  127. typedef struct xl_heap_header
  128. {
  129. uint16 t_infomask2;
  130. uint16 t_infomask;
  131. uint8 t_hoff;
  132. } xl_heap_header;
  133. #define SizeOfHeapHeader (offsetof(xl_heap_header, t_hoff) + sizeof(uint8))
  134. /* This is what we need to know about insert */
  135. typedef struct xl_heap_insert
  136. {
  137. OffsetNumber offnum; /* inserted tuple's offset */
  138. uint8 flags;
  139. /* xl_heap_header & TUPLE DATA in backup block 0 */
  140. } xl_heap_insert;
  141. #define SizeOfHeapInsert (offsetof(xl_heap_insert, flags) + sizeof(uint8))
  142. /*
  143. * This is what we need to know about a multi-insert.
  144. *
  145. * The main data of the record consists of this xl_heap_multi_insert header.
  146. * 'offsets' array is omitted if the whole page is reinitialized
  147. * (XLOG_HEAP_INIT_PAGE).
  148. *
  149. * In block 0's data portion, there is an xl_multi_insert_tuple struct,
  150. * followed by the tuple data for each tuple. There is padding to align
  151. * each xl_multi_insert struct.
  152. */
  153. typedef struct xl_heap_multi_insert
  154. {
  155. uint8 flags;
  156. uint16 ntuples;
  157. OffsetNumber offsets[FLEXIBLE_ARRAY_MEMBER];
  158. } xl_heap_multi_insert;
  159. #define SizeOfHeapMultiInsert offsetof(xl_heap_multi_insert, offsets)
  160. typedef struct xl_multi_insert_tuple
  161. {
  162. uint16 datalen; /* size of tuple data that follows */
  163. uint16 t_infomask2;
  164. uint16 t_infomask;
  165. uint8 t_hoff;
  166. /* TUPLE DATA FOLLOWS AT END OF STRUCT */
  167. } xl_multi_insert_tuple;
  168. #define SizeOfMultiInsertTuple (offsetof(xl_multi_insert_tuple, t_hoff) + sizeof(uint8))
  169. /*
  170. * This is what we need to know about update|hot_update
  171. *
  172. * Backup blk 0: new page
  173. *
  174. * If XLOG_HEAP_PREFIX_FROM_OLD or XLOG_HEAP_SUFFIX_FROM_OLD flags are set,
  175. * the prefix and/or suffix come first, as one or two uint16s.
  176. *
  177. * After that, xl_heap_header and new tuple data follow. The new tuple
  178. * data doesn't include the prefix and suffix, which are copied from the
  179. * old tuple on replay.
  180. *
  181. * If HEAP_CONTAINS_NEW_TUPLE_DATA flag is given, the tuple data is
  182. * included even if a full-page image was taken.
  183. *
  184. * Backup blk 1: old page, if different. (no data, just a reference to the blk)
  185. */
  186. typedef struct xl_heap_update
  187. {
  188. TransactionId old_xmax; /* xmax of the old tuple */
  189. OffsetNumber old_offnum; /* old tuple's offset */
  190. uint8 old_infobits_set; /* infomask bits to set on old tuple */
  191. uint8 flags;
  192. TransactionId new_xmax; /* xmax of the new tuple */
  193. OffsetNumber new_offnum; /* new tuple's offset */
  194. /*
  195. * If XLOG_HEAP_CONTAINS_OLD_TUPLE or XLOG_HEAP_CONTAINS_OLD_KEY flags are
  196. * set, a xl_heap_header struct and tuple data for the old tuple follows.
  197. */
  198. } xl_heap_update;
  199. #define SizeOfHeapUpdate (offsetof(xl_heap_update, new_offnum) + sizeof(OffsetNumber))
  200. /*
  201. * This is what we need to know about vacuum page cleanup/redirect
  202. *
  203. * The array of OffsetNumbers following the fixed part of the record contains:
  204. * * for each redirected item: the item offset, then the offset redirected to
  205. * * for each now-dead item: the item offset
  206. * * for each now-unused item: the item offset
  207. * The total number of OffsetNumbers is therefore 2*nredirected+ndead+nunused.
  208. * Note that nunused is not explicitly stored, but may be found by reference
  209. * to the total record length.
  210. */
  211. typedef struct xl_heap_clean
  212. {
  213. TransactionId latestRemovedXid;
  214. uint16 nredirected;
  215. uint16 ndead;
  216. /* OFFSET NUMBERS are in the block reference 0 */
  217. } xl_heap_clean;
  218. #define SizeOfHeapClean (offsetof(xl_heap_clean, ndead) + sizeof(uint16))
  219. /*
  220. * Cleanup_info is required in some cases during a lazy VACUUM.
  221. * Used for reporting the results of HeapTupleHeaderAdvanceLatestRemovedXid()
  222. * see vacuumlazy.c for full explanation
  223. */
  224. typedef struct xl_heap_cleanup_info
  225. {
  226. RelFileNode node;
  227. TransactionId latestRemovedXid;
  228. } xl_heap_cleanup_info;
  229. #define SizeOfHeapCleanupInfo (sizeof(xl_heap_cleanup_info))
  230. /* flags for infobits_set */
  231. #define XLHL_XMAX_IS_MULTI 0x01
  232. #define XLHL_XMAX_LOCK_ONLY 0x02
  233. #define XLHL_XMAX_EXCL_LOCK 0x04
  234. #define XLHL_XMAX_KEYSHR_LOCK 0x08
  235. #define XLHL_KEYS_UPDATED 0x10
  236. /* flag bits for xl_heap_lock / xl_heap_lock_updated's flag field */
  237. #define XLH_LOCK_ALL_FROZEN_CLEARED 0x01
  238. /* This is what we need to know about lock */
  239. typedef struct xl_heap_lock
  240. {
  241. TransactionId locking_xid; /* might be a MultiXactId not xid */
  242. OffsetNumber offnum; /* locked tuple's offset on page */
  243. int8 infobits_set; /* infomask and infomask2 bits to set */
  244. uint8 flags; /* XLH_LOCK_* flag bits */
  245. } xl_heap_lock;
  246. #define SizeOfHeapLock (offsetof(xl_heap_lock, flags) + sizeof(int8))
  247. /* This is what we need to know about locking an updated version of a row */
  248. typedef struct xl_heap_lock_updated
  249. {
  250. TransactionId xmax;
  251. OffsetNumber offnum;
  252. uint8 infobits_set;
  253. uint8 flags;
  254. } xl_heap_lock_updated;
  255. #define SizeOfHeapLockUpdated (offsetof(xl_heap_lock_updated, flags) + sizeof(uint8))
  256. /* This is what we need to know about confirmation of speculative insertion */
  257. typedef struct xl_heap_confirm
  258. {
  259. OffsetNumber offnum; /* confirmed tuple's offset on page */
  260. } xl_heap_confirm;
  261. #define SizeOfHeapConfirm (offsetof(xl_heap_confirm, offnum) + sizeof(OffsetNumber))
  262. /* This is what we need to know about in-place update */
  263. typedef struct xl_heap_inplace
  264. {
  265. OffsetNumber offnum; /* updated tuple's offset on page */
  266. /* TUPLE DATA FOLLOWS AT END OF STRUCT */
  267. } xl_heap_inplace;
  268. #define SizeOfHeapInplace (offsetof(xl_heap_inplace, offnum) + sizeof(OffsetNumber))
  269. /*
  270. * This struct represents a 'freeze plan', which is what we need to know about
  271. * a single tuple being frozen during vacuum.
  272. */
  273. /* 0x01 was XLH_FREEZE_XMIN */
  274. #define XLH_FREEZE_XVAC 0x02
  275. #define XLH_INVALID_XVAC 0x04
  276. typedef struct xl_heap_freeze_tuple
  277. {
  278. TransactionId xmax;
  279. OffsetNumber offset;
  280. uint16 t_infomask2;
  281. uint16 t_infomask;
  282. uint8 frzflags;
  283. } xl_heap_freeze_tuple;
  284. /*
  285. * This is what we need to know about a block being frozen during vacuum
  286. *
  287. * Backup block 0's data contains an array of xl_heap_freeze_tuple structs,
  288. * one for each tuple.
  289. */
  290. typedef struct xl_heap_freeze_page
  291. {
  292. TransactionId cutoff_xid;
  293. uint16 ntuples;
  294. } xl_heap_freeze_page;
  295. #define SizeOfHeapFreezePage (offsetof(xl_heap_freeze_page, ntuples) + sizeof(uint16))
  296. /*
  297. * This is what we need to know about setting a visibility map bit
  298. *
  299. * Backup blk 0: visibility map buffer
  300. * Backup blk 1: heap buffer
  301. */
  302. typedef struct xl_heap_visible
  303. {
  304. TransactionId cutoff_xid;
  305. uint8 flags;
  306. } xl_heap_visible;
  307. #define SizeOfHeapVisible (offsetof(xl_heap_visible, flags) + sizeof(uint8))
  308. typedef struct xl_heap_new_cid
  309. {
  310. /*
  311. * store toplevel xid so we don't have to merge cids from different
  312. * transactions
  313. */
  314. TransactionId top_xid;
  315. CommandId cmin;
  316. CommandId cmax;
  317. CommandId combocid; /* just for debugging */
  318. /*
  319. * Store the relfilenode/ctid pair to facilitate lookups.
  320. */
  321. RelFileNode target_node;
  322. ItemPointerData target_tid;
  323. } xl_heap_new_cid;
  324. #define SizeOfHeapNewCid (offsetof(xl_heap_new_cid, target_tid) + sizeof(ItemPointerData))
  325. /* logical rewrite xlog record header */
  326. typedef struct xl_heap_rewrite_mapping
  327. {
  328. TransactionId mapped_xid; /* xid that might need to see the row */
  329. Oid mapped_db; /* DbOid or InvalidOid for shared rels */
  330. Oid mapped_rel; /* Oid of the mapped relation */
  331. off_t offset; /* How far have we written so far */
  332. uint32 num_mappings; /* Number of in-memory mappings */
  333. XLogRecPtr start_lsn; /* Insert LSN at begin of rewrite */
  334. } xl_heap_rewrite_mapping;
  335. extern void HeapTupleHeaderAdvanceLatestRemovedXid(HeapTupleHeader tuple,
  336. TransactionId *latestRemovedXid);
  337. extern void heap_redo(XLogReaderState *record);
  338. extern void heap_desc(StringInfo buf, XLogReaderState *record);
  339. extern const char *heap_identify(uint8 info);
  340. extern void heap_mask(char *pagedata, BlockNumber blkno);
  341. extern void heap2_redo(XLogReaderState *record);
  342. extern void heap2_desc(StringInfo buf, XLogReaderState *record);
  343. extern const char *heap2_identify(uint8 info);
  344. extern void heap_xlog_logical_rewrite(XLogReaderState *r);
  345. extern XLogRecPtr log_heap_cleanup_info(RelFileNode rnode,
  346. TransactionId latestRemovedXid);
  347. extern XLogRecPtr log_heap_clean(Relation reln, Buffer buffer,
  348. OffsetNumber *redirected, int nredirected,
  349. OffsetNumber *nowdead, int ndead,
  350. OffsetNumber *nowunused, int nunused,
  351. TransactionId latestRemovedXid);
  352. extern XLogRecPtr log_heap_freeze(Relation reln, Buffer buffer,
  353. TransactionId cutoff_xid, xl_heap_freeze_tuple *tuples,
  354. int ntuples);
  355. extern bool heap_prepare_freeze_tuple(HeapTupleHeader tuple,
  356. TransactionId relfrozenxid,
  357. TransactionId relminmxid,
  358. TransactionId cutoff_xid,
  359. TransactionId cutoff_multi,
  360. xl_heap_freeze_tuple *frz,
  361. bool *totally_frozen);
  362. extern void heap_execute_freeze_tuple(HeapTupleHeader tuple,
  363. xl_heap_freeze_tuple *xlrec_tp);
  364. extern XLogRecPtr log_heap_visible(RelFileNode rnode, Buffer heap_buffer,
  365. Buffer vm_buffer, TransactionId cutoff_xid, uint8 flags);
  366. #endif /* HEAPAM_XLOG_H */
上海开阖软件有限公司 沪ICP备12045867号-1