btree_gist provides GiST index
operator classes that implement B-tree equivalent behavior for the
with time zone,
timestamp without time
time with time zone,
time without time zone,
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 multicolumn 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.
In addition to the typical B-tree search operators,
btree_gist also provides index support for
<> (“not equals”). This may
be useful in combination with an exclusion
constraint, as described below.
Also, for data types for which there is a natural distance
btree_gist defines a distance
<->, and provides GiST
index support for nearest-neighbor searches using this operator.
Distance operators are provided for
timestamp with time zone,
timestamp without time zone,
without time zone,
Simple example using
CREATE TABLE test (a int4); -- create index CREATE INDEX testidx ON test USING GIST (a); -- query SELECT * FROM test WHERE a < 10; -- nearest-neighbor search: find the ten entries closest to "42" SELECT *, a <-> 42 AS dist FROM test ORDER BY a <-> 42 LIMIT 10;
Use an exclusion constraint to enforce the rule that a cage at a zoo can contain only one kind of animal:
=> CREATE TABLE zoo ( cage INTEGER, animal TEXT, EXCLUDE USING GIST (cage WITH =, animal WITH <>) ); => INSERT INTO zoo VALUES(123, 'zebra'); INSERT 0 1 => INSERT INTO zoo VALUES(123, 'zebra'); INSERT 0 1 => 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). => INSERT INTO zoo VALUES(124, 'lion'); INSERT 0 1
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.