B-Tree, Hash, GIN, GiST & BRIN Indexes
Match index types to equality, range, full-text, geospatial, and append-only table access patterns.
In plain words
Equality, range, containment, or spatial. Next, match an index access method. Finally, plan use, size, update cost, and read time.
Think of it like…
A B-Tree is a sorted catalog, hash is a direct locker map, GIN is an inverted word index, GiST is a shape-aware organizer, and BRIN is a row of box labels for sequential shelves.
Where you’ve seen it
CREATE INDEX ON orders (tenant_id, created_at DESC) WHERE status='open' serves tenant status queries compactly; GIN serves JSONB @> while BRIN on append-only event_time stores tiny min/max ranges per block.
Words you’ll hear
- B-Tree
- A balanced ordered index suited to equality, prefix, range, and ordering operations with logarithmic lookup depth.
- GIN/GiST
- Indexes for multi-component searchable values and extensible tree classes, commonly JSON/full-text arrays and spatial/range operators.
- BRIN
- A block range index storing min/max summaries that is tiny and fast when physical table order matches the query range.
- Operational boundary
- Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key. Start from actual predicates and
EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.
Visual diagram
Theory & deep dive
B-Tree: the working mechanism
B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables.
Equality, range, containment, or spatial. Match an index access method, then row ordering and correlation; this exposes the state governed by BRIN.
CREATE INDEX ON orders (tenant_id, created_at DESC) WHERE status='open' serves tenant status queries compactly; GIN serves JSONB @> while BRIN on append-only event_time stores tiny min/max ranges per block.
B-Tree, Hash, GIN, GiST & BRIN Indexes: failure mode and production response
Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column. Track B-Tree prefix as "only a leftmost prefix is independently searchable", and make BRIN correlation visible to the dataset owner.
EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.How it works, step by step
Step 1 of 41. Classify predicate
Equality, range, containment, or spatial. B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables.
- B-Tree prefix
- only a leftmost prefix is independently searchable
- GIN/GiST
- Indexes for multi-component searchable values and extensible tree classes, commonly JSON/full-text arrays and spatial/range operators.
When to use it & trade-offs
What it is
B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables. CREATE INDEX ON orders (tenant_id, created_at DESC) WHERE status='open' serves tenant status queries compactly; GIN serves JSONB @> while BRIN on append-only event_time stores tiny min/max ranges per block.
Why it matters: Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key. Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.
When to reach for it
- Match index types to equality, range, full-text, geospatial, and append-only table access patterns.
- Choose B-Tree because it provides this capability: A balanced ordered index suited to equality, prefix, range, and ordering operations with logarithmic lookup depth.
- Keep evidence for B-Tree prefix at "only a leftmost prefix is independently searchable" so the last production stage-Measure - plan use, size, update cost, and read time-remains testable.
When to avoid or be careful
- Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
- Do not conflate GIN/GiST with B-Tree. The second concept contributes different behavior: Indexes for multi-component searchable values and extensible tree classes, commonly JSON/full-text arrays and spatial/range operators.
- Release only after this guardrail is in place: Start from actual predicates and
EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.
B-Tree / GIN/GiST / BRIN - roles, limits, and operating cost
Strengths: A balanced ordered index suited to equality, prefix, range, and ordering operations with logarithmic lookup depth. B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables.
Watch out: Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
Strengths: Indexes for multi-component searchable values and extensible tree classes, commonly JSON/full-text arrays and spatial/range operators. In this workflow it supports: Model locality - row ordering and correlation.
Watch out: The operating cost is real: high table-value order makes range summaries useful.
Strengths: A block range index storing min/max summaries that is tiny and fast when physical table order matches the query range. Its output can be checked with Index cost.
Watch out: Operating guardrail: Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for B-Tree, and how would B-Tree prefix reveal it?”
- “Why is GIN/GiST distinct from this behavior: a block range index storing min/max summaries that is tiny and fast when physical table order matches the query range?”
- Production clue: Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
- Design probe: Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.
Benchmarks & rules of thumb
- B-Tree prefix
- only a leftmost prefix is independently searchable
- BRIN correlation
- high table-value order makes range summaries useful
- Index cost
- measure bytes written and maintenance on every UPDATE
Common mistakes & follow-ups
Common interview mistakes
- Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
- Reporting success from a job exit code without checking B-Tree prefix and BRIN correlation.
- Treating this example as complete without replay, rollback, or access boundaries:
CREATE INDEX ON orders (tenant_id, created_at DESC) WHERE status='open'serves tenant status queries compactly; GIN serves JSONB@>while BRIN on append-onlyevent_timestores tiny min/max ranges per block.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: A balanced ordered index suited to equality, prefix, range, and ordering operations with logarithmic lookup depth.
- What does the recovery plan do when this occurs? Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
- When is B-Tree insufficient for BRIN correlation, and what does adding GIN/GiST cost in B-Tree prefix?
EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.Knowledge check
Put it into practice
Practice related interview Q&A
Apply B-Tree, Hash, GIN, GiST & BRIN Indexes with data engineer interview Q&A and hands-on practice.