B-Tree Support Functions
B-Tree Support Functions
As shown in B-Tree Support Functions, btree defines one required and four optional support functions. The five user-defined methods are:
order-
For each combination of data types that a btree operator family provides comparison operators for, it must provide a comparison support function, registered in
pg_amprocwith support function number 1 andamproclefttype/amprocrighttypeequal to the left and right data types for the comparison (i.e., the same data types that the matching operators are registered with inpg_amop). The comparison function must take two non-null values A and B and return anint32value that is<0,0, or>0when A<B, A=B, or A>B, respectively. A null result is disallowed: all values of the data type must be comparable. Seesrc/backend/access/nbtree/nbtcompare.cfor examples.If the compared values are of a collatable data type, the appropriate collation OID will be passed to the comparison support function, using the standard
PG_GET_COLLATION()mechanism. sortsupport- Optionally, a btree operator family may provide sort support function(s), registered under support function number 2. These functions allow implementing comparisons for sorting purposes in a more efficient way than naively calling the comparison support function. The APIs involved in this are defined in
src/include/utils/sortsupport.h. in_range-
Optionally, a btree operator family may provide in_range support function(s), registered under support function number 3. These are not used during btree index operations; rather, they extend the semantics of the operator family so that it can support window clauses containing the
RANGEoffsetPRECEDINGandRANGEoffsetFOLLOWINGframe bound types (see Window Function Calls). Fundamentally, the extra information provided is how to add or subtract an offset value in a way that is compatible with the family's data ordering.An
in_rangefunction must have the signatureval and base must be of the same type, which is one of the types supported by the operator family (i.e., a type for which it provides an ordering). However, offset could be of a different type, which might be one otherwise unsupported by the family. An example is that the built-inin_range(VAL type1, BASE type1, OFFSET type2, SUB bool, LESS bool) returns booltime_opsfamily provides anin_rangefunction that has offset of typeinterval. A family can providein_rangefunctions for any of its supported types and one or more offset types. Eachin_rangefunction should be entered inpg_amprocwithamproclefttypeequal totype1andamprocrighttypeequal totype2.The essential semantics of an
in_rangefunction depend on the two Boolean flag parameters. It should add or subtract base and offset, then compare val to the result, as follows:- if
!sub and!less, return val>=(base+offset) - if
!sub and less, return val<=(base+offset) - if sub and
!less, return val>=(base-offset) - if sub and less, return val
<=(base-offset) Before doing so, the function should check the sign of offset: if it is less than zero, raise errorERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE(22013) with error text like “invalid preceding or following size in window function”. (This is required by the SQL standard, although nonstandard operator families might perhaps choose to ignore this restriction, since there seems to be little semantic necessity for it.) This requirement is delegated to thein_rangefunction so that the core code needn't understand what “less than zero” means for a particular data type.
An additional expectation is that
in_rangefunctions should, if practical, avoid throwing an error if base+offset or base-offset would overflow. The correct comparison result can be determined even if that value would be out of the data type's range. Note that if the data type includes concepts such as “infinity” or “NaN”, extra care may be needed to ensure thatin_range's results agree with the normal sort order of the operator family.The results of the
in_rangefunction must be consistent with the sort ordering imposed by the operator family. To be precise, given any fixed values of offset and sub, then:- If
in_rangewith less = true is true for some val1 and base, it must be true for every val2<=val1 with the same base. - If
in_rangewith less = true is false for some val1 and base, it must be false for every val2>=val1 with the same base. - If
in_rangewith less = true is true for some val and base1, it must be true for every base2>=base1 with the same val. - If
in_rangewith less = true is false for some val and base1, it must be false for every base2<=base1 with the same val. Analogous statements with inverted conditions hold when less = false.
If the type being ordered (
type1) is collatable, the appropriate collation OID will be passed to thein_rangefunction, using the standard PG_GET_COLLATION() mechanism.in_rangefunctions need not handle NULL inputs, and typically will be marked strict. - if
equalimage-
Optionally, a btree operator family may provide
equalimage(“equality implies image equality”) support functions, registered under support function number 4. These functions allow the core code to determine when it is safe to apply the btree deduplication optimization. Currently,equalimagefunctions are only called when building or rebuilding an index.An
equalimagefunction must have the signatureThe return value is static information about an operator class and collation. Returningequalimage(OPCINTYPE oid) returns booltrueindicates that theorderfunction for the operator class is guaranteed to only return0(“arguments are equal”) when its A and B arguments are also interchangeable without any loss of semantic information. Not registering anequalimagefunction or returningfalseindicates that this condition cannot be assumed to hold.The opcintype argument is the
`pg_type.oidof the data type that the operator class indexes. This is a convenience that allows reuse of the same underlyingequalimagefunction across operator classes. If *opcintype* is a collatable data type, the appropriate collation OID will be passed to theequalimagefunction, using the standardPG_GET_COLLATION()` mechanism.As far as the operator class is concerned, returning
trueindicates that deduplication is safe (or safe for the collation whose OID was passed to itsequalimagefunction). However, the core code will only deem deduplication safe for an index when every indexed column uses an operator class that registers anequalimagefunction, and each function actually returnstruewhen called.Image equality is almost the same condition as simple bitwise equality. There is one subtle difference: When indexing a varlena data type, the on-disk representation of two image equal datums may not be bitwise equal due to inconsistent application of TOAST compression on input. Formally, when an operator class's
equalimagefunction returnstrue, it is safe to assume that thedatum_image_eq()C function will always agree with the operator class'sorderfunction (provided that the same collation OID is passed to both theequalimageandorderfunctions).The core code is fundamentally unable to deduce anything about the “equality implies image equality” status of an operator class within a multiple-data-type family based on details from other operator classes in the same family. Also, it is not sensible for an operator family to register a cross-type
equalimagefunction, and attempting to do so will result in an error. This is because “equality implies image equality” status does not just depend on sorting/equality semantics, which are more or less defined at the operator family level. In general, the semantics that one particular data type implements must be considered separately.The convention followed by the operator classes included with the core PostgreSQL distribution is to register a stock, generic
equalimagefunction. Most operator classes registerbtequalimage(), which indicates that deduplication is safe unconditionally. Operator classes for collatable data types such astextregisterbtvarstrequalimage(), which indicates that deduplication is safe with deterministic collations. Best practice for third-party extensions is to register their own custom function to retain control. options-
Optionally, a B-tree operator family may provide
options(“operator class specific options”) support functions, registered under support function number 5. These functions define a set of user-visible parameters that control operator class behavior.An
optionssupport function must have the signatureThe function is passed a pointer to aoptions(RELOPTS local_relopts *) returns voidlocal_reloptsstruct, which needs to be filled with a set of operator class specific options. The options can be accessed from other support functions using thePG_HAS_OPCLASS_OPTIONS()andPG_GET_OPCLASS_OPTIONS()macros.Currently, no B-Tree operator class has an
optionssupport function. B-tree doesn't allow flexible representation of keys like GiST, SP-GiST, GIN and BRIN do. So,optionsprobably doesn't have much application in the current B-tree index access method. Nevertheless, this support function was added to B-tree for uniformity, and will probably find uses during further evolution of B-tree in PostgreSQL.