<!-- doc/src/sgml/btree-gist.sgml -->

<sect1 id="btree-gist">
 <title>btree_gist</title>

 <indexterm zone="btree-gist">
  <primary>btree_gist</primary>
 </indexterm>

 <para>
  <filename>btree_gist</> provides GiST index operator classes that
  implement B-tree equivalent behavior for the data types
  <type>int2</>, <type>int4</>, <type>int8</>, <type>float4</>,
  <type>float8</>, <type>numeric</>, <type>timestamp with time zone</>,
  <type>timestamp without time zone</>, <type>time with time zone</>,
  <type>time without time zone</>, <type>date</>, <type>interval</>,
  <type>oid</>, <type>money</>, <type>char</>,
  <type>varchar</>, <type>text</>, <type>bytea</>, <type>bit</>,
  <type>varbit</>, <type>macaddr</>, <type>inet</>, and <type>cidr</>.
 </para>

 <para>
  In general, these operator classes will not outperform the equivalent
  standard B-tree index methods, and they lack one major feature of the
  standard B-tree code: the ability to enforce uniqueness.  However,
  they provide some other features that are not available with a B-tree
  index, as described below.  Also, these operator classes are useful
  when a multi-column GiST index is needed, wherein some of the columns
  are of data types that are only indexable with GiST but other columns
  are just simple data types.  Lastly, these operator classes are useful for
  GiST testing and as a base for developing other GiST operator classes.
 </para>

 <para>
  In addition to the typical B-tree search operators, <filename>btree_gist</>
  also provides index support for <literal>&lt;&gt;</literal> (<quote>not
  equals</quote>). This may be useful in combination with an
  <link linkend="SQL-CREATETABLE-EXCLUDE">exclusion constraint</link>,
  as described below.
 </para>

 <para>
  Also, for data types for which there is a natural distance metric,
  <filename>btree_gist</> defines a distance operator <literal>&lt;-&gt;</>,
  and provides GiST index support for nearest-neighbor searches using
  this operator.  Distance operators are provided for
  <type>int2</>, <type>int4</>, <type>int8</>, <type>float4</>,
  <type>float8</>, <type>timestamp with time zone</>,
  <type>timestamp without time zone</>,
  <type>time without time zone</>, <type>date</>, <type>interval</>,
  <type>oid</>, and <type>money</>.
 </para>

 <sect2>
  <title>Example Usage</title>

  <para>
   Simple example using btree_gist instead of btree:
  </para>

<programlisting>
CREATE TABLE test (a int4);
-- create index
CREATE INDEX testidx ON test USING gist (a);
-- query
SELECT * FROM test WHERE a &lt; 10;
-- nearest-neighbor search: find the ten entries closest to "42"
SELECT *, a &lt;-&gt; 42 AS dist FROM test ORDER BY a &lt;-&gt; 42 LIMIT 10;
</programlisting>

  <para>
   Use an <link linkend="SQL-CREATETABLE-EXCLUDE">exclusion
   constraint</link> to enforce the rule that a cage at a zoo
   can contain only one kind of animal:
  </para>

<programlisting>
=&gt; CREATE TABLE zoo (
  cage   INTEGER,
  animal TEXT,
  EXCLUDE USING gist (cage WITH =, animal WITH &lt;&gt;)
);

=&gt; INSERT INTO zoo VALUES(123, 'zebra');
INSERT 0 1
=&gt; INSERT INTO zoo VALUES(123, 'zebra');
INSERT 0 1
=&gt; INSERT INTO zoo VALUES(123, 'lion');
ERROR:  conflicting key value violates exclusion constraint "zoo_cage_animal_excl"
DETAIL:  Key (cage, animal)=(123, lion) conflicts with existing key (cage, animal)=(123, zebra).
=&gt; INSERT INTO zoo VALUES(124, 'lion');
INSERT 0 1
</programlisting>

 </sect2>

 <sect2>
  <title>Authors</title>

  <para>
   Teodor Sigaev (<email>teodor@stack.net</email>) ,
   Oleg Bartunov (<email>oleg@sai.msu.su</email>), and
   Janko Richter (<email>jankorichter@yahoo.de</email>).  See
   <ulink url="http://www.sai.msu.su/~megera/postgres/gist/"></ulink>
   for additional information.
  </para>

 </sect2>

</sect1>
