gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

262 lignes
9.7KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * nbtxlog.h
  4. * header file for postgres btree xlog routines
  5. *
  6. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/access/nbtxlog.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef NBTXLOG_H
  14. #define NBTXLOG_H
  15. #include "access/xlogreader.h"
  16. #include "lib/stringinfo.h"
  17. #include "storage/off.h"
  18. /*
  19. * XLOG records for btree operations
  20. *
  21. * XLOG allows to store some information in high 4 bits of log
  22. * record xl_info field
  23. */
  24. #define XLOG_BTREE_INSERT_LEAF 0x00 /* add index tuple without split */
  25. #define XLOG_BTREE_INSERT_UPPER 0x10 /* same, on a non-leaf page */
  26. #define XLOG_BTREE_INSERT_META 0x20 /* same, plus update metapage */
  27. #define XLOG_BTREE_SPLIT_L 0x30 /* add index tuple with split */
  28. #define XLOG_BTREE_SPLIT_R 0x40 /* as above, new item on right */
  29. /* 0x50 and 0x60 are unused */
  30. #define XLOG_BTREE_DELETE 0x70 /* delete leaf index tuples for a page */
  31. #define XLOG_BTREE_UNLINK_PAGE 0x80 /* delete a half-dead page */
  32. #define XLOG_BTREE_UNLINK_PAGE_META 0x90 /* same, and update metapage */
  33. #define XLOG_BTREE_NEWROOT 0xA0 /* new root page */
  34. #define XLOG_BTREE_MARK_PAGE_HALFDEAD 0xB0 /* mark a leaf as half-dead */
  35. #define XLOG_BTREE_VACUUM 0xC0 /* delete entries on a page during
  36. * vacuum */
  37. #define XLOG_BTREE_REUSE_PAGE 0xD0 /* old page is about to be reused from
  38. * FSM */
  39. #define XLOG_BTREE_META_CLEANUP 0xE0 /* update cleanup-related data in the
  40. * metapage */
  41. /*
  42. * All that we need to regenerate the meta-data page
  43. */
  44. typedef struct xl_btree_metadata
  45. {
  46. uint32 version;
  47. BlockNumber root;
  48. uint32 level;
  49. BlockNumber fastroot;
  50. uint32 fastlevel;
  51. TransactionId oldest_btpo_xact;
  52. float8 last_cleanup_num_heap_tuples;
  53. } xl_btree_metadata;
  54. /*
  55. * This is what we need to know about simple (without split) insert.
  56. *
  57. * This data record is used for INSERT_LEAF, INSERT_UPPER, INSERT_META.
  58. * Note that INSERT_META implies it's not a leaf page.
  59. *
  60. * Backup Blk 0: original page (data contains the inserted tuple)
  61. * Backup Blk 1: child's left sibling, if INSERT_UPPER or INSERT_META
  62. * Backup Blk 2: xl_btree_metadata, if INSERT_META
  63. */
  64. typedef struct xl_btree_insert
  65. {
  66. OffsetNumber offnum;
  67. } xl_btree_insert;
  68. #define SizeOfBtreeInsert (offsetof(xl_btree_insert, offnum) + sizeof(OffsetNumber))
  69. /*
  70. * On insert with split, we save all the items going into the right sibling
  71. * so that we can restore it completely from the log record. This way takes
  72. * less xlog space than the normal approach, because if we did it standardly,
  73. * XLogInsert would almost always think the right page is new and store its
  74. * whole page image. The left page, however, is handled in the normal
  75. * incremental-update fashion.
  76. *
  77. * Note: XLOG_BTREE_SPLIT_L and XLOG_BTREE_SPLIT_R share this data record.
  78. * There are two variants to indicate whether the inserted tuple went into the
  79. * left or right split page (and thus, whether the new item is stored or not).
  80. * We always log the left page high key because suffix truncation can generate
  81. * a new leaf high key using user-defined code. This is also necessary on
  82. * internal pages, since the first right item that the left page's high key
  83. * was based on will have been truncated to zero attributes in the right page
  84. * (the original is unavailable from the right page).
  85. *
  86. * Backup Blk 0: original page / new left page
  87. *
  88. * The left page's data portion contains the new item, if it's the _L variant.
  89. * An IndexTuple representing the high key of the left page must follow with
  90. * either variant.
  91. *
  92. * Backup Blk 1: new right page
  93. *
  94. * The right page's data portion contains the right page's tuples in the form
  95. * used by _bt_restore_page. This includes the new item, if it's the _R
  96. * variant. The right page's tuples also include the right page's high key
  97. * with either variant (moved from the left/original page during the split),
  98. * unless the split happened to be of the rightmost page on its level, where
  99. * there is no high key for new right page.
  100. *
  101. * Backup Blk 2: next block (orig page's rightlink), if any
  102. * Backup Blk 3: child's left sibling, if non-leaf split
  103. */
  104. typedef struct xl_btree_split
  105. {
  106. uint32 level; /* tree level of page being split */
  107. OffsetNumber firstright; /* first item moved to right page */
  108. OffsetNumber newitemoff; /* new item's offset (useful for _L variant) */
  109. } xl_btree_split;
  110. #define SizeOfBtreeSplit (offsetof(xl_btree_split, newitemoff) + sizeof(OffsetNumber))
  111. /*
  112. * This is what we need to know about delete of individual leaf index tuples.
  113. * The WAL record can represent deletion of any number of index tuples on a
  114. * single index page when *not* executed by VACUUM.
  115. *
  116. * Backup Blk 0: index page
  117. */
  118. typedef struct xl_btree_delete
  119. {
  120. TransactionId latestRemovedXid;
  121. int nitems;
  122. /* TARGET OFFSET NUMBERS FOLLOW AT THE END */
  123. } xl_btree_delete;
  124. #define SizeOfBtreeDelete (offsetof(xl_btree_delete, nitems) + sizeof(int))
  125. /*
  126. * This is what we need to know about page reuse within btree.
  127. */
  128. typedef struct xl_btree_reuse_page
  129. {
  130. RelFileNode node;
  131. BlockNumber block;
  132. TransactionId latestRemovedXid;
  133. } xl_btree_reuse_page;
  134. #define SizeOfBtreeReusePage (sizeof(xl_btree_reuse_page))
  135. /*
  136. * This is what we need to know about vacuum of individual leaf index tuples.
  137. * The WAL record can represent deletion of any number of index tuples on a
  138. * single index page when executed by VACUUM.
  139. *
  140. * For MVCC scans, lastBlockVacuumed will be set to InvalidBlockNumber.
  141. * For a non-MVCC index scans there is an additional correctness requirement
  142. * for applying these changes during recovery, which is that we must do one
  143. * of these two things for every block in the index:
  144. * * lock the block for cleanup and apply any required changes
  145. * * EnsureBlockUnpinned()
  146. * The purpose of this is to ensure that no index scans started before we
  147. * finish scanning the index are still running by the time we begin to remove
  148. * heap tuples.
  149. *
  150. * Any changes to any one block are registered on just one WAL record. All
  151. * blocks that we need to run EnsureBlockUnpinned() are listed as a block range
  152. * starting from the last block vacuumed through until this one. Individual
  153. * block numbers aren't given.
  154. *
  155. * Note that the *last* WAL record in any vacuum of an index is allowed to
  156. * have a zero length array of offsets. Earlier records must have at least one.
  157. */
  158. typedef struct xl_btree_vacuum
  159. {
  160. BlockNumber lastBlockVacuumed;
  161. /* TARGET OFFSET NUMBERS FOLLOW */
  162. } xl_btree_vacuum;
  163. #define SizeOfBtreeVacuum (offsetof(xl_btree_vacuum, lastBlockVacuumed) + sizeof(BlockNumber))
  164. /*
  165. * This is what we need to know about marking an empty branch for deletion.
  166. * The target identifies the tuple removed from the parent page (note that we
  167. * remove this tuple's downlink and the *following* tuple's key). Note that
  168. * the leaf page is empty, so we don't need to store its content --- it is
  169. * just reinitialized during recovery using the rest of the fields.
  170. *
  171. * Backup Blk 0: leaf block
  172. * Backup Blk 1: top parent
  173. */
  174. typedef struct xl_btree_mark_page_halfdead
  175. {
  176. OffsetNumber poffset; /* deleted tuple id in parent page */
  177. /* information needed to recreate the leaf page: */
  178. BlockNumber leafblk; /* leaf block ultimately being deleted */
  179. BlockNumber leftblk; /* leaf block's left sibling, if any */
  180. BlockNumber rightblk; /* leaf block's right sibling */
  181. BlockNumber topparent; /* topmost internal page in the branch */
  182. } xl_btree_mark_page_halfdead;
  183. #define SizeOfBtreeMarkPageHalfDead (offsetof(xl_btree_mark_page_halfdead, topparent) + sizeof(BlockNumber))
  184. /*
  185. * This is what we need to know about deletion of a btree page. Note we do
  186. * not store any content for the deleted page --- it is just rewritten as empty
  187. * during recovery, apart from resetting the btpo.xact.
  188. *
  189. * Backup Blk 0: target block being deleted
  190. * Backup Blk 1: target block's left sibling, if any
  191. * Backup Blk 2: target block's right sibling
  192. * Backup Blk 3: leaf block (if different from target)
  193. * Backup Blk 4: metapage (if rightsib becomes new fast root)
  194. */
  195. typedef struct xl_btree_unlink_page
  196. {
  197. BlockNumber leftsib; /* target block's left sibling, if any */
  198. BlockNumber rightsib; /* target block's right sibling */
  199. /*
  200. * Information needed to recreate the leaf page, when target is an
  201. * internal page.
  202. */
  203. BlockNumber leafleftsib;
  204. BlockNumber leafrightsib;
  205. BlockNumber topparent; /* next child down in the branch */
  206. TransactionId btpo_xact; /* value of btpo.xact for use in recovery */
  207. /* xl_btree_metadata FOLLOWS IF XLOG_BTREE_UNLINK_PAGE_META */
  208. } xl_btree_unlink_page;
  209. #define SizeOfBtreeUnlinkPage (offsetof(xl_btree_unlink_page, btpo_xact) + sizeof(TransactionId))
  210. /*
  211. * New root log record. There are zero tuples if this is to establish an
  212. * empty root, or two if it is the result of splitting an old root.
  213. *
  214. * Note that although this implies rewriting the metadata page, we don't need
  215. * an xl_btree_metadata record --- the rootblk and level are sufficient.
  216. *
  217. * Backup Blk 0: new root page (2 tuples as payload, if splitting old root)
  218. * Backup Blk 1: left child (if splitting an old root)
  219. * Backup Blk 2: metapage
  220. */
  221. typedef struct xl_btree_newroot
  222. {
  223. BlockNumber rootblk; /* location of new root (redundant with blk 0) */
  224. uint32 level; /* its tree level */
  225. } xl_btree_newroot;
  226. #define SizeOfBtreeNewroot (offsetof(xl_btree_newroot, level) + sizeof(uint32))
  227. /*
  228. * prototypes for functions in nbtxlog.c
  229. */
  230. extern void btree_redo(XLogReaderState *record);
  231. extern void btree_desc(StringInfo buf, XLogReaderState *record);
  232. extern const char *btree_identify(uint8 info);
  233. extern void btree_mask(char *pagedata, BlockNumber blkno);
  234. #endif /* NBXLOG_H */
上海开阖软件有限公司 沪ICP备12045867号-1