Sub topic 2
Checking that archiving works
This chapter assumes you can edit postgresql.conf, run psql as a superuser, and read a PostgreSQL 12 or later data directory. You should already have archive_mode = on and an archive_command set. Archiving is not confirmed until you check it.
Archiving copies each finished WAL segment to a safe location. PostgreSQL keeps counters for these copies in the pg_stat_archiver view. Query it to confirm that copies succeed:
SELECT archived_count, last_archived_wal, last_archived_time,
failed_count, last_failed_wal, last_failed_time
FROM pg_stat_archiver;Force a segment switch to test the pipeline without waiting for a segment to fill:
SELECT pg_switch_wal();Run the query again. archived_count should increase, and failed_count should not. If failed_count rises, check the server log. A failing archive_command keeps WAL in pg_wal, which can fill the disk.
Telling PostgreSQL how to fetch archived WAL
During recovery, the server needs a way to fetch archived segments. The restore_command setting provides it. PostgreSQL replaces %f with the segment file name and %p with the destination path.
# postgresql.conf
restore_command = 'cp /mnt/archive/%f "%p"'Recovery mode starts only when the data directory contains a recovery.signal file. Create it with touch, then start the server.
Choosing where recovery stops
Recovery replays WAL until it reaches a target. Set one target at a time:
| Setting | Stops recovery at |
|---|---|
| `recovery_target_time` | A timestamp, such as `'2026-11-27 14:30:00+00'` |
| `recovery_target_xid` | A transaction ID |
| `recovery_target_lsn` | A WAL location |
| `recovery_target_name` | A named restore point |
| `recovery_target_immediate` | The first consistent state reached |
Recovery stops at the target, and by default it pauses there (recovery_target_action = 'pause').
Marking safe points with named restore points
A named restore point records a label in the WAL before a risky change, such as a schema migration:
SELECT pg_create_restore_point('before_schema_migration');Restoring to that label works only if the WAL containing it was archived. Call pg_switch_wal() right after creating the point, then check pg_stat_archiver.
Exercise
You set recoverytargetname = 'beforeschemamigration', but the restore fails because the WAL is missing. Which check on pgstatarchiver would have caught this earlier, and why?
Show answer
Check failedcount and lastfailed_wal. A failed copy means the segment that holds the restore point never reached the archive, so the named target cannot be reached. Run this check right after creating the restore point, not only after the migration.