Re: Replace magic numbers with strategy numbers for B-tree indexes

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Daniil Davydov <3danissimo(at)gmail(dot)com>
Cc: Nikita Malakhov <hukutoc(at)gmail(dot)com>, Peter Eisentraut <peter(at)eisentraut(dot)org>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Replace magic numbers with strategy numbers for B-tree indexes
Date: 2025-09-01 23:51:21
Message-ID: aLYxeYIY70jup555@paquier.xyz
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Sep 01, 2025 at 09:04:04PM +0700, Daniil Davydov wrote:
> I don't think that we can just create different enums for each index strategies.
> We have (for example) ScanKey functionality, which can work with different
> indexes (and such a functions has a uint16 argument for strategy number).
>
> Or are you talking about a single huge enum for all index types? I don't
> mind trying to do something like this, but I'm not sure how
> "beautiful" it will be.

+typedef enum BTStrategy
+{
+ BTInvalidStrategy,
+ BTLessStrategy,
+ BTLessEqualStrategy,
+ BTEqualStrategy,
+ BTGreaterEqualStrategy,
+ BTGreaterStrategy,
+ BTNumOfStrategies
+} BTStrategy;
[...]
- List *btree_clauses[BTMaxStrategyNumber + 1],
+ List *btree_clauses[BTNumOfStrategies],

Isn't that where you'd want to introduce a separate #define to track
the maximum number in the enum? Adding the total number inside
BTStrategy would be wrong IMO. Anyway, the advantage of an enum is
also to be able to initialize the first value, with the next one
following suit. With most of the code using the strategy numbers in
for loops, we are not taking advantage of an enum structure, which is
relevant for example to find paths with switch/case without default,
where compilers would warn about missing values. A second one would
be type enforcement in dedicated APIs, and we already have
StrategyNumber for this job in ScanKeyInit(), for example.

+/* Static inline function check */
+static inline bool BTStrategyIsValidFunc(uint16 strat)
+{
+ switch(strat)

Note that this is used nowhere :)
--
Michael

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Julien Rouhaud 2025-09-02 00:03:04 Re: Extension security improvement: Add support for extensions with an owned schema
Previous Message Michael Paquier 2025-09-01 23:19:22 Re: Get rid of pgstat_count_backend_io_op*() functions