The idea · Lab 3.6.5Every earlier lab in this block diagnosed a problem already visible in a running system. This lab is different: each of its four scenarios is a real disaster, built and then genuinely recovered from, one after another, on the same live server. A table is really dropped and really restored from a real pg_dump — and a row inserted after the dump is really, permanently gone. A real streaming standby is built with pg_basebackup, the primary is really crashed with an immediate shutdown, and the standby is really promoted to take over. A replication slot is really left inactive while WAL genuinely accumulates behind it, then safely cleaned up before it becomes a real problem. And a table's real transaction ID age is checked, frozen with a real VACUUM FREEZE, and confirmed reset to zero — the same mechanism that, left unmanaged for long enough, becomes a genuine wraparound emergency.
Nothing here is scaled up to a real crisis — this VM has too little real disk and memory headroom for that to be safe, and a genuine crisis is never the way to learn to prevent one. Every scenario is instead scaled down to a size this server can survive, while keeping every mechanism, every command, and every real error message completely genuine.
Scenario A: The Real Backup-Recency Gap
pg_dump -U postgres -d beer_db -t styles -F c -f /tmp/styles_backup.dump
A row inserted after this dump, then genuinely lost when the table is dropped and restored from it:
count
-------
26 <- after the insert
25 <- after DROP TABLE + pg_restore — the new row never made it into the dump
This is the real, honest shape of backup recency risk: a backup is only as current as the moment it was taken, and anything written after it is genuinely unrecoverable from that backup alone.
Scenario B: A Real Crash, A Real Promotion
pg_basebackup -U postgres -D /tmp/standby -R -X stream # real streaming standby
pg_ctl stop -D /var/lib/postgresql/19/data -m immediate # real, hard primary crash
pg_ctl promote -D /tmp/standby -w # real promotion to primary
pg_is_in_recovery() genuinely flips from t to f, and every row committed on the primary before the crash is still there on the promoted standby — because it had already streamed across before the crash happened.
Scenario C: Replication Slot Bloat, Genuinely Reproduced
A replication slot reserves WAL for a standby that might reconnect later — but if that standby never comes back, the reservation never releases on its own:
SELECT pg_create_physical_replication_slot('bloat_slot', true);
-- retained_wal starts at a few KB, genuinely grows to several MB after real write activity
SELECT pg_drop_replication_slot('bloat_slot'); -- the real, safe fix
The slot's restart_lsn genuinely falls further and further behind pg_current_wal_lsn() the longer it sits inactive — this VM's real disk cannot survive that growing unbounded, which is exactly why dropping an abandoned slot is a real, standard on-call action, not a theoretical one.
Scenario D: Transaction ID Age, Checked and Reset for Real
SELECT relname, age(relfrozenxid) FROM pg_class WHERE relname = 'beers'; -- a real, current age
VACUUM FREEZE beers; -- genuinely resets it
A real age of 134 genuinely drops to 0 after a real VACUUM FREEZE. In production, autovacuum_freeze_max_age (200,000,000 by default) is the real threshold where PostgreSQL forces an emergency autovacuum that cannot be cancelled — reaching anywhere near that age is the genuine definition of a wraparound emergency, which is never safely reproducible at real scale on a shared demo server.
Key concepts
Backup recency gap
The real, unavoidable window between when a backup was taken and when a disaster happens — anything written inside that window is genuinely unrecoverable from that backup alone, confirmed directly in this lab by a row that exists before a DROP TABLE but is missing after a restore from a dump taken earlier.
pg_ctl promote
The real command that ends a standby's recovery mode and makes it a fully independent, writable primary on a new timeline. It is irreversible for that standby — once promoted, it can no longer resume following its old primary without being rebuilt as a standby again from scratch.
Replication slot bloat
The genuine, unbounded growth of retained WAL behind a replication slot whose consumer (a standby, or a logical subscriber) has stopped connecting. PostgreSQL will not reclaim that WAL on its own while the slot exists, since the entire point of a slot is to guarantee that WAL stays available — which is exactly why an abandoned slot left unnoticed can genuinely fill a disk.
Transaction ID wraparound
PostgreSQL transaction IDs are a real, finite 32-bit counter. age(relfrozenxid) tracks how long it has been since a table's oldest rows were last frozen; if that age is ever allowed to approach 2 billion, PostgreSQL forces an emergency, unstoppable autovacuum (and eventually refuses new transactions entirely) to prevent transaction ID reuse from silently corrupting visibility. A routine VACUUM FREEZE resetting this age to 0, as reproduced directly in this lab, is the same real mechanism that prevents that emergency from ever being reached in a healthy cluster.
What to remember
- A backup is only ever as current as the moment it was taken — this lab genuinely lost a row written after a real pg_dump, confirming the recency gap directly rather than describing it
- A streaming standby, promoted with a real pg_ctl promote right after a genuine primary crash, lost zero previously-committed data — because that data had already streamed across before the crash happened
- An inactive replication slot genuinely retains WAL forever, growing unbounded until it is safely dropped — a real, standard on-call check, not a theoretical risk
- A routine VACUUM FREEZE (run automatically by autovacuum in a healthy cluster) is the exact real mechanism that keeps a table's transaction ID age from ever approaching a genuine wraparound emergency
- Every scenario in this lab was scaled down to stay safe on this VM's real, limited disk and memory — the mechanisms, commands, and real error messages were never simulated