Skip to content

Commit 7c07305

Browse files
Fix btmarkpos/btrestrpos array key wraparound bug.
nbtree's mark/restore processing failed to correctly handle an edge case involving array key advancement and related search-type scan key state. Scans with ScalarArrayScalarArrayOpExpr quals requiring mark/restore processing (for a merge join) could incorrectly conclude that an affected array/scan key must not have advanced during the time between marking and restoring the scan's position. As a result of all this, array key handling within btrestrpos could skip a required call to _bt_preprocess_keys(). This confusion allowed later primitive index scans to overlook tuples matching the true current array keys. The scan's search-type scan keys would still have spurious values corresponding to the final array element(s) -- not values matching the first/now-current array element(s). To fix, remember that "array key wraparound" has taken place during the ongoing btrescan in a flag variable stored in the scan's state, and use that information at the point where btrestrpos decides if another call to _bt_preprocess_keys is required. Oversight in commit 70bc583, which taught nbtree to handle array keys during mark/restore processing, but missed this subtlety. That commit was itself a bug fix for an issue in commit 9e8da0f, which taught nbtree to handle ScalarArrayOpExpr quals natively. Author: Peter Geoghegan <pg@bowt.ie> Discussion: https://postgr.es/m/CAH2-WzkgP3DDRJxw6DgjCxo-cu-DKrvjEv_ArkP2ctBJatDCYg@mail.gmail.com Backpatch: 11- (all supported branches).
1 parent 3caedf2 commit 7c07305

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/backend/access/nbtree/nbtree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ btbeginscan(Relation rel, int nkeys, int norderbys)
364364
so->keyData = NULL;
365365

366366
so->arrayKeyData = NULL; /* assume no array keys for now */
367+
so->arraysStarted = false;
367368
so->numArrayKeys = 0;
368369
so->arrayKeys = NULL;
369370
so->arrayContext = NULL;

src/backend/access/nbtree/nbtutils.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ _bt_start_array_keys(IndexScanDesc scan, ScanDirection dir)
546546
curArrayKey->cur_elem = 0;
547547
skey->sk_argument = curArrayKey->elem_values[curArrayKey->cur_elem];
548548
}
549+
550+
so->arraysStarted = true;
549551
}
550552

551553
/*
@@ -605,6 +607,14 @@ _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir)
605607
if (scan->parallel_scan != NULL)
606608
_bt_parallel_advance_array_keys(scan);
607609

610+
/*
611+
* When no new array keys were found, the scan is "past the end" of the
612+
* array keys. _bt_start_array_keys can still "restart" the array keys if
613+
* a rescan is required.
614+
*/
615+
if (!found)
616+
so->arraysStarted = false;
617+
608618
return found;
609619
}
610620

@@ -658,8 +668,13 @@ _bt_restore_array_keys(IndexScanDesc scan)
658668
* If we changed any keys, we must redo _bt_preprocess_keys. That might
659669
* sound like overkill, but in cases with multiple keys per index column
660670
* it seems necessary to do the full set of pushups.
671+
*
672+
* Also do this whenever the scan's set of array keys "wrapped around" at
673+
* the end of the last primitive index scan. There won't have been a call
674+
* to _bt_preprocess_keys from some other place following wrap around, so
675+
* we do it for ourselves.
661676
*/
662-
if (changed)
677+
if (changed || !so->arraysStarted)
663678
{
664679
_bt_preprocess_keys(scan);
665680
/* The mark should have been set on a consistent set of keys... */

src/include/access/nbtree.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,10 @@ typedef struct BTArrayKeyInfo
435435

436436
typedef struct BTScanOpaqueData
437437
{
438-
/* these fields are set by _bt_preprocess_keys(): */
438+
/* all fields (except arraysStarted) are set by _bt_preprocess_keys(): */
439439
bool qual_ok; /* false if qual can never be satisfied */
440+
bool arraysStarted; /* Started array keys, but have yet to "reach
441+
* past the end" of all arrays? */
440442
int numberOfKeys; /* number of preprocessed scan keys */
441443
ScanKey keyData; /* array of preprocessed scan keys */
442444

0 commit comments

Comments
 (0)