Skip to content

Commit ee7cddf

Browse files
author
Commitfest Bot
committed
[CF 5875] v3 - Replace magic numbers with strategy numbers for B-tree indexes
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5875 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CAN-LCVNBk9Tdt8PdUjPGC1hBLgoxL5EX_NEegJpK5OsnBGKeOQ@mail.gmail.com Author(s): Daniil Davydov
2 parents 5092aae + 2458ca3 commit ee7cddf

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

src/backend/access/nbtree/nbtvalidate.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ btvalidate(Oid opclassoid)
140140
Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
141141

142142
/* Check that only allowed strategy numbers exist */
143-
if (oprform->amopstrategy < 1 ||
144-
oprform->amopstrategy > BTMaxStrategyNumber)
143+
if (!BTStrategyIsValid(oprform->amopstrategy))
145144
{
146145
ereport(INFO,
147146
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),

src/backend/partitioning/partprune.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ gen_prune_steps_from_opexps(GeneratePruningStepsContext *context,
14141414
{
14151415
PartitionScheme part_scheme = context->rel->part_scheme;
14161416
List *opsteps = NIL;
1417-
List *btree_clauses[BTMaxStrategyNumber + 1],
1417+
List *btree_clauses[BTNumOfStrategies],
14181418
*hash_clauses[HTMaxStrategyNumber + 1];
14191419
int i;
14201420
ListCell *lc;
@@ -1509,11 +1509,22 @@ gen_prune_steps_from_opexps(GeneratePruningStepsContext *context,
15091509
case PARTITION_STRATEGY_LIST:
15101510
case PARTITION_STRATEGY_RANGE:
15111511
{
1512-
List *eq_clauses = btree_clauses[BTEqualStrategyNumber];
1513-
List *le_clauses = btree_clauses[BTLessEqualStrategyNumber];
1514-
List *ge_clauses = btree_clauses[BTGreaterEqualStrategyNumber];
1512+
List *eq_clauses = btree_clauses[BTEqualStrategy];
1513+
List *le_clauses = btree_clauses[BTLessEqualStrategy];
1514+
List *ge_clauses = btree_clauses[BTGreaterEqualStrategy];
15151515
int strat;
1516+
/*
1517+
typedef enum
1518+
{
1519+
BTInvalidStrategy,
1520+
BTLessStrategy,
1521+
BTLessEqualStrategy,
1522+
BTEqualStrategy,
1523+
BTGreaterEqualStrategy,
1524+
BTGreaterStrategy
1525+
} BTStrategy;
15161526
1527+
*/
15171528
/*
15181529
* For each clause under consideration for a given strategy,
15191530
* we collect expressions from clauses for earlier keys, whose
@@ -1526,7 +1537,7 @@ gen_prune_steps_from_opexps(GeneratePruningStepsContext *context,
15261537
* combinations of expressions of different keys, which
15271538
* get_steps_using_prefix takes care of for us.
15281539
*/
1529-
for (strat = 1; strat <= BTMaxStrategyNumber; strat++)
1540+
for (strat = BTLessStrategy; strat < BTNumOfStrategies; strat++)
15301541
{
15311542
foreach(lc, btree_clauses[strat])
15321543
{

src/include/access/stratnum.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,39 @@ typedef uint16 StrategyNumber;
2323

2424
#define InvalidStrategy ((StrategyNumber) 0)
2525

26+
typedef enum BTStrategy
27+
{
28+
BTInvalidStrategy,
29+
BTLessStrategy,
30+
BTLessEqualStrategy,
31+
BTEqualStrategy,
32+
BTGreaterEqualStrategy,
33+
BTGreaterStrategy,
34+
BTNumOfStrategies
35+
} BTStrategy;
36+
37+
/* Macro check, like most such checks in Pg */
38+
#define BTStrategyIsValid(strat) \
39+
((bool) (strat == BTLessStrategy || strat == BTLessEqualStrategy || strat == BTEqualStrategy || strat == BTGreaterEqualStrategy || strat == BTGreaterStrategy))
40+
41+
/* Static inline function check */
42+
static inline bool BTStrategyIsValidFunc(uint16 strat)
43+
{
44+
switch(strat)
45+
{
46+
case BTLessStrategy:
47+
case BTLessEqualStrategy:
48+
case BTEqualStrategy:
49+
case BTGreaterEqualStrategy:
50+
case BTGreaterStrategy:
51+
return true;
52+
case BTInvalidStrategy:
53+
case BTNumOfStrategies:
54+
default:
55+
return false;
56+
}
57+
}
58+
2659
/*
2760
* Strategy numbers for B-tree indexes.
2861
*/

0 commit comments

Comments
 (0)