The idea · Lab 3.2.5A B-tree stores one entry per row, which is precise but expensive at real scale — a 500-million-row table's B-tree index can genuinely run into tens of gigabytes. BRIN (Block Range Index) takes a fundamentally different, much cheaper approach: instead of one entry per row, it stores one summary — just a min and max value — per contiguous range of physical table pages. For a naturally-ordered column like an append-only created_at timestamp, that summary is nearly as useful as a full B-tree for range queries, at a tiny fraction of the storage and maintenance cost, because rows inserted close together in time also live close together on disk.
That last clause is the whole story, and also the catch: BRIN's usefulness depends entirely on the data's physical order matching the column being indexed. When that correlation breaks — rows updated out of order, or a column with no natural relationship to insertion order — a page range's min/max summary stops being narrow and starts covering almost the entire value domain, and the index stops eliminating anything useful. This lab measures both the real win and the real limit, plus a second, differently-shaped index type: Hash, built for equality alone, and genuinely unable to serve a range query at all.
BRIN vs B-tree, Measured
CREATE INDEX idx_ts_brin ON idx_timeseries USING brin(created_at) WITH (pages_per_range=128);
CREATE INDEX idx_ts_btree ON idx_timeseries(created_at);
SELECT pg_size_pretty(pg_relation_size('idx_ts_brin')), pg_size_pretty(pg_relation_size('idx_ts_btree'));
brin_size | btree_size
-----------+------------
24 kB | 11 MB
Roughly 458 times smaller, on 500,000 physically time-ordered rows. pages_per_range=128 means each BRIN summary entry covers 128 table pages at once — one min/max pair standing in for a whole block of rows, instead of the one-entry-per-row cost a B-tree pays.
BRIN, Genuinely Used for a Range Query
EXPLAIN (ANALYZE, TIMING OFF) SELECT * FROM idx_timeseries WHERE created_at BETWEEN '2025-01-02' AND '2025-01-03';
Bitmap Heap Scan on idx_timeseries (actual rows=86401.00 loops=1)
Recheck Cond: (...)
Rows Removed by Index Recheck: 26027
Heap Blocks: lossy=768
-> Bitmap Index Scan on idx_ts_brin (actual rows=7680.00 loops=1)
Execution Time: 433.628 ms
"lossy" is the key word — BRIN's bitmap is not exact per row, only per page range, so PostgreSQL fetches every row in every flagged range and rechecks the real condition, discarding the 26,027 rows that fell inside a matching range's min/max but didn't actually qualify. This is genuinely more rechecking than a B-tree needs, but the index itself cost 458 times less to build and maintain.
The Same Column, Out of Physical Order
Insert the identical 500,000 rows with created_at values assigned randomly instead of sequentially, then build the identical kind of BRIN index and run the identical query:
Seq Scan on idx_ts_shuffled (actual rows=86260.00 loops=1)
Filter: (...)
Rows Removed by Filter: 413740
The planner does not even choose the BRIN index this time — with created_at values scattered randomly across every physical page, each page range's min/max summary now spans nearly the entire table's value domain, so almost every range would need to be checked anyway. BRIN did not give a wrong answer; it simply stopped being able to eliminate anything, and the planner correctly recognized a Seq Scan was cheaper than a bitmap scan that would have to check almost every page regardless.
Hash: Equality Only, By Design
CREATE INDEX idx_hashonly_idx ON idx_hashonly USING hash(id);
EXPLAIN SELECT * FROM idx_hashonly WHERE id = 12345; -- Index Scan using idx_hashonly_idx
EXPLAIN SELECT * FROM idx_hashonly WHERE id > 12345 AND id < 12400; -- Seq Scan
A Hash index stores entries by hash value, with no relationship between a row's hash and its neighbors' — perfect for "find this exact value" and structurally unable to answer "find values in this range" at all, unlike a B-tree, which supports both.
Key concepts
BRIN (Block Range Index)
An index storing one summary value (typically a min/max pair) per contiguous range of physical table pages, rather than one entry per row. Dramatically smaller and cheaper to maintain than a B-tree, but only useful when the indexed column's values correlate strongly with physical row order — an append-only timestamp being the canonical case. A BRIN scan is inherently "lossy": it identifies candidate page ranges, then rechecks every row in them against the real condition.
Hash index
An index storing entries by the hash of the indexed value, supporting equality lookups only — there is no ordering relationship between nearby hash values and nearby original values, so range queries, sorting, and pattern matching are all structurally impossible with a Hash index. Since PostgreSQL 10, Hash indexes are WAL-logged and crash-safe, making them a legitimate (if narrow) choice specifically for high-volume equality-only lookups.
What to remember
- BRIN trades per-row precision for enormous size savings by summarizing whole page ranges — a strong fit specifically for naturally-ordered, append-mostly data
- A BRIN scan is inherently lossy: it identifies candidate page ranges and rechecks every row in them, which is real overhead in exchange for the size win
- BRIN never returns an incorrect result on out-of-order data — it simply stops eliminating anything useful, and the planner correctly falls back to another plan
- Hash indexes support equality only, by structural design — no column reordering, tuning, or data reorganization makes a Hash index support a range query
- Troubleshooting a BRIN index that stopped helping means investigating whether the data's physical order changed; troubleshooting a Hash index for a range query means recognizing it was never going to work for that query shape at all