PostgreSQL replica stops streaming WAL after pg_repack on primary; replication slot reports missing WAL
Environment
Primary PostgreSQL 13.9 on Ubuntu 20.04 with streaming replication to a hot standby using physical replication slots. pg_repack 1.4.9 run on large table during low-traffic window. WAL archiving to file system enabled. Replica on same LAN with identical PostgreSQL minor version.
Symptoms
After pg_repack completed, replica's recovery process logs 'requested WAL segment ... has already been removed', replication slot on primary shows 'confirmed_flush_lsn' lagging and pg_stat_replication shows replication state 'streaming' then 'replication terminated'. Replica stops applying new WAL and falls behind. No manual base backup was performed after pg_repack.
Observations
Primary's pg_wal still contains newer segments, but some earlier WAL files referenced by the replica's last received LSN are no longer present in pg_wal and not in archive directory. pg_repack was run with default options (not using --preserve-row-order) and completed successfully. Archive_command reported some 'permission denied' messages during the repack window (log entries show copy to archive failed for a few files). pg_repack does not alter physical replication state but does create lots of WAL while rebuilding tables.
Already attempted
Checked archive directory for missing WALs and filesystem permissions; ensured archive_command is functional; tried restarting replica, which then fails to fetch older WAL segments. Considered promoting replica but it's behind. Restarts of primary didn't re-create removed WAL segments. No base backup taken post-repack.
Question
How can I recover the replica that stopped applying WAL because referenced WAL segments were removed, and what precautions should I take when running heavy WAL-producing operations like pg_repack to avoid replication slot invalidation?
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
- Databases36% match
PostgreSQL logical replication falls behind after ALTER TABLE ADD COLUMN with non-null DEFAULT on large table
Open0 answers - Databases33% match
PostgreSQL replica falls behind and reports WAL streaming delay when frequent autovacuum runs on primary
Open0 answers - Databases33% match
PostgreSQL standby stops applying WAL during long-running transactions on primary after pg_basebackup restore
Open0 answers - Databases33% match
PostgreSQL logical replication slot grows until disk full after subscriber disconnects during nightly load
Open0 answers - Databases30% match
PostgreSQL primary WAL sender CPU spikes and replication lag after enabling frequent TLS renegotiation on streaming replication
Open0 answers