]> begriffs open source - ai-pg/blob - full-docs/txt/xindex.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / xindex.txt
1
2 36.16. Interfacing Extensions to Indexes #
3
4    36.16.1. Index Methods and Operator Classes
5    36.16.2. Index Method Strategies
6    36.16.3. Index Method Support Routines
7    36.16.4. An Example
8    36.16.5. Operator Classes and Operator Families
9    36.16.6. System Dependencies on Operator Classes
10    36.16.7. Ordering Operators
11    36.16.8. Special Features of Operator Classes
12
13    The procedures described thus far let you define new types, new
14    functions, and new operators. However, we cannot yet define an index on
15    a column of a new data type. To do this, we must define an operator
16    class for the new data type. Later in this section, we will illustrate
17    this concept in an example: a new operator class for the B-tree index
18    method that stores and sorts complex numbers in ascending absolute
19    value order.
20
21    Operator classes can be grouped into operator families to show the
22    relationships between semantically compatible classes. When only a
23    single data type is involved, an operator class is sufficient, so we'll
24    focus on that case first and then return to operator families.
25
26 36.16.1. Index Methods and Operator Classes #
27
28    Operator classes are associated with an index access method, such as
29    B-Tree or GIN. Custom index access method may be defined with CREATE
30    ACCESS METHOD. See Chapter 63 for details.
31
32    The routines for an index method do not directly know anything about
33    the data types that the index method will operate on. Instead, an
34    operator class identifies the set of operations that the index method
35    needs to use to work with a particular data type. Operator classes are
36    so called because one thing they specify is the set of WHERE-clause
37    operators that can be used with an index (i.e., can be converted into
38    an index-scan qualification). An operator class can also specify some
39    support function that are needed by the internal operations of the
40    index method, but do not directly correspond to any WHERE-clause
41    operator that can be used with the index.
42
43    It is possible to define multiple operator classes for the same data
44    type and index method. By doing this, multiple sets of indexing
45    semantics can be defined for a single data type. For example, a B-tree
46    index requires a sort ordering to be defined for each data type it
47    works on. It might be useful for a complex-number data type to have one
48    B-tree operator class that sorts the data by complex absolute value,
49    another that sorts by real part, and so on. Typically, one of the
50    operator classes will be deemed most commonly useful and will be marked
51    as the default operator class for that data type and index method.
52
53    The same operator class name can be used for several different index
54    methods (for example, both B-tree and hash index methods have operator
55    classes named int4_ops), but each such class is an independent entity
56    and must be defined separately.
57
58 36.16.2. Index Method Strategies #
59
60    The operators associated with an operator class are identified by
61    “strategy numbers”, which serve to identify the semantics of each
62    operator within the context of its operator class. For example, B-trees
63    impose a strict ordering on keys, lesser to greater, and so operators
64    like “less than” and “greater than or equal to” are interesting with
65    respect to a B-tree. Because PostgreSQL allows the user to define
66    operators, PostgreSQL cannot look at the name of an operator (e.g., <
67    or >=) and tell what kind of comparison it is. Instead, the index
68    method defines a set of “strategies”, which can be thought of as
69    generalized operators. Each operator class specifies which actual
70    operator corresponds to each strategy for a particular data type and
71    interpretation of the index semantics.
72
73    The B-tree index method defines five strategies, shown in Table 36.3.
74
75    Table 36.3. B-Tree Strategies
76          Operation       Strategy Number
77    less than             1
78    less than or equal    2
79    equal                 3
80    greater than or equal 4
81    greater than          5
82
83    Hash indexes support only equality comparisons, and so they use only
84    one strategy, shown in Table 36.4.
85
86    Table 36.4. Hash Strategies
87    Operation Strategy Number
88    equal     1
89
90    GiST indexes are more flexible: they do not have a fixed set of
91    strategies at all. Instead, the “consistency” support routine of each
92    particular GiST operator class interprets the strategy numbers however
93    it likes. As an example, several of the built-in GiST index operator
94    classes index two-dimensional geometric objects, providing the “R-tree”
95    strategies shown in Table 36.5. Four of these are true two-dimensional
96    tests (overlaps, same, contains, contained by); four of them consider
97    only the X direction; and the other four provide the same tests in the
98    Y direction.
99
100    Table 36.5. GiST Two-Dimensional “R-tree” Strategies
101             Operation          Strategy Number
102    strictly left of            1
103    does not extend to right of 2
104    overlaps                    3
105    does not extend to left of  4
106    strictly right of           5
107    same                        6
108    contains                    7
109    contained by                8
110    does not extend above       9
111    strictly below              10
112    strictly above              11
113    does not extend below       12
114
115    SP-GiST indexes are similar to GiST indexes in flexibility: they don't
116    have a fixed set of strategies. Instead the support routines of each
117    operator class interpret the strategy numbers according to the operator
118    class's definition. As an example, the strategy numbers used by the
119    built-in operator classes for points are shown in Table 36.6.
120
121    Table 36.6. SP-GiST Point Strategies
122        Operation     Strategy Number
123    strictly left of  1
124    strictly right of 5
125    same              6
126    contained by      8
127    strictly below    10
128    strictly above    11
129
130    GIN indexes are similar to GiST and SP-GiST indexes, in that they don't
131    have a fixed set of strategies either. Instead the support routines of
132    each operator class interpret the strategy numbers according to the
133    operator class's definition. As an example, the strategy numbers used
134    by the built-in operator class for arrays are shown in Table 36.7.
135
136    Table 36.7. GIN Array Strategies
137       Operation    Strategy Number
138    overlap         1
139    contains        2
140    is contained by 3
141    equal           4
142
143    BRIN indexes are similar to GiST, SP-GiST and GIN indexes in that they
144    don't have a fixed set of strategies either. Instead the support
145    routines of each operator class interpret the strategy numbers
146    according to the operator class's definition. As an example, the
147    strategy numbers used by the built-in Minmax operator classes are shown
148    in Table 36.8.
149
150    Table 36.8. BRIN Minmax Strategies
151          Operation       Strategy Number
152    less than             1
153    less than or equal    2
154    equal                 3
155    greater than or equal 4
156    greater than          5
157
158    Notice that all the operators listed above return Boolean values. In
159    practice, all operators defined as index method search operators must
160    return type boolean, since they must appear at the top level of a WHERE
161    clause to be used with an index. (Some index access methods also
162    support ordering operators, which typically don't return Boolean
163    values; that feature is discussed in Section 36.16.7.)
164
165 36.16.3. Index Method Support Routines #
166
167    Strategies aren't usually enough information for the system to figure
168    out how to use an index. In practice, the index methods require
169    additional support routines in order to work. For example, the B-tree
170    index method must be able to compare two keys and determine whether one
171    is greater than, equal to, or less than the other. Similarly, the hash
172    index method must be able to compute hash codes for key values. These
173    operations do not correspond to operators used in qualifications in SQL
174    commands; they are administrative routines used by the index methods,
175    internally.
176
177    Just as with strategies, the operator class identifies which specific
178    functions should play each of these roles for a given data type and
179    semantic interpretation. The index method defines the set of functions
180    it needs, and the operator class identifies the correct functions to
181    use by assigning them to the “support function numbers” specified by
182    the index method.
183
184    Additionally, some opclasses allow users to specify parameters which
185    control their behavior. Each builtin index access method has an
186    optional options support function, which defines a set of
187    opclass-specific parameters.
188
189    B-trees require a comparison support function, and allow four
190    additional support functions to be supplied at the operator class
191    author's option, as shown in Table 36.9. The requirements for these
192    support functions are explained further in Section 65.1.3.
193
194    Table 36.9. B-Tree Support Functions
195    Function Support Number
196    Compare two keys and return an integer less than zero, zero, or greater
197    than zero, indicating whether the first key is less than, equal to, or
198    greater than the second 1
199    Return the addresses of C-callable sort support function(s) (optional)
200    2
201    Compare a test value to a base value plus/minus an offset, and return
202    true or false according to the comparison result (optional) 3
203    Determine if it is safe for indexes that use the operator class to
204    apply the btree deduplication optimization (optional) 4
205    Define options that are specific to this operator class (optional) 5
206    Return the addresses of C-callable skip support function(s) (optional)
207    6
208
209    Hash indexes require one support function, and allow two additional
210    ones to be supplied at the operator class author's option, as shown in
211    Table 36.10.
212
213    Table 36.10. Hash Support Functions
214    Function Support Number
215    Compute the 32-bit hash value for a key 1
216    Compute the 64-bit hash value for a key given a 64-bit salt; if the
217    salt is 0, the low 32 bits of the result must match the value that
218    would have been computed by function 1 (optional) 2
219    Define options that are specific to this operator class (optional) 3
220
221    GiST indexes have twelve support functions, seven of which are
222    optional, as shown in Table 36.11. (For more information see
223    Section 65.2.)
224
225    Table 36.11. GiST Support Functions
226    Function Description Support Number
227    consistent determine whether key satisfies the query qualifier 1
228    union compute union of a set of keys 2
229    compress compute a compressed representation of a key or value to be
230    indexed (optional) 3
231    decompress compute a decompressed representation of a compressed key
232    (optional) 4
233    penalty compute penalty for inserting new key into subtree with given
234    subtree's key 5
235    picksplit determine which entries of a page are to be moved to the new
236    page and compute the union keys for resulting pages 6
237    same compare two keys and return true if they are equal 7
238    distance determine distance from key to query value (optional) 8
239    fetch compute original representation of a compressed key for
240    index-only scans (optional) 9
241    options define options that are specific to this operator class
242    (optional) 10
243    sortsupport provide a sort comparator to be used in fast index builds
244    (optional) 11
245    translate_cmptype translate compare types to strategy numbers used by
246    the operator class (optional) 12
247
248    SP-GiST indexes have six support functions, one of which is optional,
249    as shown in Table 36.12. (For more information see Section 65.3.)
250
251    Table 36.12. SP-GiST Support Functions
252    Function Description Support Number
253    config provide basic information about the operator class 1
254    choose determine how to insert a new value into an inner tuple 2
255    picksplit determine how to partition a set of values 3
256    inner_consistent determine which sub-partitions need to be searched for
257    a query 4
258    leaf_consistent determine whether key satisfies the query qualifier 5
259    options define options that are specific to this operator class
260    (optional) 6
261
262    GIN indexes have seven support functions, four of which are optional,
263    as shown in Table 36.13. (For more information see Section 65.4.)
264
265    Table 36.13. GIN Support Functions
266    Function Description Support Number
267    compare compare two keys and return an integer less than zero, zero, or
268    greater than zero, indicating whether the first key is less than, equal
269    to, or greater than the second 1
270    extractValue extract keys from a value to be indexed 2
271    extractQuery extract keys from a query condition 3
272    consistent determine whether value matches query condition (Boolean
273    variant) (optional if support function 6 is present) 4
274    comparePartial compare partial key from query and key from index, and
275    return an integer less than zero, zero, or greater than zero,
276    indicating whether GIN should ignore this index entry, treat the entry
277    as a match, or stop the index scan (optional) 5
278    triConsistent determine whether value matches query condition (ternary
279    variant) (optional if support function 4 is present) 6
280    options define options that are specific to this operator class
281    (optional) 7
282
283    BRIN indexes have five basic support functions, one of which is
284    optional, as shown in Table 36.14. Some versions of the basic functions
285    require additional support functions to be provided. (For more
286    information see Section 65.5.3.)
287
288    Table 36.14. BRIN Support Functions
289    Function Description Support Number
290    opcInfo return internal information describing the indexed columns'
291    summary data 1
292    add_value add a new value to an existing summary index tuple 2
293    consistent determine whether value matches query condition 3
294    union compute union of two summary tuples 4
295    options define options that are specific to this operator class
296    (optional) 5
297
298    Unlike search operators, support functions return whichever data type
299    the particular index method expects; for example in the case of the
300    comparison function for B-trees, a signed integer. The number and types
301    of the arguments to each support function are likewise dependent on the
302    index method. For B-tree and hash the comparison and hashing support
303    functions take the same input data types as do the operators included
304    in the operator class, but this is not the case for most GiST, SP-GiST,
305    GIN, and BRIN support functions.
306
307 36.16.4. An Example #
308
309    Now that we have seen the ideas, here is the promised example of
310    creating a new operator class. (You can find a working copy of this
311    example in src/tutorial/complex.c and src/tutorial/complex.sql in the
312    source distribution.) The operator class encapsulates operators that
313    sort complex numbers in absolute value order, so we choose the name
314    complex_abs_ops. First, we need a set of operators. The procedure for
315    defining operators was discussed in Section 36.14. For an operator
316    class on B-trees, the operators we require are:
317      * absolute-value less-than (strategy 1)
318      * absolute-value less-than-or-equal (strategy 2)
319      * absolute-value equal (strategy 3)
320      * absolute-value greater-than-or-equal (strategy 4)
321      * absolute-value greater-than (strategy 5)
322
323    The least error-prone way to define a related set of comparison
324    operators is to write the B-tree comparison support function first, and
325    then write the other functions as one-line wrappers around the support
326    function. This reduces the odds of getting inconsistent results for
327    corner cases. Following this approach, we first write:
328 #define Mag(c)  ((c)->x*(c)->x + (c)->y*(c)->y)
329
330 static int
331 complex_abs_cmp_internal(Complex *a, Complex *b)
332 {
333     double      amag = Mag(a),
334                 bmag = Mag(b);
335
336     if (amag < bmag)
337         return -1;
338     if (amag > bmag)
339         return 1;
340     return 0;
341 }
342
343
344    Now the less-than function looks like:
345 PG_FUNCTION_INFO_V1(complex_abs_lt);
346
347 Datum
348 complex_abs_lt(PG_FUNCTION_ARGS)
349 {
350     Complex    *a = (Complex *) PG_GETARG_POINTER(0);
351     Complex    *b = (Complex *) PG_GETARG_POINTER(1);
352
353     PG_RETURN_BOOL(complex_abs_cmp_internal(a, b) < 0);
354 }
355
356
357    The other four functions differ only in how they compare the internal
358    function's result to zero.
359
360    Next we declare the functions and the operators based on the functions
361    to SQL:
362 CREATE FUNCTION complex_abs_lt(complex, complex) RETURNS bool
363     AS 'filename', 'complex_abs_lt'
364     LANGUAGE C IMMUTABLE STRICT;
365
366 CREATE OPERATOR < (
367    leftarg = complex, rightarg = complex, procedure = complex_abs_lt,
368    commutator = > , negator = >= ,
369    restrict = scalarltsel, join = scalarltjoinsel
370 );
371
372    It is important to specify the correct commutator and negator
373    operators, as well as suitable restriction and join selectivity
374    functions, otherwise the optimizer will be unable to make effective use
375    of the index.
376
377    Other things worth noting are happening here:
378      * There can only be one operator named, say, = and taking type
379        complex for both operands. In this case we don't have any other
380        operator = for complex, but if we were building a practical data
381        type we'd probably want = to be the ordinary equality operation for
382        complex numbers (and not the equality of the absolute values). In
383        that case, we'd need to use some other operator name for
384        complex_abs_eq.
385      * Although PostgreSQL can cope with functions having the same SQL
386        name as long as they have different argument data types, C can only
387        cope with one global function having a given name. So we shouldn't
388        name the C function something simple like abs_eq. Usually it's a
389        good practice to include the data type name in the C function name,
390        so as not to conflict with functions for other data types.
391      * We could have made the SQL name of the function abs_eq, relying on
392        PostgreSQL to distinguish it by argument data types from any other
393        SQL function of the same name. To keep the example simple, we make
394        the function have the same names at the C level and SQL level.
395
396    The next step is the registration of the support routine required by
397    B-trees. The example C code that implements this is in the same file
398    that contains the operator functions. This is how we declare the
399    function:
400 CREATE FUNCTION complex_abs_cmp(complex, complex)
401     RETURNS integer
402     AS 'filename'
403     LANGUAGE C IMMUTABLE STRICT;
404
405    Now that we have the required operators and support routine, we can
406    finally create the operator class:
407 CREATE OPERATOR CLASS complex_abs_ops
408     DEFAULT FOR TYPE complex USING btree AS
409         OPERATOR        1       < ,
410         OPERATOR        2       <= ,
411         OPERATOR        3       = ,
412         OPERATOR        4       >= ,
413         OPERATOR        5       > ,
414         FUNCTION        1       complex_abs_cmp(complex, complex);
415
416
417    And we're done! It should now be possible to create and use B-tree
418    indexes on complex columns.
419
420    We could have written the operator entries more verbosely, as in:
421         OPERATOR        1       < (complex, complex) ,
422
423    but there is no need to do so when the operators take the same data
424    type we are defining the operator class for.
425
426    The above example assumes that you want to make this new operator class
427    the default B-tree operator class for the complex data type. If you
428    don't, just leave out the word DEFAULT.
429
430 36.16.5. Operator Classes and Operator Families #
431
432    So far we have implicitly assumed that an operator class deals with
433    only one data type. While there certainly can be only one data type in
434    a particular index column, it is often useful to index operations that
435    compare an indexed column to a value of a different data type. Also, if
436    there is use for a cross-data-type operator in connection with an
437    operator class, it is often the case that the other data type has a
438    related operator class of its own. It is helpful to make the
439    connections between related classes explicit, because this can aid the
440    planner in optimizing SQL queries (particularly for B-tree operator
441    classes, since the planner contains a great deal of knowledge about how
442    to work with them).
443
444    To handle these needs, PostgreSQL uses the concept of an operator
445    family. An operator family contains one or more operator classes, and
446    can also contain indexable operators and corresponding support
447    functions that belong to the family as a whole but not to any single
448    class within the family. We say that such operators and functions are
449    “loose” within the family, as opposed to being bound into a specific
450    class. Typically each operator class contains single-data-type
451    operators while cross-data-type operators are loose in the family.
452
453    All the operators and functions in an operator family must have
454    compatible semantics, where the compatibility requirements are set by
455    the index method. You might therefore wonder why bother to single out
456    particular subsets of the family as operator classes; and indeed for
457    many purposes the class divisions are irrelevant and the family is the
458    only interesting grouping. The reason for defining operator classes is
459    that they specify how much of the family is needed to support any
460    particular index. If there is an index using an operator class, then
461    that operator class cannot be dropped without dropping the index — but
462    other parts of the operator family, namely other operator classes and
463    loose operators, could be dropped. Thus, an operator class should be
464    specified to contain the minimum set of operators and functions that
465    are reasonably needed to work with an index on a specific data type,
466    and then related but non-essential operators can be added as loose
467    members of the operator family.
468
469    As an example, PostgreSQL has a built-in B-tree operator family
470    integer_ops, which includes operator classes int8_ops, int4_ops, and
471    int2_ops for indexes on bigint (int8), integer (int4), and smallint
472    (int2) columns respectively. The family also contains cross-data-type
473    comparison operators allowing any two of these types to be compared, so
474    that an index on one of these types can be searched using a comparison
475    value of another type. The family could be duplicated by these
476    definitions:
477 CREATE OPERATOR FAMILY integer_ops USING btree;
478
479 CREATE OPERATOR CLASS int8_ops
480 DEFAULT FOR TYPE int8 USING btree FAMILY integer_ops AS
481   -- standard int8 comparisons
482   OPERATOR 1 < ,
483   OPERATOR 2 <= ,
484   OPERATOR 3 = ,
485   OPERATOR 4 >= ,
486   OPERATOR 5 > ,
487   FUNCTION 1 btint8cmp(int8, int8) ,
488   FUNCTION 2 btint8sortsupport(internal) ,
489   FUNCTION 3 in_range(int8, int8, int8, boolean, boolean) ,
490   FUNCTION 4 btequalimage(oid) ,
491   FUNCTION 6 btint8skipsupport(internal) ;
492
493 CREATE OPERATOR CLASS int4_ops
494 DEFAULT FOR TYPE int4 USING btree FAMILY integer_ops AS
495   -- standard int4 comparisons
496   OPERATOR 1 < ,
497   OPERATOR 2 <= ,
498   OPERATOR 3 = ,
499   OPERATOR 4 >= ,
500   OPERATOR 5 > ,
501   FUNCTION 1 btint4cmp(int4, int4) ,
502   FUNCTION 2 btint4sortsupport(internal) ,
503   FUNCTION 3 in_range(int4, int4, int4, boolean, boolean) ,
504   FUNCTION 4 btequalimage(oid) ,
505   FUNCTION 6 btint4skipsupport(internal) ;
506
507 CREATE OPERATOR CLASS int2_ops
508 DEFAULT FOR TYPE int2 USING btree FAMILY integer_ops AS
509   -- standard int2 comparisons
510   OPERATOR 1 < ,
511   OPERATOR 2 <= ,
512   OPERATOR 3 = ,
513   OPERATOR 4 >= ,
514   OPERATOR 5 > ,
515   FUNCTION 1 btint2cmp(int2, int2) ,
516   FUNCTION 2 btint2sortsupport(internal) ,
517   FUNCTION 3 in_range(int2, int2, int2, boolean, boolean) ,
518   FUNCTION 4 btequalimage(oid) ,
519   FUNCTION 6 btint2skipsupport(internal) ;
520
521 ALTER OPERATOR FAMILY integer_ops USING btree ADD
522   -- cross-type comparisons int8 vs int2
523   OPERATOR 1 < (int8, int2) ,
524   OPERATOR 2 <= (int8, int2) ,
525   OPERATOR 3 = (int8, int2) ,
526   OPERATOR 4 >= (int8, int2) ,
527   OPERATOR 5 > (int8, int2) ,
528   FUNCTION 1 btint82cmp(int8, int2) ,
529
530   -- cross-type comparisons int8 vs int4
531   OPERATOR 1 < (int8, int4) ,
532   OPERATOR 2 <= (int8, int4) ,
533   OPERATOR 3 = (int8, int4) ,
534   OPERATOR 4 >= (int8, int4) ,
535   OPERATOR 5 > (int8, int4) ,
536   FUNCTION 1 btint84cmp(int8, int4) ,
537
538   -- cross-type comparisons int4 vs int2
539   OPERATOR 1 < (int4, int2) ,
540   OPERATOR 2 <= (int4, int2) ,
541   OPERATOR 3 = (int4, int2) ,
542   OPERATOR 4 >= (int4, int2) ,
543   OPERATOR 5 > (int4, int2) ,
544   FUNCTION 1 btint42cmp(int4, int2) ,
545
546   -- cross-type comparisons int4 vs int8
547   OPERATOR 1 < (int4, int8) ,
548   OPERATOR 2 <= (int4, int8) ,
549   OPERATOR 3 = (int4, int8) ,
550   OPERATOR 4 >= (int4, int8) ,
551   OPERATOR 5 > (int4, int8) ,
552   FUNCTION 1 btint48cmp(int4, int8) ,
553
554   -- cross-type comparisons int2 vs int8
555   OPERATOR 1 < (int2, int8) ,
556   OPERATOR 2 <= (int2, int8) ,
557   OPERATOR 3 = (int2, int8) ,
558   OPERATOR 4 >= (int2, int8) ,
559   OPERATOR 5 > (int2, int8) ,
560   FUNCTION 1 btint28cmp(int2, int8) ,
561
562   -- cross-type comparisons int2 vs int4
563   OPERATOR 1 < (int2, int4) ,
564   OPERATOR 2 <= (int2, int4) ,
565   OPERATOR 3 = (int2, int4) ,
566   OPERATOR 4 >= (int2, int4) ,
567   OPERATOR 5 > (int2, int4) ,
568   FUNCTION 1 btint24cmp(int2, int4) ,
569
570   -- cross-type in_range functions
571   FUNCTION 3 in_range(int4, int4, int8, boolean, boolean) ,
572   FUNCTION 3 in_range(int4, int4, int2, boolean, boolean) ,
573   FUNCTION 3 in_range(int2, int2, int8, boolean, boolean) ,
574   FUNCTION 3 in_range(int2, int2, int4, boolean, boolean) ;
575
576
577    Notice that this definition “overloads” the operator strategy and
578    support function numbers: each number occurs multiple times within the
579    family. This is allowed so long as each instance of a particular number
580    has distinct input data types. The instances that have both input types
581    equal to an operator class's input type are the primary operators and
582    support functions for that operator class, and in most cases should be
583    declared as part of the operator class rather than as loose members of
584    the family.
585
586    In a B-tree operator family, all the operators in the family must sort
587    compatibly, as is specified in detail in Section 65.1.2. For each
588    operator in the family there must be a support function having the same
589    two input data types as the operator. It is recommended that a family
590    be complete, i.e., for each combination of data types, all operators
591    are included. Each operator class should include just the
592    non-cross-type operators and support function for its data type.
593
594    To build a multiple-data-type hash operator family, compatible hash
595    support functions must be created for each data type supported by the
596    family. Here compatibility means that the functions are guaranteed to
597    return the same hash code for any two values that are considered equal
598    by the family's equality operators, even when the values are of
599    different types. This is usually difficult to accomplish when the types
600    have different physical representations, but it can be done in some
601    cases. Furthermore, casting a value from one data type represented in
602    the operator family to another data type also represented in the
603    operator family via an implicit or binary coercion cast must not change
604    the computed hash value. Notice that there is only one support function
605    per data type, not one per equality operator. It is recommended that a
606    family be complete, i.e., provide an equality operator for each
607    combination of data types. Each operator class should include just the
608    non-cross-type equality operator and the support function for its data
609    type.
610
611    GiST, SP-GiST, and GIN indexes do not have any explicit notion of
612    cross-data-type operations. The set of operators supported is just
613    whatever the primary support functions for a given operator class can
614    handle.
615
616    In BRIN, the requirements depends on the framework that provides the
617    operator classes. For operator classes based on minmax, the behavior
618    required is the same as for B-tree operator families: all the operators
619    in the family must sort compatibly, and casts must not change the
620    associated sort ordering.
621
622 Note
623
624    Prior to PostgreSQL 8.3, there was no concept of operator families, and
625    so any cross-data-type operators intended to be used with an index had
626    to be bound directly into the index's operator class. While this
627    approach still works, it is deprecated because it makes an index's
628    dependencies too broad, and because the planner can handle
629    cross-data-type comparisons more effectively when both data types have
630    operators in the same operator family.
631
632 36.16.6. System Dependencies on Operator Classes #
633
634    PostgreSQL uses operator classes to infer the properties of operators
635    in more ways than just whether they can be used with indexes.
636    Therefore, you might want to create operator classes even if you have
637    no intention of indexing any columns of your data type.
638
639    In particular, there are SQL features such as ORDER BY and DISTINCT
640    that require comparison and sorting of values. To implement these
641    features on a user-defined data type, PostgreSQL looks for the default
642    B-tree operator class for the data type. The “equals” member of this
643    operator class defines the system's notion of equality of values for
644    GROUP BY and DISTINCT, and the sort ordering imposed by the operator
645    class defines the default ORDER BY ordering.
646
647    If there is no default B-tree operator class for a data type, the
648    system will look for a default hash operator class. But since that kind
649    of operator class only provides equality, it is only able to support
650    grouping not sorting.
651
652    When there is no default operator class for a data type, you will get
653    errors like “could not identify an ordering operator” if you try to use
654    these SQL features with the data type.
655
656 Note
657
658    In PostgreSQL versions before 7.4, sorting and grouping operations
659    would implicitly use operators named =, <, and >. The new behavior of
660    relying on default operator classes avoids having to make any
661    assumption about the behavior of operators with particular names.
662
663    Sorting by a non-default B-tree operator class is possible by
664    specifying the class's less-than operator in a USING option, for
665    example
666 SELECT * FROM mytable ORDER BY somecol USING ~<~;
667
668    Alternatively, specifying the class's greater-than operator in USING
669    selects a descending-order sort.
670
671    Comparison of arrays of a user-defined type also relies on the
672    semantics defined by the type's default B-tree operator class. If there
673    is no default B-tree operator class, but there is a default hash
674    operator class, then array equality is supported, but not ordering
675    comparisons.
676
677    Another SQL feature that requires even more data-type-specific
678    knowledge is the RANGE offset PRECEDING/FOLLOWING framing option for
679    window functions (see Section 4.2.8). For a query such as
680 SELECT sum(x) OVER (ORDER BY x RANGE BETWEEN 5 PRECEDING AND 10 FOLLOWING)
681   FROM mytable;
682
683    it is not sufficient to know how to order by x; the database must also
684    understand how to “subtract 5” or “add 10” to the current row's value
685    of x to identify the bounds of the current window frame. Comparing the
686    resulting bounds to other rows' values of x is possible using the
687    comparison operators provided by the B-tree operator class that defines
688    the ORDER BY ordering — but addition and subtraction operators are not
689    part of the operator class, so which ones should be used? Hard-wiring
690    that choice would be undesirable, because different sort orders
691    (different B-tree operator classes) might need different behavior.
692    Therefore, a B-tree operator class can specify an in_range support
693    function that encapsulates the addition and subtraction behaviors that
694    make sense for its sort order. It can even provide more than one
695    in_range support function, in case there is more than one data type
696    that makes sense to use as the offset in RANGE clauses. If the B-tree
697    operator class associated with the window's ORDER BY clause does not
698    have a matching in_range support function, the RANGE offset
699    PRECEDING/FOLLOWING option is not supported.
700
701    Another important point is that an equality operator that appears in a
702    hash operator family is a candidate for hash joins, hash aggregation,
703    and related optimizations. The hash operator family is essential here
704    since it identifies the hash function(s) to use.
705
706 36.16.7. Ordering Operators #
707
708    Some index access methods (currently, only GiST and SP-GiST) support
709    the concept of ordering operators. What we have been discussing so far
710    are search operators. A search operator is one for which the index can
711    be searched to find all rows satisfying WHERE indexed_column operator
712    constant. Note that nothing is promised about the order in which the
713    matching rows will be returned. In contrast, an ordering operator does
714    not restrict the set of rows that can be returned, but instead
715    determines their order. An ordering operator is one for which the index
716    can be scanned to return rows in the order represented by ORDER BY
717    indexed_column operator constant. The reason for defining ordering
718    operators that way is that it supports nearest-neighbor searches, if
719    the operator is one that measures distance. For example, a query like
720 SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
721
722
723    finds the ten places closest to a given target point. A GiST index on
724    the location column can do this efficiently because <-> is an ordering
725    operator.
726
727    While search operators have to return Boolean results, ordering
728    operators usually return some other type, such as float or numeric for
729    distances. This type is normally not the same as the data type being
730    indexed. To avoid hard-wiring assumptions about the behavior of
731    different data types, the definition of an ordering operator is
732    required to name a B-tree operator family that specifies the sort
733    ordering of the result data type. As was stated in the previous
734    section, B-tree operator families define PostgreSQL's notion of
735    ordering, so this is a natural representation. Since the point <->
736    operator returns float8, it could be specified in an operator class
737    creation command like this:
738 OPERATOR 15    <-> (point, point) FOR ORDER BY float_ops
739
740
741    where float_ops is the built-in operator family that includes
742    operations on float8. This declaration states that the index is able to
743    return rows in order of increasing values of the <-> operator.
744
745 36.16.8. Special Features of Operator Classes #
746
747    There are two special features of operator classes that we have not
748    discussed yet, mainly because they are not useful with the most
749    commonly used index methods.
750
751    Normally, declaring an operator as a member of an operator class (or
752    family) means that the index method can retrieve exactly the set of
753    rows that satisfy a WHERE condition using the operator. For example:
754 SELECT * FROM table WHERE integer_column < 4;
755
756    can be satisfied exactly by a B-tree index on the integer column. But
757    there are cases where an index is useful as an inexact guide to the
758    matching rows. For example, if a GiST index stores only bounding boxes
759    for geometric objects, then it cannot exactly satisfy a WHERE condition
760    that tests overlap between nonrectangular objects such as polygons. Yet
761    we could use the index to find objects whose bounding box overlaps the
762    bounding box of the target object, and then do the exact overlap test
763    only on the objects found by the index. If this scenario applies, the
764    index is said to be “lossy” for the operator. Lossy index searches are
765    implemented by having the index method return a recheck flag when a row
766    might or might not really satisfy the query condition. The core system
767    will then test the original query condition on the retrieved row to see
768    whether it should be returned as a valid match. This approach works if
769    the index is guaranteed to return all the required rows, plus perhaps
770    some additional rows, which can be eliminated by performing the
771    original operator invocation. The index methods that support lossy
772    searches (currently, GiST, SP-GiST and GIN) allow the support functions
773    of individual operator classes to set the recheck flag, and so this is
774    essentially an operator-class feature.
775
776    Consider again the situation where we are storing in the index only the
777    bounding box of a complex object such as a polygon. In this case
778    there's not much value in storing the whole polygon in the index entry
779    — we might as well store just a simpler object of type box. This
780    situation is expressed by the STORAGE option in CREATE OPERATOR CLASS:
781    we'd write something like:
782 CREATE OPERATOR CLASS polygon_ops
783     DEFAULT FOR TYPE polygon USING gist AS
784         ...
785         STORAGE box;
786
787    At present, only the GiST, SP-GiST, GIN and BRIN index methods support
788    a STORAGE type that's different from the column data type. The GiST
789    compress and decompress support routines must deal with data-type
790    conversion when STORAGE is used. SP-GiST likewise requires a compress
791    support function to convert to the storage type, when that is
792    different; if an SP-GiST opclass also supports retrieving data, the
793    reverse conversion must be handled by the consistent function. In GIN,
794    the STORAGE type identifies the type of the “key” values, which
795    normally is different from the type of the indexed column — for
796    example, an operator class for integer-array columns might have keys
797    that are just integers. The GIN extractValue and extractQuery support
798    routines are responsible for extracting keys from indexed values. BRIN
799    is similar to GIN: the STORAGE type identifies the type of the stored
800    summary values, and operator classes' support procedures are
801    responsible for interpreting the summary values correctly.