gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

831 lines
34KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * nbtree.h
  4. * header file for postgres btree access method implementation.
  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/nbtree.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef NBTREE_H
  15. #define NBTREE_H
  16. #include "access/amapi.h"
  17. #include "access/itup.h"
  18. #include "access/sdir.h"
  19. #include "access/xlogreader.h"
  20. #include "catalog/pg_index.h"
  21. #include "lib/stringinfo.h"
  22. #include "storage/bufmgr.h"
  23. #include "storage/shm_toc.h"
  24. /* There's room for a 16-bit vacuum cycle ID in BTPageOpaqueData */
  25. typedef uint16 BTCycleId;
  26. /*
  27. * BTPageOpaqueData -- At the end of every page, we store a pointer
  28. * to both siblings in the tree. This is used to do forward/backward
  29. * index scans. The next-page link is also critical for recovery when
  30. * a search has navigated to the wrong page due to concurrent page splits
  31. * or deletions; see src/backend/access/nbtree/README for more info.
  32. *
  33. * In addition, we store the page's btree level (counting upwards from
  34. * zero at a leaf page) as well as some flag bits indicating the page type
  35. * and status. If the page is deleted, we replace the level with the
  36. * next-transaction-ID value indicating when it is safe to reclaim the page.
  37. *
  38. * We also store a "vacuum cycle ID". When a page is split while VACUUM is
  39. * processing the index, a nonzero value associated with the VACUUM run is
  40. * stored into both halves of the split page. (If VACUUM is not running,
  41. * both pages receive zero cycleids.) This allows VACUUM to detect whether
  42. * a page was split since it started, with a small probability of false match
  43. * if the page was last split some exact multiple of MAX_BT_CYCLE_ID VACUUMs
  44. * ago. Also, during a split, the BTP_SPLIT_END flag is cleared in the left
  45. * (original) page, and set in the right page, but only if the next page
  46. * to its right has a different cycleid.
  47. *
  48. * NOTE: the BTP_LEAF flag bit is redundant since level==0 could be tested
  49. * instead.
  50. */
  51. typedef struct BTPageOpaqueData
  52. {
  53. BlockNumber btpo_prev; /* left sibling, or P_NONE if leftmost */
  54. BlockNumber btpo_next; /* right sibling, or P_NONE if rightmost */
  55. union
  56. {
  57. uint32 level; /* tree level --- zero for leaf pages */
  58. TransactionId xact; /* next transaction ID, if deleted */
  59. } btpo;
  60. uint16 btpo_flags; /* flag bits, see below */
  61. BTCycleId btpo_cycleid; /* vacuum cycle ID of latest split */
  62. } BTPageOpaqueData;
  63. typedef BTPageOpaqueData *BTPageOpaque;
  64. /* Bits defined in btpo_flags */
  65. #define BTP_LEAF (1 << 0) /* leaf page, i.e. not internal page */
  66. #define BTP_ROOT (1 << 1) /* root page (has no parent) */
  67. #define BTP_DELETED (1 << 2) /* page has been deleted from tree */
  68. #define BTP_META (1 << 3) /* meta-page */
  69. #define BTP_HALF_DEAD (1 << 4) /* empty, but still in tree */
  70. #define BTP_SPLIT_END (1 << 5) /* rightmost page of split group */
  71. #define BTP_HAS_GARBAGE (1 << 6) /* page has LP_DEAD tuples */
  72. #define BTP_INCOMPLETE_SPLIT (1 << 7) /* right sibling's downlink is missing */
  73. /*
  74. * The max allowed value of a cycle ID is a bit less than 64K. This is
  75. * for convenience of pg_filedump and similar utilities: we want to use
  76. * the last 2 bytes of special space as an index type indicator, and
  77. * restricting cycle ID lets btree use that space for vacuum cycle IDs
  78. * while still allowing index type to be identified.
  79. */
  80. #define MAX_BT_CYCLE_ID 0xFF7F
  81. /*
  82. * The Meta page is always the first page in the btree index.
  83. * Its primary purpose is to point to the location of the btree root page.
  84. * We also point to the "fast" root, which is the current effective root;
  85. * see README for discussion.
  86. */
  87. typedef struct BTMetaPageData
  88. {
  89. uint32 btm_magic; /* should contain BTREE_MAGIC */
  90. uint32 btm_version; /* nbtree version (always <= BTREE_VERSION) */
  91. BlockNumber btm_root; /* current root location */
  92. uint32 btm_level; /* tree level of the root page */
  93. BlockNumber btm_fastroot; /* current "fast" root location */
  94. uint32 btm_fastlevel; /* tree level of the "fast" root page */
  95. /* remaining fields only valid when btm_version >= BTREE_NOVAC_VERSION */
  96. TransactionId btm_oldest_btpo_xact; /* oldest btpo_xact among all deleted
  97. * pages */
  98. float8 btm_last_cleanup_num_heap_tuples; /* number of heap tuples
  99. * during last cleanup */
  100. } BTMetaPageData;
  101. #define BTPageGetMeta(p) \
  102. ((BTMetaPageData *) PageGetContents(p))
  103. /*
  104. * The current Btree version is 4. That's what you'll get when you create
  105. * a new index.
  106. *
  107. * Btree version 3 was used in PostgreSQL v11. It is mostly the same as
  108. * version 4, but heap TIDs were not part of the keyspace. Index tuples
  109. * with duplicate keys could be stored in any order. We continue to
  110. * support reading and writing Btree versions 2 and 3, so that they don't
  111. * need to be immediately re-indexed at pg_upgrade. In order to get the
  112. * new heapkeyspace semantics, however, a REINDEX is needed.
  113. *
  114. * Btree version 2 is mostly the same as version 3. There are two new
  115. * fields in the metapage that were introduced in version 3. A version 2
  116. * metapage will be automatically upgraded to version 3 on the first
  117. * insert to it. INCLUDE indexes cannot use version 2.
  118. */
  119. #define BTREE_METAPAGE 0 /* first page is meta */
  120. #define BTREE_MAGIC 0x053162 /* magic number in metapage */
  121. #define BTREE_VERSION 4 /* current version number */
  122. #define BTREE_MIN_VERSION 2 /* minimal supported version number */
  123. #define BTREE_NOVAC_VERSION 3 /* minimal version with all meta fields */
  124. /*
  125. * Maximum size of a btree index entry, including its tuple header.
  126. *
  127. * We actually need to be able to fit three items on every page,
  128. * so restrict any one item to 1/3 the per-page available space.
  129. *
  130. * There are rare cases where _bt_truncate() will need to enlarge
  131. * a heap index tuple to make space for a tiebreaker heap TID
  132. * attribute, which we account for here.
  133. */
  134. #define BTMaxItemSize(page) \
  135. MAXALIGN_DOWN((PageGetPageSize(page) - \
  136. MAXALIGN(SizeOfPageHeaderData + \
  137. 3*sizeof(ItemIdData) + \
  138. 3*sizeof(ItemPointerData)) - \
  139. MAXALIGN(sizeof(BTPageOpaqueData))) / 3)
  140. #define BTMaxItemSizeNoHeapTid(page) \
  141. MAXALIGN_DOWN((PageGetPageSize(page) - \
  142. MAXALIGN(SizeOfPageHeaderData + 3*sizeof(ItemIdData)) - \
  143. MAXALIGN(sizeof(BTPageOpaqueData))) / 3)
  144. /*
  145. * The leaf-page fillfactor defaults to 90% but is user-adjustable.
  146. * For pages above the leaf level, we use a fixed 70% fillfactor.
  147. * The fillfactor is applied during index build and when splitting
  148. * a rightmost page; when splitting non-rightmost pages we try to
  149. * divide the data equally. When splitting a page that's entirely
  150. * filled with a single value (duplicates), the effective leaf-page
  151. * fillfactor is 96%, regardless of whether the page is a rightmost
  152. * page.
  153. */
  154. #define BTREE_MIN_FILLFACTOR 10
  155. #define BTREE_DEFAULT_FILLFACTOR 90
  156. #define BTREE_NONLEAF_FILLFACTOR 70
  157. #define BTREE_SINGLEVAL_FILLFACTOR 96
  158. /*
  159. * In general, the btree code tries to localize its knowledge about
  160. * page layout to a couple of routines. However, we need a special
  161. * value to indicate "no page number" in those places where we expect
  162. * page numbers. We can use zero for this because we never need to
  163. * make a pointer to the metadata page.
  164. */
  165. #define P_NONE 0
  166. /*
  167. * Macros to test whether a page is leftmost or rightmost on its tree level,
  168. * as well as other state info kept in the opaque data.
  169. */
  170. #define P_LEFTMOST(opaque) ((opaque)->btpo_prev == P_NONE)
  171. #define P_RIGHTMOST(opaque) ((opaque)->btpo_next == P_NONE)
  172. #define P_ISLEAF(opaque) (((opaque)->btpo_flags & BTP_LEAF) != 0)
  173. #define P_ISROOT(opaque) (((opaque)->btpo_flags & BTP_ROOT) != 0)
  174. #define P_ISDELETED(opaque) (((opaque)->btpo_flags & BTP_DELETED) != 0)
  175. #define P_ISMETA(opaque) (((opaque)->btpo_flags & BTP_META) != 0)
  176. #define P_ISHALFDEAD(opaque) (((opaque)->btpo_flags & BTP_HALF_DEAD) != 0)
  177. #define P_IGNORE(opaque) (((opaque)->btpo_flags & (BTP_DELETED|BTP_HALF_DEAD)) != 0)
  178. #define P_HAS_GARBAGE(opaque) (((opaque)->btpo_flags & BTP_HAS_GARBAGE) != 0)
  179. #define P_INCOMPLETE_SPLIT(opaque) (((opaque)->btpo_flags & BTP_INCOMPLETE_SPLIT) != 0)
  180. /*
  181. * Lehman and Yao's algorithm requires a ``high key'' on every non-rightmost
  182. * page. The high key is not a tuple that is used to visit the heap. It is
  183. * a pivot tuple (see "Notes on B-Tree tuple format" below for definition).
  184. * The high key on a page is required to be greater than or equal to any
  185. * other key that appears on the page. If we find ourselves trying to
  186. * insert a key that is strictly > high key, we know we need to move right
  187. * (this should only happen if the page was split since we examined the
  188. * parent page).
  189. *
  190. * Our insertion algorithm guarantees that we can use the initial least key
  191. * on our right sibling as the high key. Once a page is created, its high
  192. * key changes only if the page is split.
  193. *
  194. * On a non-rightmost page, the high key lives in item 1 and data items
  195. * start in item 2. Rightmost pages have no high key, so we store data
  196. * items beginning in item 1.
  197. */
  198. #define P_HIKEY ((OffsetNumber) 1)
  199. #define P_FIRSTKEY ((OffsetNumber) 2)
  200. #define P_FIRSTDATAKEY(opaque) (P_RIGHTMOST(opaque) ? P_HIKEY : P_FIRSTKEY)
  201. /*
  202. * Notes on B-Tree tuple format, and key and non-key attributes:
  203. *
  204. * INCLUDE B-Tree indexes have non-key attributes. These are extra
  205. * attributes that may be returned by index-only scans, but do not influence
  206. * the order of items in the index (formally, non-key attributes are not
  207. * considered to be part of the key space). Non-key attributes are only
  208. * present in leaf index tuples whose item pointers actually point to heap
  209. * tuples (non-pivot tuples). _bt_check_natts() enforces the rules
  210. * described here.
  211. *
  212. * Non-pivot tuple format:
  213. *
  214. * t_tid | t_info | key values | INCLUDE columns, if any
  215. *
  216. * t_tid points to the heap TID, which is a tiebreaker key column as of
  217. * BTREE_VERSION 4. Currently, the INDEX_ALT_TID_MASK status bit is never
  218. * set for non-pivot tuples.
  219. *
  220. * All other types of index tuples ("pivot" tuples) only have key columns,
  221. * since pivot tuples only exist to represent how the key space is
  222. * separated. In general, any B-Tree index that has more than one level
  223. * (i.e. any index that does not just consist of a metapage and a single
  224. * leaf root page) must have some number of pivot tuples, since pivot
  225. * tuples are used for traversing the tree. Suffix truncation can omit
  226. * trailing key columns when a new pivot is formed, which makes minus
  227. * infinity their logical value. Since BTREE_VERSION 4 indexes treat heap
  228. * TID as a trailing key column that ensures that all index tuples are
  229. * physically unique, it is necessary to represent heap TID as a trailing
  230. * key column in pivot tuples, though very often this can be truncated
  231. * away, just like any other key column. (Actually, the heap TID is
  232. * omitted rather than truncated, since its representation is different to
  233. * the non-pivot representation.)
  234. *
  235. * Pivot tuple format:
  236. *
  237. * t_tid | t_info | key values | [heap TID]
  238. *
  239. * We store the number of columns present inside pivot tuples by abusing
  240. * their t_tid offset field, since pivot tuples never need to store a real
  241. * offset (downlinks only need to store a block number in t_tid). The
  242. * offset field only stores the number of columns/attributes when the
  243. * INDEX_ALT_TID_MASK bit is set, which doesn't count the trailing heap
  244. * TID column sometimes stored in pivot tuples -- that's represented by
  245. * the presence of BT_HEAP_TID_ATTR. The INDEX_ALT_TID_MASK bit in t_info
  246. * is always set on BTREE_VERSION 4. BT_HEAP_TID_ATTR can only be set on
  247. * BTREE_VERSION 4.
  248. *
  249. * In version 3 indexes, the INDEX_ALT_TID_MASK flag might not be set in
  250. * pivot tuples. In that case, the number of key columns is implicitly
  251. * the same as the number of key columns in the index. It is not usually
  252. * set on version 2 indexes, which predate the introduction of INCLUDE
  253. * indexes. (Only explicitly truncated pivot tuples explicitly represent
  254. * the number of key columns on versions 2 and 3, whereas all pivot tuples
  255. * are formed using truncation on version 4. A version 2 index will have
  256. * it set for an internal page negative infinity item iff internal page
  257. * split occurred after upgrade to Postgres 11+.)
  258. *
  259. * The 12 least significant offset bits from t_tid are used to represent
  260. * the number of columns in INDEX_ALT_TID_MASK tuples, leaving 4 status
  261. * bits (BT_RESERVED_OFFSET_MASK bits), 3 of which that are reserved for
  262. * future use. BT_N_KEYS_OFFSET_MASK should be large enough to store any
  263. * number of columns/attributes <= INDEX_MAX_KEYS.
  264. *
  265. * Note well: The macros that deal with the number of attributes in tuples
  266. * assume that a tuple with INDEX_ALT_TID_MASK set must be a pivot tuple,
  267. * and that a tuple without INDEX_ALT_TID_MASK set must be a non-pivot
  268. * tuple (or must have the same number of attributes as the index has
  269. * generally in the case of !heapkeyspace indexes). They will need to be
  270. * updated if non-pivot tuples ever get taught to use INDEX_ALT_TID_MASK
  271. * for something else.
  272. */
  273. #define INDEX_ALT_TID_MASK INDEX_AM_RESERVED_BIT
  274. /* Item pointer offset bits */
  275. #define BT_RESERVED_OFFSET_MASK 0xF000
  276. #define BT_N_KEYS_OFFSET_MASK 0x0FFF
  277. #define BT_HEAP_TID_ATTR 0x1000
  278. /* Get/set downlink block number */
  279. #define BTreeInnerTupleGetDownLink(itup) \
  280. ItemPointerGetBlockNumberNoCheck(&((itup)->t_tid))
  281. #define BTreeInnerTupleSetDownLink(itup, blkno) \
  282. ItemPointerSetBlockNumber(&((itup)->t_tid), (blkno))
  283. /*
  284. * Get/set leaf page highkey's link. During the second phase of deletion, the
  285. * target leaf page's high key may point to an ancestor page (at all other
  286. * times, the leaf level high key's link is not used). See the nbtree README
  287. * for full details.
  288. */
  289. #define BTreeTupleGetTopParent(itup) \
  290. ItemPointerGetBlockNumberNoCheck(&((itup)->t_tid))
  291. #define BTreeTupleSetTopParent(itup, blkno) \
  292. do { \
  293. ItemPointerSetBlockNumber(&((itup)->t_tid), (blkno)); \
  294. BTreeTupleSetNAtts((itup), 0); \
  295. } while(0)
  296. /*
  297. * Get/set number of attributes within B-tree index tuple.
  298. *
  299. * Note that this does not include an implicit tiebreaker heap TID
  300. * attribute, if any. Note also that the number of key attributes must be
  301. * explicitly represented in all heapkeyspace pivot tuples.
  302. */
  303. #define BTreeTupleGetNAtts(itup, rel) \
  304. ( \
  305. (itup)->t_info & INDEX_ALT_TID_MASK ? \
  306. ( \
  307. ItemPointerGetOffsetNumberNoCheck(&(itup)->t_tid) & BT_N_KEYS_OFFSET_MASK \
  308. ) \
  309. : \
  310. IndexRelationGetNumberOfAttributes(rel) \
  311. )
  312. #define BTreeTupleSetNAtts(itup, n) \
  313. do { \
  314. (itup)->t_info |= INDEX_ALT_TID_MASK; \
  315. ItemPointerSetOffsetNumber(&(itup)->t_tid, (n) & BT_N_KEYS_OFFSET_MASK); \
  316. } while(0)
  317. /*
  318. * Get tiebreaker heap TID attribute, if any. Macro works with both pivot
  319. * and non-pivot tuples, despite differences in how heap TID is represented.
  320. */
  321. #define BTreeTupleGetHeapTID(itup) \
  322. ( \
  323. (itup)->t_info & INDEX_ALT_TID_MASK && \
  324. (ItemPointerGetOffsetNumberNoCheck(&(itup)->t_tid) & BT_HEAP_TID_ATTR) != 0 ? \
  325. ( \
  326. (ItemPointer) (((char *) (itup) + IndexTupleSize(itup)) - \
  327. sizeof(ItemPointerData)) \
  328. ) \
  329. : (itup)->t_info & INDEX_ALT_TID_MASK ? NULL : (ItemPointer) &((itup)->t_tid) \
  330. )
  331. /*
  332. * Set the heap TID attribute for a tuple that uses the INDEX_ALT_TID_MASK
  333. * representation (currently limited to pivot tuples)
  334. */
  335. #define BTreeTupleSetAltHeapTID(itup) \
  336. do { \
  337. Assert((itup)->t_info & INDEX_ALT_TID_MASK); \
  338. ItemPointerSetOffsetNumber(&(itup)->t_tid, \
  339. ItemPointerGetOffsetNumberNoCheck(&(itup)->t_tid) | BT_HEAP_TID_ATTR); \
  340. } while(0)
  341. /*
  342. * Operator strategy numbers for B-tree have been moved to access/stratnum.h,
  343. * because many places need to use them in ScanKeyInit() calls.
  344. *
  345. * The strategy numbers are chosen so that we can commute them by
  346. * subtraction, thus:
  347. */
  348. #define BTCommuteStrategyNumber(strat) (BTMaxStrategyNumber + 1 - (strat))
  349. /*
  350. * When a new operator class is declared, we require that the user
  351. * supply us with an amproc procedure (BTORDER_PROC) for determining
  352. * whether, for two keys a and b, a < b, a = b, or a > b. This routine
  353. * must return < 0, 0, > 0, respectively, in these three cases.
  354. *
  355. * To facilitate accelerated sorting, an operator class may choose to
  356. * offer a second procedure (BTSORTSUPPORT_PROC). For full details, see
  357. * src/include/utils/sortsupport.h.
  358. *
  359. * To support window frames defined by "RANGE offset PRECEDING/FOLLOWING",
  360. * an operator class may choose to offer a third amproc procedure
  361. * (BTINRANGE_PROC), independently of whether it offers sortsupport.
  362. * For full details, see doc/src/sgml/btree.sgml.
  363. */
  364. #define BTORDER_PROC 1
  365. #define BTSORTSUPPORT_PROC 2
  366. #define BTINRANGE_PROC 3
  367. #define BTNProcs 3
  368. /*
  369. * We need to be able to tell the difference between read and write
  370. * requests for pages, in order to do locking correctly.
  371. */
  372. #define BT_READ BUFFER_LOCK_SHARE
  373. #define BT_WRITE BUFFER_LOCK_EXCLUSIVE
  374. /*
  375. * BTStackData -- As we descend a tree, we push the (location, downlink)
  376. * pairs from internal pages onto a private stack. If we split a
  377. * leaf, we use this stack to walk back up the tree and insert data
  378. * into parent pages (and possibly to split them, too). Lehman and
  379. * Yao's update algorithm guarantees that under no circumstances can
  380. * our private stack give us an irredeemably bad picture up the tree.
  381. * Again, see the paper for details.
  382. */
  383. typedef struct BTStackData
  384. {
  385. BlockNumber bts_blkno;
  386. OffsetNumber bts_offset;
  387. BlockNumber bts_btentry;
  388. struct BTStackData *bts_parent;
  389. } BTStackData;
  390. typedef BTStackData *BTStack;
  391. /*
  392. * BTScanInsertData is the btree-private state needed to find an initial
  393. * position for an indexscan, or to insert new tuples -- an "insertion
  394. * scankey" (not to be confused with a search scankey). It's used to descend
  395. * a B-Tree using _bt_search.
  396. *
  397. * heapkeyspace indicates if we expect all keys in the index to be physically
  398. * unique because heap TID is used as a tiebreaker attribute, and if index may
  399. * have truncated key attributes in pivot tuples. This is actually a property
  400. * of the index relation itself (not an indexscan). heapkeyspace indexes are
  401. * indexes whose version is >= version 4. It's convenient to keep this close
  402. * by, rather than accessing the metapage repeatedly.
  403. *
  404. * anynullkeys indicates if any of the keys had NULL value when scankey was
  405. * built from index tuple (note that already-truncated tuple key attributes
  406. * set NULL as a placeholder key value, which also affects value of
  407. * anynullkeys). This is a convenience for unique index non-pivot tuple
  408. * insertion, which usually temporarily unsets scantid, but shouldn't iff
  409. * anynullkeys is true. Value generally matches non-pivot tuple's HasNulls
  410. * bit, but may not when inserting into an INCLUDE index (tuple header value
  411. * is affected by the NULL-ness of both key and non-key attributes).
  412. *
  413. * When nextkey is false (the usual case), _bt_search and _bt_binsrch will
  414. * locate the first item >= scankey. When nextkey is true, they will locate
  415. * the first item > scan key.
  416. *
  417. * pivotsearch is set to true by callers that want to re-find a leaf page
  418. * using a scankey built from a leaf page's high key. Most callers set this
  419. * to false.
  420. *
  421. * scantid is the heap TID that is used as a final tiebreaker attribute. It
  422. * is set to NULL when index scan doesn't need to find a position for a
  423. * specific physical tuple. Must be set when inserting new tuples into
  424. * heapkeyspace indexes, since every tuple in the tree unambiguously belongs
  425. * in one exact position (it's never set with !heapkeyspace indexes, though).
  426. * Despite the representational difference, nbtree search code considers
  427. * scantid to be just another insertion scankey attribute.
  428. *
  429. * scankeys is an array of scan key entries for attributes that are compared
  430. * before scantid (user-visible attributes). keysz is the size of the array.
  431. * During insertion, there must be a scan key for every attribute, but when
  432. * starting a regular index scan some can be omitted. The array is used as a
  433. * flexible array member, though it's sized in a way that makes it possible to
  434. * use stack allocations. See nbtree/README for full details.
  435. */
  436. typedef struct BTScanInsertData
  437. {
  438. bool heapkeyspace;
  439. bool anynullkeys;
  440. bool nextkey;
  441. bool pivotsearch;
  442. ItemPointer scantid; /* tiebreaker for scankeys */
  443. int keysz; /* Size of scankeys array */
  444. ScanKeyData scankeys[INDEX_MAX_KEYS]; /* Must appear last */
  445. } BTScanInsertData;
  446. typedef BTScanInsertData *BTScanInsert;
  447. /*
  448. * BTInsertStateData is a working area used during insertion.
  449. *
  450. * This is filled in after descending the tree to the first leaf page the new
  451. * tuple might belong on. Tracks the current position while performing
  452. * uniqueness check, before we have determined which exact page to insert
  453. * to.
  454. *
  455. * (This should be private to nbtinsert.c, but it's also used by
  456. * _bt_binsrch_insert)
  457. */
  458. typedef struct BTInsertStateData
  459. {
  460. IndexTuple itup; /* Item we're inserting */
  461. Size itemsz; /* Size of itup -- should be MAXALIGN()'d */
  462. BTScanInsert itup_key; /* Insertion scankey */
  463. /* Buffer containing leaf page we're likely to insert itup on */
  464. Buffer buf;
  465. /*
  466. * Cache of bounds within the current buffer. Only used for insertions
  467. * where _bt_check_unique is called. See _bt_binsrch_insert and
  468. * _bt_findinsertloc for details.
  469. */
  470. bool bounds_valid;
  471. OffsetNumber low;
  472. OffsetNumber stricthigh;
  473. } BTInsertStateData;
  474. typedef BTInsertStateData *BTInsertState;
  475. /*
  476. * BTScanOpaqueData is the btree-private state needed for an indexscan.
  477. * This consists of preprocessed scan keys (see _bt_preprocess_keys() for
  478. * details of the preprocessing), information about the current location
  479. * of the scan, and information about the marked location, if any. (We use
  480. * BTScanPosData to represent the data needed for each of current and marked
  481. * locations.) In addition we can remember some known-killed index entries
  482. * that must be marked before we can move off the current page.
  483. *
  484. * Index scans work a page at a time: we pin and read-lock the page, identify
  485. * all the matching items on the page and save them in BTScanPosData, then
  486. * release the read-lock while returning the items to the caller for
  487. * processing. This approach minimizes lock/unlock traffic. Note that we
  488. * keep the pin on the index page until the caller is done with all the items
  489. * (this is needed for VACUUM synchronization, see nbtree/README). When we
  490. * are ready to step to the next page, if the caller has told us any of the
  491. * items were killed, we re-lock the page to mark them killed, then unlock.
  492. * Finally we drop the pin and step to the next page in the appropriate
  493. * direction.
  494. *
  495. * If we are doing an index-only scan, we save the entire IndexTuple for each
  496. * matched item, otherwise only its heap TID and offset. The IndexTuples go
  497. * into a separate workspace array; each BTScanPosItem stores its tuple's
  498. * offset within that array.
  499. */
  500. typedef struct BTScanPosItem /* what we remember about each match */
  501. {
  502. ItemPointerData heapTid; /* TID of referenced heap item */
  503. OffsetNumber indexOffset; /* index item's location within page */
  504. LocationIndex tupleOffset; /* IndexTuple's offset in workspace, if any */
  505. } BTScanPosItem;
  506. typedef struct BTScanPosData
  507. {
  508. Buffer buf; /* if valid, the buffer is pinned */
  509. XLogRecPtr lsn; /* pos in the WAL stream when page was read */
  510. BlockNumber currPage; /* page referenced by items array */
  511. BlockNumber nextPage; /* page's right link when we scanned it */
  512. /*
  513. * moreLeft and moreRight track whether we think there may be matching
  514. * index entries to the left and right of the current page, respectively.
  515. * We can clear the appropriate one of these flags when _bt_checkkeys()
  516. * returns continuescan = false.
  517. */
  518. bool moreLeft;
  519. bool moreRight;
  520. /*
  521. * If we are doing an index-only scan, nextTupleOffset is the first free
  522. * location in the associated tuple storage workspace.
  523. */
  524. int nextTupleOffset;
  525. /*
  526. * The items array is always ordered in index order (ie, increasing
  527. * indexoffset). When scanning backwards it is convenient to fill the
  528. * array back-to-front, so we start at the last slot and fill downwards.
  529. * Hence we need both a first-valid-entry and a last-valid-entry counter.
  530. * itemIndex is a cursor showing which entry was last returned to caller.
  531. */
  532. int firstItem; /* first valid index in items[] */
  533. int lastItem; /* last valid index in items[] */
  534. int itemIndex; /* current index in items[] */
  535. BTScanPosItem items[MaxIndexTuplesPerPage]; /* MUST BE LAST */
  536. } BTScanPosData;
  537. typedef BTScanPosData *BTScanPos;
  538. #define BTScanPosIsPinned(scanpos) \
  539. ( \
  540. AssertMacro(BlockNumberIsValid((scanpos).currPage) || \
  541. !BufferIsValid((scanpos).buf)), \
  542. BufferIsValid((scanpos).buf) \
  543. )
  544. #define BTScanPosUnpin(scanpos) \
  545. do { \
  546. ReleaseBuffer((scanpos).buf); \
  547. (scanpos).buf = InvalidBuffer; \
  548. } while (0)
  549. #define BTScanPosUnpinIfPinned(scanpos) \
  550. do { \
  551. if (BTScanPosIsPinned(scanpos)) \
  552. BTScanPosUnpin(scanpos); \
  553. } while (0)
  554. #define BTScanPosIsValid(scanpos) \
  555. ( \
  556. AssertMacro(BlockNumberIsValid((scanpos).currPage) || \
  557. !BufferIsValid((scanpos).buf)), \
  558. BlockNumberIsValid((scanpos).currPage) \
  559. )
  560. #define BTScanPosInvalidate(scanpos) \
  561. do { \
  562. (scanpos).currPage = InvalidBlockNumber; \
  563. (scanpos).nextPage = InvalidBlockNumber; \
  564. (scanpos).buf = InvalidBuffer; \
  565. (scanpos).lsn = InvalidXLogRecPtr; \
  566. (scanpos).nextTupleOffset = 0; \
  567. } while (0)
  568. /* We need one of these for each equality-type SK_SEARCHARRAY scan key */
  569. typedef struct BTArrayKeyInfo
  570. {
  571. int scan_key; /* index of associated key in arrayKeyData */
  572. int cur_elem; /* index of current element in elem_values */
  573. int mark_elem; /* index of marked element in elem_values */
  574. int num_elems; /* number of elems in current array value */
  575. Datum *elem_values; /* array of num_elems Datums */
  576. } BTArrayKeyInfo;
  577. typedef struct BTScanOpaqueData
  578. {
  579. /* these fields are set by _bt_preprocess_keys(): */
  580. bool qual_ok; /* false if qual can never be satisfied */
  581. int numberOfKeys; /* number of preprocessed scan keys */
  582. ScanKey keyData; /* array of preprocessed scan keys */
  583. /* workspace for SK_SEARCHARRAY support */
  584. ScanKey arrayKeyData; /* modified copy of scan->keyData */
  585. int numArrayKeys; /* number of equality-type array keys (-1 if
  586. * there are any unsatisfiable array keys) */
  587. int arrayKeyCount; /* count indicating number of array scan keys
  588. * processed */
  589. BTArrayKeyInfo *arrayKeys; /* info about each equality-type array key */
  590. MemoryContext arrayContext; /* scan-lifespan context for array data */
  591. /* info about killed items if any (killedItems is NULL if never used) */
  592. int *killedItems; /* currPos.items indexes of killed items */
  593. int numKilled; /* number of currently stored items */
  594. /*
  595. * If we are doing an index-only scan, these are the tuple storage
  596. * workspaces for the currPos and markPos respectively. Each is of size
  597. * BLCKSZ, so it can hold as much as a full page's worth of tuples.
  598. */
  599. char *currTuples; /* tuple storage for currPos */
  600. char *markTuples; /* tuple storage for markPos */
  601. /*
  602. * If the marked position is on the same page as current position, we
  603. * don't use markPos, but just keep the marked itemIndex in markItemIndex
  604. * (all the rest of currPos is valid for the mark position). Hence, to
  605. * determine if there is a mark, first look at markItemIndex, then at
  606. * markPos.
  607. */
  608. int markItemIndex; /* itemIndex, or -1 if not valid */
  609. /* keep these last in struct for efficiency */
  610. BTScanPosData currPos; /* current position data */
  611. BTScanPosData markPos; /* marked position, if any */
  612. } BTScanOpaqueData;
  613. typedef BTScanOpaqueData *BTScanOpaque;
  614. /*
  615. * We use some private sk_flags bits in preprocessed scan keys. We're allowed
  616. * to use bits 16-31 (see skey.h). The uppermost bits are copied from the
  617. * index's indoption[] array entry for the index attribute.
  618. */
  619. #define SK_BT_REQFWD 0x00010000 /* required to continue forward scan */
  620. #define SK_BT_REQBKWD 0x00020000 /* required to continue backward scan */
  621. #define SK_BT_INDOPTION_SHIFT 24 /* must clear the above bits */
  622. #define SK_BT_DESC (INDOPTION_DESC << SK_BT_INDOPTION_SHIFT)
  623. #define SK_BT_NULLS_FIRST (INDOPTION_NULLS_FIRST << SK_BT_INDOPTION_SHIFT)
  624. /*
  625. * Constant definition for progress reporting. Phase numbers must match
  626. * btbuildphasename.
  627. */
  628. /* PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE is 1 (see progress.h) */
  629. #define PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN 2
  630. #define PROGRESS_BTREE_PHASE_PERFORMSORT_1 3
  631. #define PROGRESS_BTREE_PHASE_PERFORMSORT_2 4
  632. #define PROGRESS_BTREE_PHASE_LEAF_LOAD 5
  633. /*
  634. * external entry points for btree, in nbtree.c
  635. */
  636. extern void btbuildempty(Relation index);
  637. extern bool btinsert(Relation rel, Datum *values, bool *isnull,
  638. ItemPointer ht_ctid, Relation heapRel,
  639. IndexUniqueCheck checkUnique,
  640. struct IndexInfo *indexInfo);
  641. extern IndexScanDesc btbeginscan(Relation rel, int nkeys, int norderbys);
  642. extern Size btestimateparallelscan(void);
  643. extern void btinitparallelscan(void *target);
  644. extern bool btgettuple(IndexScanDesc scan, ScanDirection dir);
  645. extern int64 btgetbitmap(IndexScanDesc scan, TIDBitmap *tbm);
  646. extern void btrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
  647. ScanKey orderbys, int norderbys);
  648. extern void btparallelrescan(IndexScanDesc scan);
  649. extern void btendscan(IndexScanDesc scan);
  650. extern void btmarkpos(IndexScanDesc scan);
  651. extern void btrestrpos(IndexScanDesc scan);
  652. extern IndexBulkDeleteResult *btbulkdelete(IndexVacuumInfo *info,
  653. IndexBulkDeleteResult *stats,
  654. IndexBulkDeleteCallback callback,
  655. void *callback_state);
  656. extern IndexBulkDeleteResult *btvacuumcleanup(IndexVacuumInfo *info,
  657. IndexBulkDeleteResult *stats);
  658. extern bool btcanreturn(Relation index, int attno);
  659. /*
  660. * prototypes for internal functions in nbtree.c
  661. */
  662. extern bool _bt_parallel_seize(IndexScanDesc scan, BlockNumber *pageno);
  663. extern void _bt_parallel_release(IndexScanDesc scan, BlockNumber scan_page);
  664. extern void _bt_parallel_done(IndexScanDesc scan);
  665. extern void _bt_parallel_advance_array_keys(IndexScanDesc scan);
  666. /*
  667. * prototypes for functions in nbtinsert.c
  668. */
  669. extern bool _bt_doinsert(Relation rel, IndexTuple itup,
  670. IndexUniqueCheck checkUnique, Relation heapRel);
  671. extern Buffer _bt_getstackbuf(Relation rel, BTStack stack);
  672. extern void _bt_finish_split(Relation rel, Buffer bbuf, BTStack stack);
  673. /*
  674. * prototypes for functions in nbtsplitloc.c
  675. */
  676. extern OffsetNumber _bt_findsplitloc(Relation rel, Page page,
  677. OffsetNumber newitemoff, Size newitemsz, IndexTuple newitem,
  678. bool *newitemonleft);
  679. /*
  680. * prototypes for functions in nbtpage.c
  681. */
  682. extern void _bt_initmetapage(Page page, BlockNumber rootbknum, uint32 level);
  683. extern void _bt_update_meta_cleanup_info(Relation rel,
  684. TransactionId oldestBtpoXact, float8 numHeapTuples);
  685. extern void _bt_upgrademetapage(Page page);
  686. extern Buffer _bt_getroot(Relation rel, int access);
  687. extern Buffer _bt_gettrueroot(Relation rel);
  688. extern int _bt_getrootheight(Relation rel);
  689. extern bool _bt_heapkeyspace(Relation rel);
  690. extern void _bt_checkpage(Relation rel, Buffer buf);
  691. extern Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access);
  692. extern Buffer _bt_relandgetbuf(Relation rel, Buffer obuf,
  693. BlockNumber blkno, int access);
  694. extern void _bt_relbuf(Relation rel, Buffer buf);
  695. extern void _bt_pageinit(Page page, Size size);
  696. extern bool _bt_page_recyclable(Page page);
  697. extern void _bt_delitems_delete(Relation rel, Buffer buf,
  698. OffsetNumber *itemnos, int nitems, Relation heapRel);
  699. extern void _bt_delitems_vacuum(Relation rel, Buffer buf,
  700. OffsetNumber *itemnos, int nitems,
  701. BlockNumber lastBlockVacuumed);
  702. extern uint32 _bt_pagedel(Relation rel, Buffer leafbuf,
  703. TransactionId *oldestBtpoXact);
  704. /*
  705. * prototypes for functions in nbtsearch.c
  706. */
  707. extern BTStack _bt_search(Relation rel, BTScanInsert key, Buffer *bufP,
  708. int access, Snapshot snapshot);
  709. extern Buffer _bt_moveright(Relation rel, BTScanInsert key, Buffer buf,
  710. bool forupdate, BTStack stack, int access, Snapshot snapshot);
  711. extern OffsetNumber _bt_binsrch_insert(Relation rel, BTInsertState insertstate);
  712. extern int32 _bt_compare(Relation rel, BTScanInsert key, Page page, OffsetNumber offnum);
  713. extern bool _bt_first(IndexScanDesc scan, ScanDirection dir);
  714. extern bool _bt_next(IndexScanDesc scan, ScanDirection dir);
  715. extern Buffer _bt_get_endpoint(Relation rel, uint32 level, bool rightmost,
  716. Snapshot snapshot);
  717. /*
  718. * prototypes for functions in nbtutils.c
  719. */
  720. extern BTScanInsert _bt_mkscankey(Relation rel, IndexTuple itup);
  721. extern void _bt_freestack(BTStack stack);
  722. extern void _bt_preprocess_array_keys(IndexScanDesc scan);
  723. extern void _bt_start_array_keys(IndexScanDesc scan, ScanDirection dir);
  724. extern bool _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir);
  725. extern void _bt_mark_array_keys(IndexScanDesc scan);
  726. extern void _bt_restore_array_keys(IndexScanDesc scan);
  727. extern void _bt_preprocess_keys(IndexScanDesc scan);
  728. extern bool _bt_checkkeys(IndexScanDesc scan, IndexTuple tuple,
  729. int tupnatts, ScanDirection dir, bool *continuescan);
  730. extern void _bt_killitems(IndexScanDesc scan);
  731. extern BTCycleId _bt_vacuum_cycleid(Relation rel);
  732. extern BTCycleId _bt_start_vacuum(Relation rel);
  733. extern void _bt_end_vacuum(Relation rel);
  734. extern void _bt_end_vacuum_callback(int code, Datum arg);
  735. extern Size BTreeShmemSize(void);
  736. extern void BTreeShmemInit(void);
  737. extern bytea *btoptions(Datum reloptions, bool validate);
  738. extern bool btproperty(Oid index_oid, int attno,
  739. IndexAMProperty prop, const char *propname,
  740. bool *res, bool *isnull);
  741. extern char *btbuildphasename(int64 phasenum);
  742. extern IndexTuple _bt_truncate(Relation rel, IndexTuple lastleft,
  743. IndexTuple firstright, BTScanInsert itup_key);
  744. extern int _bt_keep_natts_fast(Relation rel, IndexTuple lastleft,
  745. IndexTuple firstright);
  746. extern bool _bt_check_natts(Relation rel, bool heapkeyspace, Page page,
  747. OffsetNumber offnum);
  748. extern void _bt_check_third_page(Relation rel, Relation heap,
  749. bool needheaptidspace, Page page, IndexTuple newtup);
  750. /*
  751. * prototypes for functions in nbtvalidate.c
  752. */
  753. extern bool btvalidate(Oid opclassoid);
  754. /*
  755. * prototypes for functions in nbtsort.c
  756. */
  757. extern IndexBuildResult *btbuild(Relation heap, Relation index,
  758. struct IndexInfo *indexInfo);
  759. extern void _bt_parallel_build_main(dsm_segment *seg, shm_toc *toc);
  760. #endif /* NBTREE_H */
上海开阖软件有限公司 沪ICP备12045867号-1