]> granicus.if.org Git - postgresql/blob - doc/src/sgml/xindex.sgml
Editorial improvements for GIN documentation.
[postgresql] / doc / src / sgml / xindex.sgml
1 <!-- $PostgreSQL: pgsql/doc/src/sgml/xindex.sgml,v 1.53 2006/12/01 23:46:46 tgl Exp $ -->
2
3 <sect1 id="xindex">
4  <title>Interfacing Extensions To Indexes</title>
5
6  <indexterm zone="xindex">
7   <primary>index</primary>
8   <secondary>for user-defined data type</secondary>
9  </indexterm>
10
11   <para>
12    The procedures described thus far let you define new types, new
13    functions, and new operators. However, we cannot yet define an
14    index on a column of a new data type.  To do this, we must define an
15    <firstterm>operator class</> for the new data type.  Later in this
16    section, we will illustrate this concept in an example: a new
17    operator class for the B-tree index method that stores and sorts
18    complex numbers in ascending absolute value order.
19   </para>
20
21   <note>
22    <para>
23     Prior to <productname>PostgreSQL</productname> release 7.3, it was
24     necessary to make manual additions to the system catalogs
25     <classname>pg_amop</>, <classname>pg_amproc</>, and
26     <classname>pg_opclass</> in order to create a user-defined
27     operator class.  That approach is now deprecated in favor of using
28     <xref linkend="sql-createopclass" endterm="sql-createopclass-title">,
29     which is a much simpler and less error-prone way of creating the
30     necessary catalog entries.
31    </para>
32   </note>
33
34  <sect2 id="xindex-im">
35   <title>Index Methods and Operator Classes</title>
36
37   <para>
38    The <classname>pg_am</classname> table contains one row for every
39    index method (internally known as access method).  Support for
40    regular access to tables is built into
41    <productname>PostgreSQL</productname>, but all index methods are
42    described in <classname>pg_am</classname>.  It is possible to add a
43    new index method by defining the required interface routines and
44    then creating a row in <classname>pg_am</classname> &mdash; but that is
45    beyond the scope of this chapter (see <xref linkend="indexam">).
46   </para>
47
48   <para>
49    The routines for an index method do not directly know anything
50    about the data types that the index method will operate on.
51    Instead, an <firstterm>operator
52    class</><indexterm><primary>operator class</></indexterm>
53    identifies the set of operations that the index method needs to use
54    to work with a particular data type.  Operator classes are so
55    called because one thing they specify is the set of
56    <literal>WHERE</>-clause operators that can be used with an index
57    (i.e., can be converted into an index-scan qualification).  An
58    operator class may also specify some <firstterm>support
59    procedures</> that are needed by the internal operations of the
60    index method, but do not directly correspond to any
61    <literal>WHERE</>-clause operator that can be used with the index.
62   </para>
63
64   <para>
65    It is possible to define multiple operator classes for the same
66    data type and index method.  By doing this, multiple
67    sets of indexing semantics can be defined for a single data type.
68    For example, a B-tree index requires a sort ordering to be defined
69    for each data type it works on.
70    It might be useful for a complex-number data type
71    to have one B-tree operator class that sorts the data by complex
72    absolute value, another that sorts by real part, and so on.
73    Typically, one of the operator classes will be deemed most commonly
74    useful and will be marked as the default operator class for that
75    data type and index method.
76   </para>
77
78   <para>
79    The same operator class name
80    can be used for several different index methods (for example, both B-tree
81    and hash index methods have operator classes named
82    <literal>int4_ops</literal>), but each such class is an independent
83    entity and must be defined separately.
84   </para>
85  </sect2>
86
87  <sect2 id="xindex-strategies">
88   <title>Index Method Strategies</title>
89
90   <para>
91    The operators associated with an operator class are identified by
92    <quote>strategy numbers</>, which serve to identify the semantics of
93    each operator within the context of its operator class.
94    For example, B-trees impose a strict ordering on keys, lesser to greater,
95    and so operators like <quote>less than</> and <quote>greater than or equal
96    to</> are interesting with respect to a B-tree.
97    Because
98    <productname>PostgreSQL</productname> allows the user to define operators,
99    <productname>PostgreSQL</productname> cannot look at the name of an operator
100    (e.g., <literal>&lt;</> or <literal>&gt;=</>) and tell what kind of
101    comparison it is.  Instead, the index method defines a set of
102    <quote>strategies</>, which can be thought of as generalized operators.
103    Each operator class specifies which actual operator corresponds to each
104    strategy for a particular data type and interpretation of the index
105    semantics.
106   </para>
107
108   <para>
109    The B-tree index method defines five strategies, shown in <xref
110    linkend="xindex-btree-strat-table">.
111   </para>
112
113    <table tocentry="1" id="xindex-btree-strat-table">
114     <title>B-tree Strategies</title>
115     <tgroup cols="2">
116      <thead>
117       <row>
118        <entry>Operation</entry>
119        <entry>Strategy Number</entry>
120       </row>
121      </thead>
122      <tbody>
123       <row>
124        <entry>less than</entry>
125        <entry>1</entry>
126       </row>
127       <row>
128        <entry>less than or equal</entry>
129        <entry>2</entry>
130       </row>
131       <row>
132        <entry>equal</entry>
133        <entry>3</entry>
134       </row>
135       <row>
136        <entry>greater than or equal</entry>
137        <entry>4</entry>
138       </row>
139       <row>
140        <entry>greater than</entry>
141        <entry>5</entry>
142       </row>
143      </tbody>
144     </tgroup>
145    </table>
146
147   <para>
148    Hash indexes express only bitwise equality, and so they use only one
149    strategy, shown in <xref linkend="xindex-hash-strat-table">.
150   </para>
151
152    <table tocentry="1" id="xindex-hash-strat-table">
153     <title>Hash Strategies</title>
154     <tgroup cols="2">
155      <thead>
156       <row>
157        <entry>Operation</entry>
158        <entry>Strategy Number</entry>
159       </row>
160      </thead>
161      <tbody>
162       <row>
163        <entry>equal</entry>
164        <entry>1</entry>
165       </row>
166      </tbody>
167     </tgroup>
168    </table>
169
170   <para>
171    GiST indexes are even more flexible: they do not have a fixed set of
172    strategies at all.  Instead, the <quote>consistency</> support routine
173    of each particular GiST operator class interprets the strategy numbers
174    however it likes.  As an example, several of the built-in GiST index
175    operator classes index two-dimensional geometric objects, providing
176    the <quote>R-tree</> strategies shown in
177    <xref linkend="xindex-rtree-strat-table">.  Four of these are true
178    two-dimensional tests (overlaps, same, contains, contained by);
179    four of them consider only the X direction; and the other four
180    provide the same tests in the Y direction.
181   </para>
182
183    <table tocentry="1" id="xindex-rtree-strat-table">
184     <title>GiST Two-Dimensional <quote>R-tree</> Strategies</title>
185     <tgroup cols="2">
186      <thead>
187       <row>
188        <entry>Operation</entry>
189        <entry>Strategy Number</entry>
190       </row>
191      </thead>
192      <tbody>
193       <row>
194        <entry>strictly left of</entry>
195        <entry>1</entry>
196       </row>
197       <row>
198        <entry>does not extend to right of</entry>
199        <entry>2</entry>
200       </row>
201       <row>
202        <entry>overlaps</entry>
203        <entry>3</entry>
204       </row>
205       <row>
206        <entry>does not extend to left of</entry>
207        <entry>4</entry>
208       </row>
209       <row>
210        <entry>strictly right of</entry>
211        <entry>5</entry>
212       </row>
213       <row>
214        <entry>same</entry>
215        <entry>6</entry>
216       </row>
217       <row>
218        <entry>contains</entry>
219        <entry>7</entry>
220       </row>
221       <row>
222        <entry>contained by</entry>
223        <entry>8</entry>
224       </row>
225       <row>
226        <entry>does not extend above</entry>
227        <entry>9</entry>
228       </row>
229       <row>
230        <entry>strictly below</entry>
231        <entry>10</entry>
232       </row>
233       <row>
234        <entry>strictly above</entry>
235        <entry>11</entry>
236       </row>
237       <row>
238        <entry>does not extend below</entry>
239        <entry>12</entry>
240       </row>
241      </tbody>
242     </tgroup>
243    </table>
244
245   <para>
246    GIN indexes are similar to GiST indexes in flexibility: they don't have a
247    fixed set of strategies. Instead the support routines of each operator
248    class interpret the strategy numbers according to the operator class's
249    definition. As an example, the strategy numbers used by the built-in
250    operator classes for arrays are
251    shown in <xref linkend="xindex-gin-array-strat-table">.
252   </para>
253
254    <table tocentry="1" id="xindex-gin-array-strat-table">
255     <title>GIN Array Strategies</title>
256     <tgroup cols="2">
257      <thead>
258       <row>
259        <entry>Operation</entry>
260        <entry>Strategy Number</entry>
261       </row>
262      </thead>
263      <tbody>
264       <row>
265        <entry>overlap</entry>
266        <entry>1</entry>
267       </row>
268       <row>
269        <entry>contains</entry>
270        <entry>2</entry>
271       </row>
272       <row>
273        <entry>is contained by</entry>
274        <entry>3</entry>
275       </row>
276       <row>
277        <entry>equal</entry>
278        <entry>4</entry>
279       </row>
280      </tbody>
281     </tgroup>
282    </table>
283
284   <para>
285    Note that all strategy operators return Boolean values.  In
286    practice, all operators defined as index method strategies must
287    return type <type>boolean</type>, since they must appear at the top
288    level of a <literal>WHERE</> clause to be used with an index.
289   </para>
290
291   <para>
292    By the way, the <structfield>amorderstrategy</structfield> column
293    in <classname>pg_am</> tells whether
294    the index method supports ordered scans.  Zero means it doesn't; if it
295    does, <structfield>amorderstrategy</structfield> is the strategy
296    number that corresponds to the ordering operator.  For example, B-tree
297    has <structfield>amorderstrategy</structfield> = 1, which is its
298    <quote>less than</quote> strategy number.
299   </para>
300  </sect2>
301
302  <sect2 id="xindex-support">
303   <title>Index Method Support Routines</title>
304
305   <para>
306    Strategies aren't usually enough information for the system to figure
307    out how to use an index.  In practice, the index methods require
308    additional support routines in order to work. For example, the B-tree
309    index method must be able to compare two keys and determine whether one
310    is greater than, equal to, or less than the other.  Similarly, the
311    hash index method must be able to compute hash codes for key values.
312    These operations do not correspond to operators used in qualifications in
313    SQL commands;  they are administrative routines used by
314    the index methods, internally.
315   </para>
316
317   <para>
318    Just as with strategies, the operator class identifies which specific
319    functions should play each of these roles for a given data type and
320    semantic interpretation.  The index method defines the set
321    of functions it needs, and the operator class identifies the correct
322    functions to use by assigning them to the <quote>support function numbers</>.
323   </para>
324
325   <para>
326    B-trees require a single support function, shown in <xref
327    linkend="xindex-btree-support-table">.
328   </para>
329
330    <table tocentry="1" id="xindex-btree-support-table">
331     <title>B-tree Support Functions</title>
332     <tgroup cols="2">
333      <thead>
334       <row>
335        <entry>Function</entry>
336        <entry>Support Number</entry>
337       </row>
338      </thead>
339      <tbody>
340       <row>
341        <entry>
342    Compare two keys and return an integer less than zero, zero, or
343    greater than zero, indicating whether the first key is less than, equal to,
344    or greater than the second.
345        </entry>
346        <entry>1</entry>
347       </row>
348      </tbody>
349     </tgroup>
350    </table>
351
352   <para>
353    Hash indexes likewise require one support function, shown in <xref
354    linkend="xindex-hash-support-table">.
355   </para>
356
357    <table tocentry="1" id="xindex-hash-support-table">
358     <title>Hash Support Functions</title>
359     <tgroup cols="2">
360      <thead>
361       <row>
362        <entry>Function</entry>
363        <entry>Support Number</entry>
364       </row>
365      </thead>
366      <tbody>
367       <row>
368        <entry>Compute the hash value for a key</entry>
369        <entry>1</entry>
370       </row>
371      </tbody>
372     </tgroup>
373    </table>
374
375   <para>
376    GiST indexes require seven support functions,
377    shown in <xref linkend="xindex-gist-support-table">.
378   </para>
379
380    <table tocentry="1" id="xindex-gist-support-table">
381     <title>GiST Support Functions</title>
382     <tgroup cols="2">
383      <thead>
384       <row>
385        <entry>Function</entry>
386        <entry>Support Number</entry>
387       </row>
388      </thead>
389      <tbody>
390       <row>
391        <entry>consistent - determine whether key satisfies the 
392         query qualifier</entry>
393        <entry>1</entry>
394       </row>
395       <row>
396        <entry>union - compute union of a set of keys</entry>
397        <entry>2</entry>
398       </row>
399       <row>
400        <entry>compress - compute a compressed representation of a key or value
401         to be indexed</entry>
402        <entry>3</entry>
403       </row>
404       <row>
405        <entry>decompress - compute a decompressed representation of a 
406         compressed key</entry>
407        <entry>4</entry>
408       </row>
409       <row>
410        <entry>penalty - compute penalty for inserting new key into subtree 
411        with given subtree's key</entry>
412        <entry>5</entry>
413       </row>
414       <row>
415        <entry>picksplit - determine which entries of a page are to be moved
416        to the new page and compute the union keys for resulting pages</entry>
417        <entry>6</entry>
418       </row>
419       <row>
420        <entry>equal - compare two keys and return true if they are equal</entry>
421        <entry>7</entry>
422       </row>
423      </tbody>
424     </tgroup>
425    </table>
426
427   <para>
428    GIN indexes require four support functions,
429    shown in <xref linkend="xindex-gin-support-table">.
430   </para>
431
432    <table tocentry="1" id="xindex-gin-support-table">
433     <title>GIN Support Functions</title>
434     <tgroup cols="2">
435      <thead>
436       <row>
437        <entry>Function</entry>
438        <entry>Support Number</entry>
439       </row>
440      </thead>
441      <tbody>
442       <row>
443        <entry>
444         compare - compare two keys and return an integer less than zero, zero,
445         or greater than zero, indicating whether the first key is less than,
446         equal to, or greater than the second
447        </entry>
448        <entry>1</entry>
449       </row>
450       <row>
451        <entry>extractValue - extract keys from a value to be indexed</entry>
452        <entry>2</entry>
453       </row>
454       <row>
455        <entry>extractQuery - extract keys from a query condition</entry>
456        <entry>3</entry>
457       </row>
458       <row>
459        <entry>consistent - determine whether value matches query condition</entry>
460        <entry>4</entry>
461       </row>
462      </tbody>
463     </tgroup>
464    </table>
465
466   <para>
467    Unlike strategy operators, support functions return whichever data
468    type the particular index method expects; for example in the case
469    of the comparison function for B-trees, a signed integer.
470   </para>
471  </sect2>
472
473  <sect2 id="xindex-example">
474   <title>An Example</title>
475
476   <para>
477    Now that we have seen the ideas, here is the promised example of
478    creating a new operator class.
479    (You can find a working copy of this example in
480    <filename>src/tutorial/complex.c</filename> and
481    <filename>src/tutorial/complex.sql</filename> in the source
482    distribution.)
483    The operator class encapsulates
484    operators that sort complex numbers in absolute value order, so we
485    choose the name <literal>complex_abs_ops</literal>.  First, we need
486    a set of operators.  The procedure for defining operators was
487    discussed in <xref linkend="xoper">.  For an operator class on
488    B-trees, the operators we require are:
489
490    <itemizedlist spacing="compact">
491     <listitem><simpara>absolute-value less-than (strategy 1)</></>
492     <listitem><simpara>absolute-value less-than-or-equal (strategy 2)</></>
493     <listitem><simpara>absolute-value equal (strategy 3)</></>
494     <listitem><simpara>absolute-value greater-than-or-equal (strategy 4)</></>
495     <listitem><simpara>absolute-value greater-than (strategy 5)</></>
496    </itemizedlist>
497   </para>
498
499   <para>
500    The least error-prone way to define a related set of comparison operators
501    is to write the B-tree comparison support function first, and then write the
502    other functions as one-line wrappers around the support function.  This
503    reduces the odds of getting inconsistent results for corner cases.
504    Following this approach, we first write
505
506 <programlisting>
507 #define Mag(c)  ((c)-&gt;x*(c)-&gt;x + (c)-&gt;y*(c)-&gt;y)
508
509 static int
510 complex_abs_cmp_internal(Complex *a, Complex *b)
511 {
512     double      amag = Mag(a),
513                 bmag = Mag(b);
514
515     if (amag &lt; bmag)
516         return -1;
517     if (amag &gt; bmag)
518         return 1;
519     return 0;
520 }
521 </programlisting>
522
523    Now the less-than function looks like
524
525 <programlisting>
526 PG_FUNCTION_INFO_V1(complex_abs_lt);
527
528 Datum
529 complex_abs_lt(PG_FUNCTION_ARGS)
530 {
531     Complex    *a = (Complex *) PG_GETARG_POINTER(0);
532     Complex    *b = (Complex *) PG_GETARG_POINTER(1);
533
534     PG_RETURN_BOOL(complex_abs_cmp_internal(a, b) &lt; 0);
535 }
536 </programlisting>
537
538    The other four functions differ only in how they compare the internal
539    function's result to zero.
540   </para>
541
542   <para>
543    Next we declare the functions and the operators based on the functions
544    to SQL:
545
546 <programlisting>
547 CREATE FUNCTION complex_abs_lt(complex, complex) RETURNS bool
548     AS '<replaceable>filename</replaceable>', 'complex_abs_lt'
549     LANGUAGE C IMMUTABLE STRICT;
550
551 CREATE OPERATOR &lt; (
552    leftarg = complex, rightarg = complex, procedure = complex_abs_lt,
553    commutator = &gt; , negator = &gt;= ,
554    restrict = scalarltsel, join = scalarltjoinsel
555 );
556 </programlisting>
557    It is important to specify the correct commutator and negator operators,
558    as well as suitable restriction and join selectivity
559    functions, otherwise the optimizer will be unable to make effective
560    use of the index.  Note that the less-than, equal, and
561    greater-than cases should use different selectivity functions.
562   </para>
563
564   <para>
565    Other things worth noting are happening here:
566
567   <itemizedlist>
568    <listitem>
569     <para>
570      There can only be one operator named, say, <literal>=</literal>
571      and taking type <type>complex</type> for both operands.  In this
572      case we don't have any other operator <literal>=</literal> for
573      <type>complex</type>, but if we were building a practical data
574      type we'd probably want <literal>=</literal> to be the ordinary
575      equality operation for complex numbers (and not the equality of
576      the absolute values).  In that case, we'd need to use some other
577      operator name for <function>complex_abs_eq</>.
578     </para>
579    </listitem>
580
581    <listitem>
582     <para>
583      Although <productname>PostgreSQL</productname> can cope with
584      functions having the same SQL name as long as they have different
585      argument data types, C can only cope with one global function
586      having a given name.  So we shouldn't name the C function
587      something simple like <filename>abs_eq</filename>.  Usually it's
588      a good practice to include the data type name in the C function
589      name, so as not to conflict with functions for other data types.
590     </para>
591    </listitem>
592
593    <listitem>
594     <para>
595      We could have made the SQL name
596      of the function <filename>abs_eq</filename>, relying on
597      <productname>PostgreSQL</productname> to distinguish it by
598      argument data types from any other SQL function of the same name.
599      To keep the example simple, we make the function have the same
600      names at the C level and SQL level.
601     </para>
602    </listitem>
603   </itemizedlist>
604   </para>
605
606   <para>
607    The next step is the registration of the support routine required
608    by B-trees.  The example C code that implements this is in the same
609    file that contains the operator functions.  This is how we declare
610    the function:
611
612 <programlisting>
613 CREATE FUNCTION complex_abs_cmp(complex, complex)
614     RETURNS integer
615     AS '<replaceable>filename</replaceable>'
616     LANGUAGE C IMMUTABLE STRICT;
617 </programlisting>
618   </para>
619
620   <para>
621    Now that we have the required operators and support routine,
622    we can finally create the operator class:
623
624 <programlisting>
625 CREATE OPERATOR CLASS complex_abs_ops
626     DEFAULT FOR TYPE complex USING btree AS
627         OPERATOR        1       &lt; ,
628         OPERATOR        2       &lt;= ,
629         OPERATOR        3       = ,
630         OPERATOR        4       &gt;= ,
631         OPERATOR        5       &gt; ,
632         FUNCTION        1       complex_abs_cmp(complex, complex);
633 </programlisting>
634   </para>
635
636   <para>
637    And we're done!  It should now be possible to create
638    and use B-tree indexes on <type>complex</type> columns.
639   </para>
640
641   <para>
642    We could have written the operator entries more verbosely, as in
643 <programlisting>
644         OPERATOR        1       &lt; (complex, complex) ,
645 </programlisting>
646    but there is no need to do so when the operators take the same data type
647    we are defining the operator class for.
648   </para>
649
650   <para>
651    The above example assumes that you want to make this new operator class the
652    default B-tree operator class for the <type>complex</type> data type.
653    If you don't, just leave out the word <literal>DEFAULT</>.
654   </para>
655  </sect2>
656
657  <sect2 id="xindex-opclass-crosstype">
658   <title>Cross-Data-Type Operator Classes</title>
659
660   <para>
661    So far we have implicitly assumed that an operator class deals with
662    only one data type.  While there certainly can be only one data type in
663    a particular index column, it is often useful to index operations that
664    compare an indexed column to a value of a different data type.  This is
665    presently supported by the B-tree and GiST index methods.
666   </para>
667
668   <para>
669    B-trees require the left-hand operand of each operator to be the indexed
670    data type, but the right-hand operand can be of a different type.  There
671    must be a support function having a matching signature.  For example,
672    the built-in operator class for type <type>bigint</> (<type>int8</>)
673    allows cross-type comparisons to <type>int4</> and <type>int2</>.  It
674    could be duplicated by this definition:
675
676 <programlisting>
677 CREATE OPERATOR CLASS int8_ops
678 DEFAULT FOR TYPE int8 USING btree AS
679   -- standard int8 comparisons
680   OPERATOR 1 &lt; ,
681   OPERATOR 2 &lt;= ,
682   OPERATOR 3 = ,
683   OPERATOR 4 &gt;= ,
684   OPERATOR 5 &gt; ,
685   FUNCTION 1 btint8cmp(int8, int8) ,
686
687   -- cross-type comparisons to int2 (smallint)
688   OPERATOR 1 &lt; (int8, int2) ,
689   OPERATOR 2 &lt;= (int8, int2) ,
690   OPERATOR 3 = (int8, int2) ,
691   OPERATOR 4 &gt;= (int8, int2) ,
692   OPERATOR 5 &gt; (int8, int2) ,
693   FUNCTION 1 btint82cmp(int8, int2) ,
694
695   -- cross-type comparisons to int4 (integer)
696   OPERATOR 1 &lt; (int8, int4) ,
697   OPERATOR 2 &lt;= (int8, int4) ,
698   OPERATOR 3 = (int8, int4) ,
699   OPERATOR 4 &gt;= (int8, int4) ,
700   OPERATOR 5 &gt; (int8, int4) ,
701   FUNCTION 1 btint84cmp(int8, int4) ;
702 </programlisting>
703
704    Notice that this definition <quote>overloads</> the operator strategy and
705    support function numbers.  This is allowed (for B-tree operator classes
706    only) so long as each instance of a particular number has a different
707    right-hand data type.  The instances that are not cross-type are the
708    default or primary operators of the operator class.
709   </para>
710
711   <para>
712    GiST indexes do not allow overloading of strategy or support function
713    numbers, but it is still possible to get the effect of supporting
714    multiple right-hand data types, by assigning a distinct strategy number
715    to each operator that needs to be supported.  The <literal>consistent</>
716    support function must determine what it needs to do based on the strategy
717    number, and must be prepared to accept comparison values of the appropriate
718    data types.
719   </para>
720  </sect2>
721
722  <sect2 id="xindex-opclass-dependencies">
723   <title>System Dependencies on Operator Classes</title>
724
725    <indexterm>
726     <primary>ordering operator</primary>
727    </indexterm>
728
729   <para>
730    <productname>PostgreSQL</productname> uses operator classes to infer the
731    properties of operators in more ways than just whether they can be used
732    with indexes.  Therefore, you might want to create operator classes
733    even if you have no intention of indexing any columns of your data type.
734   </para>
735
736   <para>
737    In particular, there are SQL features such as <literal>ORDER BY</> and
738    <literal>DISTINCT</> that require comparison and sorting of values.
739    To implement these features on a user-defined data type,
740    <productname>PostgreSQL</productname> looks for the default B-tree operator
741    class for the data type.  The <quote>equals</> member of this operator
742    class defines the system's notion of equality of values for
743    <literal>GROUP BY</> and <literal>DISTINCT</>, and the sort ordering
744    imposed by the operator class defines the default <literal>ORDER BY</>
745    ordering.
746   </para>
747
748   <para>
749    Comparison of arrays of user-defined types also relies on the semantics
750    defined by the default B-tree operator class.
751   </para>
752
753   <para>
754    If there is no default B-tree operator class for a data type, the system
755    will look for a default hash operator class.  But since that kind of
756    operator class only provides equality, in practice it is only enough
757    to support array equality.
758   </para>
759
760   <para>
761    When there is no default operator class for a data type, you will get
762    errors like <quote>could not identify an ordering operator</> if you
763    try to use these SQL features with the data type.
764   </para>
765
766    <note>
767     <para>
768      In <productname>PostgreSQL</productname> versions before 7.4,
769      sorting and grouping operations would implicitly use operators named
770      <literal>=</>, <literal>&lt;</>, and <literal>&gt;</>.  The new
771      behavior of relying on default operator classes avoids having to make
772      any assumption about the behavior of operators with particular names.
773     </para>
774    </note>
775  </sect2>
776
777  <sect2 id="xindex-opclass-features">
778   <title>Special Features of Operator Classes</title>
779
780   <para>
781    There are two special features of operator classes that we have
782    not discussed yet, mainly because they are not useful
783    with the most commonly used index methods.
784   </para>
785
786   <para>
787    Normally, declaring an operator as a member of an operator class means
788    that the index method can retrieve exactly the set of rows
789    that satisfy a <literal>WHERE</> condition using the operator.  For example,
790 <programlisting>
791 SELECT * FROM table WHERE integer_column &lt; 4;
792 </programlisting>
793    can be satisfied exactly by a B-tree index on the integer column.
794    But there are cases where an index is useful as an inexact guide to
795    the matching rows.  For example, if a GiST index stores only
796    bounding boxes for objects, then it cannot exactly satisfy a <literal>WHERE</>
797    condition that tests overlap between nonrectangular objects such as
798    polygons.  Yet we could use the index to find objects whose bounding
799    box overlaps the bounding box of the target object, and then do the
800    exact overlap test only on the objects found by the index.  If this
801    scenario applies, the index is said to be <quote>lossy</> for the
802    operator, and we add <literal>RECHECK</> to the <literal>OPERATOR</> clause
803    in the <command>CREATE OPERATOR CLASS</> command.
804    <literal>RECHECK</> is valid if the index is guaranteed to return
805    all the required rows, plus perhaps some additional rows, which
806    can be eliminated by performing the original operator invocation.
807   </para>
808
809   <para>
810    Consider again the situation where we are storing in the index only
811    the bounding box of a complex object such as a polygon.  In this
812    case there's not much value in storing the whole polygon in the index
813    entry &mdash; we may as well store just a simpler object of type
814    <type>box</>.  This situation is expressed by the <literal>STORAGE</>
815    option in <command>CREATE OPERATOR CLASS</>: we'd write something like
816
817 <programlisting>
818 CREATE OPERATOR CLASS polygon_ops
819     DEFAULT FOR TYPE polygon USING gist AS
820         ...
821         STORAGE box;
822 </programlisting>
823
824    At present, only the GiST and GIN index methods support a
825    <literal>STORAGE</> type that's different from the column data type.
826    The GiST <function>compress</> and <function>decompress</> support
827    routines must deal with data-type conversion when <literal>STORAGE</>
828    is used.  In GIN, the <literal>STORAGE</> type identifies the type of
829    the <quote>key</> values, which normally is different from the type
830    of the indexed column &mdash; for example, an operator class for
831    integer array columns might have keys that are just integers.  The
832    GIN <function>extractValue</> and <function>extractQuery</> support
833    routines are responsible for extracting keys from indexed values.
834   </para>
835  </sect2>
836
837 </sect1>