PostgreSQL replica SQL apply thread stalls 'waiting for table metadata lock' after large ALTER TABLE on primary
Environment
PostgreSQL 14 primary and streaming standby on separate hosts; primary runs nightly bulk jobs including ALTER TABLE ... ADD COLUMN with non-null default on a large partitioned table; replication is streaming WAL over TCP with replication slots.
Symptoms
After the primary's DDL completes, the standby's walreceiver/walapply process appears idle and replication lag grows. standby logs show repeated messages about waiting for locks or attempts to acquire relation locks; queries against standby that check pg_locks show relations with waiting lockers.
Observations
On the standby pg_locks and pg_stat_activity indicate walapply is blocked waiting for a table metadata lock (access exclusive style). There are no long-running user queries on the standby, but some autovacuum workers and checkpointer activity present. Killing processes on standby briefly changes state but walapply remains blocked until a restart in one test scenario. Basebackup restore later required when manual recovery attempts failed.
Already attempted
Restarted standby PostgreSQL to clear locks; this allowed replay to continue in one test but required re-sync in another. Attempted to stop writes on primary and re-run, but issue recurred when large DDL executed. Considered repromote and rebuild but want guidance to recover safely without data loss.
Question
How can I recover the standby without destructive steps, what exactly causes a standby to wait for a metadata lock when applying WAL for DDL, and how to avoid this during future large DDL on primary?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases46% match
PostgreSQL logical replication falls behind after ALTER TABLE ADD COLUMN with non-null DEFAULT on large table
Open0 answers - Databases41% match
MySQL replica SQL thread stalls with 'Waiting for table metadata lock' after direct ALTER TABLE on primary during peak writes
Open0 answers - Databases41% match
PostgreSQL replica apply stalls 'waiting for lock' during bursts of DDL on primary
Solved1 answer - Databases33% match
PostgreSQL replica falls behind and reports WAL streaming delay when frequent autovacuum runs on primary
Open0 answers - Databases33% match
PostgreSQL replica stops streaming WAL after pg_repack on primary; replication slot reports missing WAL
Open0 answers