sql-server

7 Пост

oracle

7 Пост

postgresql

10 Пост

my-sql

2 Пост

common-sql

1 Пост

News

5 Новости

Основная тема

 

In PostgreSQL, GiST is one index access method, not one single operator. What changes from case to case is the GiST operator class (opclass) used for a data type. In other words, when people say “GiST index types”, the precise thing is usually GiST operator classes / supported data-type families. GiST is a generalized balanced tree framework that can implement behaviors similar to R-tree and other search structures, and it also supports nearest-neighbor searches when the opclass defines a distance operator.

1) Core built-in GiST operator classes

PostgreSQL 18’s built-in GiST operator classes are these: box_ops, circle_ops, point_ops, poly_ops, inet_ops, range_ops, multirange_ops, tsvector_ops, and tsquery_ops. These are the native GiST families shipped in core, before adding optional extensions.

A. Geometric GiST types

These are the classic spatial-style GiST families:

  • box_ops
  • circle_ops
  • point_ops
  • poly_ops

They support geometric predicates such as overlap, containment, left/right/above/below relationships, and several of them also support nearest-neighbor ordering with <->. This is the area where GiST behaves most like an R-tree-style index.

Typical use:

CREATE INDEX ix_gist_point ON places USING GIST (location);

Good for:

  • 2D geometry
  • overlap/containment queries
  • nearest-point searches

B. Network-address GiST type

inet_ops is the GiST operator class for inet/cidr style network data. It supports subnet and ordering-style comparisons such as containment and overlap semantics for network ranges. One detail from the docs: inet_ops is not the default opclass for inet and cidr, so if you want GiST behavior there, you should specify it explicitly.

Example:

CREATE INDEX ix_gist_inet
ON hosts USING GIST (ip inet_ops);

Good for:

  • subnet containment
  • network overlap style searches
  • combining network columns with other GiST-indexed columns in multicolumn GiST

C. Range GiST type

range_ops is for PostgreSQL range types such as int4range, numrange, tsrange, tstzrange, and daterange. It supports overlap, containment, “strictly left/right of”, “does not extend to the right/left of”, adjacency, and related predicates.

Example:

CREATE INDEX ix_gist_booking_range
ON bookings USING GIST (period);

Good for:

  • period overlap checks
  • scheduling systems
  • exclusion constraints on time intervals

This is one of the most important real-world GiST uses in PostgreSQL. And yes, tsrange is not a separate index type by itself; it is a range data type that uses the range_ops GiST operator class.

D. Multirange GiST type

multirange_ops is for multirange values, such as datemultirange, tsmultirange, tstzmultirange, etc. It supports overlap, containment, adjacency, left/right relations, and comparisons between multirange-to-multirange as well as multirange-to-range. Multiranges were added in PostgreSQL 14.

Good for:

  • non-contiguous availability windows
  • calendars with gaps
  • compact modeling of multiple time spans in one column

E. Full-text related GiST types

Core PostgreSQL also includes:

  • tsvector_ops
  • tsquery_ops

tsvector_ops supports @@ (tsvector, tsquery), while tsquery_ops supports containment relationships between tsquery values. PostgreSQL’s GiST examples explicitly note that core provides text search support through these GiST implementations.

Example:

CREATE INDEX ix_gist_search
ON docs USING GIST (search_vector);

Good for:

  • full-text search, when GiST tradeoffs fit your case
  • cases where you want GiST-specific behavior or multicolumn combinations

But in many text-search workloads, GIN is usually the more common choice; GiST is still valid, just not usually the default recommendation for pure full-text search.


2) Important GiST operator classes from extensions

Besides core, PostgreSQL documentation points to several extensions that add GiST operator classes: btree_gist, cube, hstore, intarray, ltree, pg_trgm, and seg.

A. btree_gist

This extension gives GiST operator classes with B-tree-like behavior for many scalar data types, including integers, numerics, timestamps, date/time types, text, bytea, inet/cidr-like address types, uuid, bool, enums, and more. The docs note that these generally do not outperform normal B-tree indexes and cannot enforce uniqueness, but they are useful in multicolumn GiST indexes and for nearest-neighbor support on some scalar types.

Example:

CREATE EXTENSION btree_gist;

CREATE INDEX ix_gist_mixed
ON reservations USING GIST (room_id, period);

Why this matters:

  • lets scalar columns participate in a GiST index
  • crucial for exclusion constraints like “same room cannot overlap in time”

B. cube

The cube extension adds a GiST operator class for multidimensional cube values. It supports equality, overlap, containment, contained-by, and nearest-neighbor style ordering operators such as <->, <#>, and <=>.

Good for:

  • multidimensional points
  • similarity / nearest-neighbor in vector-like coordinate spaces
  • analytic geometry use cases

C. hstore

The hstore extension provides the hstore data type for key/value pairs, and PostgreSQL lists it among the optional GiST-enabled modules.

Good for:

  • semi-structured key/value data
  • certain containment/search patterns on hstore

D. intarray

The intarray extension provides GiST operator classes for int4[]. The docs describe two GiST classes: gist__int_ops for small/medium data sets and gist__intbig_ops with a larger signature better suited to larger data sets with many distinct array values.

Good for:

  • integer-array containment / overlap style searches
  • specialized integer set workloads

E. ltree

The ltree extension supports hierarchical tree-like labels and is listed among PostgreSQL’s GiST-capable extensions.

Good for:

  • path hierarchies
  • org trees
  • category trees
  • ancestor/descendant style path matching

F. pg_trgm

pg_trgm provides trigram-based similarity search and includes GiST operator classes for fast similar-string searching.

Good for:

  • fuzzy text matching
  • typo-tolerant search
  • similarity operators on text

G. seg

The seg extension provides a floating-point interval / segment type and is listed among the GiST-enabled contrib modules.

Good for:

  • scientific/lab interval values
  • uncertain numeric segment comparisons

3) The practical classification

If you want the cleanest “full” view, GiST operator classes in PostgreSQL can be grouped like this:

Core GiST families

  • Geometry: box_ops, circle_ops, point_ops, poly_ops
  • Network: inet_ops
  • Temporal/range: range_ops, multirange_ops
  • Text search: tsvector_ops, tsquery_ops

Extension GiST families

  • Scalar/B-tree-like: btree_gist
  • Multidimensional numeric space: cube
  • Key/value: hstore
  • Integer arrays: intarray
  • Hierarchies: ltree
  • Trigram similarity text: pg_trgm
  • Floating intervals: seg

4) What GiST is best at

GiST is strongest when your predicates are not simple = / < / > only, but involve:

  • overlap
  • containment
  • spatial relationships
  • range intersection
  • nearest-neighbor
  • custom domain-specific matching

So in practice, the most important GiST use cases are usually:

  • geometry/spatial-like searches
  • range and multirange overlap logic
  • exclusion constraints with ranges
  • nearest-neighbor queries
  • specialized extensions like trigram, ltree, cube, and mixed multicolumn GiST via btree_gist

5) Key caveat

Do not think of GiST as “one better B-tree”. For ordinary equality/range lookups on scalar columns, normal B-tree is usually the right first choice.

Even PostgreSQL says btree_gist generally does not outperform standard B-tree and cannot enforce uniqueness. GiST is chosen because it supports operator semantics B-tree cannot express well.