]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix btmarkpos/btrestrpos array key wraparound bug.
authorPeter Geoghegan <pg@bowt.ie>
Thu, 28 Sep 2023 23:29:32 +0000 (16:29 -0700)
committerPeter Geoghegan <pg@bowt.ie>
Thu, 28 Sep 2023 23:29:32 +0000 (16:29 -0700)
commitcac37c1a1b7c64e60832b764e3828a9aca95bc48
tree2a15e0c4b0626dc2b3b4d80022260d2278e3e6d7
parent59371f1aeb9a6a3d296c0278dba4e4a8b9a53f65
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 70bc5833, 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 9e8da0f7, 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).
src/backend/access/nbtree/nbtree.c
src/backend/access/nbtree/nbtutils.c
src/include/access/nbtree.h