Databases
VERIFIED FIXES

PostgreSQL replica apply stalls 'waiting for lock' during bursts of DDL on primary

Environment

Environment

Primary and hot-standby replica running PostgreSQL 13 on Linux; streaming replication via async WAL shipping, hot_standby = on; primary performs frequent maintenance windows with ALTER TABLE and index maintenance during daytime loads; replica used for reporting and occasional ad-hoc queries.

Symptoms

Symptoms

Replica WAL receiver keeps up but apply (recovery) process stalls; replication lag grows minutes to hours during bursts of DDL on primary. Replica logs show messages about waiting for relation lock and queries on replica block apply until terminated.

Observations

Observations

pg_stat_activity on replica shows long-running read-only reporting queries with start times overlapping the DDL windows. pg_locks shows ACCESS SHARE locks held by reporting sessions while apply needs ACCESS EXCLUSIVE for DDL replay. No hardware faults; killing blocking sessions resumes WAL apply and lag reduces.

Already attempted

Already attempted

Restarted replica (temporary relief), adjusted wal_sender_timeout, disabled hot_standby_feedback (no change), scheduled DDL in off-hours but workload occasionally forces online DDL. Tried pg_terminate_backend for blocking sessions manually but need automated mitigation.

Question

Question

What safe configuration or operational changes let WAL apply continue without frequent manual terminations? Looking for settings, hot_standby_feedback tradeoffs, or scheduling guidance.

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
simonsupport Answer
user
Posted 1mo ago • 0 confirmations

The most practical fix is to avoid long-running read queries on the standby during windows you run exclusive DDL on the primary. Start by listing long-running sessions on the replica (pg_stat_activity) and add a short statement_timeout for ad-hoc reporting or schedule heavy reports after DDL windows. If you must run queries, switch DDL to online methods (CREATE INDEX CONCURRENTLY, add nullable columns then backfill) so WAL apply doesn't need ACCESS EXCLUSIVE. As a shorter-term mitigation, enable hot_standby_feedback on the replica to avoid query cancellations on the primary — but monitor primary bloat and autovacuum. Verify by running a small DDL while no long queries exist and confirm WAL applies without lag. Rollback by disabling hot_standby_feedback or restoring timeouts if it causes bloat.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Long-running read-only queries on replica hold ACCESS SHARE locks, blocking WAL apply of DDL that needs stronger locks. 95%
hot_standby_feedback disabled causing bloat/autovacuum activity on primary, indirectly increasing DDL duration and conflicts. 40%
Background tasks (autovacuum or custom maintenance) on replica holding locks at inopportune times. 35%

Summary

Recovery waits when WAL contains operations that require exclusive locks (DDL) if local queries hold conflicting locks or snapshots. The logs and pg_locks indicate long-lived read-only queries on the replica are holding ACCESS SHARE and preventing application of WAL records for DDL. Most likely cause: reporting queries or long snapshots on replica conflict with DDL on primary. Secondary causes include hot_standby_feedback off leading to VACUUM on primary behaving differently (less likely to directly cause waits) and logical replication or extensions interfering.

Suggested Fixes

Diagnostic step 1
Identify and list long-running sessions on replica: SELECT pid, query, state, backend_start FROM pg_stat_activity WHERE state <> 'idle' ORDER BY backend_start DESC; consider setting statement_timeout or canceling old queries automatically during maintenance windows.
Diagnostic step 2
If reporting queries must run long, schedule DDL on primary during windows when replica is quiescent or use transactional DDL strategies (CREATE INDEX CONCURRENTLY, ALTER TABLE ... ADD COLUMN with default NULL then backfill) to reduce exclusive locks.
Diagnostic step 3
Consider enabling hot_standby_feedback = on on the replica to prevent query cancellations on the primary, but be aware it can delay vacuum on primary (trade-off with bloat).

Continue troubleshooting

Matched from the title, description and tags