cloudparts

Block-range indexes (BRIN and zone maps)

A minimum and maximum, or another summary, for each run of adjacent pages, so a scan skips runs that can’t match: tiny, but only as good as the table’s physical order.

How they work

PostgreSQL’s BRIN (block range index) divides the table into ranges of pages_per_range adjacent pages, 128 by default, and stores one summary per range. For sortable types the summary is the minimum and maximum; inclusion classes instead store one value that contains every value in the range, such as a bounding box. A query reads the whole index, which is tiny, and marks every range whose summary could hold a match. It then reads every row in those ranges and rechecks each one against the condition. The index is lossy by design, so PostgreSQL uses it only for bitmap scans and never to return rows in order. Smaller ranges make the index bigger but cut the rows read and thrown away.

It works when a column follows the physical order of the rows: timestamps in an append-only table, IDs that grow with each insert. On 10 million time-ordered rows a BRIN index took 32 kB against 214 MB for a B-tree, and an hour’s query read one 128-page range. After the rows were shuffled (correlation near 0) the same query matched every range and read all 63,744 pages. Order can also decay. A row inserted into a summarized range widens its summary, and deleting rows never narrows it again. New ranges at the end of the table stay unsummarized, and every query reads them in full, until vacuum runs, brin_summarize_new_values() is called, or the index has autosummarize on (it is off by default). For data that isn’t neatly ordered, PostgreSQL 14 added minmax-multi classes, which keep several smaller intervals per range so a few outliers don’t stretch one interval across the table, and bloom classes for equality searches. Since PostgreSQL 16, updating a column that only BRIN indexes cover still allows a heap-only (HOT) update.

Analytic systems use the same idea under other names. Oracle calls it a zone map: a materialized view of each column’s minimum and maximum for every zone of contiguous blocks. ClickHouse has no row-level secondary indexes; its data skipping indexes keep, for example, a minmax per block of granules, which helps only when the indexed column correlates with the table’s sort key. Parquet files carry min/max statistics per column chunk and, with the page index, per page. Oracle Exadata storage servers keep in-memory storage indexes of each region’s column minimums and maximums and skip the I/O for regions a predicate rules out.

Block-range model
Pages per range
TABLE PAGESBRIN INDEX: ONE MIN–MAX PER RANGE00:0003:0006:0009:0012:0015:0018:0021:0024:00Range 0, pages 0–7: 00:00 to 01:27. Skipped.0–7Range 1, pages 8–15: 01:30 to 02:57. Skipped.8–15Range 2, pages 16–23: 03:00 to 04:27. Skipped.16–23Range 3, pages 24–31: 04:30 to 05:57. Skipped.24–31Range 4, pages 32–39: 06:00 to 07:27. Read, every row rechecked.32–39Range 5, pages 40–47: 07:30 to 08:57. Read, every row rechecked.40–47Range 6, pages 48–55: 09:00 to 10:27. Skipped.48–55Range 7, pages 56–63: 10:30 to 11:57. Skipped.56–63Range 8, pages 64–71: 12:00 to 13:27. Skipped.64–71Range 9, pages 72–79: 13:30 to 14:57. Skipped.72–79Range 10, pages 80–87: 15:00 to 16:27. Skipped.80–87Range 11, pages 88–95: 16:30 to 17:57. Skipped.88–95Range 12, pages 96–103: 18:00 to 19:27. Skipped.96–103Range 13, pages 104–111: 19:30 to 20:57. Skipped.104–111Range 14, pages 112–119: 21:00 to 22:27. Skipped.112–119Range 15, pages 120–127: 22:30 to 23:57. Skipped.120–127
WHERE ts >= '06:00' AND ts < '08:00'
Ranges read
2 of 16
Pages read
16 of 128 (13%)
Rows kept / thrown away
43 / 21
Correlation
1.00
page with matching rowsread, nothing matchedskippedrange summary
What you’re watchingA table of 512 readings, one every few minutes over a day, on 128 pages; real tables have far more pages, and PostgreSQL’s default range is 128 of them. Each row of the figure is one block range: its pages on the left and the one minimum–maximum the index keeps for it on the right. The query reads every range whose bar touches the shaded window and rechecks each row in it. Late rows stretch a range’s bar back in time, shuffling stretches every bar across the day, and a range that isn’t summarized yet is always read.

Sources

Improve this page on GitHub