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

Commit 7969629

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 7dcea51 commit 7969629

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
@@ -1412,6 +1412,7 @@ _bt_skiparray_strat_decrement(IndexScanDesc scan, ScanKey arraysk,
14121412
Datum orig_sk_argument = high_compare->sk_argument,
14131413
new_sk_argument;
14141414
bool uflow;
1415+
int16 lookupstrat;
14151416

14161417
Assert(high_compare->sk_strategy == BTLessStrategyNumber);
14171418

@@ -1433,9 +1434,14 @@ _bt_skiparray_strat_decrement(IndexScanDesc scan, ScanKey arraysk,
14331434
return;
14341435
}
14351436

1436-
/* Look up <= operator (might fail) */
1437-
leop = get_opfamily_member(opfamily, opcintype, opcintype,
1438-
BTLessEqualStrategyNumber);
1437+
/*
1438+
* Look up <= operator (might fail), accounting for the fact that a
1439+
* high_compare on a DESC column already had its strategy commuted
1440+
*/
1441+
lookupstrat = BTLessEqualStrategyNumber;
1442+
if (high_compare->sk_flags & SK_BT_DESC)
1443+
lookupstrat = BTGreaterEqualStrategyNumber; /* commute this too */
1444+
leop = get_opfamily_member(opfamily, opcintype, opcintype, lookupstrat);
14391445
if (!OidIsValid(leop))
14401446
return;
14411447
cmp_proc = get_opcode(leop);
@@ -1464,6 +1470,7 @@ _bt_skiparray_strat_increment(IndexScanDesc scan, ScanKey arraysk,
14641470
Datum orig_sk_argument = low_compare->sk_argument,
14651471
new_sk_argument;
14661472
bool oflow;
1473+
int16 lookupstrat;
14671474

14681475
Assert(low_compare->sk_strategy == BTGreaterStrategyNumber);
14691476

@@ -1485,9 +1492,14 @@ _bt_skiparray_strat_increment(IndexScanDesc scan, ScanKey arraysk,
14851492
return;
14861493
}
14871494

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

0 commit comments

Comments
 (0)