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

Commitc177726

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 parent936ab6d commitc177726

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

‎src/backend/access/nbtree/nbtsearch.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,7 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
16281628
* corresponding need for the left-link, since splits always go right.
16291629
*/
16301630
so->currPos.nextPage=opaque->btpo_next;
1631+
so->currPos.dir=dir;
16311632

16321633
/* initialize tuple workspace to empty */
16331634
so->currPos.nextTupleOffset=0;
@@ -2083,16 +2084,24 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
20832084
* In effect, btrestpos leaves advancing the arrays up to the first
20842085
* _bt_readpage call (that takes place after it has restored markPos).
20852086
*/
2086-
Assert(so->markPos.dir==dir);
20872087
if (so->needPrimScan)
20882088
{
2089-
if (ScanDirectionIsForward(dir))
2089+
if (ScanDirectionIsForward(so->currPos.dir))
20902090
so->markPos.moreRight= true;
20912091
else
20922092
so->markPos.moreLeft= true;
20932093
}
20942094
}
20952095

2096+
/*
2097+
* Cancel primitive index scans that were scheduled when the call to
2098+
* _bt_readpage for currPos happened to use the opposite direction to the
2099+
* one that we're stepping in now. (It's okay to leave the scan's array
2100+
* keys as-is, since the next _bt_readpage will advance them.)
2101+
*/
2102+
if (so->currPos.dir!=dir)
2103+
so->needPrimScan= false;
2104+
20962105
if (ScanDirectionIsForward(dir))
20972106
{
20982107
/* Walk right to the next page with data */
@@ -2653,7 +2662,6 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
26532662
staticinlinevoid
26542663
_bt_initialize_more_data(BTScanOpaqueso,ScanDirectiondir)
26552664
{
2656-
so->currPos.dir=dir;
26572665
if (so->needPrimScan)
26582666
{
26592667
Assert(so->numArrayKeys);

‎src/backend/access/nbtree/nbtutils.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,8 +2426,10 @@ _bt_advance_array_keys(IndexScanDesc scan, BTReadPageState *pstate,
24262426
/*
24272427
* End this primitive index scan, but schedule another.
24282428
*
2429-
* Note: If the scan direction happens to change, this scheduled primitive
2430-
* index scan won't go ahead after all.
2429+
* Note: We make a soft assumption that the current scan direction will
2430+
* also be used within _bt_next, when it is asked to step off this page.
2431+
* It is up to _bt_next to cancel this scheduled primitive index scan
2432+
* whenever it steps to a page in the direction opposite currPos.dir.
24312433
*/
24322434
pstate->continuescan= false;/* Tell _bt_readpage we're done... */
24332435
so->needPrimScan= true;/* ...but call _bt_first again */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp