stack = &fakestack;
stack->bts_blkno = BufferGetBlockNumber(pbuf);
stack->bts_offset = InvalidOffsetNumber;
- stack->bts_btentry = InvalidBlockNumber;
stack->bts_parent = NULL;
_bt_relbuf(rel, pbuf);
}
* new downlink will be inserted at the correct offset. Even buf's
* parent may have changed.
*/
- stack->bts_btentry = bknum;
- pbuf = _bt_getstackbuf(rel, stack);
+ pbuf = _bt_getstackbuf(rel, stack, bknum);
/*
* Now we can unlock the right child. The left child will be unlocked
errmsg_internal("failed to re-find parent key in index \"%s\" for split pages %u/%u",
RelationGetRelationName(rel), bknum, rbknum)));
- /* Recursively update the parent */
+ /* Recursively insert into the parent */
_bt_insertonpg(rel, NULL, pbuf, buf, stack->bts_parent,
new_item, stack->bts_offset + 1,
is_only);
}
/*
- * _bt_getstackbuf() -- Walk back up the tree one step, and find the item
- * we last looked at in the parent.
+ * _bt_getstackbuf() -- Walk back up the tree one step, and find the pivot
+ * tuple whose downlink points to child page.
*
- * This is possible because we save the downlink from the parent item,
- * which is enough to uniquely identify it. Insertions into the parent
- * level could cause the item to move right; deletions could cause it
- * to move left, but not left of the page we previously found it in.
+ * Caller passes child's block number, which is used to identify
+ * associated pivot tuple in parent page using a linear search that
+ * matches on pivot's downlink/block number. The expected location of
+ * the pivot tuple is taken from the stack one level above the child
+ * page. This is used as a starting point. Insertions into the
+ * parent level could cause the pivot tuple to move right; deletions
+ * could cause it to move left, but not left of the page we previously
+ * found it on.
*
- * Adjusts bts_blkno & bts_offset if changed.
+ * Caller can use its stack to relocate the pivot tuple/downlink for
+ * any same-level page to the right of the page found by its initial
+ * descent. This is necessary because of the possibility that caller
+ * moved right to recover from a concurrent page split. It's also
+ * convenient for certain callers to be able to step right when there
+ * wasn't a concurrent page split, while still using their original
+ * stack. For example, the checkingunique _bt_doinsert() case may
+ * have to step right when there are many physical duplicates, and its
+ * scantid forces an insertion to the right of the "first page the
+ * value could be on".
*
- * Returns write-locked buffer, or InvalidBuffer if item not found
- * (should not happen).
+ * Returns write-locked parent page buffer, or InvalidBuffer if pivot
+ * tuple not found (should not happen). Adjusts bts_blkno &
+ * bts_offset if changed. Page split caller should insert its new
+ * pivot tuple for its new right sibling page on parent page, at the
+ * offset number bts_offset + 1.
*/
Buffer
-_bt_getstackbuf(Relation rel, BTStack stack)
+_bt_getstackbuf(Relation rel, BTStack stack, BlockNumber child)
{
BlockNumber blkno;
OffsetNumber start;
itemid = PageGetItemId(page, offnum);
item = (IndexTuple) PageGetItem(page, itemid);
- if (BTreeInnerTupleGetDownLink(item) == stack->bts_btentry)
+ if (BTreeInnerTupleGetDownLink(item) == child)
{
/* Return accurate pointer to where link is now */
stack->bts_blkno = blkno;
itemid = PageGetItemId(page, offnum);
item = (IndexTuple) PageGetItem(page, itemid);
- if (BTreeInnerTupleGetDownLink(item) == stack->bts_btentry)
+ if (BTreeInnerTupleGetDownLink(item) == child)
{
/* Return accurate pointer to where link is now */
stack->bts_blkno = blkno;
par_blkno = BufferGetBlockNumber(*bufP);
/*
- * We need to save the location of the index entry we chose in the
- * parent page on a stack. In case we split the tree, we'll use the
- * stack to work back up to the parent page. We also save the actual
- * downlink (block) to uniquely identify the index entry, in case it
- * moves right while we're working lower in the tree. See the paper
- * by Lehman and Yao for how this is detected and handled. (We use the
- * child link during the second half of a page split -- if caller ends
- * up splitting the child it usually ends up inserting a new pivot
- * tuple for child's new right sibling immediately after the original
- * bts_offset offset recorded here. The downlink block will be needed
- * to check if bts_offset remains the position of this same pivot
- * tuple.)
+ * We need to save the location of the pivot tuple we chose in the
+ * parent page on a stack. If we need to split a page, we'll use
+ * the stack to work back up to its parent page. If caller ends up
+ * splitting a page one level down, it usually ends up inserting a
+ * new pivot tuple/downlink immediately after the location recorded
+ * here.
*/
new_stack = (BTStack) palloc(sizeof(BTStackData));
new_stack->bts_blkno = par_blkno;
new_stack->bts_offset = offnum;
- new_stack->bts_btentry = blkno;
new_stack->bts_parent = stack_in;
/*
#define BT_WRITE BUFFER_LOCK_EXCLUSIVE
/*
- * BTStackData -- As we descend a tree, we push the (location, downlink)
- * pairs from internal pages onto a private stack. If we split a
- * leaf, we use this stack to walk back up the tree and insert data
- * into parent pages (and possibly to split them, too). Lehman and
- * Yao's update algorithm guarantees that under no circumstances can
- * our private stack give us an irredeemably bad picture up the tree.
- * Again, see the paper for details.
+ * BTStackData -- As we descend a tree, we push the location of pivot
+ * tuples whose downlink we are about to follow onto a private stack. If
+ * we split a leaf, we use this stack to walk back up the tree and insert
+ * data into its parent page at the correct location. We may also have to
+ * recursively split a grandparent of the leaf page (and so on).
*/
-
typedef struct BTStackData
{
BlockNumber bts_blkno;
OffsetNumber bts_offset;
- BlockNumber bts_btentry;
struct BTStackData *bts_parent;
} BTStackData;
*/
extern bool _bt_doinsert(Relation rel, IndexTuple itup,
IndexUniqueCheck checkUnique, Relation heapRel);
-extern Buffer _bt_getstackbuf(Relation rel, BTStack stack);
+extern Buffer _bt_getstackbuf(Relation rel, BTStack stack, BlockNumber child);
extern void _bt_finish_split(Relation rel, Buffer bbuf, BTStack stack);
/*