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

MethodGood forNotes
B-tree (default)=, <, >, BETWEEN, ORDER BY, prefix LIKESupports UNIQUE, INCLUDE columns, index-only scans
HashEquality onlyWAL-logged since PostgreSQL 10; rarely better than B-tree
GINContainment in composite values: arrays, jsonb, full-text (tsvector), trigramSlow to update, fast to search; fastupdate buffers writes
GiSTGeometric data, ranges, nearest-neighbour, exclusion constraintsLossy; supports ORDER BY <-> distance
SP-GiSTPartitioned search trees (tries, quadtrees)Specialised
BRINHuge 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_hash

Sizes 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_partial

The 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 an INVALID index to drop) (safe-schema-migrations).
  • jsonb_path_ops is smaller and faster for @> but supports fewer operators than the default jsonb_ops.
  • Do not index every column. Check pg_stat_user_indexes.idx_scan for unused indexes.
  • An expression index is used only if the query repeats the exact expression.
  • Text search and LIKE '%x%' need GIN with tsvector or pg_trgm, not B-tree.

Practice questions

  1. When would BRIN fail badly on a time-series table?
  2. Which index supports WHERE tags @> ARRAY[...] and why not a B-tree?
  3. Why can a partial index be both smaller and faster than a full one?

References

indexing-theory · pg-explain-analyze · pg-vacuum-bloat