How to Use BRIN Indexes for Large Tables
Use BRIN indexes for huge, naturally ordered tables: tiny size, cheap maintenance and fast range scans when the data correlates with its position.
On this page
Introduction
A B-tree index on a table with billions of rows is large, and maintaining it costs writes and disk. BRIN (Block Range INdex) is a different bet: instead of indexing every value, it stores a summary per range of physical blocks, such as the min and max value in that range. For a table whose rows are inserted in a naturally ordered way, an append-only log, a time-series, an events table, the block ranges correlate with the value, so a range query can skip most of the table. The index is tiny and cheap to maintain.
How BRIN Works
BRIN divides the table’s physical blocks into ranges and stores, for each range, a summary of the indexed column, typically min and max. A query for a range of values consults the summaries and reads only the block ranges that could contain matches, skipping the rest. It is an index over physical location, not over values.
The crucial property is correlation: for BRIN to help, the indexed value must correlate with physical order, so that a value range maps to a small set of block ranges. Insert data sorted by the indexed column and the correlation is high; insert it randomly and every block range contains the whole value range, so the index cannot skip anything.
-- A tiny index over block ranges, ideal for append-only time-series.
CREATE INDEX CONCURRENTLY events_created_brin
ON events USING brin (created_at)
WITH (pages_per_range = 32);
-- correlation between value and physical order decides its usefulness:
SELECT attname, correlation
FROM pg_stats
WHERE tablename = 'events' AND attname = 'created_at';
-- correlation near 1.0 => BRIN can skip; near 0 => it cannot
When BRIN Beats B-Tree
BRIN wins when the table is very large, the indexed column correlates with physical order, and the query is a range scan rather than a point lookup. On an append-only events table queried by time range, BRIN may be megabytes where a B-tree is gigabytes, and it is far cheaper to update on every insert.
BRIN loses on point lookups and on weakly correlated data. A query for a single id wants a B-tree; a query over a non-correlated column cannot use BRIN, because the summaries cover the whole value range. Evaluate BRIN against the actual query patterns and the correlation of the column, not against a general rule.
# BRIN is a good fit when
[ ] table is very large (many GB+)
[ ] indexed column correlates with physical order (append-only)
[ ] queries are range scans on that column
# BRIN is a poor fit when
[ ] point lookups dominate (use B-tree)
[ ] the column is randomly ordered (no pruning possible)
[ ] the table is small (a B-tree is fine and more flexible)
pages_per_range and Correlation
pages_per_range controls how many blocks share a summary. Smaller ranges give more precise skipping but a larger index; larger ranges give a tinier index but coarser skipping. The default is 128 blocks. Tune it to the query: precise range queries favour smaller ranges, and a table with high correlation tolerates larger ones.
Because the benefit depends on correlation, monitor it. If the table’s insert pattern changes and correlation falls, BRIN’s effectiveness drops and may disappear. Re-check the correlation after bulk loads or reordering, and if it stays low, switch to a B-tree or partition the table instead.
-- Smaller pages_per_range => finer pruning, larger index.
CREATE INDEX events_created_brin_fine ON events USING brin (created_at)
WITH (pages_per_range = 8); -- finer than the default 128
-- After a bulk load that may have changed physical order:
VACUUM ANALYZE events; -- refresh stats, including correlation
-- then re-check pg_stats.correlation and re-evaluate the index
Maintenance and Vacuum
BRIN has a summarising mechanism for recently added pages so it does not need a full rebuild on every write; a maintenance step (during VACUUM or an explicit call) folds new pages into the summaries. This makes it cheap to maintain under continuous inserts, which is why it suits append-only tables.
The index is only as good as its summaries. If the summaries are stale, the index reads more ranges than necessary. Keep autovacuum healthy on the table so summaries are refreshed, and after a large bulk load, vacuum and analyze to update both the summaries and the correlation statistics the planner uses.
-- Explicit summarisation of new ranges (usually handled by autovacuum).
SELECT brin_summarize_new_values('events_created_brin');
-- Keep autovacuum aggressive enough on a high-write table:
ALTER TABLE events SET (autovacuum_vacuum_scale_factor = 0.02);
-- ensures summaries and stats refresh, keeping BRIN effective
BRIN for Multi-Column Correlation
BRIN can index multiple columns, but it is most useful when a single column correlates strongly with physical order. A composite BRIN helps when the leading column defines the physical order and the second narrows within it, such as a tenant-partitioned append log where (tenant_id, created_at) matches the insert order.
Do not expect BRIN to behave like a composite B-tree. It still summarises ranges; a composite index only helps if the ranges remain selective for the combination. Test with EXPLAIN against the real queries before committing to a composite BRIN.
-- Composite BRIN when inserts follow (tenant_id, created_at) order.
CREATE INDEX sessions_brin ON sessions USING brin (tenant_id, created_at)
WITH (pages_per_range = 32);
-- verify it actually prunes for your query:
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM sessions WHERE tenant_id = $1 AND created_at > now() - interval '1 hour';
BRIN Versus Partitioning
Partitioning by time and using a BRIN index on the partition key often overlap. Partitioning gives pruning at the table level, dropping entire partitions from the plan; BRIN gives pruning at the block level within a table. For very large time-series, partitioning is usually the stronger tool because it also enables cheap retention through detaching old partitions.
BRIN is attractive when you do not want the operational complexity of partitioning, or when the natural order is not the partition key. A large events table with one BRIN index is far simpler to operate than one with monthly partitions, at the cost of coarser pruning and no cheap retention.
# partitioning vs BRIN for a big time-series table
partitioning: table-level pruning + cheap retention (detach/drop)
more operational complexity (partition creation)
BRIN: block-level pruning, tiny index, no retention benefit
far simpler to operate
# choose partitioning for retention + strongest pruning
# choose BRIN for simplicity when retention is handled elsewhere
Common Pitfalls and Conclusion
Do not put a BRIN on a randomly ordered column and expect it to help; without correlation there is nothing to skip. Do not use BRIN for point lookups. Do not forget to check correlation after the insert pattern changes. Do not leave summaries stale under heavy insert load. And do not expect BRIN to compete with an index-only scan on a B-tree for point queries.
BRIN trades precise per-value lookup for a tiny, cheap-to-maintain summary of block ranges, and it pays off exactly when the data is large and naturally ordered so the ranges correlate with the values. Check the correlation, tune pages_per_range, keep the summaries fresh, and prefer partitioning when you also need retention. Then a billion-row table gets useful pruning from an index measured in megabytes.
Sources: PostgreSQL: BRIN indexes, PostgreSQL: Index types, PostgreSQL: Planner statistics.
FAQ
- When is a BRIN index the wrong choice?
- For point lookups (a B-tree is better) and for columns whose values do not correlate with physical order, where the block-range summaries span the whole value range and cannot skip anything. Small tables do not benefit either.
- How do I know if BRIN is helping?
- Check pg_stats.correlation for the indexed column: near 1.0 means values correlate with physical order and BRIN can prune; near 0 means it cannot. Confirm with EXPLAIN that the plan reads only the relevant block ranges.
Report a problem with this articleOpens a pre-filled issue on GitHub.