Scrivio Interview library
ScenarioIntermediate8 min read · updated 12 November 2026

A PostgreSQL table events(80 columns) holds 600 million rows. Analysts run queries like SELECT date_trunc('day', ts), COUNT(*), SUM(value) FROM events WHERE ts > now() - interval '90 days' GROUP BY 1 and each takes 10 minutes. The product team does not want a new database. What options do you have, and what do you recommend?

The query is slow because PostgreSQL is a row store: to sum one column over 90 days it reads every 80-column row in that range, so almost all the I/O is wasted. Without a new database, the biggest and safest win is a pre-aggregated daily rollup table, backed by time partitioning and a BRIN index on ts. A columnar extension or an export to Parquet and DuckDB comes next, only if the variety of queries keeps growing.

Why this question is asked

Sooner or later every product database gets an analyst who wants totals over months of events. The query looks innocent, the table is the right one, and it still takes ten minutes. Interviewers ask this scenario because the answer shows whether we understand how a storage engine lays out data on disk, and whether we can improve things without the easy but expensive answer of 'buy a warehouse'. In this answer, we will see why a row store struggles with this query, go through the options PostgreSQL itself offers, look at columnar extensions and external engines, and then decide what to do first and why.

1A Quick Recap of Row Stores and Column Stores

PostgreSQL stores a table as a heap of 8 KB pages. Each page holds whole rows, called tuples: all 80 columns of one event sit next to each other on disk. This layout is ideal for the work a product database usually does, such as reading one order by its id or inserting a new event, because one page read brings back everything about that row.

A column store turns the layout around. It keeps all the values of one column together, so the ts values for millions of rows sit in one block and the value column in another. A query that needs two columns reads only those two, and because similar values sit next to each other they compress very well, often 5 to 10 times.

Our analytics query needs exactly two columns, ts and value. In a row store, every row that matches the 90-day filter is read in full, so roughly 78 of 80 columns travel from disk to memory for nothing.

Where the bytes go for this query
LayoutData read for 150M rowsWhy
Row store (PostgreSQL heap)about 60 GBall 80 columns of every matching row
Column storeabout 2 GB before compressiononly ts and value, 8 bytes each
Daily rollup table90 rowsthe answer is already computed
Rough numbers for 400-byte rows; the point is the ratio, not the exact sizes.

Notice that this is not a missing index problem. Even a perfect index on ts only tells PostgreSQL which rows to read; it still has to read each of those rows as a whole tuple. If 90 days hold, say, 150 million events of 400 bytes each, that is about 60 GB to scan for a result of 90 rows.

2Option 1: Partition by Time and Add a BRIN Index

The first lever inside PostgreSQL is to stop reading data the query does not ask for. With declarative partitioning we split events into one partition per month (or per day at this volume). When the WHERE clause says ts > now() - interval '90 days', the planner skips every partition that ends before that date. This is called partition pruning, and it turns a scan of 600 million rows into a scan of the last three or four partitions.

Inside each partition, a BRIN index helps. BRIN stands for block range index: instead of one entry per row like a B-tree, it stores the minimum and maximum ts for each range of 128 pages. Events are inserted roughly in time order, so those ranges barely overlap, and PostgreSQL can skip whole ranges cheaply. A BRIN index on a huge table is often only a few megabytes.

SQL
CREATE TABLE events (
id bigint, ts timestamptz NOT NULL, value numeric, ... -- 80 columns
) PARTITION BY RANGE (ts);
CREATE TABLE events_2026_10 PARTITION OF events
FOR VALUES FROM ('2026-10-01') TO ('2026-11-01');
CREATE INDEX ON events USING brin (ts); -- created on every partition

But there is a catch: partitioning and BRIN reduce how many rows we read, not how wide they are. The query gets faster, maybe from ten minutes to two or three, but it still reads full 80-column tuples for every event in the last 90 days.

3Option 2: Pre-Aggregate Into a Daily Rollup

The analysts ask the same shape of question again and again: counts and sums per day. Instead of computing that from raw events every time, we can compute it once and store it. A rollup table holds one row per day (and per any dimension the reports need, such as country or event type).

SQL
CREATE TABLE events_daily (
day date PRIMARY KEY, event_count bigint, value_sum numeric
);
-- run every few minutes: recompute only the recent days
INSERT INTO events_daily (day, event_count, value_sum)
SELECT date_trunc('day', ts)::date, count(*), sum(value)
FROM events
WHERE ts >= current_date - 2
GROUP BY 1
ON CONFLICT (day) DO UPDATE
SET event_count = EXCLUDED.event_count, value_sum = EXCLUDED.value_sum;
SELECT day, event_count, value_sum FROM events_daily
WHERE day > current_date - 90; -- 90 rows, a few milliseconds

Notice that the refresh job only recomputes the last two days, so it reads a small slice of events each time, while the analyst query reads 90 tiny rows. A materialized view can do the same job, but REFRESH MATERIALIZED VIEW recomputes the whole view each time, so for 600 million rows an incremental table like this one is usually the better choice.

