Index Types: Synthesis and Self-Test
Synthesis
B-tree vs Partial Indexes: Same structure, radically different cost profile
A partial index and a full B-tree on the same column use the identical underlying data structure — the only difference is the WHERE clause at creation time. That one clause changes everything downstream: a full B-tree on status pays maintenance cost on every write to any row, regardless of which status it has. A partial index on status = 'pending' pays that cost only for rows entering or leaving the pending state. The B-tree mechanism is unchanged; the selectivity contract with the planner is what makes one of them 1,000× cheaper.
GIN vs GiST: Same use cases, opposite ends of the read/write tradeoff
tsvector full-text search can be indexed with either GIN or GiST — they overlap on purpose. GIN's exact posting lists make it the better choice for a read-heavy search workload where each lookup should intersect lists directly. GiST's lossy, bounding-box-based approach is cheaper to maintain on writes and is the only option once you need ordering (KNN via <->) rather than pure membership — which is why PostGIS defaults to GiST for geometry despite GIN existing. Choosing between them isn't "which is better," it's "do I need nearest-neighbor ordering, and can I afford GIN's write amplification."
BRIN vs B-tree: The 10,000× win depends on an invariant B-tree never needed
A B-tree's correctness and performance don't depend on how rows are physically ordered on disk — it maintains its own sorted structure regardless. BRIN's entire size advantage comes from not doing that: it trusts that physical order already correlates with the indexed column, and stores only a min/max per block range as a bet on that trust. When the bet is wrong — an out-of-order backfill, a multi-writer ingest path — BRIN doesn't error or warn, it just quietly stops pruning anything, and every query touching it degrades to something close to a full table scan.
Partial Indexes ← Planner statistics (see Reading the planner)
A partial index is only selected when the planner's row-estimate for the predicate is close to reality. If statistics are stale after a burst of writes — say, status = 'pending' briefly spikes from 0.1% to 20% of rows during an incident — the planner may conclude a sequential scan is cheaper and ignore a perfectly well-defined partial index. Running ANALYZE isn't a micro-optimization here; it's the precondition for the partial index to be used at all. See Reading the planner for how to read EXPLAIN output and confirm which plan the planner actually chose.
Self-Test
Question 1
You have a payments table with 50 million rows, 99.9% of which have status = 'completed'. Write the CREATE INDEX for the fastest possible query on pending payments. Explain why it beats a standard B-tree on status — and name one failure scenario where the planner would ignore your partial index even though it's defined.
Show answer
CREATE INDEX idx_payments_pending ON payments (created_at)
WHERE status = 'pending';
Why it wins: Only ~50,000 rows (0.1%) satisfy status = 'pending'. The partial index contains 50,000 entries vs. 50,000,000 in a full B-tree — 1,000× smaller. It fits in a handful of shared_buffer pages and stays cache-resident. It incurs zero write overhead when completed payments are updated (they don't match the predicate). The planner can do an index scan returning 50K rows with minimal I/O rather than scanning 50M rows for status.
Failure scenario: If statistics are stale and the planner believes 10% of rows are pending (5M rows), it will calculate the index scan as more expensive than a sequential scan for that many rows and ignore the partial index — even though it's perfectly defined and would be fast. Fix: run ANALYZE payments; so the planner knows the true 0.1% selectivity.
Question 2
You need an index on a jsonb column to support @> containment queries under a heavy write path (thousands of inserts/sec). Would you reach for GIN or GiST, and what concrete write-time tradeoff are you accepting?
Show answer
GIN — it's the canonical choice for jsonb containment (@>). The write-time tradeoff: every insert or update must add entries to the posting list of every distinct key/value pair in the document, which is far more write work per row than a B-tree's single key insert. fastupdate (on by default) buffers this into a pending list to absorb bursts, but that pending list must eventually flush — and if it grows large under sustained heavy writes, a flush can happen synchronously during a read, stalling that query behind the backlog.
Question 3
A logs table has a BRIN index on logged_at. A backfill job now bulk-inserts two years of historical rows out of chronological order. What happens to the BRIN index's effectiveness, and how would you detect the problem before dashboard queries silently start doing full scans?
Show answer
BRIN's block-range min/max values start overlapping once rows are no longer physically ordered by logged_at. A range that used to span one day might now span the whole two-year backfill, so WHERE logged_at BETWEEN ... can no longer skip it — BRIN degrades toward scanning nearly every block range, i.e., close to a full table scan.
There is no query-plan warning; EXPLAIN will still show the BRIN index "in use." Detect it before it bites by checking:
SELECT attname, correlation
FROM pg_stats
WHERE tablename = 'logs' AND attname = 'logged_at';A value that has dropped from near 1.0 toward 0 signals the index has lost its pruning power — re-cluster the table or drop BRIN in favor of a different index shape for that column.
Question 4
You have CREATE INDEX idx_jobs_pending ON jobs (created_at) WHERE status = 'pending'. A teammate writes a query with WHERE status != 'done' expecting it to use this index. Does it? Why or why not, and what would need to change for it to work?
Show answer
No, it does not use the index automatically. The planner requires the query's WHERE clause to syntactically imply the index predicate. status != 'done' is not syntactically the same as status = 'pending', even if the two are logically equivalent given a fixed, known set of status values.
For the index to be used, either the query must be rewritten to WHERE status = 'pending', or the index predicate itself must be changed to WHERE status != 'done' so it matches the query verbatim. Always confirm with EXPLAIN rather than assuming logical equivalence is enough.
Related pages
- MVCC, VACUUM & Query Planning → — Dead tuples, autovacuum, XID wraparound, and how to read EXPLAIN output — the mechanics that decide whether any index in this guide actually gets picked.