PostgreSQL Index Types

Five index types, each trading away something a B-tree assumes it can have for free. This guide covers when to reach for each one and the silent failure modes that mean an index you created is never actually used. Transaction internals (MVCC, VACUUM, wraparound) and how to read the planner live in the companion guide.

B-tree: The Default

The default for CREATE INDEX. Stores sorted key values in a balanced tree. Supports =, <, >, BETWEEN, IN, IS NULL, LIKE 'prefix%'. Use for almost everything unless a specific reason from GIN through Partial indexes applies.

The baseline every other index type is measured against: a B-tree indexes every row, maintains that index on every write, and pays a random I/O for every lookup. The rest of this guide is about which of those three costs you can avoid — and what you give up to avoid it.

GIN: Generalized Inverted Index

Use for: columns that contain multiple values per row — tsvector, jsonb, array, hstore.

GIN builds an inverted index: for each distinct element (lexeme, JSON key, array value), it stores a posting list of (heap tid) entries pointing to every row containing that element.

ASCII: GIN posting lists

"search" → [(1,3), (4,1), (7,2)]
"fast"   → [(1,3), (9,1)]
"index"  → [(4,1), (12,5)]

A @@ (text search) query looks up entries and intersects lists — fast for large document sets.
CREATE INDEX idx_docs_fts ON documents USING GIN (to_tsvector('english', body));

SELECT * FROM documents
WHERE to_tsvector('english', body) @@ to_tsquery('english', 'postgres & index');

GiST: Generalized Search Tree

Use for: types that need overlap, containment, or nearest-neighbor operators — geometry, range types (tsrange, int4range), tsvector (phrase search), inet.

GiST is a framework: each data type provides its own split and penalty functions, resulting in a balanced tree where inner nodes store bounding boxes that prune subtrees during search.

-- Find all reservations overlapping a time range
CREATE INDEX idx_res_range ON reservations USING GIST (during);

SELECT * FROM reservations
WHERE during && '[2026-06-01, 2026-06-10)'::tsrange;

GiST supports KNN (k-nearest-neighbor) queries with ORDER BY ... <-> point for PostGIS geometry — a query shape GIN cannot serve at all, since GIN has no notion of "distance," only membership.

BRIN: Block Range Index

Use for: very large, physically ordered, append-only tables — time-series logs, event tables, IoT sensor data.

BRIN divides the heap into block ranges (default: 128 pages) and stores just the min and max value of the indexed column within each range. The entire index is tiny — typically 10,000× smaller than a B-tree on the same column.

ASCII: BRIN block ranges

Block range 0–127:   min=2026-01-01, max=2026-01-03
Block range 128–255: min=2026-01-03, max=2026-01-05
...
Query: WHERE logged_at BETWEEN '2026-01-04' AND '2026-01-05'
  → skips range 0–127 entirely, scans only range 128–255
CREATE INDEX idx_events_brin ON events USING BRIN (logged_at)
WITH (pages_per_range = 64);  -- smaller ranges = more precise, larger index

-- Check correlation (close to 1.0 = good for BRIN)
SELECT attname, correlation
FROM pg_stats
WHERE tablename = 'events' AND attname = 'logged_at';

Partial Indexes

A partial index includes a WHERE clause — it only indexes rows satisfying the predicate. The index is smaller, faster to update, and the planner uses it only for queries whose WHERE clause implies the index predicate.

✗ Full B-tree on status — 10M entries, low selectivity

CREATE INDEX idx_jobs_status
ON jobs (status);
-- 10,000,000 entries
-- updated on every status change
-- planner may choose Seq Scan anyway

✓ Partial index on pending only — 10K entries, tiny, cache-resident

CREATE INDEX idx_jobs_pending
ON jobs (created_at)
WHERE status = 'pending';
-- ~10,000 entries (0.1% of rows)
-- updated only when rows enter/leave pending
-- fits in a few shared_buffer pages
-- This query uses the partial index:
SELECT id, payload
FROM jobs
WHERE status = 'pending'
  AND created_at < now() - interval '5 minutes'
ORDER BY created_at
LIMIT 100;
Partial Index PatternUse Case
WHERE status = 'pending'Job queue — only index the small hot set
WHERE deleted_at IS NULLSoft-delete tables — index only non-deleted rows
WHERE is_active = trueUser accounts — index only active accounts
WHERE error_count > 0Failed records — index only failure minority