🌐 AI搜索 & 代理 主页
Skip to content

Commit e457312

Browse files
Always commute strategy when preprocessing DESC keys.
A recently added nbtree preprocessing step failed to account for the fact that DESC columns already had their B-Tree strategy number commuted at this point in preprocessing. As a result, preprocessing could output a set of scan keys where one or more keys had the correct strategy number, but used the wrong comparison routine. To fix, make the faulty code path that looks up a more restrictive replacement operator/comparison routine commute its requested inequality strategy (while outputting the transformed strategy number as before). This makes the final transformed scan key comport with the approach preprocessing has always used to deal with DESC columns (which is described by comments above _bt_fix_scankey_strategy). Oversight in commit commit b3f1a13, which made nbtree preprocessing perform transformations on skip array inequalities that can reduce the total number of index searches. Author: Peter Geoghegan <pg@bowt.ie> Reported-By: Natalya Aksman <natalya@timescale.com> Discussion: https://postgr.es/m/19049-b7df801e71de41b2@postgresql.org Backpatch-through: 18
1 parent 3c0018a commit e457312

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/backend/access/nbtree/nbtpreprocesskeys.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,7 @@ _bt_skiparray_strat_decrement(IndexScanDesc scan, ScanKey arraysk,
14101410
Datum orig_sk_argument = high_compare->sk_argument,
14111411
new_sk_argument;
14121412
bool uflow;
1413+
int16 lookupstrat;
14131414

14141415
Assert(high_compare->sk_strategy == BTLessStrategyNumber);
14151416

@@ -1431,9 +1432,14 @@ _bt_skiparray_strat_decrement(IndexScanDesc scan, ScanKey arraysk,
14311432
return;
14321433
}
14331434

1434-
/* Look up <= operator (might fail) */
1435-
leop = get_opfamily_member(opfamily, opcintype, opcintype,
1436-
BTLessEqualStrategyNumber);
1435+
/*
1436+
* Look up <= operator (might fail), accounting for the fact that a
1437+
* high_compare on a DESC column already had its strategy commuted
1438+
*/
1439+
lookupstrat = BTLessEqualStrategyNumber;
1440+
if (high_compare->sk_flags & SK_BT_DESC)
1441+
lookupstrat = BTGreaterEqualStrategyNumber; /* commute this too */
1442+
leop = get_opfamily_member(opfamily, opcintype, opcintype, lookupstrat);
14371443
if (!OidIsValid(leop))
14381444
return;
14391445
cmp_proc = get_opcode(leop);
@@ -1462,6 +1468,7 @@ _bt_skiparray_strat_increment(IndexScanDesc scan, ScanKey arraysk,
14621468
Datum orig_sk_argument = low_compare->sk_argument,
14631469
new_sk_argument;
14641470
bool oflow;
1471+
int16 lookupstrat;
14651472

14661473
Assert(low_compare->sk_strategy == BTGreaterStrategyNumber);
14671474

@@ -1483,9 +1490,14 @@ _bt_skiparray_strat_increment(IndexScanDesc scan, ScanKey arraysk,
14831490
return;
14841491
}
14851492

1486-
/* Look up >= operator (might fail) */
1487-
geop = get_opfamily_member(opfamily, opcintype, opcintype,
1488-
BTGreaterEqualStrategyNumber);
1493+
/*
1494+
* Look up >= operator (might fail), accounting for the fact that a
1495+
* low_compare on a DESC column already had its strategy commuted
1496+
*/
1497+
lookupstrat = BTGreaterEqualStrategyNumber;
1498+
if (low_compare->sk_flags & SK_BT_DESC)
1499+
lookupstrat = BTLessEqualStrategyNumber; /* commute this too */
1500+
geop = get_opfamily_member(opfamily, opcintype, opcintype, lookupstrat);
14891501
if (!OidIsValid(geop))
14901502
return;
14911503
cmp_proc = get_opcode(geop);

0 commit comments

Comments
 (0)