The cost is that the rollup only answers the questions we designed it for. If an analyst wants totals by a column we did not include, they are back to the raw table. That is the real trade-off, and it is the reason we look at columnar storage next.

4Option 3: Columnar Storage Inside PostgreSQL

PostgreSQL lets extensions provide their own table access methods, so a table can be stored in a different layout. Citus offers a columnar access method, and other extensions and forks provide columnar tables too. With one of them, a copy of the events table can be stored by column, and the analytics query reads only ts and value, compressed.

Before choosing this, we check three things. How mature is the extension, and does it support updates and deletes, or only appends? Does it support our PostgreSQL major version, so we are not stuck on an old release? And, if we run on a managed service, does the provider allow it at all?

5Option 4: Move Analytics Off the OLTP Database

If the analysts' questions keep multiplying, the honest long-term answer is to stop running them on the database that serves users. We can export events daily (or stream them) into Parquet files, an open columnar file format, in object storage. DuckDB, an in-process analytical engine, can query those files directly with plain SQL, and a cloud warehouse can do the same at larger scale.

SQL
-- in DuckDB, over files exported from PostgreSQL
SELECT date_trunc('day', ts) AS day, count(*), sum(value)
FROM read_parquet('s3://analytics/events/*.parquet')
WHERE ts > now() - INTERVAL 90 DAY
GROUP BY 1; -- reads only the ts and value columns

6What We Would Recommend, and in What Order

We would start with the rollup table. It gives the biggest improvement, from minutes to milliseconds, for the queries the analysts actually run, it uses nothing but plain PostgreSQL, and it is easy to undo. In the same project we would partition the events table by time, because it helps the rollup refresh, makes retention as cheap as dropping an old partition, and keeps vacuum work small.

Then we watch what the analysts ask next. If most questions fit a handful of rollups, we stop there. If the questions vary widely, we choose between a columnar extension (if our platform allows a mature one) and exporting to Parquet with DuckDB or a warehouse. The decision is driven by query variety, not by the size of the table alone.

Step throughChoosing the next step
  1. Do most queries fit a few fixed shapes?
    YesBuild rollup tables and stopNoLook at columnar options
  2. Is a mature columnar extension allowed here?
    YesUse a columnar copy of eventsNoExport to Parquet for DuckDB or a warehouse
  3. Partition events by time either waypruning, cheap retention, smaller vacuums

3 steps in order. Play them, or step through them one at a time.

7Follow-up questions

How would you keep rollup tables correct when late events arrive?

We recompute a window of recent days on every run rather than only today, sized to how late events really arrive; if 99.9 percent arrive within 48 hours, we recompute the last three days. For rare very late events, we can record the affected days in a small table (from a trigger or from the ingest code) and let the refresh job recompute exactly those days. Because the refresh uses INSERT ... ON CONFLICT DO UPDATE with full recomputation of each day, running it twice is harmless.

Would a B-tree index on ts help as much as BRIN?

A B-tree would let PostgreSQL find the matching rows, but for 150 million matching rows the planner would still choose a sequential or bitmap scan, and the index itself would be many gigabytes. BRIN gives most of the skipping benefit for time-ordered data at a tiny size. Neither changes the fact that full rows are read.

8Conclusion

In this answer, we saw that the ten-minute query is slow because a row store reads every column of every row it touches, so most of the I/O is wasted. Inside PostgreSQL we can read fewer rows with time partitioning and a BRIN index, and we can avoid reading raw rows at all with incrementally refreshed daily rollups. Columnar extensions and Parquet with DuckDB or a warehouse fix the layout itself. We would build rollups first, because they give the biggest win for the least risk, and let the variety of analysts' questions decide whether a columnar store is needed.

What to say in the interviewAbout 80 seconds aloud

Open with

The root cause is the storage layout. PostgreSQL is a row store, so to sum one column over 90 days it reads every 80-column tuple in that range, and most of that I/O is wasted on columns the query never uses.

Then, if they want more

An index does not change that, it only picks which rows to read. Since the team does not want a new database, my first step would be a daily rollup table, refreshed every few minutes by recomputing the last two or three days with an upsert. The analyst query then reads 90 small rows and returns in milliseconds.

In the same change I would partition events by time and put a BRIN index on ts, so the refresh and any ad hoc query only touch recent partitions, and retention becomes dropping a partition.

If the analysts' questions keep varying beyond what a few rollups cover, I would look at a columnar access method such as Citus columnar, after checking its maturity, version support and whether our managed service allows it, or export to Parquet and query it with DuckDB or a warehouse, which also takes analytical load off the OLTP database.

So: rollups first, because that is the biggest win with the least risk, then let query variety decide on columnar.

About this answer

Last updated
12 November 2026
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.
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 "A PostgreSQL table events(80 columns) holds 600 million rows. Analysts run queries like...": https://scrivio-interview-library.netlify.app/databases/storage-internals/867ac49f55/