Btree handles equality and range. BRIN handles correlated time-series. The rest — full-text, JSONB containment, trigram similarity, geometric overlap, array containment — falls to GiST and GIN. They are both generalised index AMs, but they have very different shapes and the right pick depends on whether your workload is search-heavy or write-heavy. This article is the decision aid.
How to verify
Look at current indexes and their sizes — the disk footprint usually tells the story:
psql -d app -c "SELECT indexrelname, indexdef, pg_size_pretty(pg_relation_size(indexrelid))
FROM pg_stat_user_indexes
JOIN pg_indexes ON indexrelname = indexname
WHERE schemaname = 'public'
ORDER BY pg_relation_size(indexrelid) DESC LIMIT 20;"
psql -d app -c "EXPLAIN (ANALYZE, BUFFERS)
SELECT id FROM documents WHERE tags @> ARRAY['urgent'];"
psql -d app -c "EXPLAIN (ANALYZE, BUFFERS)
SELECT id FROM documents
WHERE search_vector @@ to_tsquery('postgres & tuning');"
If GIN is the right index but EXPLAIN shows Seq Scan, the index probably isn’t being used because of operator-class mismatch — verify the index ops match the operator in the query.
What’s happening
GIN — Generalised Inverted Index. For each distinct value (a lexeme, a JSON key/value, an array element), GIN stores a posting list of TIDs pointing at the rows that contain it. Queries that look up “documents containing the lexeme postgres” walk the posting list directly — fast. The cost is the index itself, which is larger than GiST and slower to update.
GiST — Generalised Search Tree. A balanced tree where each node summarises the children below it (a bounding box for geometries, a signature for tsvectors). Lookups walk the tree, pruning subtrees whose summary can’t satisfy the query. Faster builds and updates, smaller index, slower lookups than GIN.
The rule of thumb: GIN if reads dominate writes, GiST if writes dominate or you need k-nearest-neighbour ordering. For full-text search on a mostly-static corpus, GIN. For real-time geometry updates, GiST. For trigram similarity (pg_trgm), both work — GIN for exact-prefix lookups, GiST for similarity ranking.
The decision
-
Full-text search (tsvector):
- GIN — better lookup speed on
@@. Most production deployments use this. - GiST — only if write rate is so high GIN can’t keep up. Larger workloads usually solve this with
gin_pending_list_limittuning instead.
- GIN — better lookup speed on
-
JSONB containment (
@>,?,?|,?&):- GIN with
jsonb_ops— indexes every key and every value. Largest, broadest. - GIN with
jsonb_path_ops— indexes only the JSONPATH-style paths. Smaller, faster for containment, but doesn’t accelerate key-only existence (?operator). - See JSONB indexing.
- GIN with
-
Trigram similarity (
pg_trgm,LIKE '%foo%',ILIKE):- GIN with
gin_trgm_ops— fast for exact and ILIKE pattern matching. - GiST with
gist_trgm_ops— slower, but supports KNN ordering (ORDER BY col <-> 'query').
- GIN with
-
PostGIS geometry/geography:
- GiST — the only realistic choice; geometries are spatial and tree-based bounding boxes are how spatial indexes work.
- SP-GiST (specialised partitioned GiST) for very high-cardinality point data.
-
Array containment (
@>,&&):- GIN with default
array_ops— fast lookups, slow writes.
- GIN with default
-
Range types (
int4range,tstzrange,numrange):- GiST — supports overlap, contains, adjacency. PG 14+ also has SP-GiST range support.
The procedure
-
Create the right ops class explicitly:
CREATE EXTENSION IF NOT EXISTS pg_trgm; CREATE INDEX docs_search_gin ON documents USING GIN (search_vector); -- tsvector CREATE INDEX docs_tags_gin ON documents USING GIN (tags); -- array CREATE INDEX docs_meta_gin ON documents USING GIN (meta jsonb_path_ops); -- JSONB containment CREATE INDEX docs_title_trgm ON documents USING GIN (title gin_trgm_ops); -- ILIKE CREATE INDEX shapes_geom_gist ON shapes USING GIST (geom); -- PostGIS -
Tune GIN pending list for write-heavy workloads:
ALTER TABLE documents SET (gin_pending_list_limit = '8MB');GIN batches new entries in a pending list and flushes them in bulk — this keeps inserts fast but means recent inserts use a fallback scan path until the pending list flushes. A larger limit favours write throughput; a smaller one favours search latency on recent data.
-
Build concurrently in production:
CREATE INDEX CONCURRENTLY docs_search_gin ON documents USING GIN (search_vector);GIN builds can be slow (multi-hour for 100M-row tables) — concurrent keeps reads and writes flowing.
-
Maintain with REINDEX: Both GiST and GIN can bloat. Schedule
REINDEX INDEX CONCURRENTLYon large indexes when bloat exceeds 30% — see pg_repack for the bulk path. -
Check the planner is using it:
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;Bitmap Index Scan using docs_search_ginis the win. If you seeSeq Scan, check operator-class match, statistics, andenable_seqscan = offto confirm the index even works.
Common pitfalls
- Operator-class mismatch is the most common failure:
CREATE INDEX ... USING GIN (col)defaults to a class that may not match your query’s operator. Always specify. - GIN’s pending list creates a bimodal latency: queries hitting recent inserts are slower until the pending list flushes. Monitor
pg_stat_progress_create_indexand the GIN-specific stats. - GiST quality depends on the operator class’s pick/split logic — defaults are fine for tsvector and PostGIS, but custom types may benefit from non-default options.
CREATE INDEX CONCURRENTLYcan leave INVALID indexes on failure; verify with\dand drop+rebuild if you seeINVALID.- A GIN index on a column with thousands of distinct values per row (large arrays, large JSONB) can balloon — measure before deploying to production.
In the engagements we run, GIN is the default for full-text, JSONB, and array workloads, GiST is the default for PostGIS and range types, with gin_pending_list_limit and concurrent rebuilds tuned to the write rate. The right index for the right operator is the cheapest performance win in Postgres, and the one we audit on every onboarding. For the operating model behind a tuned Postgres footprint, see managed operations.