Quick revision
The short answer
Probably yes for the range queries. A BRIN index on recorded_at stores only min and max per block range, so it would be a few megabytes instead of 90 GB, and it works because append-only inserts keep physical order close to time order (pg_stats.correlation near 1). We test one-hour and one-day queries with both indexes, remembering that BRIN returns candidate block ranges and rechecks rows, so point lookups stay slower. Late data, updates and out-of-order backfills widen the ranges and can make BRIN useless; time partitioning with BRIN on old partitions and a B-tree on the current one combines the best of both.
What to remember
In this answer, we saw that a BRIN index on recorded_at stores min and max per block range, 128 pages by default, so it is a few megabytes instead of 90 GB. It works only while physical order follows time, which pg_stats.correlation near 1 confirms for an append-only table. We tested hour and day ranges with both indexes and saw that BRIN returns a lossy bitmap and rechecks rows, so point lookups remain slower. Late data, updates and out-of-order backfills widen ranges and can make BRIN useless, and time partitioning with BRIN on old partitions and a B-tree on the current one handles both risks.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Probably yes for the range queries. A BRIN index on recorded_at stores only min and max per block range, so it would be a few megabytes instead of 90 GB, and it works because append-only inserts keep physical order close to time order (pg_stats.correlation near 1). We test one-hour and one-day queries with both indexes, remembering that BRIN returns candidate block ranges and rechecks rows, so point lookups stay slower. Late data, updates and out-of-order backfills widen the ranges and can make BRIN useless; time partitioning with BRIN on old partitions and a B-tree on the current one combines the best of both.
Why this question is asked
A 90 GB index on a timestamp is a lot of disk, memory and write cost for queries that always ask for a contiguous slice of time. BRIN exists for exactly this shape of data, and interviewers ask the question to see whether we know both why it fits and what can silently break it. In this answer, we will recap how BRIN works, check that the data suits it, size the index, design the test, look at what makes BRIN useless, and combine it with partitioning.
1A Quick Recap of How BRIN Works
BRIN, block range index, divides the table into consecutive groups of pages. By default each group, called a block range, is 128 pages of 8 KB, so 1 MB of table. For each range, the index stores a summary: for a timestamp, the minimum and maximum value found in that range. That is all. There is no entry per row.
For a query WHERE recorded_at BETWEEN '2026-11-01 10:00' AND '2026-11-01 11:00', PostgreSQL reads the whole small index, marks every range whose min-max interval overlaps the hour, and builds a bitmap of those pages. Then it reads those pages and rechecks each row against the condition, because a range that overlaps the hour can still contain rows outside it.
| Block range (pages) | min(recorded_at) | max(recorded_at) | Read for 10:00-11:00? |
|---|---|---|---|
| 0-127 | 2026-11-01 09:41 | 2026-11-01 09:58 | no |
| 128-255 | 2026-11-01 09:58 | 2026-11-01 10:16 | yes |
| 256-383 | 2026-11-01 10:16 | 2026-11-01 10:33 | yes |
| ... | ... | ... | yes, through 11:00 |
| 2048-2175 | 2026-11-01 07:02 | 2026-11-03 18:40 | yes, but a late backfill widened it |
2Checking That the Data Suits BRIN
BRIN only helps when values that are close in time are also close on disk. For an append-only table where sensors insert readings as they happen, new rows go to the end of the table, so this usually holds. The planner's statistics measure it as correlation, between -1 and 1, the agreement between physical row order and column order.
A correlation of 0.998 means the table is almost perfectly in time order. The size follows from the design: 3 billion rows on, say, 30 million pages is about 234,000 block ranges, each summary a few dozen bytes, so a few megabytes. Compare that with 90 GB for the B-tree, which also has to be updated on every insert.
3Designing the Test
We test on a copy with production data, with both indexes available and each one disabled in turn. The queries are the real ones: one hour, one day, and any point lookups such as 'the reading at exactly this timestamp for this sensor'. We run each several times to see cold and warm cache behaviour, using EXPLAIN (ANALYZE, BUFFERS) to compare pages read and time.
For hour and day ranges, BRIN usually matches the B-tree closely, because both end up reading the same contiguous heap pages, and BRIN reads a tiny index instead of a large one. For point lookups, BRIN is slower: it must read and recheck at least a whole block range, 128 pages, to find a few rows, where a B-tree goes straight to them. If the workload has many point lookups, we keep a B-tree for them, perhaps a smaller composite one on (sensor_id, recorded_at).
4What Could Make BRIN Useless
BRIN depends on order, so anything that breaks order hurts it. Late-arriving data, such as a sensor that was offline for a day and uploads its buffer, lands at the end of the table with old timestamps, which stretches that range's min-max to cover two days. Updates in PostgreSQL write new row versions wherever there is space, possibly in an old range. A bulk backfill of last year's data loaded into a non-partitioned table puts year-old values into the newest ranges.
Each of these widens some ranges until they overlap almost every query. Queries then read many pages, recheck millions of rows and lose most of the benefit. A small number of wide ranges is fine; many of them make BRIN no better than a sequential scan. The correlation statistic falling over time is the early warning. The minmax_multi operator class, available since PostgreSQL 14, stores several intervals per range and tolerates some outliers better; check your version.
5Combining BRIN With Time Partitioning
Time partitioning, one partition per day or month, fixes most of the risks. Late data goes into the partition of its own date, so it cannot widen today's ranges. Backfills load into their own partitions. Old partitions can be dropped instead of deleted. And each partition can have the index that suits its use: the current partition, where point lookups and fresh data are common, keeps a B-tree; older partitions keep only BRIN, which costs almost nothing.
- readings_2026_11 (current)B-tree on (sensor_id, recorded_at) + BRIN
- readings_2026_10BRIN only, a few MB
- readings_2026_09 and olderBRIN only; drop whole partitions when retention ends
3 layers, from the top down. Step through them one at a time.
6Follow-up questions
How do you summarize new block ranges without waiting for vacuum?
New pages at the end of the table are not summarized until vacuum or autovacuum processes the table, so for a while queries must read those unsummarized ranges in full. We can call SELECT brin_summarize_new_values('readings_recorded_brin') to summarize all unsummarized ranges immediately, or brin_summarize_range for a single range. We can also create the index WITH (autosummarize = on), so a request to summarize the previous range is queued for autovacuum as soon as inserts move on to a new range. On a table that receives a steady stream, autosummarize keeps the index current with no manual step.
What does pages_per_range control?
It sets how many heap pages each summary covers. A smaller value, such as 32, gives narrower ranges, so queries skip more precisely and recheck fewer rows, at the cost of a larger index. A larger value makes the index smaller but each match reads more pages. 128 is a sensible default; we tune it only if the test shows many rows removed by recheck.
7Conclusion
In this answer, we saw that a BRIN index on recorded_at stores min and max per block range, 128 pages by default, so it is a few megabytes instead of 90 GB. It works only while physical order follows time, which pg_stats.correlation near 1 confirms for an append-only table. We tested hour and day ranges with both indexes and saw that BRIN returns a lossy bitmap and rechecks rows, so point lookups remain slower. Late data, updates and out-of-order backfills widen ranges and can make BRIN useless, and time partitioning with BRIN on old partitions and a B-tree on the current one handles both risks.
Open with
Very likely, yes, for the range queries. A BRIN index on recorded_at stores just the minimum and maximum per block range, which is 128 pages by default, so on this table it would be a few megabytes instead of 90 GB, and it costs almost nothing on insert.
Then, if they want more
It works because the table is append-only by time, so physical order follows recorded_at. I would confirm that with pg_stats correlation, which should be close to 1. Then I would test the real queries, one hour and one day, with EXPLAIN ANALYZE BUFFERS on a production copy, with each index disabled in turn.
For ranges, BRIN should be close to the B-tree, since both read the same heap pages. BRIN returns a lossy bitmap of block ranges and rechecks rows, so point lookups are slower, and if we have many of those I would keep a smaller B-tree for them.
What could break BRIN is anything that destroys the order: late-arriving data, updates that move rows, or out-of-order backfills. They widen ranges until many queries read most pages. Correlation dropping is the warning sign.
The way I would protect against that is time partitioning: late data and backfills go to their own partitions, the current partition keeps a B-tree for hot lookups, and older partitions only need BRIN. I would also enable autosummarize so new ranges are summarized quickly.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 14
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An append-only sensor_readings table has 3 billion rows and a 90 GB B-tree on...": https://scrivio-interview-library.netlify.app/databases/indexing/b5f7021d3c/