BRIN
a min and a max per block range. absurdly small, and only useful when the table is physically sorted.
- address
- everything.index_internals.brin
- part
- part 13, inside indexes
- chapter
- chapter 10 of 12
- reading time
- 8 min
a BRIN index (block range index) stores one tiny summary per block range, 128 table pages by default: for a plain column, just the min and max in those pages. a search reads the whole index (a few pages), keeps the ranges whose min..max could match, and reads every page in them. it's absurdly small, and it only works when the column's values follow the table's physical order, like the timestamps of an append-only log.
the picture
you know how a b-tree has one entry per row? BRIN has one entry per hundred-odd pages. it doesn't point at rows at all. it just says "somewhere in pages 512 to 639, the values go from February 25th to March 11th".
think of a shelf of notebooks, one per fortnight, with the dates written on each spine. to find March 1st you don't need an index of every entry: you read the spines and open one notebook. that only works because the notebooks were filled in order. if someone had shuffled every page across all the notebooks, every spine would say "January to May", and you'd have to open them all.
here are both, from a real table of 200,000 readings, one a minute, inserted in time order, and a copy of the same rows in a shuffled order:
at, one bar per 128-page range from its min to its max. a query for one day (the hot band) only has to read the ranges it overlaps: one on the sorted table, every one on the shuffled copy.the whole index is three pages
SET TimeZone = 'UTC';
CREATE TABLE readings (id int, at timestamptz, temp int);
INSERT INTO readings
SELECT g, timestamptz '2026-01-01 00:00+00' + g * interval '1 minute', abs(hashint4(g)) % 40
FROM generate_series(1, 200000) AS g;
CREATE INDEX readings_brin ON readings USING brin (at);
CREATE INDEX readings_btree ON readings (at);
ANALYZE readings;
SELECT pg_relation_size('readings') / 8192 AS table_pages,
pg_relation_size('readings_brin') / 8192 AS brin_pages,
pg_relation_size('readings_btree') / 8192 AS btree_pages;| table_pagesint8 | brin_pagesint8 | btree_pagesint8 |
|---|---|---|
| 1274 | 3 | 551 |
at, in 8 KB pages, from the run block above. log scale: otherwise the BRIN bar is invisible.1,274 table pages, a 551-page b-tree, and a 3-page BRIN. the b-tree has an entry per row; BRIN has an entry per 128 pages, so 10 entries. page 0 is the metapage, page 1 the range map (revmap, one pointer per range to its summary), page 2 the summaries themselves:
SELECT pagesperrange, lastrevmappage FROM brin_metapage_info(get_raw_page('readings_brin', 0));
SELECT itemoffset, blknum, value
FROM brin_page_items(get_raw_page('readings_brin', 2), 'readings_brin')
ORDER BY blknum;| pagesperrangeint4 | lastrevmappageint8 |
|---|---|
| 128 | 1 |
| itemoffsetint4 | blknumint8 | valuetext |
|---|---|---|
| 1 | 0 | {2026-01-01 00:01:00+00 .. 2026-01-14 22:56:00+00} |
| 2 | 128 | {2026-01-14 22:57:00+00 .. 2026-01-28 21:52:00+00} |
| 3 | 256 | {2026-01-28 21:53:00+00 .. 2026-02-11 20:48:00+00} |
| 4 | 384 | {2026-02-11 20:49:00+00 .. 2026-02-25 19:44:00+00} |
| 5 | 512 | {2026-02-25 19:45:00+00 .. 2026-03-11 18:40:00+00} |
| 6 | 640 | {2026-03-11 18:41:00+00 .. 2026-03-25 17:36:00+00} |
| 7 | 768 | {2026-03-25 17:37:00+00 .. 2026-04-08 16:32:00+00} |
| 8 | 896 | {2026-04-08 16:33:00+00 .. 2026-04-22 15:28:00+00} |
| 9 | 1024 | {2026-04-22 15:29:00+00 .. 2026-05-06 14:24:00+00} |
| 10 | 1152 | {2026-05-06 14:25:00+00 .. 2026-05-19 21:20:00+00} |
ten ranges starting at block 0, 128, 256 … each with the earliest and latest at in its pages. because the rows went in in time order, the ranges are back to back: each one starts a minute after the last one ended.
a lossy bitmap
DROP INDEX readings_btree;
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT count(*) FROM readings WHERE at >= '2026-03-01' AND at < '2026-03-02';that's every BRIN scan. the Bitmap Index Scan reads the index (5 buffers) and marks whole ranges. its 1,280 "rows" isn't a count: BRIN can't count rows, so it reports ten for every page it marked, 128 × 10. the bitmap is lossy: it knows pages, not rows. so the Bitmap Heap Scan reads all 128 pages of the one matching range (Heap Blocks: lossy=128) and rechecks every row in them: 1,440 rows for March 1st (one per minute), 18,656 rows removed by the recheck. 133 page reads to find a day in a 1,274-page table, with a 3-page index. (lossy bitmaps in general.)
SELECT count(*) FROM shuffled WHERE at >= '2026-03-01' AND at < '2026-03-02';physically sorted, or useless
CREATE TABLE shuffled AS SELECT * FROM readings ORDER BY hashint4(id);
CREATE INDEX shuffled_brin ON shuffled USING brin (at);
ALTER TABLE shuffled ALTER COLUMN at SET STATISTICS 10000;
ALTER TABLE readings ALTER COLUMN at SET STATISTICS 10000;
ANALYZE shuffled, readings;
SELECT tablename, round(correlation::numeric, 2) AS correlation
FROM pg_stats WHERE attname = 'at' AND tablename IN ('readings', 'shuffled')
ORDER BY tablename;
SELECT itemoffset, blknum, value
FROM brin_page_items(get_raw_page('shuffled_brin', 2), 'shuffled_brin')
ORDER BY blknum;
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT count(*) FROM shuffled WHERE at >= '2026-03-01' AND at < '2026-03-02';
SET enable_seqscan = off;
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT count(*) FROM shuffled WHERE at >= '2026-03-01' AND at < '2026-03-02';
RESET enable_seqscan;| tablenamename | correlationnumeric |
|---|---|
| readings | 1.00 |
| shuffled | 0.00 |
| itemoffsetint4 | blknumint8 | valuetext |
|---|---|---|
| 1 | 0 | {2026-01-01 00:01:00+00 .. 2026-05-19 21:17:00+00} |
| 2 | 128 | {2026-01-01 00:05:00+00 .. 2026-05-19 21:18:00+00} |
| 3 | 256 | {2026-01-01 00:04:00+00 .. 2026-05-19 21:19:00+00} |
| 4 | 384 | {2026-01-01 00:16:00+00 .. 2026-05-19 21:04:00+00} |
| 5 | 512 | {2026-01-01 00:03:00+00 .. 2026-05-19 20:38:00+00} |
| 6 | 640 | {2026-01-01 00:52:00+00 .. 2026-05-19 21:16:00+00} |
| 7 | 768 | {2026-01-01 00:06:00+00 .. 2026-05-19 21:20:00+00} |
| 8 | 896 | {2026-01-01 00:02:00+00 .. 2026-05-19 21:15:00+00} |
| 9 | 1024 | {2026-01-01 00:09:00+00 .. 2026-05-19 21:11:00+00} |
| 10 | 1152 | {2026-01-01 00:11:00+00 .. 2026-05-19 21:08:00+00} |
correlation in pg_stats measures how well a column's order matches the table's physical order: 1.00 for the readings, 0.00 for the shuffled copy. the shuffled index's ranges all run from January 1st to May 19th. the planner reads that correlation and chooses a seq scan; forced to use the index, it marks all 1,280 pages lossy and rechecks 198,560 rows to find the same 1,440.
that's the whole rule: BRIN is for columns whose values grow with the table. timestamps on an append-only table, a serial id, a date the rows arrived in. it goes bad slowly as updates and deletes move rows around (an UPDATE writes the new version wherever there's room, maybe in an old range, and that range's max jumps forward), and that's the honest trade for its size.
pages_per_range
the 128 is a setting. smaller ranges are more precise, at the cost of a bigger index:
CREATE INDEX readings_brin16 ON readings USING brin (at) WITH (pages_per_range = 16);
DROP INDEX readings_brin;
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT count(*) FROM readings WHERE at >= '2026-03-01' AND at < '2026-03-02';
SELECT pg_relation_size('readings_brin16') / 8192 AS brin16_pages;| brin16_pagesint8 |
|---|
| 3 |
with 16-page ranges the day spans two ranges, 32 pages, and the recheck throws away 3,584 rows instead of 18,656. the index is still 3 pages here. on a table of a billion rows it would be eight times bigger than the 128-page one and still tiny next to a b-tree.
one level deepermore than min and max
the summary is up to the operator class. minmax is the default. minmax_multi pg 14+ keeps several small min/max intervals per range, so a range with a few outliers doesn't become "everything". bloom pg 14+ keeps a bloom filter of the values, for equality on data that isn't sorted but is clustered (the same few values per range): the filter says "definitely not in this range" or "maybe" (bloom filters have the math). inclusion stores the bounding box for ranges, inet and geometry. and since postgres 16, an UPDATE that only changes BRIN-indexed columns can still be HOT.
keylines
- BRIN keeps one summary per block range: by default the min and max of 128 pages.
- a search keeps the ranges that could match, and reads every page in them: a lossy bitmap, then a recheck.
- it's tiny: 3 pages against a 551-page b-tree on the same 200,000 rows.
- it only helps when values follow physical order. correlation 1.00: one range of ten; 0.00: all of them.
- smaller pages_per_range is more precise; unsummarized new pages are always read.