postgresql_16:6.1.16.3 Index Method Support Routines
PostgreSQL 16 > Server Programming > Extending SQL > Interfacing Extensions to Indexes > Index Method Support Routines
Strategies aren't usually enough information for the system to figure out how to use an index. In practice, the index methods require additional support routines in order to work. For example, the B-tree index method must be able to compare two keys and determine whether one is greater than, equal to, or less than the other. Similarly, the hash index method must be able to compute hash codes for key values. These operations do not correspond to operators used in qualifications in SQL commands; they are administrative routines used by the index methods, internally.
Just as with strategies, the operator class identifies which specific functions should play each of these roles for a given data type and semantic interpretation. The index method defines the set of functions it needs, and the operator class identifies the correct functions to use by assigning them to the “support function numbers” specified by the index method.
Additionally, some opclasses allow users to specify parameters which control their behavior. Each builtin index access method has an optional options support function, which defines a set of opclass-specific parameters.
B-trees require a comparison support function, and allow four additional support functions to be supplied at the operator class author's option, as shown in (see xindex-btree-support-table). The requirements for these support functions are explained further in (see postgresql_16:8.16.3).
| Function | Support Number | | --- | --- | | Compare two keys and return an integer less than zero, zero, or greater than zero, indicating whether the first key is less than, equal to, or greater than the second | 1 | | Return the addresses of C-callable sort support function(s) (optional) | 2 | | Compare a test value to a base value plus/minus an offset, and return true or false according to the comparison result (optional) | 3 | | Determine if it is safe for indexes that use the operator class to apply the btree deduplication optimization (optional) | 4 | | Define options that are specific to this operator class (optional) | 5 |
Hash indexes require one support function, and allow two additional ones to be supplied at the operator class author's option, as shown in (see xindex-hash-support-table).
| Function | Support Number | | --- | --- | | Compute the 32-bit hash value for a key | 1 | | Compute the 64-bit hash value for a key given a 64-bit salt; if the salt is 0, the low 32 bits of the result must match the value that would have been computed by function 1 (optional) | 2 | | Define options that are specific to this operator class (optional) | 3 |
GiST indexes have eleven support functions, six of which are optional, as shown in (see xindex-gist-support-table). (For more information see (see postgresql_16:8.17).)
| Function | Description | Support Number | | --- | --- | --- | | consistent | determine whether key satisfies the query qualifier | 1 | | union | compute union of a set of keys | 2 | | compress | compute a compressed representation of a key or value to be indexed (optional) | 3 | | decompress | compute a decompressed representation of a compressed key (optional) | 4 | | penalty | compute penalty for inserting new key into subtree with given subtree's key | 5 | | picksplit | determine which entries of a page are to be moved to the new page and compute the union keys for resulting pages | 6 | | same | compare two keys and return true if they are equal | 7 | | distance | determine distance from key to query value (optional) | 8 | | fetch | compute original representation of a compressed key for index-only scans (optional) | 9 | | options | define options that are specific to this operator class (optional) | 10 | | sortsupport | provide a sort comparator to be used in fast index builds (optional) | 11 |
SP-GiST indexes have six support functions, one of which is optional, as shown in (see xindex-spgist-support-table). (For more information see (see postgresql_16:8.18).)
| Function | Description | Support Number | | --- | --- | --- | | config | provide basic information about the operator class | 1 | | choose | determine how to insert a new value into an inner tuple | 2 | | picksplit | determine how to partition a set of values | 3 | | inner_consistent | determine which sub-partitions need to be searched for a query | 4 | | leaf_consistent | determine whether key satisfies the query qualifier | 5 | | options | define options that are specific to this operator class (optional) | 6 |
GIN indexes have seven support functions, four of which are optional, as shown in (see xindex-gin-support-table). (For more information see (see postgresql_16:8.19).)
| Function | Description | Support Number | | --- | --- | --- | | compare | compare two keys and return an integer less than zero, zero, or greater than zero, indicating whether the first key is less than, equal to, or greater than the second | 1 | | extractValue | extract keys from a value to be indexed | 2 | | extractQuery | extract keys from a query condition | 3 | | consistent | determine whether value matches query condition (Boolean variant) (optional if support function 6 is present) | 4 | | comparePartial | compare partial key from query and key from index, and return an integer less than zero, zero, or greater than zero, indicating whether GIN should ignore this index entry, treat the entry as a match, or stop the index scan (optional) | 5 | | triConsistent | determine whether value matches query condition (ternary variant) (optional if support function 4 is present) | 6 | | options | define options that are specific to this operator class (optional) | 7 |
BRIN indexes have five basic support functions, one of which is optional, as shown in (see xindex-brin-support-table). Some versions of the basic functions require additional support functions to be provided. (For more information see (see postgresql_16:8.20.3).)
| Function | Description | Support Number | | --- | --- | --- | | opcInfo | return internal information describing the indexed columns' summary data | 1 | | add_value | add a new value to an existing summary index tuple | 2 | | consistent | determine whether value matches query condition | 3 | | union | compute union of two summary tuples | 4 | | options | define options that are specific to this operator class (optional) | 5 |
Unlike search operators, support functions return whichever data type the particular index method expects; for example in the case of the comparison function for B-trees, a signed integer. The number and types of the arguments to each support function are likewise dependent on the index method. For B-tree and hash the comparison and hashing support functions take the same input data types as do the operators included in the operator class, but this is not the case for most GiST, SP-GiST, GIN, and BRIN support functions.