liveBook · Manning

GiST index

As explained in Just Use Postgres!

A GiST index is an index built with the GiST access method for searching complex data types and operator-defined predicates.

A GiST index is a flexible, balanced-tree access method for complex data types and operator-defined searches. It supports containment, proximity, and similarity checks across data such as full-text lexemes, geometric shapes, network addresses, and ranges. section 6.6.2 section 10.6.1

For geometric data, GiST organizes objects into hierarchical bounding boxes. Larger boxes are recursively divided into smaller ones, letting a search traverse only boxes that might contain or intersect the requested object. PostGIS uses this structure for geometry and geography data, implementing an R-tree on top of GiST. section 10.6 section 10.6.1

For full-text search, GiST builds a signature tree over tsvector values. Each lexeme contributes a bit-string signature, and a document signature combines those lexeme signatures with bitwise OR. Longer signatures reduce false positives during index scanning but increase index size. The default signature length is 124 bytes; the maximum is 2,024 bytes. section 6.6.2

Speeding up a movie search

Store normalized lexemes in a generated tsvector column, then create a GiST index: ALTER TABLE omdb.movies ADD COLUMN lexemes tsvector GENERATED ALWAYS AS ( to_tsvector( 'english', coalesce(name, '') || ' ' || coalesce(description, ''))) STORED; CREATE INDEX idx_movie_lexemes_gist ON omdb.movies USING GIST (lexemes); A search for historical & fight returns three movies. Before indexing, EXPLAIN shows a sequential scan examining all 4,225 rows, removing 4,222. With the GiST index, the plan uses an index scan and completes in the shown example in 0.395 milliseconds instead of 15.328 milliseconds. section 6.2.2 section 6.6 section 6.6.2

Listing 6.17 Creating a GiST index over tsvector lexemes
CREATE INDEX idx_movie_lexemes_gist
ON omdb.movies 
USING GIST (lexemes);

Use the executable definition to create a GiST index over the movies table's tsvector lexemes. section 6.6.2

How Just Use Postgres! covers it

How deep it goes
Explains hierarchical traversal, tsvector signatures, signature sizing, and measured query-plan changes, without covering implementation interfaces in detail.
Examples
Demonstrates movie lexeme indexing, historical-film search plans, geometric bounding boxes, and PostGIS searches near Downtown Miami.
What it assumes
Assumes familiarity with PostgreSQL CREATE INDEX, tsvector, full-text operators, and EXPLAIN; teaches GiST search structure from its basic purpose.
What you can do after
Operate/configure GiST by creating indexes, inspecting EXPLAIN plans, and choosing signature lengths for tsvector searches.
How it connects
Connects GiST with tsvector, full-text search, GIN, PostGIS, R-tree, geometry, geography, ranges, and network addresses.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.