Sub topic 9
Query conflicts, `max_standby_streaming_delay` and `hot_standby_feedback`
This chapter assumes that you can edit PostgreSQL settings, run SQL with psql, and understand primary and standby roles. You should also know that a standby replays WAL while it serves read-only queries.
Why queries conflict with WAL replay
A hot standby must apply WAL in order. Some WAL records require locks or remove row versions that an active standby query still needs. PostgreSQL cannot always do both operations at once.
Common conflict sources include:
VACUUMon the primary removing row versions needed by a standby query.DROP TABLE,ALTER TABLE, or another operation requiring a strong table lock.- Removing a tablespace or database that a standby query is using.
- A long-running transaction holding a snapshot while replay needs to clean up old data.
When a conflict lasts too long, PostgreSQL cancels the standby query so recovery can continue. The client usually receives an error such as “canceling statement due to conflict with recovery.”
Controlling the wait
max_standby_streaming_delay sets how long the standby may delay WAL replay because of conflicting queries received through streaming replication. It is measured against the time available for replaying WAL, not as a fresh timer for every query.
For example:
# postgresql.conf on the standby
max_standby_streaming_delay = '30s'A larger value gives reports more time to finish, but it allows replay lag to grow. A value of -1 permits an unlimited wait for streaming conflicts. That can make the standby many minutes or hours behind the primary.
For WAL restored from an archive, use the related setting:
max_standby_archive_delay = '30s'These settings do not make a query faster. They only change whether PostgreSQL waits for the query or cancels it.
| Choice | Benefit | Cost |
|---|---|---|
| Short delay | Low replay lag | More canceled queries |
| Long delay | Reports are less likely to be canceled | Standby data becomes older |
| `-1` | Queries can wait indefinitely | Recovery and failover may be severely delayed |
Using `hot_standby_feedback`
hot_standby_feedback lets the standby tell the primary about the oldest transaction snapshot still needed by standby queries.
# postgresql.conf on the standby
hot_standby_feedback = onThe primary uses this information to avoid removing some row versions that the standby may need. This reduces conflicts caused by cleanup, especially long-running reports.
The trade-off is table bloat on the primary. If a standby holds an old snapshot for a long time, VACUUM may be unable to remove dead row versions. Monitor table growth and vacuum progress when enabling this setting.
hot_standby_feedback does not prevent every conflict. Strong locks from DDL can still conflict with standby queries. It also cannot help when the standby is disconnected or unable to send feedback.
A practical tuning approach
- Keep reporting transactions short.
- Avoid idle transactions that remain open after a query finishes.
- Set a finite delay that matches the business tolerance for stale data.
- Enable feedback only when cleanup conflicts are common and bloat is monitored.
- Put long reports on a dedicated standby.
- Check
pg_stat_database_conflictson the standby.
SELECT datname,
confl_tablespace,
confl_lock,
confl_snapshot,
confl_bufferpin,
confl_deadlock
FROM pg_stat_database_conflicts;Exercise
A standby cancels long reports because of recovery conflicts. Name two changes that reduce cancellations and one cost or risk for each change.
Show answer
Increase maxstandbystreamingdelay, which can increase replay lag. Enable hotstandby_feedback, which can increase dead-row retention and table bloat on the primary.