1
<!-- doc/src/sgml/btree-gist.sgml -->
3
<sect1 id="btree-gist">
4
<title>btree_gist</title>
6
<indexterm zone="btree-gist">
7
<primary>btree_gist</primary>
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</>, and <type>cidr</>.
23
In general, these operator classes will not outperform the equivalent
24
standard B-tree index methods, and they lack one major feature of the
25
standard B-tree code: the ability to enforce uniqueness. However,
26
they provide some other features that are not available with a B-tree
27
index, as described below. Also, these operator classes are useful
28
when a multi-column GiST index is needed, wherein some of the columns
29
are of data types that are only indexable with GiST but other columns
30
are just simple data types. Lastly, these operator classes are useful for
31
GiST testing and as a base for developing other GiST operator classes.
35
In addition to the typical B-tree search operators, <filename>btree_gist</>
36
also provides index support for <literal><></literal> (<quote>not
37
equals</quote>). This may be useful in combination with an
38
<link linkend="SQL-CREATETABLE-EXCLUDE">exclusion constraint</link>,
43
Also, for data types for which there is a natural distance metric,
44
<filename>btree_gist</> defines a distance operator <literal><-></>,
45
and provides GiST index support for nearest-neighbor searches using
46
this operator. Distance operators are provided for
47
<type>int2</>, <type>int4</>, <type>int8</>, <type>float4</>,
48
<type>float8</>, <type>timestamp with time zone</>,
49
<type>timestamp without time zone</>,
50
<type>time without time zone</>, <type>date</>, <type>interval</>,
51
<type>oid</>, and <type>money</>.
55
<title>Example Usage</title>
58
Simple example using btree_gist instead of btree:
62
CREATE TABLE test (a int4);
64
CREATE INDEX testidx ON test USING gist (a);
66
SELECT * FROM test WHERE a < 10;
67
-- nearest-neighbor search: find the ten entries closest to "42"
68
SELECT *, a <-> 42 AS dist FROM test ORDER BY a <-> 42 LIMIT 10;
72
Use an <link linkend="SQL-CREATETABLE-EXCLUDE">exclusion
73
constraint</link> to enforce the rule that a cage at a zoo
74
can contain only one kind of animal:
78
=> CREATE TABLE zoo (
81
EXCLUDE USING gist (cage WITH =, animal WITH <>)
84
=> INSERT INTO zoo VALUES(123, 'zebra');
86
=> INSERT INTO zoo VALUES(123, 'zebra');
88
=> INSERT INTO zoo VALUES(123, 'lion');
89
ERROR: conflicting key value violates exclusion constraint "zoo_cage_animal_excl"
90
DETAIL: Key (cage, animal)=(123, lion) conflicts with existing key (cage, animal)=(123, zebra).
91
=> INSERT INTO zoo VALUES(124, 'lion');
98
<title>Authors</title>
101
Teodor Sigaev (<email>teodor@stack.net</email>) ,
102
Oleg Bartunov (<email>oleg@sai.msu.su</email>), and
103
Janko Richter (<email>jankorichter@yahoo.de</email>). See
104
<ulink url="http://www.sai.msu.su/~megera/postgres/gist/"></ulink>
105
for additional information.