PostgreSQL replica apply stalls 'waiting for lock' during bursts of DDL on primary
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
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
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
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
What safe configuration or operational changes let WAL apply continue without frequent manual terminations? Looking for settings, hot_standby_feedback tradeoffs, or scheduling guidance.
Verified community troubleshooting
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.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases47% match
PostgreSQL replica SQL apply thread stalls 'waiting for table metadata lock' after large ALTER TABLE on primary
Open0 answers - Databases36% match
MySQL replica SQL thread stalls with 'Waiting for table metadata lock' after direct ALTER TABLE on primary during peak writes
Open0 answers - Databases34% match
PostgreSQL standby stops applying WAL during long-running transactions on primary after pg_basebackup restore
Open0 answers - Databases33% match
PostgreSQL replica falls behind and reports WAL streaming delay when frequent autovacuum runs on primary
Open0 answers - Databases33% match
PostgreSQL logical replication falls behind after ALTER TABLE ADD COLUMN with non-null DEFAULT on large table
Open0 answers