Overview
PostgreSQL ships several index access methods. Choosing the right one is often the difference between a millisecond lookup and a sequential scan. The general theory (B-tree, composite order, covering, sargability) is in indexing-theory; this note covers what is specific to PostgreSQL.
Core concepts
| Method | Good for | Notes |
|---|---|---|
| B-tree (default) | =, <, >, BETWEEN, ORDER BY, prefix LIKE | Supports UNIQUE, INCLUDE columns, index-only scans |
| Hash | Equality only | WAL-logged since PostgreSQL 10; rarely better than B-tree |
| GIN | Containment in composite values: arrays, jsonb, full-text (tsvector), trigram | Slow to update, fast to search; fastupdate buffers writes |
| GiST | Geometric data, ranges, nearest-neighbour, exclusion constraints | Lossy; supports ORDER BY <-> distance |
| SP-GiST | Partitioned search trees (tries, quadtrees) | Specialised |
| BRIN | Huge tables whose values correlate with physical order (timestamps in append-only data) | Tiny; stores min/max per block range |
Other features: partial indexes (WHERE ...), expression indexes (ON lower(email)), multicolumn and INCLUDE covering indexes, CREATE INDEX CONCURRENTLY, and pg_trgm / pgvector extensions.
Worked example
200,000-row table i(id, tags text[], doc jsonb, ts timestamptz, body text), PostgreSQL 17:
CREATE INDEX i_gin ON i USING gin (tags);
CREATE INDEX i_jsonb ON i USING gin (doc jsonb_path_ops);
CREATE INDEX i_brin ON i USING brin (ts);
CREATE INDEX i_hash ON i USING hash (body);
EXPLAIN SELECT * FROM i WHERE tags @> ARRAY['b7']; -- Bitmap Index Scan on i_gin
EXPLAIN SELECT * FROM i WHERE doc @> '{"k":5}'; -- Bitmap Index Scan on i_jsonb
EXPLAIN SELECT * FROM i WHERE body = 'word7'; -- Bitmap Index Scan on i_hashSizes on a 26 MB table: BRIN 24 kB, GIN on the array 640 kB, hash index 7.4 MB. BRIN is tiny because it stores one summary per block range, which only works when the column is physically ordered (here timestamps assigned at insert).
Partial index, only the rows you query:
CREATE INDEX i_partial ON i(id) WHERE body = 'word7';
EXPLAIN SELECT id FROM i WHERE body = 'word7' AND id > 10; -- Index Only Scan using i_partialThe query predicate must imply the index predicate for the planner to use it.
Pitfalls and production notes
- Always build on a live table with
CREATE INDEX CONCURRENTLY(not inside a transaction; a failed build leaves anINVALIDindex to drop) (safe-schema-migrations). jsonb_path_opsis smaller and faster for@>but supports fewer operators than the defaultjsonb_ops.- Do not index every column. Check
pg_stat_user_indexes.idx_scanfor unused indexes. - An expression index is used only if the query repeats the exact expression.
- Text search and
LIKE '%x%'need GIN withtsvectororpg_trgm, not B-tree.
Practice questions
- When would BRIN fail badly on a time-series table?
- Which index supports
WHERE tags @> ARRAY[...]and why not a B-tree? - Why can a partial index be both smaller and faster than a full one?
References
- PostgreSQL manual, Indexes: https://www.postgresql.org/docs/current/indexes.html