PostgreSQL logical replication falls behind after ALTER TABLE ADD COLUMN with non-null DEFAULT on large table
Environment
Primary: PostgreSQL 13 on bare-metal (12 cores, 64GB), Logical replication slot streaming to replica on PostgreSQL 13; large table (~2 TB, 500M rows) in production; ALTER TABLE ADD COLUMN ... DEFAULT X NOT NULL executed during low-traffic window.
Symptoms
After running ALTER TABLE ADD COLUMN ... DEFAULT ... NOT NULL, the logical replica's apply lag increased dramatically and eventually stopped applying WAL; on the primary the replication slot retention caused pg_wal to grow; replica showed idle apply worker and no progress for hours; queries that rely on replica read-only reports stale results.
Observations
Primary executed ALTER which, on PostgreSQL 11+, can avoid table rewrite when adding a column with constant default but will write a fast-path column fill on first read; in our case, the ALTER completed quickly on primary, but the logical replication stream shows a huge burst of UPDATE-like change events for rows (visible in pg_logical or decoding plugin logs). Replica's apply process used excessive CPU and paused with errors about out-of-memory for apply worker's work_mem (temporary), then stalled. There are many large transactions in logical decoding stream corresponding to the ALTER column default application. Replica pg_stat_replication and pg_replication_slots show lag; no WAL corruption messages.
Already attempted
Paused application writes and retried alter in a maintenance window (same behavior); increased replica work_mem and max_apply_workers (temporary partial progress); attempted to create a new logical slot and rebase replica with pg_dump/pg_restore (too slow for 2 TB); monitored logical decoding output and confirmed huge change set generated by ALTER. Collected WAL segments and decoding logs; preserved system state for further analysis.
Question
Why did an ALTER TABLE ADD COLUMN with a constant default cause logical replication to emit massive change events and stall the replica, and what safe, reversible strategies can I use to bring replica back in sync without long downtime or full logical reinitialization?
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
- Databases40% match
PostgreSQL replica stops streaming WAL after pg_repack on primary; replication slot reports missing WAL
Open0 answers - Databases39% match
PostgreSQL logical replication slot grows until disk full after subscriber disconnects during nightly load
Open0 answers - Databases37% match
PostgreSQL replica falls behind and reports WAL streaming delay when frequent autovacuum runs on primary
Open0 answers - Databases30% match
PostgreSQL primary WAL sender CPU spikes and replication lag after enabling frequent TLS renegotiation on streaming replication
Open0 answers - Databases30% match
Logical backups hang waiting for locks when VACUUM FULL is running on large PostgreSQL tables
Open0 answers