Skip to content

Commit 7392eed

Browse files
committed
Fix btree mark/restore bug.
Commit 2ed5b87 introduced a bug in mark/restore, in an attempt to optimize repeated restores to the same page. This caused an assertion failure during a merge join which fed directly from an index scan, although the impact would not be limited to that case. Revert the bad chunk of code from that commit. While investigating this bug it was discovered that a particular "paranoia" set of the mark position field would not prevent bad behavior; it would just make it harder to diagnose. Change that into an assertion, which will draw attention to any future problem in that area more directly. Backpatch to 9.5, where the bug was introduced. Bug #14169 reported by Shinta Koyanagi. Preliminary analysis by Tom Lane identified which commit caused the bug.
1 parent 763eec6 commit 7392eed

File tree

2 files changed

+1
-20
lines changed

2 files changed

+1
-20
lines changed

src/backend/access/nbtree/nbtree.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -608,25 +608,6 @@ btrestrpos(IndexScanDesc scan)
608608
*/
609609
so->currPos.itemIndex = so->markItemIndex;
610610
}
611-
else if (so->currPos.currPage == so->markPos.currPage)
612-
{
613-
/*
614-
* so->markItemIndex < 0 but mark and current positions are on the
615-
* same page. This would be an unusual case, where the scan moved to
616-
* a new index page after the mark, restored, and later restored again
617-
* without moving off the marked page. It is not clear that this code
618-
* can currently be reached, but it seems better to make this function
619-
* robust for this case than to Assert() or elog() that it can't
620-
* happen.
621-
*
622-
* We neither want to set so->markItemIndex >= 0 (because that could
623-
* cause a later move to a new page to redo the memcpy() executions)
624-
* nor re-execute the memcpy() functions for a restore within the same
625-
* page. The previous restore to this page already set everything
626-
* except markPos as it should be.
627-
*/
628-
so->currPos.itemIndex = so->markPos.itemIndex;
629-
}
630611
else
631612
{
632613
/*

src/backend/access/nbtree/nbtsearch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
10111011
so->currPos.moreRight = false;
10121012
}
10131013
so->numKilled = 0; /* just paranoia */
1014-
so->markItemIndex = -1; /* ditto */
1014+
Assert(so->markItemIndex == -1);
10151015

10161016
/* position to the precise item on the page */
10171017
offnum = _bt_binsrch(rel, buf, keysCount, scankeys, nextkey);

0 commit comments

Comments
 (0)