Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e25c93

Browse files
nbtree: tighten up array recheck rules.
Be more conservative when performing a scheduled recheck of an nbtree scan's array keys once on the next page, having set so->scanBehind: back out of reading the page (perform another primitive scan instead) when the next page's high key/finaltup has an untruncated prefix of matching values and truncated suffix attributes associated with lower-order keys. In other words, stop assuming that the lower-order keys have been satisfied by the truncated suffix attributes in this context (only do so when considering scheduling a recheck within _bt_advance_array_keys). The new behavior is more logical: if the next page read after setting so->scanBehind can only contain tuples that are themselves "behind the scan", that's reason enough to cut our losses. In general, when we set so->scanBehind, we only expect to perform one recheck on the next page to make a final decision about whether or not to continue the current primitive index scan. It seems unprincipled for the recheck to allow a _bt_readpage to continue unless the scan's arrays will advance/unless the page might actually contain relevant tuples. In practice it is highly unlikely that things will line up like this (the untruncated prefix of attribute values from the next page's high key is seldom an exact match for their corresponding array's current element following array advancement on the original/previous page). That gives us all the more reason to keep things simple and consistent. This was arguably an oversight in commit 9a2e2a2, which improved nbtree array primitive scan scheduling. Author: Peter Geoghegan <[email protected]> Discussion: https://postgr.es/m/CAH2-WzkXzJajgyW-pCQ7vaDPhaT3huU+Zw_j448rpCBEsu2YOQ@mail.gmail.com
1 parent acea3fc commit 7e25c93

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/backend/access/nbtree/nbtutils.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -2393,11 +2393,27 @@ _bt_scanbehind_checkkeys(IndexScanDesc scan, ScanDirection dir,
23932393
TupleDesc tupdesc = RelationGetDescr(rel);
23942394
BTScanOpaque so = (BTScanOpaque) scan->opaque;
23952395
int nfinaltupatts = BTreeTupleGetNAtts(finaltup, rel);
2396+
bool scanBehind;
23962397

23972398
Assert(so->numArrayKeys);
23982399

23992400
if (_bt_tuple_before_array_skeys(scan, dir, finaltup, tupdesc,
2400-
nfinaltupatts, false, 0, NULL))
2401+
nfinaltupatts, false, 0, &scanBehind))
2402+
return false;
2403+
2404+
/*
2405+
* If scanBehind was set, all of the untruncated attribute values from
2406+
* finaltup that correspond to an array match the array's current element,
2407+
* but there are other keys associated with truncated suffix attributes.
2408+
* Array advancement must have incremented the scan's arrays on the
2409+
* previous page, resulting in a set of array keys that happen to be an
2410+
* exact match for the current page high key's untruncated prefix values.
2411+
*
2412+
* This page definitely doesn't contain tuples that the scan will need to
2413+
* return. The next page may or may not contain relevant tuples. Handle
2414+
* this by cutting our losses and starting a new primscan.
2415+
*/
2416+
if (scanBehind)
24012417
return false;
24022418

24032419
if (!so->oppositeDirCheck)

0 commit comments

Comments
 (0)