Databases

PostgreSQL replica SQL apply thread stalls 'waiting for table metadata lock' after large ALTER TABLE on primary

astridapps
1mo ago
Environment

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

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

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

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

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?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Standby has background worker (autovacuum or user process) holding a metadata lock that conflicts with WAL-applied DDL changes 85%
Large DDL produced catalog changes that require exclusive locks; simultaneous activity on standby caused replay to block 70%
WAL apply encountering sequence that expects a different object identity due to prior out-of-band manual catalog change (rare) 20%

Summary

WAL replay may need to acquire locks to modify system catalogs or relation metadata when applying DDL. If WAL application collides with other activities on standby (autovacuum, extension background workers) that hold incompatible locks, the replay can block waiting for those locks. The most likely root cause is the DDL on primary generates catalog changes that must be applied exclusively and the standby has a process holding a conflicting metadata lock (most likely autovacuum or background activity). Less likely is corruption or a replication bug but those are rarer.

Suggested Fixes

Diagnostic step 1
On the standby, inspect pg_locks and pg_stat_activity to identify which process holds the conflicting lock (look for relation or meta-lock types) and consider terminating nonessential background workers (autovacuum) temporarily to let replay proceed.
Diagnostic step 2
If replay remains blocked and no clear blocker is present, stop the standby and start in single-user or recovery mode to examine logs, but prefer safe restart over forcing catalog changes.
Diagnostic step 3
To prevent recurrence: avoid running heavy DDL with non-null defaults on huge tables during peak replication; instead create columns NULL, backfill in batches, then set NOT NULL in a quick transactional step.

Continue troubleshooting

Matched from the title, description and tags