Overview
Curated: · Written: · Reviewed:
Indexing & Query Performance
In one line
A B-tree is a sorted map from key to row location, so it serves equality, range and prefix predicates written against a bare indexed column. Reach for one when a selective predicate is slow, and only after reading the plan.
Key definitions
- Sargable: a predicate the engine can turn into a range over the index's sorted keys.
- Selectivity: the fraction of rows a predicate matches. An index wins only when that fraction is small.
- Index-only scan: answered from the index alone, valid only where the visibility map marks heap pages all-visible.
- Partial index: covers the rows matching its WHERE clause. The planner uses it only when the query predicate implies that clause.
- Expression index: indexes a computed value. The query must use the same immutable expression.
- Estimate/actual gap: the planner's row estimate against the real count from EXPLAIN ANALYZE. Check it first.
Formulas and complexity
How it works
The planner checks whether the predicate is sargable on the bare column, then estimates how many rows it matches. A seek into the sorted keys beats a sequential scan only when the match is small. Each matching entry then costs a heap fetch unless the scan is index-only. Composite indexes follow the same rule: equality columns narrow the seek, and a range column ends it.
When to use and when not to
| Situation | Index? | Why |
|---|---|---|
| Equality or range on a selective column | Yes, B-tree | Seek reads few pages |
| LIKE '%term%' | No B-tree | Needs trigram or full-text index |
| Boolean matching half the table | No | Sequential scan wins |
| Rare value of a boolean | Partial index | Small, and seekable |
| Write-heavy table, rare reads | Minimal | Each index taxes every insert |
| Predicate matches most rows | No | Sequential reads beat random lookups |
Traps interviewers probe
- Blaming the planner. Fix: compare estimate to actual. The scan may have won the arithmetic, or the statistics are stale or correlated.
- Wrapping the column in a function or arithmetic. Fix: rewrite as a half-open range on the bare column, or create an expression index that matches the predicate exactly.
- Putting a range column first in a composite. Fix: equality columns first, then the range or sort column, for example (tenant_id, created_at).
- Building a plain CREATE INDEX on a live table. Fix: CREATE INDEX CONCURRENTLY. A plain build takes a SHARE lock that blocks writes for the whole build. After a failed concurrent build, check pg_index.indisvalid and drop the invalid index.
- Trusting an index-only scan on a churned table. Fix: vacuum. Heap fetches in the plan show the visibility map is stale, and a new index will not fix that.
Interview tips
Open with the method: "I'd read the plan first, check estimate against actual, confirm the predicate is sargable on that index, and only then decide whether an index is the fix and what it costs."
Expect these follow-ups:
- Why didn't the planner use my index? Estimate gap, then selectivity, then sargability and operator class.
- What does it cost on writes? One entry per insert, one per update of an indexed column, plus WAL, storage and vacuum.
- What happens at 10x rows? Height grows by about one level, the selectivity crossover moves, and CONCURRENTLY becomes mandatory.
- How do you verify the fix? Re-run EXPLAIN ANALYZE and compare actual rows and time, not only the node type.
- How do you find unused indexes? pg_stat_user_indexes where idx_scan = 0, over a full workload cycle, ordered by size.
30-second outline: name the mechanism (sorted keys, seek versus scan), state the plan check (estimate versus actual, sargability), give the fix (composite order, expression index, or rewrite), and name the cost (write amplification and vacuum) with the verification step. Confirm the plan after the change, and report the actual rows and time it produced.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.