BRIN Index (Block-Range Summaries for Huge Datasets)
Last updated: 2026-11-04
BRIN Index (Block-Range Summaries for Huge Datasets)
BRIN (Block Range INdex) stores no per-row keys. It groups the table into ranges of pages (pages_per_range, 128 by default) and keeps one summary per group, so the index stays tiny and suits billion-row tables. The price is that BRIN is lossy: matching rows must be rechecked one by one in the heap, which shows up as Rows Removed by Index Recheck. It only prunes effectively when the physical row order correlates with the indexed column — the classic case is an append-only time-series or log table.
| Kind | index |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE INDEX <index_name> ON <table_name> USING brin ( <column> ) WITH ( pages_per_range = <pages> )
⚙️ Parameters
| index_name | Name of the new index. |
|---|---|
| table_name | The table to index. |
| column_name | The indexed column. Built-in support covers range types (range_inclusion_ops), minmax summaries for integer and timestamp columns, and inet inclusion summaries. |
| pages_per_range | Number of pages covered by each summary, 128 by default. Smaller values prune more precisely but grow the index; larger values shrink the index but may fail to exclude irrelevant pages. |
Returns:No return value. On success the command tag is CREATE INDEX.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
CREATE TABLE
INSERT 0 20000
CREATE INDEX
ANALYZE
SET
RESET
-- EXPLAIN: Bitmap Index Scan on idx_brin_span confirms the BRIN index is used
Aggregate (cost=390.05..390.06 rows=1 width=8)
-> Bitmap Heap Scan on idx_demo_brin (cost=12.03..390.03 rows=5 width=0)
Recheck Cond: (span @> 199995)
Rows Removed by Index Recheck: 4923
Heap Blocks: lossy=32
Buffers: shared hit=36 read=1
-> Bitmap Index Scan on idx_brin_span (cost=0.00..12.03 rows=20000 width=0)
Index Cond: (span @> 199995)
Index Searches: 1
Buffers: shared hit=4 read=1
Planning Time: 0.215 ms
Execution Time: 0.692 ms
-- Query result
count
-------
5
(1 row)
(The EXPLAIN plans above come from a real PostgreSQL 18.6 run; actual time / Planning Time / Execution Time are indicative values and vary with machine load.
Note: at only twenty thousand rows this table is far below the size where BRIN pays off, so the planner would normally choose a sequential scan. The example therefore uses SET enable_seqscan = off to force the index path and expose the BRIN plan. In production, with tens of millions of rows and an insertion order correlated with the indexed column, the planner picks BRIN on its own.)
📌 Key Points
- BRIN's value shows up on very large datasets: the index is a tiny fraction of a B-tree's size and costs far less to maintain on writes.
- A BRIN index is lossy: a summary can only say "might contain", so matching rows are rechecked one by one — hence Rows Removed by Index Recheck and lossy Heap Blocks.
- BRIN only prunes when the physical row order correlates with the indexed column. If values are scattered, the summary range covers everything and pruning fails completely.
- BRIN ships several operator classes: range_inclusion_ops for range types, minmax summaries for integers and timestamps, and inclusion summaries for inet.
- This example uses SET enable_seqscan = off to force the index path: at twenty thousand rows the planner would pick a sequential scan, and the switch exists purely to expose the BRIN plan.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QBRIN or B-tree — which should I pick?
AFor tens of millions of rows with write-heavy workloads and only a few indexed columns, BRIN's size and write costs win. For smaller tables, or when you need range lookups or a unique constraint, B-tree is the better choice. BRIN cannot back a unique constraint and supports far fewer operators.
QWhat does Rows Removed by Index Recheck mean?
AThe BRIN summary marks a whole page range as "might match", so the index scan returns more candidates than necessary; the heap recheck then discards most of them, and the plan reports how many were dropped. A large number means the summary is coarse — try a smaller pages_per_range.