Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

A company runs all analytics directly on read replicas of its PostgreSQL OLTP databases. Long queries get cancelled with "canceling statement due to conflict with recovery", replicas fall behind, and every team builds its own reports. Design the path to a proper analytics architecture.

The replicas fail because long analytical queries clash with WAL replay: when the primary's vacuum removes row versions a standby query still needs, the standby must either cancel the query or pause replay and fall behind. We would stabilise things short term with one dedicated analytics replica with relaxed settings and per-role limits, then build the target: CDC or batch extracts into a columnar warehouse, modelled into curated tables, protected by data contracts, with reports migrated gradually starting from the heaviest.

Why this question is asked

Running analytics on read replicas is a natural first step for a growing company, and for a while it works. Then the error 'canceling statement due to conflict with recovery' appears, replicas lag minutes behind, and every team writes its own slightly different revenue query. Interviewers use this scenario to see whether we understand why hot standbys behave this way, and whether we can lead a migration that improves things without breaking the reports people rely on. In this answer, we will explain the replica problem and its tuning knobs, stabilise the current setup, design the target architecture, and plan the migration.

1Why Replica Queries Get Cancelled

A PostgreSQL read replica, called a hot standby, continuously replays the primary's write-ahead log (WAL), the stream of every change made on the primary. Replay must apply changes in order, including cleanup done by vacuum on the primary.

Here is the clash. An analyst starts a 40-minute query on the standby, which needs a snapshot of the data as it was at the start. Meanwhile, vacuum on the primary removes old row versions that no transaction on the primary needs any more. When the record of that cleanup reaches the standby, applying it would delete rows the analyst's query might still read. The standby has two choices: wait, which makes it fall behind the primary, or cancel the query. It waits up to max_standby_streaming_delay (30 seconds by default) and then cancels, which is the error the analysts see.

Step throughA vacuum on the primary cancels a standby query
PrimaryStandbyAnalyst query
start a 40-minute report with a snapshot
vacuum removes old row versions
WAL record: these versions are gone
conflict: the report may still need them
wait up to max_standby_streaming_delay (30 s)
canceling statement due to conflict with recovery

6 messages in order. Play them, or step through them one at a time.

The standby can delay replay or cancel the query; it cannot do both.

So the cancellations and the lag are two faces of the same conflict. Every setting that reduces one makes the other worse, or moves the cost somewhere else.

2The Knobs and What Each One Costs

Two settings change the balance. Raising max_standby_streaming_delay, for example to 30 minutes or -1 (wait forever), lets long queries finish, but replay pauses during the conflict, so the replica lags. That is fine for a reporting replica and unacceptable for one that serves fresh reads to an application.

Turning on hot_standby_feedback makes the standby tell the primary which old row versions its queries still need. The primary's vacuum then keeps them, so there is no conflict and no cancellation. But there is a catch: the primary now cannot clean up those rows for as long as the longest analyst query runs, so busy tables on the primary bloat. We have moved the cost of analytics back onto the production database, the very thing replicas were meant to avoid.

3Short Term: Contain the Damage

Before any new architecture, we stop the bleeding. We create one replica that serves only analytics, with relaxed conflict settings and no application traffic, so lag there hurts nobody. Application replicas keep strict settings and stay fresh.

Then we put limits around the analytics replica, because one runaway query should not starve everyone else. We give each team a role with its own timeout and connection limit.

SQL
-- on the dedicated analytics replica (postgresql.conf)
max_standby_streaming_delay = 30min -- let long reports finish; accept some lag
hot_standby_feedback = off -- do not push bloat back to the primary
-- per-role limits (set on the primary, replicated to every standby)
ALTER ROLE analyst_finance SET statement_timeout = '15min';
ALTER ROLE analyst_finance CONNECTION LIMIT 10;

Notice that roles and their settings are stored in the catalog, so we create them on the primary and they appear on the standby. These steps buy months of calm. They do not fix the duplicated reports or the fact that a row store is slow for analytics.

4The Target Architecture

The lasting fix separates analytics completely. Changes flow out of each OLTP database into a warehouse or lakehouse with columnar storage, where analytical queries are fast and cannot touch production. For freshness, we use change data capture: Debezium or PostgreSQL logical replication reads the WAL and streams each insert, update and delete, usually through Kafka. For less critical sources, a nightly batch extract is simpler and good enough.

Raw copies of source tables are not what analysts should query. We model them into curated tables, one agreed definition of an order, a customer, revenue, using a tool such as dbt that keeps the transformations as tested SQL in version control. This is what ends 'every team builds its own reports': the shared definitions live in one place.

