1999-07-22 17:11:05 +02:00
|
|
|
<!--
|
2003-04-13 11:57:35 +02:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/xindex.sgml,v 1.30 2003/04/13 09:57:35 petere Exp $
|
1999-07-22 17:11:05 +02:00
|
|
|
-->
|
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
<sect1 id="xindex">
|
2002-01-07 03:29:15 +01:00
|
|
|
<title>Interfacing Extensions To Indexes</title>
|
|
|
|
|
1999-05-27 17:44:54 +02:00
|
|
|
<para>
|
2002-01-07 03:29:15 +01:00
|
|
|
The procedures described thus far let you define new types, new
|
2003-04-13 11:57:35 +02:00
|
|
|
functions, and new operators. However, we cannot yet define an
|
|
|
|
index on a column of a new data type. To do this, we must define an
|
|
|
|
<firstterm>operator class</> for the new data type. Later in this
|
|
|
|
section, we will illustrate this concept in an example: a new
|
|
|
|
operator class for the B-tree index method that stores and sorts
|
|
|
|
complex numbers in ascending absolute value order.
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
2002-07-30 07:24:56 +02:00
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
Prior to <productname>PostgreSQL</productname> release 7.3, it was
|
2003-04-13 11:57:35 +02:00
|
|
|
necessary to make manual additions to the system catalogs
|
2002-07-30 07:24:56 +02:00
|
|
|
<classname>pg_amop</>, <classname>pg_amproc</>, and
|
|
|
|
<classname>pg_opclass</> in order to create a user-defined
|
|
|
|
operator class. That approach is now deprecated in favor of
|
|
|
|
using <command>CREATE OPERATOR CLASS</>, which is a much simpler
|
|
|
|
and less error-prone way of creating the necessary catalog entries.
|
|
|
|
</para>
|
|
|
|
</note>
|
2002-01-07 03:29:15 +01:00
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
<sect2 id="xindex-im">
|
|
|
|
<title>Index Methods and Operator Classes</title>
|
2002-07-30 07:24:56 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
The <classname>pg_am</classname> table contains one row for every
|
2003-04-13 11:57:35 +02:00
|
|
|
index method (internally known as access method). Support for
|
|
|
|
regular access to tables is built into
|
|
|
|
<productname>PostgreSQL</productname>, but all index methods are
|
|
|
|
described in <classname>pg_am</classname>. It is possible to add a
|
|
|
|
new index method by defining the required interface routines and
|
|
|
|
then creating a row in <classname>pg_am</classname> --- but that is
|
|
|
|
far beyond the scope of this chapter.
|
2002-07-30 07:24:56 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
The routines for an index method do not directly know anything
|
|
|
|
about the data types that the index method will operate on. Instead, an
|
2002-07-30 07:24:56 +02:00
|
|
|
<firstterm>operator class</> identifies the set of operations that the
|
2003-04-13 11:57:35 +02:00
|
|
|
index method needs to use to work with a particular data type.
|
2002-07-30 07:24:56 +02:00
|
|
|
Operator classes are so called because one thing they specify is the set
|
2003-04-13 11:57:35 +02:00
|
|
|
of <literal>WHERE</>-clause operators that can be used with an index (i.e., can be
|
|
|
|
converted into an index-scan qualification). An operator class may also
|
2002-07-30 07:24:56 +02:00
|
|
|
specify some <firstterm>support procedures</> that are needed by the
|
2003-04-13 11:57:35 +02:00
|
|
|
internal operations of the index method, but do not directly
|
|
|
|
correspond to any <literal>WHERE</>-clause operator that can be used with the index.
|
2002-07-30 07:24:56 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
It is possible to define multiple operator classes for the same
|
2003-04-13 11:57:35 +02:00
|
|
|
data type and index method. By doing this, multiple
|
2002-09-21 20:32:54 +02:00
|
|
|
sets of indexing semantics can be defined for a single data type.
|
2002-07-30 07:24:56 +02:00
|
|
|
For example, a B-tree index requires a sort ordering to be defined
|
2002-09-21 20:32:54 +02:00
|
|
|
for each data type it works on.
|
|
|
|
It might be useful for a complex-number data type
|
2002-07-30 07:24:56 +02:00
|
|
|
to have one B-tree operator class that sorts the data by complex
|
|
|
|
absolute value, another that sorts by real part, and so on.
|
2003-04-13 11:57:35 +02:00
|
|
|
Typically, one of the operator classes will be deemed most commonly
|
2002-07-30 07:24:56 +02:00
|
|
|
useful and will be marked as the default operator class for that
|
2003-04-13 11:57:35 +02:00
|
|
|
data type and index method.
|
2002-07-30 07:24:56 +02:00
|
|
|
</para>
|
1999-05-27 17:44:54 +02:00
|
|
|
|
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
The same operator class name
|
2003-04-13 11:57:35 +02:00
|
|
|
can be used for several different index methods (for example, both B-tree
|
|
|
|
and hash index methods have operator classes named
|
2002-07-30 07:24:56 +02:00
|
|
|
<literal>oid_ops</literal>), but each such class is an independent
|
|
|
|
entity and must be defined separately.
|
|
|
|
</para>
|
2003-04-13 11:57:35 +02:00
|
|
|
</sect2>
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
<sect2 id="xindex-strategies">
|
|
|
|
<title>Index Method Strategies</title>
|
2002-07-30 07:24:56 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
The operators associated with an operator class are identified by
|
|
|
|
<quote>strategy numbers</>, which serve to identify the semantics of
|
|
|
|
each operator within the context of its operator class.
|
|
|
|
For example, B-trees impose a strict ordering on keys, lesser to greater,
|
|
|
|
and so operators like <quote>less than</> and <quote>greater than or equal
|
|
|
|
to</> are interesting with respect to a B-tree.
|
|
|
|
Because
|
|
|
|
<productname>PostgreSQL</productname> allows the user to define operators,
|
|
|
|
<productname>PostgreSQL</productname> cannot look at the name of an operator
|
2002-07-30 19:34:37 +02:00
|
|
|
(e.g., <literal><</> or <literal>>=</>) and tell what kind of
|
2003-04-13 11:57:35 +02:00
|
|
|
comparison it is. Instead, the index method defines a set of
|
2002-07-30 07:24:56 +02:00
|
|
|
<quote>strategies</>, which can be thought of as generalized operators.
|
2003-04-13 11:57:35 +02:00
|
|
|
Each operator class specifies which actual operator corresponds to each
|
2002-09-21 20:32:54 +02:00
|
|
|
strategy for a particular data type and interpretation of the index
|
2002-07-30 07:24:56 +02:00
|
|
|
semantics.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
The B-tree index method defines five strategies, shown in <xref
|
2002-07-30 07:24:56 +02:00
|
|
|
linkend="xindex-btree-strat-table">.
|
|
|
|
</para>
|
2001-05-17 23:50:18 +02:00
|
|
|
|
2002-07-30 07:24:56 +02:00
|
|
|
<table tocentry="1" id="xindex-btree-strat-table">
|
|
|
|
<title>B-tree Strategies</title>
|
1999-05-27 17:44:54 +02:00
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>Operation</entry>
|
|
|
|
<entry>Strategy Number</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>less than</entry>
|
|
|
|
<entry>1</entry>
|
2001-08-21 18:36:06 +02:00
|
|
|
</row>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>less than or equal</entry>
|
|
|
|
<entry>2</entry>
|
2001-08-21 18:36:06 +02:00
|
|
|
</row>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>equal</entry>
|
|
|
|
<entry>3</entry>
|
2001-08-21 18:36:06 +02:00
|
|
|
</row>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>greater than or equal</entry>
|
|
|
|
<entry>4</entry>
|
2001-08-21 18:36:06 +02:00
|
|
|
</row>
|
1999-05-27 17:44:54 +02:00
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>greater than</entry>
|
|
|
|
<entry>5</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
</row>
|
2002-07-30 07:24:56 +02:00
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
Hash indexes express only bitwise equality, and so they use only one
|
|
|
|
strategy, shown in <xref linkend="xindex-hash-strat-table">.
|
2002-07-30 07:24:56 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<table tocentry="1" id="xindex-hash-strat-table">
|
|
|
|
<title>Hash Strategies</title>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
1999-05-27 17:44:54 +02:00
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>Operation</entry>
|
|
|
|
<entry>Strategy Number</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
</row>
|
2002-07-30 07:24:56 +02:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
1999-05-27 17:44:54 +02:00
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>equal</entry>
|
|
|
|
<entry>1</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
2002-05-29 19:36:40 +02:00
|
|
|
|
1999-05-27 17:44:54 +02:00
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
R-tree indexes express rectangle-containment relationships.
|
2003-04-13 11:57:35 +02:00
|
|
|
They use eight strategies, shown in <xref linkend="xindex-rtree-strat-table">.
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
|
|
|
|
2002-07-30 07:24:56 +02:00
|
|
|
<table tocentry="1" id="xindex-rtree-strat-table">
|
|
|
|
<title>R-tree Strategies</title>
|
1999-05-27 17:44:54 +02:00
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>Operation</entry>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>Strategy Number</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>left of</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
<entry>1</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>left of or overlapping</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
<entry>2</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>overlapping</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
<entry>3</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>right of or overlapping</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
<entry>4</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>right of</entry>
|
1999-05-27 17:44:54 +02:00
|
|
|
<entry>5</entry>
|
|
|
|
</row>
|
2002-07-30 07:24:56 +02:00
|
|
|
<row>
|
|
|
|
<entry>same</entry>
|
|
|
|
<entry>6</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>contains</entry>
|
|
|
|
<entry>7</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>contained by</entry>
|
|
|
|
<entry>8</entry>
|
|
|
|
</row>
|
1999-05-27 17:44:54 +02:00
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
GiST indexes are even more flexible: they do not have a fixed set of
|
|
|
|
strategies at all. Instead, the <quote>consistency</> support routine
|
2003-04-13 11:57:35 +02:00
|
|
|
of each particular GiST operator class interprets the strategy numbers
|
2002-07-30 07:24:56 +02:00
|
|
|
however it likes.
|
|
|
|
</para>
|
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
<para>
|
|
|
|
Note that all strategy operators return Boolean values. In
|
|
|
|
practice, all operators defined as index method strategies must
|
|
|
|
return type <type>boolean</type>, since they must appear at the top
|
|
|
|
level of a <literal>WHERE</> clause to be used with an index.
|
|
|
|
</para>
|
|
|
|
|
2002-07-30 07:24:56 +02:00
|
|
|
<para>
|
|
|
|
By the way, the <structfield>amorderstrategy</structfield> column
|
|
|
|
in <classname>pg_am</> tells whether
|
2003-04-13 11:57:35 +02:00
|
|
|
the index method supports ordered scans. Zero means it doesn't; if it
|
2002-07-30 07:24:56 +02:00
|
|
|
does, <structfield>amorderstrategy</structfield> is the strategy
|
|
|
|
number that corresponds to the ordering operator. For example, B-tree
|
|
|
|
has <structfield>amorderstrategy</structfield> = 1, which is its
|
|
|
|
<quote>less than</quote> strategy number.
|
|
|
|
</para>
|
2003-04-13 11:57:35 +02:00
|
|
|
</sect2>
|
2002-07-30 07:24:56 +02:00
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
<sect2 id="xindex-support">
|
|
|
|
<title>Index Method Support Routines</title>
|
1999-05-27 17:44:54 +02:00
|
|
|
|
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
Strategies aren't usually enough information for the system to figure
|
2003-04-13 11:57:35 +02:00
|
|
|
out how to use an index. In practice, the index methods require
|
2002-07-30 07:24:56 +02:00
|
|
|
additional support routines in order to work. For example, the B-tree
|
2003-04-13 11:57:35 +02:00
|
|
|
index method must be able to compare two keys and determine whether one
|
1999-05-27 17:44:54 +02:00
|
|
|
is greater than, equal to, or less than the other. Similarly, the
|
2003-04-13 11:57:35 +02:00
|
|
|
R-tree index method must be able to compute
|
1999-05-27 17:44:54 +02:00
|
|
|
intersections, unions, and sizes of rectangles. These
|
2001-08-21 18:36:06 +02:00
|
|
|
operations do not correspond to operators used in qualifications in
|
2003-04-13 11:57:35 +02:00
|
|
|
SQL commands; they are administrative routines used by
|
|
|
|
the index methods, internally.
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
Just as with strategies, the operator class identifies which specific
|
2002-09-21 20:32:54 +02:00
|
|
|
functions should play each of these roles for a given data type and
|
2003-04-13 11:57:35 +02:00
|
|
|
semantic interpretation. The index method defines the set
|
2002-07-30 07:24:56 +02:00
|
|
|
of functions it needs, and the operator class identifies the correct
|
2003-04-13 11:57:35 +02:00
|
|
|
functions to use by assigning them to the <quote>support function numbers</>.
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
B-trees require a single support function, shown in <xref
|
2002-07-30 07:24:56 +02:00
|
|
|
linkend="xindex-btree-support-table">.
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
|
|
|
|
2002-07-30 07:24:56 +02:00
|
|
|
<table tocentry="1" id="xindex-btree-support-table">
|
|
|
|
<title>B-tree Support Functions</title>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
2002-07-30 19:34:37 +02:00
|
|
|
<entry>Function</entry>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>Support Number</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
|
|
|
<entry>
|
2002-07-30 19:34:37 +02:00
|
|
|
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.
|
2002-07-30 07:24:56 +02:00
|
|
|
</entry>
|
|
|
|
<entry>1</entry>
|
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
|
|
|
|
2000-02-17 04:40:02 +01:00
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
Hash indexes likewise require one support function, shown in <xref
|
2002-07-30 07:24:56 +02:00
|
|
|
linkend="xindex-hash-support-table">.
|
2000-02-17 04:40:02 +01:00
|
|
|
</para>
|
2002-01-07 03:29:15 +01:00
|
|
|
|
2002-07-30 07:24:56 +02:00
|
|
|
<table tocentry="1" id="xindex-hash-support-table">
|
|
|
|
<title>Hash Support Functions</title>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
2002-07-30 19:34:37 +02:00
|
|
|
<entry>Function</entry>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>Support Number</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
2002-07-30 19:34:37 +02:00
|
|
|
<entry>Compute the hash value for a key</entry>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>1</entry>
|
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
2000-02-17 04:40:02 +01:00
|
|
|
|
1999-05-27 17:44:54 +02:00
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
R-tree indexes require three support functions,
|
2003-04-13 11:57:35 +02:00
|
|
|
shown in <xref linkend="xindex-rtree-support-table">.
|
2001-08-21 18:36:06 +02:00
|
|
|
</para>
|
|
|
|
|
2002-07-30 07:24:56 +02:00
|
|
|
<table tocentry="1" id="xindex-rtree-support-table">
|
|
|
|
<title>R-tree Support Functions</title>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
2002-07-30 19:34:37 +02:00
|
|
|
<entry>Function</entry>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>Support Number</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
|
|
|
<entry>union</entry>
|
|
|
|
<entry>1</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>intersection</entry>
|
|
|
|
<entry>2</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>size</entry>
|
|
|
|
<entry>3</entry>
|
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2000-03-28 04:53:02 +02:00
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
GiST indexes require seven support functions,
|
2003-04-13 11:57:35 +02:00
|
|
|
shown in <xref linkend="xindex-gist-support-table">.
|
2000-03-28 04:53:02 +02:00
|
|
|
</para>
|
2002-07-30 07:24:56 +02:00
|
|
|
|
|
|
|
<table tocentry="1" id="xindex-gist-support-table">
|
|
|
|
<title>GiST Support Functions</title>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
2002-07-30 19:34:37 +02:00
|
|
|
<entry>Function</entry>
|
2002-07-30 07:24:56 +02:00
|
|
|
<entry>Support Number</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
|
|
|
<entry>consistent</entry>
|
|
|
|
<entry>1</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>union</entry>
|
|
|
|
<entry>2</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>compress</entry>
|
|
|
|
<entry>3</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>decompress</entry>
|
|
|
|
<entry>4</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>penalty</entry>
|
|
|
|
<entry>5</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>picksplit</entry>
|
|
|
|
<entry>6</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>equal</entry>
|
|
|
|
<entry>7</entry>
|
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
<para>
|
|
|
|
Unlike strategy 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.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
2002-01-07 03:29:15 +01:00
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
<sect2 id="xindex-example">
|
|
|
|
<title>An Example</title>
|
2000-03-28 04:53:02 +02:00
|
|
|
|
1999-05-27 17:44:54 +02:00
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
Now that we have seen the ideas, here is the promised example of
|
|
|
|
creating a new operator class. The operator class encapsulates
|
|
|
|
operators that sort complex numbers in absolute value order, so we
|
|
|
|
choose the name <literal>complex_abs_ops</literal>. First, we need
|
|
|
|
a set of operators. The procedure for defining operators was
|
|
|
|
discussed in <xref linkend="xoper">. For an operator class on
|
|
|
|
B-trees, the operators we require are:
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
<itemizedlist spacing="compact">
|
|
|
|
<listitem><simpara>absolute-value less-than (strategy 1)</></>
|
|
|
|
<listitem><simpara>absolute-value less-than-or-equal (strategy 2)</></>
|
|
|
|
<listitem><simpara>absolute-value equal (strategy 3)</></>
|
|
|
|
<listitem><simpara>absolute-value greater-than-or-equal (strategy 4)</></>
|
|
|
|
<listitem><simpara>absolute-value greater-than (strategy 5)</></>
|
|
|
|
</itemizedlist>
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
The C code for the equality operator look like this:
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
<programlisting>
|
1998-03-01 09:16:16 +01:00
|
|
|
#define Mag(c) ((c)->x*(c)->x + (c)->y*(c)->y)
|
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
bool
|
|
|
|
complex_abs_eq(Complex *a, Complex *b)
|
|
|
|
{
|
|
|
|
double amag = Mag(a), bmag = Mag(b);
|
|
|
|
return (amag == bmag);
|
|
|
|
}
|
2002-01-07 03:29:15 +01:00
|
|
|
</programlisting>
|
2003-04-13 11:57:35 +02:00
|
|
|
The other four operators are very similar. You can find their code
|
|
|
|
in <filename>src/tutorial/complex.c</filename> and
|
|
|
|
<filename>src/tutorial/complex.sql</filename> in the source
|
|
|
|
distribution.
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
Now declare the functions and the operators based on the functions:
|
2002-01-07 03:29:15 +01:00
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION complex_abs_eq(complex, complex) RETURNS boolean
|
2003-04-13 11:57:35 +02:00
|
|
|
AS '<replaceable>filename</replaceable>', 'complex_abs_eq'
|
2002-01-07 03:29:15 +01:00
|
|
|
LANGUAGE C;
|
2003-04-13 11:57:35 +02:00
|
|
|
|
|
|
|
CREATE OPERATOR = (
|
|
|
|
leftarg = complex,
|
|
|
|
rightarg = complex,
|
|
|
|
procedure = complex_abs_eq,
|
|
|
|
restrict = eqsel,
|
|
|
|
join = eqjoinsel
|
|
|
|
);
|
2002-01-07 03:29:15 +01:00
|
|
|
</programlisting>
|
2003-04-13 11:57:35 +02:00
|
|
|
It is important to specify the restriction and join selectivity
|
|
|
|
functions, otherwise the optimizer will be unable to make effective
|
|
|
|
use of the index. Note that there less-than, equal, and
|
|
|
|
greater-than cases should use different selectivity functions.
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
Other things worth noting are happening here:
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
2003-04-13 11:57:35 +02:00
|
|
|
<para>
|
|
|
|
There can only be one operator named, say, <literal>=</literal>
|
|
|
|
and taking type <type>complex</type> for both operands. In this
|
|
|
|
case we don't have any other operator <literal>=</literal> for
|
|
|
|
<type>complex</type>, but if we were building a practical data
|
|
|
|
type we'd probably want <literal>=</literal> to be the ordinary
|
|
|
|
equality operation for complex numbers (and not the equality of
|
|
|
|
the absolute values). In that case, we'd need to use some other
|
|
|
|
operator name for <function>complex_abs_eq</>.
|
|
|
|
</para>
|
2002-01-07 03:29:15 +01:00
|
|
|
</listitem>
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
<listitem>
|
2003-04-13 11:57:35 +02:00
|
|
|
<para>
|
|
|
|
Although <productname>PostgreSQL</productname> can cope with
|
|
|
|
functions having the same name as long as they have different
|
|
|
|
argument data types, C can only cope with one global function
|
|
|
|
having a given name. So we shouldn't name the C function
|
|
|
|
something simple like <filename>abs_eq</filename>. Usually it's
|
|
|
|
a good practice to include the data type name in the C function
|
|
|
|
name, so as not to conflict with functions for other data types.
|
|
|
|
</para>
|
2002-01-07 03:29:15 +01:00
|
|
|
</listitem>
|
1998-03-01 09:16:16 +01:00
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
<listitem>
|
2003-04-13 11:57:35 +02:00
|
|
|
<para>
|
|
|
|
We could have made the <productname>PostgreSQL</productname> name
|
|
|
|
of the function <filename>abs_eq</filename>, relying on
|
|
|
|
<productname>PostgreSQL</productname> to distinguish it by
|
|
|
|
argument data types from any other
|
|
|
|
<productname>PostgreSQL</productname> function of the same name.
|
|
|
|
To keep the example simple, we make the function have the same
|
|
|
|
names at the C level and <productname>PostgreSQL</productname>
|
|
|
|
level.
|
|
|
|
</para>
|
2002-01-07 03:29:15 +01:00
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
2001-08-21 18:36:06 +02:00
|
|
|
</para>
|
|
|
|
|
1999-05-27 17:44:54 +02:00
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
The next step is the registration of the support routine required
|
|
|
|
by B-trees. The example C code that implements this is in the same
|
|
|
|
file that contains the operator functions. This is how we declare
|
|
|
|
the function:
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
<programlisting>
|
2002-07-30 07:24:56 +02:00
|
|
|
CREATE FUNCTION complex_abs_cmp(complex, complex)
|
|
|
|
RETURNS integer
|
2003-04-13 11:57:35 +02:00
|
|
|
AS '<replaceable>filename</replaceable>'
|
2002-07-30 07:24:56 +02:00
|
|
|
LANGUAGE C;
|
2002-01-07 03:29:15 +01:00
|
|
|
</programlisting>
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
Now that we have the required operators and support routine,
|
|
|
|
we can finally create the operator class:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE OPERATOR CLASS complex_abs_ops
|
|
|
|
DEFAULT FOR TYPE complex USING btree AS
|
2002-07-30 19:34:37 +02:00
|
|
|
OPERATOR 1 < ,
|
|
|
|
OPERATOR 2 <= ,
|
2002-07-30 07:24:56 +02:00
|
|
|
OPERATOR 3 = ,
|
2002-07-30 19:34:37 +02:00
|
|
|
OPERATOR 4 >= ,
|
|
|
|
OPERATOR 5 > ,
|
2002-07-30 07:24:56 +02:00
|
|
|
FUNCTION 1 complex_abs_cmp(complex, complex);
|
|
|
|
</programlisting>
|
2001-08-21 18:36:06 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-13 11:57:35 +02:00
|
|
|
And we're done! It should now be possible to create
|
2002-07-30 07:24:56 +02:00
|
|
|
and use B-tree indexes on <type>complex</type> columns.
|
2002-01-07 03:29:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
We could have written the operator entries more verbosely, as in
|
2002-01-07 03:29:15 +01:00
|
|
|
<programlisting>
|
2002-07-30 19:34:37 +02:00
|
|
|
OPERATOR 1 < (complex, complex) ,
|
2002-01-07 03:29:15 +01:00
|
|
|
</programlisting>
|
2002-09-21 20:32:54 +02:00
|
|
|
but there is no need to do so when the operators take the same data type
|
2002-07-30 07:24:56 +02:00
|
|
|
we are defining the operator class for.
|
|
|
|
</para>
|
2002-01-07 03:29:15 +01:00
|
|
|
|
2002-07-30 07:24:56 +02:00
|
|
|
<para>
|
|
|
|
The above example assumes that you want to make this new operator class the
|
|
|
|
default B-tree operator class for the <type>complex</type> data type.
|
|
|
|
If you don't, just leave out the word <literal>DEFAULT</>.
|
2002-01-07 03:29:15 +01:00
|
|
|
</para>
|
2003-04-13 11:57:35 +02:00
|
|
|
</sect2>
|
2002-07-30 07:24:56 +02:00
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
<sect2 id="xindex-opclass-features">
|
2002-07-30 07:24:56 +02:00
|
|
|
<title>Special Features of Operator Classes</title>
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
There are two special features of operator classes that we have
|
|
|
|
not discussed yet, mainly because they are not very useful
|
2003-04-13 11:57:35 +02:00
|
|
|
with the default B-tree index method.
|
2002-07-30 07:24:56 +02:00
|
|
|
</para>
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2002-07-30 07:24:56 +02:00
|
|
|
<para>
|
|
|
|
Normally, declaring an operator as a member of an operator class means
|
2003-04-13 11:57:35 +02:00
|
|
|
that the index method can retrieve exactly the set of rows
|
|
|
|
that satisfy a <literal>WHERE</> condition using the operator. For example,
|
2002-01-07 03:29:15 +01:00
|
|
|
<programlisting>
|
2002-07-30 19:34:37 +02:00
|
|
|
SELECT * FROM table WHERE integer_column < 4;
|
2002-01-07 03:29:15 +01:00
|
|
|
</programlisting>
|
2002-07-30 07:24:56 +02:00
|
|
|
can be satisfied exactly by a B-tree index on the integer column.
|
|
|
|
But there are cases where an index is useful as an inexact guide to
|
|
|
|
the matching rows. For example, if an R-tree index stores only
|
2003-04-13 11:57:35 +02:00
|
|
|
bounding boxes for objects, then it cannot exactly satisfy a <literal>WHERE</>
|
2002-07-30 07:24:56 +02:00
|
|
|
condition that tests overlap between nonrectangular objects such as
|
|
|
|
polygons. Yet we could use the index to find objects whose bounding
|
|
|
|
box overlaps the bounding box of the target object, and then do the
|
|
|
|
exact overlap test only on the objects found by the index. If this
|
|
|
|
scenario applies, the index is said to be <quote>lossy</> for the
|
2002-07-30 19:34:37 +02:00
|
|
|
operator, and we add <literal>RECHECK</> to the <literal>OPERATOR</> clause
|
|
|
|
in the <command>CREATE OPERATOR CLASS</> command.
|
2002-07-30 07:24:56 +02:00
|
|
|
<literal>RECHECK</> is valid if the index is guaranteed to return
|
2003-04-13 11:57:35 +02:00
|
|
|
all the required rows, plus perhaps some additional rows, which
|
|
|
|
can be eliminated by performing the original operator invocation.
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2002-07-30 07:24:56 +02:00
|
|
|
Consider again the situation where we are storing in the index only
|
|
|
|
the bounding box of a complex object such as a polygon. In this
|
|
|
|
case there's not much value in storing the whole polygon in the index
|
|
|
|
entry --- we may as well store just a simpler object of type
|
2003-04-13 11:57:35 +02:00
|
|
|
<type>box</>. This situation is expressed by the <literal>STORAGE</>
|
2002-07-30 07:24:56 +02:00
|
|
|
option in <command>CREATE OPERATOR CLASS</>: we'd write something like
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE OPERATOR CLASS polygon_ops
|
|
|
|
DEFAULT FOR TYPE polygon USING gist AS
|
|
|
|
...
|
|
|
|
STORAGE box;
|
|
|
|
</programlisting>
|
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
At present, only the GiST index method supports a
|
2002-09-21 20:32:54 +02:00
|
|
|
<literal>STORAGE</> type that's different from the column data type.
|
2002-07-30 07:24:56 +02:00
|
|
|
The GiST <literal>compress</> and <literal>decompress</> support
|
2002-09-21 20:32:54 +02:00
|
|
|
routines must deal with data-type conversion when <literal>STORAGE</>
|
2002-07-30 07:24:56 +02:00
|
|
|
is used.
|
1999-05-27 17:44:54 +02:00
|
|
|
</para>
|
2003-04-13 11:57:35 +02:00
|
|
|
</sect2>
|
1999-05-27 17:44:54 +02:00
|
|
|
|
2003-04-13 11:57:35 +02:00
|
|
|
</sect1>
|
1999-05-27 17:44:54 +02:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
2000-03-31 05:27:42 +02:00
|
|
|
mode:sgml
|
1999-05-27 17:44:54 +02:00
|
|
|
sgml-omittag:nil
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"./reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
2000-03-31 05:27:42 +02:00
|
|
|
sgml-local-catalogs:("/usr/lib/sgml/catalog")
|
1999-05-27 17:44:54 +02:00
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|