Skip to content

Commit 9e85b20

Browse files
Avoid nbtree index scan SAOP scanBehind confusion.
Consistently reset so->scanBehind at the beginning of nbtree array advancement, even during sktrig_required=false calls (calls where array advancement is triggered by an unsatisfied non-required array scan key). Otherwise, it's possible for queries to fail to return all relevant tuples to the scan given a low-order required scan key that was previously deemed "satisfied" by a truncated high key attribute value. This only happened at the point where a later non-required array scan key needed to be "advanced" once on the next leaf page (that is, once the right sibling of the truncated high key page was reached). The underlying issue was that later code within _bt_advance_array_keys assumed that the so->scanBehind flag must have been set using the current page's high key (not the previous page's high key). Any later successful recheck call to _bt_check_compare would therefore spuriously be prevented from making _bt_advance_array_keys return true, based on the faulty belief that the truncated attribute must be from the scan's current tuple (i.e. the non-pivot tuple at the start of the next page). _bt_advance_array_keys would return false for the tuple, ultimately resulting in _bt_checkkeys failing to return a matching tuple. Oversight in commit 5bf748b, which enhanced nbtree ScalarArrayOp execution. Author: Peter Geoghegan <pg@bowt.ie> Discussion: https://postgr.es/m/CAH2-WzkJKncfqyAUTeuB5GgRhT1vhsWO2q11dbZNqKmvjopP_g@mail.gmail.com Backpatch: 17-, where commit 5bf748b first appears.
1 parent 7b8d45d commit 9e85b20

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/backend/access/nbtree/nbtutils.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,12 @@ _bt_advance_array_keys(IndexScanDesc scan, BTReadPageState *pstate,
18001800
all_required_satisfied = true,
18011801
all_satisfied = true;
18021802

1803+
/*
1804+
* Unset so->scanBehind in case it is still set from back when we dealt
1805+
* with the previous page's high key/finaltup
1806+
*/
1807+
so->scanBehind = false;
1808+
18031809
if (sktrig_required)
18041810
{
18051811
/*
@@ -1808,8 +1814,6 @@ _bt_advance_array_keys(IndexScanDesc scan, BTReadPageState *pstate,
18081814
Assert(!_bt_tuple_before_array_skeys(scan, dir, tuple, tupdesc,
18091815
tupnatts, false, 0, NULL));
18101816

1811-
so->scanBehind = false; /* reset */
1812-
18131817
/*
18141818
* Required scan key wasn't satisfied, so required arrays will have to
18151819
* advance. Invalidate page-level state that tracks whether the

0 commit comments

Comments
 (0)