Skip to content

Commit 763d65a

Browse files
Fix bug in nbtree array primitive scan scheduling.
A bug in nbtree's handling of primitive index scan scheduling could lead to wrong answers when a scrollable cursor was used with an index scan that had a SAOP index qual. Wrong answers were only possible when the scan direction changed after a primitive scan was scheduled, but before _bt_next was asked to fetch the next tuple in line (i.e. for things to break, _bt_next had to be denied the opportunity to step off the page in the same direction as the one used when the primscan was scheduled). Furthermore, the issue only occurred when the page in question happened to be the first page to be visited by the entire top-level scan; the issue hinged upon the cursor backing up to the absolute beginning of the key space that it returns tuples from (fetching in the opposite scan direction across a "primitive scan boundary" always worked correctly). To fix, make _bt_next unset the "needs primitive index scan" flag when it detects that the current scan direction is not the one that was used by _bt_readpage back when the primitive scan in question was scheduled. This fixes the cases that are known to be faulty, and also seems like a good idea on general robustness grounds. Affected scrollable cursor cases now avoid a spurious primitive index scan when they fetch backwards to the absolute start of the key space to be visited by their cursor. Fetching backwards now only returns those tuples at the start of the scan, as expected. It'll also be okay to once again fetch forwards from the start at that point, since the scan will be left in a state that's exactly consistent with the state it was in before any tuples were ever fetched, as expected. Oversight in commit 5bf748b, which enhanced nbtree ScalarArrayOp execution. Author: Peter Geoghegan <pg@bowt.ie> Discussion: https://postgr.es/m/CAH2-Wznv49bFsE2jkt4GuZ0tU2C91dEST=50egzjY2FeOcHL4Q@mail.gmail.com Backpatch: 17-, where commit 5bf748b first appears.
1 parent 2d5fe51 commit 763d65a

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/backend/access/nbtree/nbtsearch.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,7 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
15681568

15691569
Assert(!P_IGNORE(opaque));
15701570
Assert(BTScanPosIsPinned(so->currPos));
1571+
Assert(!so->needPrimScan);
15711572

15721573
if (scan->parallel_scan)
15731574
{
@@ -1594,7 +1595,6 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
15941595
maxoff = PageGetMaxOffsetNumber(page);
15951596

15961597
/* initialize page-level state that we'll pass to _bt_checkkeys */
1597-
pstate.dir = dir;
15981598
pstate.minoff = minoff;
15991599
pstate.maxoff = maxoff;
16001600
pstate.finaltup = NULL;
@@ -2088,7 +2088,7 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
20882088
*/
20892089
if (so->needPrimScan)
20902090
{
2091-
if (ScanDirectionIsForward(dir))
2091+
if (ScanDirectionIsForward(so->currPos.dir))
20922092
so->markPos.moreRight = true;
20932093
else
20942094
so->markPos.moreLeft = true;
@@ -2109,6 +2109,15 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
21092109
else
21102110
blkno = so->currPos.prevPage;
21112111
lastcurrblkno = so->currPos.currPage;
2112+
2113+
/*
2114+
* Cancel primitive index scans that were scheduled when the call to
2115+
* _bt_readpage for currPos happened to use the opposite direction to
2116+
* the one that we're stepping in now. (It's okay to leave the scan's
2117+
* array keys as-is, since the next _bt_readpage will advance them.)
2118+
*/
2119+
if (so->currPos.dir != dir)
2120+
so->needPrimScan = false;
21122121
}
21132122
else
21142123
{
@@ -2118,6 +2127,8 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
21182127
*/
21192128
if (!_bt_parallel_seize(scan, &blkno, &lastcurrblkno, false))
21202129
return false;
2130+
2131+
Assert(!so->needPrimScan);
21212132
}
21222133

21232134
return _bt_readnextpage(scan, blkno, lastcurrblkno, dir);

src/backend/access/nbtree/nbtutils.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ _bt_advance_array_keys(IndexScanDesc scan, BTReadPageState *pstate,
18001800
{
18011801
BTScanOpaque so = (BTScanOpaque) scan->opaque;
18021802
Relation rel = scan->indexRelation;
1803-
ScanDirection dir = pstate ? pstate->dir : ForwardScanDirection;
1803+
ScanDirection dir = so->currPos.dir;
18041804
int arrayidx = 0;
18051805
bool beyond_end_advance = false,
18061806
has_required_opposite_direction_only = false,
@@ -2400,8 +2400,10 @@ _bt_advance_array_keys(IndexScanDesc scan, BTReadPageState *pstate,
24002400
/*
24012401
* End this primitive index scan, but schedule another.
24022402
*
2403-
* Note: If the scan direction happens to change, this scheduled primitive
2404-
* index scan won't go ahead after all.
2403+
* Note: We make a soft assumption that the current scan direction will
2404+
* also be used within _bt_next, when it is asked to step off this page.
2405+
* It is up to _bt_next to cancel this scheduled primitive index scan
2406+
* whenever it steps to a page in the direction opposite currPos.dir.
24052407
*/
24062408
pstate->continuescan = false; /* Tell _bt_readpage we're done... */
24072409
so->needPrimScan = true; /* ...but call _bt_first again */
@@ -3458,7 +3460,7 @@ _bt_checkkeys(IndexScanDesc scan, BTReadPageState *pstate, bool arrayKeys,
34583460
{
34593461
TupleDesc tupdesc = RelationGetDescr(scan->indexRelation);
34603462
BTScanOpaque so = (BTScanOpaque) scan->opaque;
3461-
ScanDirection dir = pstate->dir;
3463+
ScanDirection dir = so->currPos.dir;
34623464
int ikey = 0;
34633465
bool res;
34643466

@@ -4062,7 +4064,8 @@ static void
40624064
_bt_checkkeys_look_ahead(IndexScanDesc scan, BTReadPageState *pstate,
40634065
int tupnatts, TupleDesc tupdesc)
40644066
{
4065-
ScanDirection dir = pstate->dir;
4067+
BTScanOpaque so = (BTScanOpaque) scan->opaque;
4068+
ScanDirection dir = so->currPos.dir;
40664069
OffsetNumber aheadoffnum;
40674070
IndexTuple ahead;
40684071

src/include/access/nbtree.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,6 @@ typedef BTScanOpaqueData *BTScanOpaque;
10781078
typedef struct BTReadPageState
10791079
{
10801080
/* Input parameters, set by _bt_readpage for _bt_checkkeys */
1081-
ScanDirection dir; /* current scan direction */
10821081
OffsetNumber minoff; /* Lowest non-pivot tuple's offset */
10831082
OffsetNumber maxoff; /* Highest non-pivot tuple's offset */
10841083
IndexTuple finaltup; /* Needed by scans with array keys */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy