Commit 7392eed7 authored by Kevin Grittner's avatar Kevin Grittner

Fix btree mark/restore bug.

Commit 2ed5b87f 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.
parent 763eec6b
...@@ -608,25 +608,6 @@ btrestrpos(IndexScanDesc scan) ...@@ -608,25 +608,6 @@ btrestrpos(IndexScanDesc scan)
*/ */
so->currPos.itemIndex = so->markItemIndex; so->currPos.itemIndex = so->markItemIndex;
} }
else if (so->currPos.currPage == so->markPos.currPage)
{
/*
* so->markItemIndex < 0 but mark and current positions are on the
* same page. This would be an unusual case, where the scan moved to
* a new index page after the mark, restored, and later restored again
* without moving off the marked page. It is not clear that this code
* can currently be reached, but it seems better to make this function
* robust for this case than to Assert() or elog() that it can't
* happen.
*
* We neither want to set so->markItemIndex >= 0 (because that could
* cause a later move to a new page to redo the memcpy() executions)
* nor re-execute the memcpy() functions for a restore within the same
* page. The previous restore to this page already set everything
* except markPos as it should be.
*/
so->currPos.itemIndex = so->markPos.itemIndex;
}
else else
{ {
/* /*
......
...@@ -1011,7 +1011,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir) ...@@ -1011,7 +1011,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
so->currPos.moreRight = false; so->currPos.moreRight = false;
} }
so->numKilled = 0; /* just paranoia */ so->numKilled = 0; /* just paranoia */
so->markItemIndex = -1; /* ditto */ Assert(so->markItemIndex == -1);
/* position to the precise item on the page */ /* position to the precise item on the page */
offnum = _bt_binsrch(rel, buf, keysCount, scankeys, nextkey); offnum = _bt_binsrch(rel, buf, keysCount, scankeys, nextkey);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment