Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
60.3. B-Tree Support Functions
Prev UpChapter 60. B-Tree IndexesHome Next

60.3. B-Tree Support Functions

As shown inTable 36.8, btree defines one required and two optional support functions.

For each combination of data types that a btree operator family provides comparison operators for, it must provide a comparison support function, registered inpg_amproc with support function number 1 andamproclefttype/amprocrighttype equal 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 valuesA andB and return anint32 value that is<0,0, or>0 whenA<B,A=B, orA>B, respectively. A null result is disallowed: all values of the data type must be comparable.

If the compared values are of a collatable data type, the appropriate collation OID will be passed to the comparison support function, using the standardPG_GET_COLLATION() mechanism.

Optionally, a btree operator family may providesort 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 insrc/include/utils/sortsupport.h.

Optionally, a btree operator family may providein_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 theRANGEoffsetPRECEDING andRANGEoffsetFOLLOWING frame bound types (seeSection 4.2.8). Fundamentally, the extra information provided is how to add or subtract anoffset value in a way that is compatible with the family's data ordering.

Anin_range function must have the signature

in_range(val type1,base type1,offset type2,sub bool,less bool)returns bool

val andbase 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-intime_ops family provides anin_range function that hasoffset of typeinterval. A family can providein_range functions for any of its supported types and one or moreoffset types. Eachin_range function should be entered inpg_amproc withamproclefttype equal totype1 andamprocrighttype equal totype2.

The essential semantics of anin_range function depend on the two Boolean flag parameters. It should add or subtractbase andoffset, then compareval to the result, as follows:

  • if!sub and!less, returnval>= (base+offset)

  • if!sub andless, returnval<= (base+offset)

  • ifsub and!less, returnval>= (base-offset)

  • ifsub andless, returnval<= (base-offset)

Before doing so, the function should check the sign ofoffset: if it is less than zero, raise errorERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE (22013) with error text likeinvalid 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_range function so that the core code needn't understand whatless than zero means for a particular data type.

An additional expectation is thatin_range functions should, if practical, avoid throwing an error ifbase+offset orbase-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 asinfinity orNaN, extra care may be needed to ensure thatin_range's results agree with the normal sort order of the operator family.

The results of thein_range function must be consistent with the sort ordering imposed by the operator family. To be precise, given any fixed values ofoffset andsub, then:

  • Ifin_range withless = true is true for someval1 andbase, it must be true for everyval2<=val1 with the samebase.

  • Ifin_range withless = true is false for someval1 andbase, it must be false for everyval2>=val1 with the samebase.

  • Ifin_range withless = true is true for someval andbase1, it must be true for everybase2>=base1 with the sameval.

  • Ifin_range withless = true is false for someval andbase1, it must be false for everybase2<=base1 with the sameval.

Analogous statements with inverted conditions hold whenless = false.

If the type being ordered (type1) is collatable, the appropriate collation OID will be passed to thein_range function, using the standard PG_GET_COLLATION() mechanism.

in_range functions need not handle NULL inputs, and typically will be marked strict.


Prev Up Next
60.2. Behavior of B-Tree Operator Classes Home Chapter 61. GiST Indexes
epubpdf
Go to Postgres Pro Standard 12
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp