Skip to content

Commit 67b0b2d

Browse files
Reconsider nbtree page deletion assertion.
Commit 624686a added an assertion that verified that _bt_search successfully relocated the leaf page undergoing deletion. Page deletion cannot deal with the case where the descent stack is to the right of the page, so this seemed critical (deletion can only handle the case where the descent stack is to the left of the leaf/target page). However, the assertion went a bit too far. Since only a buffer pin is held on the leaf page throughout the call to _bt_search, nothing guarantees that it can't have split during this small window. And if does actually split, _bt_search may end up "relocating" a page to the right of the original target leaf page. This scenario seems extremely unlikely, but it must still be considered. Remove the assertion, and document how we cope in this scenario.
1 parent c301c2e commit 67b0b2d

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/backend/access/nbtree/nbtpage.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -1599,15 +1599,22 @@ _bt_pagedel(Relation rel, Buffer leafbuf, TransactionId *oldestBtpoXact)
15991599
itup_key->pivotsearch = true;
16001600
stack = _bt_search(rel, itup_key, &sleafbuf, BT_READ, NULL);
16011601
/* won't need a second lock or pin on leafbuf */
1602-
Assert(leafblkno == BufferGetBlockNumber(sleafbuf) ||
1603-
!itup_key->heapkeyspace);
16041602
_bt_relbuf(rel, sleafbuf);
16051603

16061604
/*
16071605
* Re-lock the leaf page, and start over to use our stack
16081606
* within _bt_mark_page_halfdead. We must do it that way
16091607
* because it's possible that leafbuf can no longer be
16101608
* deleted. We need to recheck.
1609+
*
1610+
* Note: We can't simply hold on to the sleafbuf lock instead,
1611+
* because it's barely possible that sleafbuf is not the same
1612+
* page as leafbuf. This happens when leafbuf split after our
1613+
* original lock was dropped, but before _bt_search finished
1614+
* its descent. We rely on the assumption that we'll find
1615+
* leafbuf isn't safe to delete anymore in this scenario.
1616+
* (Page deletion can cope with the stack being to the left of
1617+
* leafbuf, but not to the right of leafbuf.)
16111618
*/
16121619
LockBuffer(leafbuf, BT_WRITE);
16131620
continue;
@@ -1735,9 +1742,8 @@ _bt_mark_page_halfdead(Relation rel, Buffer leafbuf, BTStack stack)
17351742
* Before attempting to lock the parent page, check that the right sibling
17361743
* is not in half-dead state. A half-dead right sibling would have no
17371744
* downlink in the parent, which would be highly confusing later when we
1738-
* delete the downlink that follows the leafbuf page's downlink. It would
1739-
* fail the "right sibling of target page is also the next child in parent
1740-
* page" cross-check below.
1745+
* delete the downlink. It would fail the "right sibling of target page
1746+
* is also the next child in parent page" cross-check below.
17411747
*/
17421748
if (_bt_rightsib_halfdeadflag(rel, leafrightsib))
17431749
{

0 commit comments

Comments
 (0)