postgresql_16:6.1.16.2 Index Method Strategies
PostgreSQL 16 > Server Programming > Extending SQL > Interfacing Extensions to Indexes > Index Method Strategies
The operators associated with an operator class are identified by “strategy numbers”, which serve to identify the semantics of each operator within the context of its operator class. For example, B-trees impose a strict ordering on keys, lesser to greater, and so operators like “less than” and “greater than or equal to” are interesting with respect to a B-tree. Because PostgreSQL allows the user to define operators, PostgreSQL cannot look at the name of an operator (e.g., < or >=) and tell what kind of comparison it is. Instead, the index method defines a set of “strategies”, which can be thought of as generalized operators. Each operator class specifies which actual operator corresponds to each strategy for a particular data type and interpretation of the index semantics.
The B-tree index method defines five strategies, shown in (see xindex-btree-strat-table).
| Operation | Strategy Number | | --- | --- | | less than | 1 | | less than or equal | 2 | | equal | 3 | | greater than or equal | 4 | | greater than | 5 |
Hash indexes support only equality comparisons, and so they use only one strategy, shown in (see xindex-hash-strat-table).
| Operation | Strategy Number | | --- | --- | | equal | 1 |
GiST indexes are more flexible: they do not have a fixed set of strategies at all. Instead, the “consistency” support routine of each particular GiST operator class interprets the strategy numbers however it likes. As an example, several of the built-in GiST index operator classes index two-dimensional geometric objects, providing the “R-tree” strategies shown in (see xindex-rtree-strat-table). Four of these are true two-dimensional tests (overlaps, same, contains, contained by); four of them consider only the X direction; and the other four provide the same tests in the Y direction.
| Operation | Strategy Number | | --- | --- | | strictly left of | 1 | | does not extend to right of | 2 | | overlaps | 3 | | does not extend to left of | 4 | | strictly right of | 5 | | same | 6 | | contains | 7 | | contained by | 8 | | does not extend above | 9 | | strictly below | 10 | | strictly above | 11 | | does not extend below | 12 |
SP-GiST indexes are similar to GiST indexes in flexibility: they don't have a fixed set of strategies. Instead the support routines of each operator class interpret the strategy numbers according to the operator class's definition. As an example, the strategy numbers used by the built-in operator classes for points are shown in (see xindex-spgist-point-strat-table).
| Operation | Strategy Number | | --- | --- | | strictly left of | 1 | | strictly right of | 5 | | same | 6 | | contained by | 8 | | strictly below | 10 | | strictly above | 11 |
GIN indexes are similar to GiST and SP-GiST indexes, in that they don't have a fixed set of strategies either. Instead the support routines of each operator class interpret the strategy numbers according to the operator class's definition. As an example, the strategy numbers used by the built-in operator class for arrays are shown in (see xindex-gin-array-strat-table).
| Operation | Strategy Number | | --- | --- | | overlap | 1 | | contains | 2 | | is contained by | 3 | | equal | 4 |
BRIN indexes are similar to GiST, SP-GiST and GIN indexes in that they don't have a fixed set of strategies either. Instead the support routines of each operator class interpret the strategy numbers according to the operator class's definition. As an example, the strategy numbers used by the built-in Minmax operator classes are shown in (see xindex-brin-minmax-strat-table).
| Operation | Strategy Number | | --- | --- | | less than | 1 | | less than or equal | 2 | | equal | 3 | | greater than or equal | 4 | | greater than | 5 |
Notice that all the operators listed above return Boolean values. In practice, all operators defined as index method search operators must return type boolean, since they must appear at the top level of a WHERE clause to be used with an index. (Some index access methods also support *ordering operators*, which typically don't return Boolean values; that feature is discussed in (see postgresql_16:6.1.16.7).)