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

Commitb36d72c

Browse files
Fix btmarkpos/btrestrpos array key wraparound bug.
nbtree's mark/restore processing failed to correctly handle an edge caseinvolving array key advancement and related search-type scan key state.Scans with ScalarArrayScalarArrayOpExpr quals requiring mark/restoreprocessing (for a merge join) could incorrectly conclude that anaffected array/scan key must not have advanced during the time betweenmarking and restoring the scan's position.As a result of all this, array key handling within btrestrpos could skipa required call to _bt_preprocess_keys(). This confusion allowed laterprimitive index scans to overlook tuples matching the true current arraykeys. The scan's search-type scan keys would still have spurious valuescorresponding to the final array element(s) -- not values matching thefirst/now-current array element(s).To fix, remember that "array key wraparound" has taken place during theongoing btrescan in a flag variable stored in the scan's state, and usethat information at the point where btrestrpos decides if another callto _bt_preprocess_keys is required.Oversight in commit70bc583, which taught nbtree to handle array keysduring mark/restore processing, but missed this subtlety. That commitwas itself a bug fix for an issue in commit9e8da0f, which taughtnbtree to handle ScalarArrayOpExpr quals natively.Author: Peter Geoghegan <pg@bowt.ie>Discussion:https://postgr.es/m/CAH2-WzkgP3DDRJxw6DgjCxo-cu-DKrvjEv_ArkP2ctBJatDCYg@mail.gmail.comBackpatch: 11- (all supported branches).
1 parentd642598 commitb36d72c

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
@@ -366,6 +366,7 @@ btbeginscan(Relation rel, int nkeys, int norderbys)
366366
so->keyData=NULL;
367367

368368
so->arrayKeyData=NULL;/* assume no array keys for now */
369+
so->arraysStarted= false;
369370
so->numArrayKeys=0;
370371
so->arrayKeys=NULL;
371372
so->arrayContext=NULL;

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ _bt_start_array_keys(IndexScanDesc scan, ScanDirection dir)
534534
curArrayKey->cur_elem=0;
535535
skey->sk_argument=curArrayKey->elem_values[curArrayKey->cur_elem];
536536
}
537+
538+
so->arraysStarted= true;
537539
}
538540

539541
/*
@@ -593,6 +595,14 @@ _bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir)
593595
if (scan->parallel_scan!=NULL)
594596
_bt_parallel_advance_array_keys(scan);
595597

598+
/*
599+
* When no new array keys were found, the scan is "past the end" of the
600+
* array keys. _bt_start_array_keys can still "restart" the array keys if
601+
* a rescan is required.
602+
*/
603+
if (!found)
604+
so->arraysStarted= false;
605+
596606
returnfound;
597607
}
598608

@@ -646,8 +656,13 @@ _bt_restore_array_keys(IndexScanDesc scan)
646656
* If we changed any keys, we must redo _bt_preprocess_keys. That might
647657
* sound like overkill, but in cases with multiple keys per index column
648658
* it seems necessary to do the full set of pushups.
659+
*
660+
* Also do this whenever the scan's set of array keys "wrapped around" at
661+
* the end of the last primitive index scan. There won't have been a call
662+
* to _bt_preprocess_keys from some other place following wrap around, so
663+
* we do it for ourselves.
649664
*/
650-
if (changed)
665+
if (changed|| !so->arraysStarted)
651666
{
652667
_bt_preprocess_keys(scan);
653668
/* 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
@@ -628,8 +628,10 @@ typedef struct BTArrayKeyInfo
628628

629629
typedefstructBTScanOpaqueData
630630
{
631-
/*these fields are set by _bt_preprocess_keys(): */
631+
/*all fields (except arraysStarted) are set by _bt_preprocess_keys(): */
632632
boolqual_ok;/* false if qual can never be satisfied */
633+
boolarraysStarted;/* Started array keys, but have yet to "reach
634+
* past the end" of all arrays? */
633635
intnumberOfKeys;/* number of preprocessed scan keys */
634636
ScanKeykeyData;/* array of preprocessed scan keys */
635637

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp