Quick revision
The short answer
Both symptoms come from the same conflict: long report queries on the standby still need old row versions that VACUUM on the primary has already removed, and the standby must either delay replaying that cleanup or cancel the query. max_standby_streaming_delay decides which: a high value or -1 lets replay wait, so lag grows to 40 minutes, while a low value cancels queries with 'canceling statement due to conflict with recovery'. hot_standby_feedback = on avoids the cancellations at the cost of bloat on the primary, and the cleanest fix is separate standbys for reports and for application reads.
What to remember
In this answer, we saw that cancelled reports and 40-minute lag are one conflict: replay of vacuum cleanup from the primary meets long queries that still need the removed rows. max_standby_streaming_delay chooses between delaying replay and cancelling the query, hot_standby_feedback avoids the conflict by keeping rows on the primary at the cost of bloat, and separating reporting standbys from low-lag application standbys, plus spreading out bulk writes, removes the tension.
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.
Both symptoms come from the same conflict: long report queries on the standby still need old row versions that VACUUM on the primary has already removed, and the standby must either delay replaying that cleanup or cancel the query. max_standby_streaming_delay decides which: a high value or -1 lets replay wait, so lag grows to 40 minutes, while a low value cancels queries with 'canceling statement due to conflict with recovery'. hot_standby_feedback = on avoids the cancellations at the cost of bloat on the primary, and the cleanest fix is separate standbys for reports and for application reads.
Why this question is asked
Reporting on a hot standby is a good idea in principle: the heavy queries stay off the primary. Then, every afternoon, two complaints arrive together. The reports team says their queries are being killed, and the application team says the replica is 40 minutes behind. Those look like two problems, but they are the two sides of one trade-off, and the settings that fix one make the other worse. Interviewers ask this to see whether we understand that trade-off rather than flipping settings until one complaint goes away. In this answer, we will recap how replay and queries share a standby, see exactly where the conflict comes from, look at the settings that decide the outcome, and design a setup where neither complaint returns.
1A Quick Recap: Queries and Replay on One Standby
A hot standby does two jobs at once. Its startup process replays WAL from the primary, applying every change, and at the same time it serves read-only queries. Each query runs on a snapshot, a consistent view of the data as of the moment it started, which relies on old row versions still being present.
On the primary, VACUUM removes dead row versions, the old copies left behind by updates and deletes, once no transaction on the primary needs them. The primary does not know what queries are running on the standby. So it happily vacuums rows that a two-hour report on the standby is still reading. That cleanup arrives on the standby as WAL, and replay must apply it.
2Where the Conflict Comes From
When replay reaches a cleanup record that removes rows some running query still needs, the standby has two choices: pause replay until the query finishes, or cancel the query and continue. It cannot do both. The same thing happens with locks: replaying an ACCESS EXCLUSIVE lock from the primary, for example from DDL or from VACUUM truncating empty pages at the end of a table, conflicts with any standby query on that table.
- Replay reaches a vacuum cleanup recordrows removed on the primary
- Does a running query need those rows?YesConflictNoApply and continue
- Pause replay and waitreplay lag grows
- Waited longer than max_standby_streaming_delay?YesCancel the queryNoKeep waiting
- Apply the recordreplay catches up
5 steps in order. Play them, or step through them one at a time.
With -1 the second decision never says yes, so replay waits as long as the report runs.
Now the afternoon pattern makes sense. The reports start after lunch. If max_standby_streaming_delay is large, or -1 which means wait forever, replay pauses behind each long report and lag climbs to 40 minutes. If it is the default of 30 seconds, replay waits 30 seconds and then cancels the report with the error the team sees, usually with the detail that the query might have needed to see row versions that must be removed. A busy afternoon of writes on the primary makes it worse, since there is more WAL to catch up on.
3Confirming It on the Standby
The standby keeps counts of cancelled queries by cause, which tells us which kind of conflict we have.
These numbers explain both complaints at once. Someone raised the delay to 30 minutes to stop the cancellations, so replay now waits up to 30 minutes behind each conflicting report, and with the afternoon write load on top, lag reaches 40 minutes. Reports that still hold their snapshot when those 30 minutes run out are cancelled anyway. A high confl_snapshot count means vacuum cleanup is the main cause. Some lock conflicts point at DDL or table truncation during vacuum, which the vacuum_truncate storage parameter can turn off for the tables involved.
4The Settings and What Each Costs
max_standby_streaming_delay, and its twin max_standby_archive_delay for WAL restored from an archive, set how long replay waits before cancelling. It is a dial between lag and cancellations; no value removes both.
hot_standby_feedback = on changes the game. The standby tells the primary the oldest snapshot its queries still use, and the primary's VACUUM leaves those rows alone. The cleanup records never arrive, so there is nothing to conflict with: no cancellations, no pause. The cost moves to the primary: dead rows stay around for as long as the longest report runs, so tables and indexes bloat, and a forgotten four-hour query on the standby makes the primary carry four hours of garbage. We pair it with a statement_timeout for report users on the standby, for example two hours, to bound that cost.
| Setting | Reports cancelled | Replay lag | Primary bloat |
|---|---|---|---|
| Delay 30s, feedback off | Often | Low | None |
| Delay -1, feedback off | Never | As long as reports run | None |
| Feedback on, statement_timeout | Rarely | Low | Bounded by the timeout |
5Separate Standbys for Separate Jobs
The best fix is to stop asking one standby to serve two masters. Application reads need low lag; reports need long, uninterrupted queries and can accept being minutes behind. So we run two kinds of standbys. The application replica keeps a short max_standby_streaming_delay and no long queries, so its lag stays in milliseconds. The reporting replica can use hot_standby_feedback with a statement timeout, or a large delay if its users accept staleness, and its lag never affects application reads. In a larger setup, reports might move to a separate analytics store fed by logical replication or CDC.
Finally, we look at the write side. Bulk loads that run in the afternoon produce the WAL bursts that make catching up slow. Moving them to the night, or committing them in smaller chunks, shortens every pause.
6Follow-up questions
Does hot_standby_feedback work if the standby disconnects?
Only while it is connected. If the standby disconnects, the primary stops holding rows back, and when it reconnects there may be cleanup records that conflict again. Using a physical replication slot for the standby makes the primary remember the standby's oldest snapshot even across short disconnects, at the price of retaining WAL.
Can we just set statement_timeout on the reports and leave the rest as is?
A timeout bounds how long any one query runs, which helps with feedback on, but on its own it does not stop the conflict between replay and a report that is well within its timeout. It is one part of the setup, not the fix.
Why does the error mention recovery when nothing is being recovered?
To PostgreSQL, a standby is permanently in recovery: it is continuously replaying WAL, exactly as a server does after a crash. pg_is_in_recovery() returns true on every standby, and conflicts with recovery means conflicts with that replay.
7Conclusion
In this answer, we saw that cancelled reports and 40-minute lag are one conflict: replay of vacuum cleanup from the primary meets long queries that still need the removed rows. max_standby_streaming_delay chooses between delaying replay and cancelling the query, hot_standby_feedback avoids the conflict by keeping rows on the primary at the cost of bloat, and separating reporting standbys from low-lag application standbys, plus spreading out bulk writes, removes the tension.
Open with
Both symptoms are the same conflict. The primary runs VACUUM and removes dead row versions, not knowing that a long report on the standby still needs them. When the standby replays that cleanup, it must either pause replay until the query finishes or cancel the query. max_standby_streaming_delay decides: with a large value or -1, replay waits behind afternoon reports and the lag grows to 40 minutes; with the default 30 seconds, it cancels the report with canceling statement due to conflict with recovery.
Then, if they want more
I would confirm it with pg_stat_database_conflicts on the standby, where confl_snapshot shows vacuum-related cancellations and confl_lock shows lock conflicts, for example from DDL or vacuum truncation.
The settings I would review are max_standby_streaming_delay, hot_standby_feedback, statement_timeout for report users, and vacuum_truncate on affected tables. hot_standby_feedback = on tells the primary not to clean rows the standby still uses, so no cancellations and no pause, but the primary bloats while long reports run, so I would bound it with a statement timeout.
The real fix is to separate jobs: an application replica with low delay and no long queries, and a reporting replica tuned to tolerate lag or with feedback on. I would also move or chunk afternoon bulk loads, since they create the WAL bursts behind the catch-up.
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 PostgreSQL hot standby used for reports shows replay lag growing to 40 minutes every...": https://scrivio-interview-library.netlify.app/databases/replication-and-ha/0882a62f03/