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
