Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit763d65a

Browse files
Fix bug in nbtree array primitive scan scheduling.
A bug in nbtree's handling of primitive index scan scheduling could leadto wrong answers when a scrollable cursor was used with an index scanthat had a SAOP index qual. Wrong answers were only possible when thescan 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 tobreak, _bt_next had to be denied the opportunity to step off the page inthe same direction as the one used when the primscan was scheduled).Furthermore, the issue only occurred when the page in question happenedto be the first page to be visited by the entire top-level scan; theissue hinged upon the cursor backing up to the absolute beginning of thekey space that it returns tuples from (fetching in the opposite scandirection across a "primitive scan boundary" always worked correctly).To fix, make _bt_next unset the "needs primitive index scan" flag whenit detects that the current scan direction is not the one that was usedby _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 agood idea on general robustness grounds.Affected scrollable cursor cases now avoid a spurious primitive indexscan when they fetch backwards to the absolute start of the key space tobe visited by their cursor. Fetching backwards now only returns thosetuples at the start of the scan, as expected. It'll also be okay toonce again fetch forwards from the start at that point, since the scanwill be left in a state that's exactly consistent with the state it wasin before any tuples were ever fetched, as expected.Oversight in commit5bf748b, which enhanced nbtree ScalarArrayOpexecution.Author: Peter Geoghegan <pg@bowt.ie>Discussion:https://postgr.es/m/CAH2-Wznv49bFsE2jkt4GuZ0tU2C91dEST=50egzjY2FeOcHL4Q@mail.gmail.comBackpatch: 17-, where commit5bf748b first appears.
1 parent2d5fe51 commit763d65a

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
BTScanOpaqueso= (BTScanOpaque)scan->opaque;
18021802
Relationrel=scan->indexRelation;
1803-
ScanDirectiondir=pstate ?pstate->dir :ForwardScanDirection;
1803+
ScanDirectiondir=so->currPos.dir;
18041804
intarrayidx=0;
18051805
boolbeyond_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
TupleDesctupdesc=RelationGetDescr(scan->indexRelation);
34603462
BTScanOpaqueso= (BTScanOpaque)scan->opaque;
3461-
ScanDirectiondir=pstate->dir;
3463+
ScanDirectiondir=so->currPos.dir;
34623464
intikey=0;
34633465
boolres;
34643466

@@ -4062,7 +4064,8 @@ static void
40624064
_bt_checkkeys_look_ahead(IndexScanDescscan,BTReadPageState*pstate,
40634065
inttupnatts,TupleDesctupdesc)
40644066
{
4065-
ScanDirectiondir=pstate->dir;
4067+
BTScanOpaqueso= (BTScanOpaque)scan->opaque;
4068+
ScanDirectiondir=so->currPos.dir;
40664069
OffsetNumberaheadoffnum;
40674070
IndexTupleahead;
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
typedefstructBTReadPageState
10791079
{
10801080
/* Input parameters, set by _bt_readpage for _bt_checkkeys */
1081-
ScanDirectiondir;/* current scan direction */
10821081
OffsetNumberminoff;/* Lowest non-pivot tuple's offset */
10831082
OffsetNumbermaxoff;/* Highest non-pivot tuple's offset */
10841083
IndexTuplefinaltup;/* Needed by scans with array keys */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp