]> granicus.if.org Git - postgresql/blob - doc/src/sgml/btree-gist.sgml
d08647ce05a49493debdb08a15abadbeb9481836
[postgresql] / doc / src / sgml / btree-gist.sgml
1 <!-- doc/src/sgml/btree-gist.sgml -->
2
3 <sect1 id="btree-gist" xreflabel="btree_gist">
4  <title>btree_gist</title>
5
6  <indexterm zone="btree-gist">
7   <primary>btree_gist</primary>
8  </indexterm>
9
10  <para>
11   <filename>btree_gist</> provides GiST index operator classes that
12   implement B-tree equivalent behavior for the data types
13   <type>int2</>, <type>int4</>, <type>int8</>, <type>float4</>,
14   <type>float8</>, <type>numeric</>, <type>timestamp with time zone</>,
15   <type>timestamp without time zone</>, <type>time with time zone</>,
16   <type>time without time zone</>, <type>date</>, <type>interval</>,
17   <type>oid</>, <type>money</>, <type>char</>,
18   <type>varchar</>, <type>text</>, <type>bytea</>, <type>bit</>,
19   <type>varbit</>, <type>macaddr</>, <type>inet</>, <type>cidr</>,
20   and <type>uuid</>.
21  </para>
22
23  <para>
24   In general, these operator classes will not outperform the equivalent
25   standard B-tree index methods, and they lack one major feature of the
26   standard B-tree code: the ability to enforce uniqueness.  However,
27   they provide some other features that are not available with a B-tree
28   index, as described below.  Also, these operator classes are useful
29   when a multicolumn GiST index is needed, wherein some of the columns
30   are of data types that are only indexable with GiST but other columns
31   are just simple data types.  Lastly, these operator classes are useful for
32   GiST testing and as a base for developing other GiST operator classes.
33  </para>
34
35  <para>
36   In addition to the typical B-tree search operators, <filename>btree_gist</>
37   also provides index support for <literal>&lt;&gt;</literal> (<quote>not
38   equals</quote>). This may be useful in combination with an
39   <link linkend="SQL-CREATETABLE-EXCLUDE">exclusion constraint</link>,
40   as described below.
41  </para>
42
43  <para>
44   Also, for data types for which there is a natural distance metric,
45   <filename>btree_gist</> defines a distance operator <literal>&lt;-&gt;</>,
46   and provides GiST index support for nearest-neighbor searches using
47   this operator.  Distance operators are provided for
48   <type>int2</>, <type>int4</>, <type>int8</>, <type>float4</>,
49   <type>float8</>, <type>timestamp with time zone</>,
50   <type>timestamp without time zone</>,
51   <type>time without time zone</>, <type>date</>, <type>interval</>,
52   <type>oid</>, and <type>money</>.
53  </para>
54
55  <sect2>
56   <title>Example Usage</title>
57
58   <para>
59    Simple example using <literal>btree_gist</literal> instead of <literal>btree</literal>:
60   </para>
61
62 <programlisting>
63 CREATE TABLE test (a int4);
64 -- create index
65 CREATE INDEX testidx ON test USING GIST (a);
66 -- query
67 SELECT * FROM test WHERE a &lt; 10;
68 -- nearest-neighbor search: find the ten entries closest to "42"
69 SELECT *, a &lt;-&gt; 42 AS dist FROM test ORDER BY a &lt;-&gt; 42 LIMIT 10;
70 </programlisting>
71
72   <para>
73    Use an <link linkend="SQL-CREATETABLE-EXCLUDE">exclusion
74    constraint</link> to enforce the rule that a cage at a zoo
75    can contain only one kind of animal:
76   </para>
77
78 <programlisting>
79 =&gt; CREATE TABLE zoo (
80   cage   INTEGER,
81   animal TEXT,
82   EXCLUDE USING GIST (cage WITH =, animal WITH &lt;&gt;)
83 );
84
85 =&gt; INSERT INTO zoo VALUES(123, 'zebra');
86 INSERT 0 1
87 =&gt; INSERT INTO zoo VALUES(123, 'zebra');
88 INSERT 0 1
89 =&gt; INSERT INTO zoo VALUES(123, 'lion');
90 ERROR:  conflicting key value violates exclusion constraint "zoo_cage_animal_excl"
91 DETAIL:  Key (cage, animal)=(123, lion) conflicts with existing key (cage, animal)=(123, zebra).
92 =&gt; INSERT INTO zoo VALUES(124, 'lion');
93 INSERT 0 1
94 </programlisting>
95
96  </sect2>
97
98  <sect2>
99   <title>Authors</title>
100
101   <para>
102    Teodor Sigaev (<email>teodor@stack.net</email>),
103    Oleg Bartunov (<email>oleg@sai.msu.su</email>),
104    Janko Richter (<email>jankorichter@yahoo.de</email>), and
105    Paul Jungwirth (<email>pj@illuminatedcomputing.com</email>).  See
106    <ulink url="http://www.sai.msu.su/~megera/postgres/gist/"></ulink>
107    for additional information.
108   </para>
109
110  </sect2>
111
112 </sect1>