Sub topic 6
Checking That WAL Archiving Works
This chapter assumes you can connect to PostgreSQL, run SQL, inspect postgresql.conf, and read files on the archive server. It focuses on verifying an existing WAL archive, not configuring the full backup system.
A configured archive_command is not proof that archiving works. PostgreSQL runs the command only after it completes a WAL segment. The command must return exit status 0. Any other status means that PostgreSQL keeps the segment in pg_wal and tries again later.
Read the archiver statistics
PostgreSQL exposes archiving counters through pg_stat_archiver. Query it from the database:
SELECT
archived_count,
failed_count,
last_archived_wal,
last_archived_time,
last_failed_wal,
last_failed_time,
stats_reset
FROM pg_stat_archiver;The important columns are:
| Column | Meaning |
|---|---|
| `archived_count` | WAL segments archived successfully since `stats_reset` |
| `failed_count` | Archive attempts that failed since `stats_reset` |
| `last_archived_wal` | Name of the most recently archived segment |
| `last_archived_time` | Time of the most recent successful archive |
| `last_failed_wal` | Name of the segment in the most recent failed attempt |
| `last_failed_time` | Time of that failure |
| `stats_reset` | Time when these statistics were reset |
A nonzero failed_count does not always mean that archiving is failing now. It might describe an old disk-full event or a temporary network outage. Compare the timestamps with the current time and inspect the PostgreSQL server log.
The counters are cumulative since the statistics reset. They can be reset by an administrator, a server restart in some configurations, or a statistics reset operation. Record the observation time when you use these values for monitoring.
Force a test archive
A WAL segment might not fill quickly on a quiet system. You can force PostgreSQL to finish the current segment:
SELECT pg_switch_wal();
SELECT
archived_count,
failed_count,
last_archived_wal,
last_archived_time,
last_failed_wal,
last_failed_time
FROM pg_stat_archiver;The first query returns the WAL location reached by the switch. The second query shows whether a completed segment was archived. Allow a short delay before checking, especially when the archive destination is remote.
The segment named by last_archived_wal should exist in the archive destination. If the command uses %f, PostgreSQL replaces it with the WAL file name. If it uses %p, PostgreSQL replaces it with the path to the local WAL file.
Investigate failures
Check the PostgreSQL log for the exact command output. Common causes include:
- The archive directory does not exist.
- The PostgreSQL service account cannot write there.
- The destination filesystem is full.
- A network mount is unavailable.
- The command treats an existing file as an error.
- Quoting or path expansion is incorrect.
- The command reports success before the remote copy is durable.
Do not delete files from pg_wal to reduce disk usage while archiving is failing. PostgreSQL needs those files for retrying and for normal recovery. Fix the archive destination first, then confirm that failed_count stops increasing and last_archived_time advances.
Exercise
After running SELECT pg_switch_wal();, failed_count increases and last_archived_time does not change. What should you inspect first?
Show answer
Inspect the PostgreSQL server log and the archive destination's permissions, free space, availability, and command quoting. Do not remove the pending WAL files.