Extracts from a PostgreSQL read-only replica (hot standby) miss rows and get canceled — diagnose both
At 00:05 a nightly job extracts yesterday's orders from an asynchronous PostgreSQL 18 hot standby. The warehouse often lacks orders from the last minutes before midnight, and the long customer-history extract at 00:20 often fails. From 23:50 the primary runs a heavy batch update and autovacuum. Constraints: explain both failures from the output below; give each fix's cost.
-- standby, 00:05:02
SELECT now() - pg_last_xact_replay_timestamp() AS replay_delay,
pg_last_wal_receive_lsn() AS received, pg_last_wal_replay_lsn() AS replayed;
replay_delay | received | replayed
--------------+-------------+-------------
00:07:41 | 5A/3C0012F8 | 5A/1F4430A0
-- primary, 00:05:02
SELECT pg_current_wal_lsn(); -- 5A/3C4F7E10
-- extract job log, 00:31:17
ERROR: canceling statement due to conflict with recovery
DETAIL: User query might have needed to see row versions that must be removed.
-- standby settings
max_standby_streaming_delay = 30s
hot_standby_feedback = offDiagnose the cause.
Missing rows: the async standby replays WAL minutes behind; extract once replay passes midnight, or read the primary. Cancels: replaying VACUUM cleanup would remove rows the extract's snapshot sees. A longer max_standby_streaming_delay adds lag; hot_standby_feedback bloats the primary.
- Assuming an asynchronous replica is current at the moment an extract starts
- Turning on hot_standby_feedback without noticing it moves the cost to bloat on the primary
- Raising max_standby_streaming_delay and making the replica fall even further behind for the next extract
- How would you make the 00:05 extract wait until the standby has replayed everything committed before midnight?
- When does a dedicated reporting replica, or a warehouse fed by change data capture (CDC), beat tuning a shared hot standby?
Missing orders. At 00:05 the standby has replayed WAL only up to 23:57: replay_delay is 7 min 41 s, and received has nearly caught up with the primary's pg_current_wal_lsn() while sitting hundreds of megabytes ahead of replayed — the WAL has arrived but is not applied yet (the primary's batch update generates it faster than the standby applies it). Asynchronous replication promises no freshness: an extract "for yesterday" sees yesterday without its last minutes. (On an idle primary replay_delay grows even without lag — compare LSNs.) The fix is to wait until replay passes the extract's cutoff, or to read from the primary:
-- on the primary, right after midnight: the position the standby must reach
SELECT pg_current_wal_lsn(); -- e.g. 5A/2E91C3A8
-- on the standby: wait until everything up to that position is applied
SELECT pg_last_wal_replay_lsn() >= '5A/2E91C3A8'::pg_lsn AS caught_up;Canceled extracts. From 23:50 the primary vacuums dead row versions, and the records of that cleanup arrive in the WAL. The long extract on the standby holds a snapshot that still sees those versions; replay waits at most max_standby_streaming_delay (30 s, counted from when that WAL was received; on a standby minutes behind, that time has already passed, so the cancel comes almost at once), then cancels the query with conflict with recovery.
Cost of the fixes.
- A larger
max_standby_streaming_delay(−1 waits forever): queries are no longer canceled, but replay stalls and the standby falls further behind — making the first problem worse. hot_standby_feedback = on: the standby tells the primary its oldest snapshot, and VACUUM there keeps the rows it needs — table bloat on the primary, as if the report ran on it.- Better: a dedicated reporting replica tuned for long queries, or a warehouse fed by CDC.