Quick revision
The short answer
The standby replays the primary's WAL, and some replayed changes, most often vacuum removing old row versions, would remove data that a running BI query still needs to see. PostgreSQL waits up to max_standby_streaming_delay (30 seconds by default) and then cancels the query with this error. We can raise that delay on a dedicated reporting replica and accept replay lag, enable hot_standby_feedback so the primary keeps those rows at the cost of bloat there, shorten the queries, or move heavy reporting to a warehouse, and we watch pg_stat_database_conflicts to see which conflicts occur.
What to remember
In this answer, we saw that canceling statement due to conflict with recovery happens when the standby must replay a change, usually vacuum cleanup of row versions, that a running query still needs, and the wait exceeds max_standby_streaming_delay. We looked at pg_stat_database_conflicts to find the cause, then weighed the options: a longer delay on a dedicated reporting replica that accepts lag, hot_standby_feedback that prevents the conflict but bloats the primary, shorter queries and summary tables, and moving heavy reporting to a warehouse.
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.
The standby replays the primary's WAL, and some replayed changes, most often vacuum removing old row versions, would remove data that a running BI query still needs to see. PostgreSQL waits up to max_standby_streaming_delay (30 seconds by default) and then cancels the query with this error. We can raise that delay on a dedicated reporting replica and accept replay lag, enable hot_standby_feedback so the primary keeps those rows at the cost of bloat there, shorten the queries, or move heavy reporting to a warehouse, and we watch pg_stat_database_conflicts to see which conflicts occur.
Why this question is asked
Connecting a BI tool to a hot standby is a common first step for reporting, and this error is usually the first surprise. It looks random: the same dashboard works at night and fails at noon. Interviewers ask about it because explaining it needs a clear picture of MVCC, vacuum and WAL replay, and because every fix has a cost somewhere else, so choosing one shows judgement. In this answer, we will recap what the standby does, reproduce the conflict on a timeline, read the conflict statistics, and then go through the options one by one.
1A Quick Recap: MVCC, Vacuum and Replay
PostgreSQL uses MVCC, multi-version concurrency control. An UPDATE does not overwrite a row; it writes a new version and marks the old one as dead from that point on. Queries that started earlier still see the old version, which is what gives every query a consistent snapshot. Vacuum later removes dead versions that no running transaction can still need.
On the primary, vacuum knows which transactions are running there and never removes versions they need. A hot standby is a different server that replays the primary's WAL, including the records that say vacuum removed these row versions. The primary has no idea that a BI query on the standby started 20 minutes ago and still needs those versions. So when that WAL record arrives, the standby has a conflict: it can either apply it, removing data under a running query, or pause replay and fall behind.
2Reproducing the Conflict
Here is the sequence that produces the error. A BI query starts on the standby at 12:00:00 and scans a large orders table. At 12:00:10, the primary updates many orders and, shortly after, autovacuum cleans the old versions. The WAL record for that cleanup reaches the standby at 12:01:00.
7 messages in order. Play them, or step through them one at a time.
Notice the 30 seconds. They are not counted from the start of the query but are a budget for how far replay may fall behind while waiting; once it is used up, PostgreSQL cancels the conflicting queries so replay can continue. That is why short queries rarely fail, long ones often do, and failures follow the primary's write and vacuum activity during the day.
3Reading pg_stat_database_conflicts
Cleanup of row versions is the most common cause, but not the only one. A standby also cancels queries when replay needs an exclusive lock, for example when the primary runs DDL or vacuum truncates empty pages at the end of a table, when a buffer pin blocks replay, or for a dropped tablespace. The view pg_stat_database_conflicts, available on the standby, counts cancellations by cause, which tells us which fix applies.
Here almost all cancellations are snapshot conflicts, caused by cleanup of rows the queries needed, so the options below about delays and feedback are the right ones. A large confl_lock count would instead point at DDL or vacuum truncation on the primary. We graph these counters over time so we can see whether a change helped.
4Option 1: Raise max_standby_streaming_delay on a Reporting Replica
We can give queries more time by raising max_standby_streaming_delay, for example to 15 minutes, or to -1 to wait forever. Replay then pauses while a conflicting query runs, and the standby falls behind the primary by up to that delay. For a replica used only for reporting, that is often an acceptable trade: dashboards a few minutes stale are fine, cancelled dashboards are not. The matching setting max_standby_archive_delay applies when WAL is read from the archive instead of streaming.
The important word is dedicated. On a standby that is also meant for failover or for application reads, a 15-minute lag means a failover could need minutes of extra replay and application reads would see stale data. So this option works best on a replica that exists only for reporting.
5Option 2: hot_standby_feedback and Its Cost
The other approach prevents the conflict at its source. With hot_standby_feedback = on, the standby tells the primary the oldest transaction it still needs, and the primary's vacuum does not remove row versions that the standby's queries might see. Snapshot conflicts largely disappear, and the standby stays current.
| Option | Effect | Cost |
|---|---|---|
| Raise max_standby_streaming_delay | queries get more time | replica lags behind the primary |
| hot_standby_feedback = on | primary keeps rows the standby needs | bloat and slower vacuum on the primary |
| Shorter queries, summary tables | fewer conflicts | work to rewrite reports |
| Move heavy reports to a warehouse | no replay conflicts at all | pipeline, minutes to hours of latency |
But the cost moves to the primary. While a two-hour report runs on the standby, vacuum on the primary cannot clean any row versions newer than that report's snapshot, across the whole database. Busy tables bloat, indexes grow and queries on the primary slow down, which is exactly the load we wanted to keep away from production. It also does not prevent lock conflicts, such as those from vacuum truncating a table end; the per-table vacuum_truncate option can switch that off where it causes trouble. hot_standby_feedback is reasonable when reports are short, and dangerous when they run for hours.
6Option 3: Shorten the Queries or Move the Workload
Conflicts grow with query duration, so faster queries help with any configuration. We can add indexes on the standby's schema, which must be created on the primary since a physical standby is read-only, have the BI tool query precomputed summary tables refreshed periodically, and avoid dashboards that scan years of data on every refresh. A statement_timeout on the BI role also stops runaway queries from holding back replay or vacuum.
If the reports are inherently heavy, scanning large tables across many joins, the lasting fix is to move them to a warehouse fed by CDC or batch loads. A warehouse has no WAL replay to conflict with, uses a column store suited to these scans, and keeps reporting load entirely away from the operational databases. Many teams use the dedicated reporting replica with a raised delay as a bridge while that warehouse is built.
7Follow-up questions
Why did the same dashboard succeed at night?
At night there are fewer updates and less vacuum activity on the primary, so fewer cleanup records conflict with running queries, and replay rarely has to wait long enough to cancel anything. The error tracks the primary's write load, not the query itself.
Can a logical replica avoid this error?
Yes, mostly. A logical replica, built with publications and subscriptions, applies row changes like a normal database and runs its own vacuum based on its own queries, so primary vacuum records do not cancel its queries. It has other costs, such as applying schema changes on both sides and needing a primary key or replica identity on each table.
Should the BI tool simply retry cancelled queries?
A retry may succeed if the primary is quiet, but under steady load the same long query will usually conflict again and waste more time. Retries hide the symptom; one of the options above addresses the cause.
8Conclusion
In this answer, we saw that canceling statement due to conflict with recovery happens when the standby must replay a change, usually vacuum cleanup of row versions, that a running query still needs, and the wait exceeds max_standby_streaming_delay. We looked at pg_stat_database_conflicts to find the cause, then weighed the options: a longer delay on a dedicated reporting replica that accepts lag, hot_standby_feedback that prevents the conflict but bloats the primary, shorter queries and summary tables, and moving heavy reporting to a warehouse.
Open with
The standby replays the primary's WAL. On the primary, an update leaves old row versions, and vacuum removes them once no transaction on the primary needs them. But the primary doesn't know about long BI queries on the standby.
Then, if they want more
When the cleanup record arrives, it would remove rows the BI query's snapshot still needs, so replay waits, up to max_standby_streaming_delay, 30 seconds by default, and then PostgreSQL cancels the query with that error. That's why it fails during busy daytime hours and works at night.
I'd first check pg_stat_database_conflicts on the standby to see which kind of conflict it is; usually it's confl_snapshot. Then there are trade-offs. On a replica dedicated to reporting, I can raise max_standby_streaming_delay to, say, 15 minutes and accept that it lags.
Or I can turn on hot_standby_feedback, which tells the primary to keep those row versions, so no conflict, but long reports then cause bloat on the primary, which is risky. I'd also shorten queries with summary tables and put a statement timeout on the BI role.
If the reports are inherently heavy, the real fix is a warehouse, where there's no replay to conflict with.
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.
- 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 "A BI tool connected to a PostgreSQL hot standby keeps failing with 'ERROR: canceling...": https://scrivio-interview-library.netlify.app/databases/analytics-and-cdc/8ddc922202/