Quick revision
The short answer
Analysts should never query the primary. I would give them a separate place to run SQL, a replica for light needs and a columnar warehouse fed by CDC for heavy ROLLUP and CUBE work, precompute the heaviest cubes as scheduled aggregate tables or materialized views, guard every reporting connection with its own role, statement timeouts and limits, and agree freshness SLAs with BI so that some lag is an accepted part of the design.
What to remember
In this answer, we separated the two workloads so that analysts never query the PostgreSQL primary. We compared a physical replica, a logical replica with its own indexes, and a columnar warehouse fed by CDC through Debezium or managed replication, modelled as star schemas. We precomputed the heaviest cubes as aggregate tables or materialized views refreshed on a schedule, guarded reporting with separate roles, statement timeouts and limits monitored through pg_stat_statements per role, and agreed freshness SLAs so BI accepts the replica or warehouse lag.
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.
Analysts should never query the primary. I would give them a separate place to run SQL, a replica for light needs and a columnar warehouse fed by CDC for heavy ROLLUP and CUBE work, precompute the heaviest cubes as scheduled aggregate tables or materialized views, guard every reporting connection with its own role, statement timeouts and limits, and agree freshness SLAs with BI so that some lag is an accepted part of the design.
Why this question is asked
Checkout slowing down because someone ran a quarterly report is a classic incident, and it is rarely anyone's fault. Analysts were given SQL on the only database there was, and ROLLUP and CUBE queries are exactly the kind that scan large tables, sort them several times and fill memory. Interviewers ask this design question to see whether we can protect the transactional workload without taking SQL away from the people who need it. In this answer, we will look at why the two workloads clash, compare the places analysts can query instead, see how CDC feeds a warehouse, precompute the worst cubes, add guard rails, and agree freshness with the BI team.
1Why Reports and Checkout Clash
OLTP, online transaction processing, is the checkout workload: thousands of short queries per second that each touch a few rows through indexes and must finish in milliseconds. Analytical queries are the opposite: a few long queries that read millions of rows. A CUBE over orders, products and regions can read the whole orders table, sort it several times, and spill gigabytes to temporary files.
On one server they compete for everything. The scans push hot checkout pages out of the shared buffer cache, so checkout queries suddenly read from disk. The sorts use CPU and memory that checkout needed. Long transactions also hold back vacuum, which cannot remove dead rows that an old snapshot might still see, so tables bloat over the day. None of this is fixed by a faster query; the fix is to stop running both on the same machine.
2Where Analysts Can Query Instead
There are three realistic places to move the analysts, and each costs something different. A physical streaming replica is an exact copy of the primary that accepts read-only queries. It is cheap to set up and almost real time, but it has the same indexes and schema as the primary, and long queries can conflict with replay: PostgreSQL either cancels them with a recovery conflict error or, with hot_standby_feedback on, keeps old rows alive on the primary and causes bloat there.
A logical replica receives changes table by table through logical replication. It is a normal writable database, so we can add reporting indexes, summary tables and materialized views without touching the primary. A columnar warehouse or lakehouse, such as BigQuery, Snowflake, Redshift or ClickHouse, stores data by column, compresses it well and scans billions of rows per second, which is what ROLLUP and CUBE really want.
| Option | Freshness | Can add indexes | Fits heavy cubes |
|---|---|---|---|
| Primary (today) | real time | affects checkout | no, harms OLTP |
| Physical replica | seconds | no, same as primary | limited, query conflicts |
| Logical replica | seconds | yes | medium |
| Columnar warehouse via CDC | minutes | own model | yes |
For a BI team that runs cubes daily, I would choose the warehouse as the main destination and keep a logical replica for operational reports that need near-real-time data and familiar PostgreSQL SQL.
3Feeding the Warehouse With CDC
Change data capture, CDC, reads committed changes from the database's log instead of querying tables. In PostgreSQL that is logical decoding of the WAL, the write-ahead log every change goes through. Debezium, or a managed replication service from the cloud provider, turns those changes into a stream and loads them into the warehouse. The primary does almost no extra work: it writes the WAL anyway.
In the warehouse, we do not simply copy the OLTP tables. We model them as star schemas: a fact table such as fact_sales with one row per order line and numeric measures, surrounded by dimension tables such as dim_product, dim_region and dim_date. Analysts then write the same GROUP BY ROLLUP queries they wrote before, only against tables built for them.
- BI tools and analystsSQL with ROLLUP, CUBE, GROUPING SETS
- Warehouse star schemafact_sales, dim_product, dim_region, dim_date
- Aggregate tablesprecomputed cubes refreshed on a schedule
- CDC pipelineDebezium or managed replication, slot lag alert
- PostgreSQL primarycheckout only, no analyst logins
5 layers, from the top down. Step through them one at a time.
There is one thing to watch on the primary side: a logical replication slot keeps WAL until the consumer has read it. If the CDC pipeline stops for a day, the primary's disk fills with WAL. So we monitor slot lag and alert long before the disk is at risk.
4Precomputing the Heaviest Cubes
Some reports run every morning with the same shape, such as revenue by day, region and category. There is no reason to compute that CUBE from raw rows each time. We compute it once on a schedule into an aggregate table or a materialized view, a stored query result that we refresh, and dashboards read the small result.
Notice the unique index: REFRESH ... CONCURRENTLY needs one so it can compare old and new rows, and NULLS NOT DISTINCT (PostgreSQL 15 and later) makes the index treat the NULLs in subtotal rows as equal values, so it really guarantees one row per key. The level column from GROUPING() keeps a subtotal and a real NULL value apart in that key. Whether the view lives in a logical replica or the warehouse, the cost is paid once per refresh instead of once per dashboard view.
5Guard Rails on Every Reporting Connection
Even after the move, we protect what remains. Each tool or team gets its own database role, so we can see and limit its load. Statement timeouts stop runaway queries, connection limits stop a BI tool from opening 200 connections, and on the primary the analyst roles simply do not exist.
pg_stat_statements groups query statistics by role, so we can show each team what its queries cost and find the next candidate for precomputation. On the primary, a short statement_timeout for application roles also protects checkout if someone points a tool at it by mistake.
6Agreeing Freshness With BI
The last part is not technical. A replica lags by seconds, a CDC pipeline into a warehouse by minutes, and a precomputed cube by up to its refresh interval. BI teams often say they need real-time data and then build daily reports. So we agree a freshness SLA per data set, for example operational order status within 1 minute from the logical replica, revenue cubes within 1 hour from the warehouse, and finance close numbers once a day after a reconciliation. We show the data's last update time on every dashboard, so nobody mistakes lag for a bug.
7Follow-up questions
Why not just add a bigger primary?
A bigger server delays the problem but keeps the clash: one large CUBE can still evict checkout's cache and fill temp space. Workload separation protects checkout regardless of how large the reports grow.
What happens if the CDC pipeline falls behind?
Dashboards get staler, which the freshness display makes visible, and the primary keeps WAL for the replication slot. We alert on slot lag and on WAL disk usage, and PostgreSQL's max_slot_wal_keep_size can cap how much WAL a slot may hold, at the cost of having to resync the consumer.
Can the physical replica's query cancellations be avoided?
Partly. Raising max_standby_streaming_delay lets queries finish but delays replay, and hot_standby_feedback avoids cancellations but bloats the primary. For long cube queries, a logical replica or warehouse is the cleaner fix.
8Conclusion
In this answer, we separated the two workloads so that analysts never query the PostgreSQL primary. We compared a physical replica, a logical replica with its own indexes, and a columnar warehouse fed by CDC through Debezium or managed replication, modelled as star schemas. We precomputed the heaviest cubes as aggregate tables or materialized views refreshed on a schedule, guarded reporting with separate roles, statement timeouts and limits monitored through pg_stat_statements per role, and agreed freshness SLAs so BI accepts the replica or warehouse lag.
Open with
The root problem is two workloads on one server. Checkout is thousands of short indexed queries, while ROLLUP and CUBE reports scan and sort millions of rows, evict checkout's cache, use CPU and memory, and hold back vacuum.
Then, if they want more
So the first rule is that analysts never query the primary. For the destination, I would compare three options. A physical replica is easy and nearly real time, but it has the primary's indexes and long queries get cancelled by replay conflicts or cause bloat with hot standby feedback. A logical replica lets us add reporting indexes and materialized views.
For heavy cubes, a columnar warehouse fed by CDC, Debezium or a managed replication service, is the best fit, with the data modelled as star schemas so analysts keep writing the same SQL. The primary barely notices CDC, but I would alert on replication slot lag so WAL cannot fill the disk.
Then I precompute the heaviest recurring cubes as aggregate tables or materialized views refreshed on a schedule. Every reporting connection gets its own role with a statement timeout, connection limit and bounded work mem, and I monitor cost per role with pg_stat_statements.
Finally, I agree freshness SLAs with BI per data set and show the last update time on dashboards, so lag is expected, not a surprise.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 15
- 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, The SQL Language
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 "BI teams run ROLLUP and CUBE reports directly on the production PostgreSQL primary and...": https://scrivio-interview-library.netlify.app/databases/advanced-sql/4767133e42/