Sub topic 6
Checking that archiving works
Archiving can fail without anyone noticing. PostgreSQL keeps running while a broken archive_command retries in the background. Your first sign of trouble is often a restore that cannot find a WAL segment, or a full disk on the primary. This chapter shows how to confirm that archiving works and how to catch problems early.
This chapter assumes you can edit postgresql.conf, run psql, and read the server log. You should also know that archive_mode and archive_command are already configured, as covered in earlier chapters.
What can go wrong
The archiver is a separate background process. It copies each completed WAL segment by running your archive_command. If the command exits with a non-zero status, PostgreSQL does the following:
- It logs the failure.
- It keeps the segment in
pg_wal. - It retries the same segment, with a pause between attempts.
While one segment is stuck, later segments pile up behind it, because PostgreSQL archives segments in order. Nothing is removed from pg_wal until it has been archived. If the problem lasts long enough, pg_wal fills the disk and the server stops accepting writes.
So there are two risks:
- A gap in the archive. Your backup chain is broken, and recovery past that point is impossible.
- Disk exhaustion on the primary. The failure turns into an outage.
Reading pg_stat_archiver
The view pg_stat_archiver has a single row with the archiver's counters. Query it directly:
SELECT archived_count,
last_archived_wal,
last_archived_time,
failed_count,
last_failed_wal,
last_failed_time,
stats_reset
FROM pg_stat_archiver;| Column | Meaning |
|---|---|
| `archived_count` | Segments archived successfully since the last reset |
| `last_archived_wal` | File name of the most recent success |
| `last_archived_time` | When that success happened |
| `failed_count` | Failed attempts since the last reset |
| `last_failed_wal` | File name of the most recent failure |
| `last_failed_time` | When that failure happened |
| `stats_reset` | When the counters were last reset |
How to interpret it
A healthy archive shows archived_count increasing over time and last_archived_time recent. A non-zero failed_count is not an emergency by itself. A single transient failure that is later followed by success is harmless.
The important comparison is between the two timestamps:
- If
last_failed_timeis later thanlast_archived_time, the archiver is currently failing. - If
last_archived_timeis later, the last failure has been resolved.
Compare last_archived_wal and last_failed_wal as well. If they differ, the archiver has moved past the failed segment. If they are the same, that exact segment is still stuck.
A low-traffic server can also look "stale" without being broken. If no WAL is being generated, no segment completes, and last_archived_time does not advance. That is why archive_timeout is useful: it forces a segment switch after the given number of seconds, so you get a regular archive heartbeat even when the server is idle.
Testing the pipeline on purpose
Do not wait for traffic to prove that archiving works. Force a segment switch and watch the counters. The first statement records the starting count, and the second forces the current segment to be completed.
SELECT archived_count AS before_count FROM pg_stat_archiver;
SELECT pg_switch_wal();
SELECT pg_sleep(5);
SELECT archived_count AS after_count, last_archived_wal, last_failed_time
FROM pg_stat_archiver;If after_count is larger than before_count and last_archived_wal names a recent segment, the whole path works: the server, the command, the permissions, and the destination.
Then check the archive directory itself. The counter reports that the command returned success. It does not prove the file is intact.
ls -l /mnt/archive | tail -n 5Each file should have the expected size. By default a WAL segment is 16 MB, unless the cluster was initialized with a different --wal-segment-size. A zero-byte or truncated file means the command reported success without copying correctly, which is a bug in the command.
Looking at the server log
Failures from archive_command are written to the server log. A typical message looks like this:
LOG: archive command failed with exit code 1
DETAIL: The failed archive command was: test ! -f /mnt/archive/000000010000000000000042 && cp pg_wal/000000010000000000000042 /mnt/archive/000000010000000000000042
WARNING: archiving write-ahead log file "000000010000000000000042" failed too many times, will try again laterThe DETAIL line shows the exact command that ran, with %p and %f expanded. Copy it and run it by hand as the postgres operating system user. Most failures reproduce immediately, and the shell tells you why.
Common causes:
- Permission denied. The
postgresuser cannot write to the archive directory. - File already exists. The
test ! -fguard fails because a file with that name is already present. This happens when a previous attempt copied the file but crashed before reporting success, or when two clusters share one archive directory. - Destination not mounted. The network share or disk is missing, so the path does not exist.
- Destination full. The archive volume has no space left.
Watching the backlog from the primary
The archiver exposes failures through statistics, but the cost of a failure appears in pg_wal. Archiver status files live in pg_wal/archive_status. A segment waiting to be archived has a .ready file. After success, PostgreSQL renames it to .done.
Count the waiting segments from the shell:
ls /var/lib/postgresql/16/main/pg_wal/archive_status/*.ready 2>/dev/null | wc -lOn a healthy system this number is small, usually 0 to 2. A number that keeps growing means the archiver is falling behind or failing. Multiply the count by the segment size to estimate how much disk is at risk. For example, 200 .ready files at 16 MB is about 3.2 GB.
You can also check the total size of the WAL directory from SQL:
SELECT count(*) AS wal_files,
pg_size_pretty(sum(size)) AS wal_size
FROM pg_ls_waldir();The function pg_ls_waldir() requires superuser rights or membership in the pg_monitor role. Run it periodically and compare. A steady value is normal, while a value that climbs is a warning.
Turning checks into alerts
Manual checks do not protect you at 3 a.m. Put the key signals in your monitoring system. A practical set:
- Archiver is failing. Alert when
last_failed_time > last_archived_time. - Archive is stale. Alert when
now() - last_archived_timeexceeds a threshold. Witharchive_timeout = 300, a threshold of 15 minutes is reasonable. On a server that is always busy, a smaller threshold works. - Backlog is growing. Alert when the number of
.readyfiles, or thepg_walsize, crosses a limit well below the disk capacity. - Archive volume is filling. Alert on free space at the destination, not only on the primary.
Here is a query that produces a single row you can feed to a checker. It returns the number of seconds since the last success and a flag that says whether the most recent attempt failed:
SELECT extract(epoch FROM (now() - last_archived_time))::int AS seconds_since_success,
(last_failed_time IS NOT NULL
AND (last_archived_time IS NULL
OR last_failed_time > last_archived_time)) AS currently_failing
FROM pg_stat_archiver;Note the NULL handling. Right after a statistics reset, or on a server that has never archived, last_archived_time is NULL. Without the explicit check, the comparison yields NULL and your alert stays silent exactly when it should fire.
Edge cases to know
- Statistics resets.
pg_stat_reset_shared('archiver')zeroes the counters and setsstats_reset. After a reset, afailed_countof 0 says nothing about the past. Look atstats_resetbefore trusting the numbers. - Crash or restart. The archiver counters are statistics, and they can be lost after a crash. The
.readyand.donefiles on disk are the more durable record. - Standbys. With
archive_mode = on, a standby does not archive WAL. Onlyalwaysmakes a standby archive what it receives. If you rely on a standby for archiving, check the setting there, and remember that two servers writing to one archive need the identical-file guard described above. - A successful command is not a good archive. The view reports the command's exit status only. Verifying that the archive can actually be restored is a separate step, covered later in the tutorial under test restores.
Exercises
Exercise
pg_stat_archiver shows failed_count = 12, last_failed_time = 2026-11-27 09:10:00, and last_archived_time = 2026-11-27 09:40:00. Is the archiver currently failing? What would change your answer?
Show answer
No. The last archived time is later than the last failed time, so the archiver succeeded after the failures. The answer would change if last_failed_time were later than last_archived_time, or if last_failed_wal equaled last_archived_wal's successor and the same segment kept failing. The stale failure count alone is not a problem.
Exercise
You force a segment switch with pg_switch_wal() and archived_count does not increase. List three things you would check, in order.
Show answer
First, confirm the switch had effect: an idle server has no WAL to switch, so generate a small write and switch again. Second, read last_failed_time and last_failed_wal in pg_stat_archiver and the server log for the exact failed command. Third, run that command by hand as the postgres user to find the cause, such as permissions, a missing mount, a full destination, or an existing file.
Exercise
Why can a growing number of .ready files in pg_wal/archive_status take the primary offline even though queries still work normally at first?
Show answer
PostgreSQL does not remove a WAL segment until it is archived. If archiving fails, segments accumulate in pg_wal. When the disk fills, the server cannot write new WAL and stops accepting writes.