Step throughAnalytics platform, top to bottom
  1. Reports and dashboardsBI tools read only curated tables
  2. Curated modelsfacts and dimensions, built and tested with dbt
  3. Raw landing tablesone copy per source table, append or upsert
  4. IngestionDebezium or logical replication for CDC, batch extracts for the rest
  5. OLTP PostgreSQL databasesserve the applications, no analytical queries

5 layers, from the top down. Step through them one at a time.

5Data Contracts and Freshness Targets

A CDC pipeline is only as stable as the source schema. If a service team renames a column on Tuesday, Wednesday's revenue dashboard breaks. A data contract is an agreement between the source team and the data platform: which tables and columns are published, what they mean, and how changes are announced, for example with a deprecation period of one release before a column disappears. Contracts can be checked in CI, so a migration that breaks a published column fails the build.

We also agree freshness targets per dataset, because they drive cost. Finance might accept data that is 24 hours old; the support dashboard might need 15 minutes. Writing these down stops every dataset from being built to the most expensive standard.

6Migrating Gradually

We do not switch every report at once. We start by measuring: which queries put the most load on the replicas and which reports matter most to the business. The heaviest few queries usually account for most of the load, so moving them first gives the quickest relief.

For each report, we build the curated tables, run old and new versions side by side until the numbers match, then switch the report and remove its access to the replica. We track two numbers throughout: load and lag on the analytics replica, and latency of the reports themselves. When the replica's analytical load reaches near zero, we retire it.

Step throughMoving one report
  1. Measure load per query on the replicapick the heaviest first
  2. Build curated tables for it in the warehouse
  3. Do old and new numbers match?
    YesSwitch the report to the warehouseNoFix the model and compare again
  4. Remove the report's replica accesstrack replica load and report latency

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

7Follow-up questions

How do you handle personal data when copying into the warehouse?

We classify columns at the contract level, so each published column is marked, for example, as public, internal or personal. Personal data is either left out of the pipeline, pseudonymised (replaced with a stable hash or token) during ingestion, or landed in a restricted area with tighter access and its own retention. Deletion requests must flow through as well, so CDC deletes are applied to the warehouse and retention rules purge raw history. Access is granted by role and audited.

Why not just turn on hot_standby_feedback everywhere?

Because it trades cancellations for bloat on the primary. A six-hour report would then stop vacuum on the primary from cleaning anything newer than six hours across the busiest tables, which slows production queries. It is acceptable for short queries, not for heavy analytics.

Is logical replication enough instead of Debezium?

PostgreSQL logical replication can copy tables to another PostgreSQL, which works for a reporting database. Debezium adds a stream that many consumers can read, schema change events, and connectors to many warehouses. Both use replication slots, which must be monitored, because an abandoned slot keeps WAL on the primary forever.

8Conclusion

In this answer, we saw that analytics on hot standbys fail because long queries conflict with WAL replay: the standby cancels them after max_standby_streaming_delay or falls behind, and hot_standby_feedback avoids cancels only by causing bloat on the primary. In the short term, a dedicated analytics replica with relaxed settings and per-role limits contains the problem. The target is CDC or batch extracts into a columnar warehouse, curated with tested models, protected by data contracts and freshness targets, and reached by migrating the heaviest reports first while measuring replica load and report latency.

What to say in the interviewAbout 80 seconds aloud

Open with

First I would explain the symptom. A hot standby replays WAL from the primary, including vacuum cleanup. When cleanup removes row versions a long standby query still needs, the standby either pauses replay, which is the lag, or cancels the query after max_standby_streaming_delay, which is the conflict with recovery error. hot_standby_feedback avoids the cancellations, but it stops vacuum on the primary from removing those rows, so production bloats.

Then, if they want more

In the short term I would create one dedicated analytics replica with a long max_standby_streaming_delay and no application traffic, so lag there is acceptable, and give each analyst role a statement timeout and connection limit.

The target is to take analytics off PostgreSQL entirely: CDC with Debezium or logical replication, or batch extracts for less critical data, into a warehouse or lakehouse with columnar storage, modelled into curated, tested tables with something like dbt, so there is one definition of revenue instead of one per team.

I would agree data contracts with source teams so schema changes do not silently break pipelines, and freshness targets per dataset. Then I would migrate gradually, heaviest queries first, run old and new reports side by side until they match, and track replica load and report latency until the replica can be retired.

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 company runs all analytics directly on read replicas of its PostgreSQL OLTP databases....": https://scrivio-interview-library.netlify.app/databases/storage-internals/8e48a9768f/