Sub topic 10
Archiving WAL continuously
This chapter assumes you know basic SQL and psql, can use the Linux command line, and understand PostgreSQL roles, databases, and the data directory. You should also know that a transaction groups changes that PostgreSQL commits as one unit.
What WAL archiving provides
Write-ahead logging (WAL) is PostgreSQL’s change record. Before PostgreSQL writes a changed data page to disk, it writes a description of that change to WAL. WAL lets PostgreSQL recover after a crash.
WAL archiving copies completed WAL segment files from the server to another location. A base backup plus the archived WAL files can rebuild the database as it existed at a later point. This supports point-in-time recovery (PITR), which means recovering to a chosen time rather than only to the time of the last backup.
WAL archiving is useful when:
- You need to recover changes made after the last base backup.
- You need to recover to just before an accidental
DROP TABLEor bad deployment. - Your recovery point objective (RPO) is shorter than your base-backup interval.
WAL archiving alone is not a complete backup. You need at least one usable base backup and every required WAL segment after that backup.
Configure the archive destination
Create an archive directory that the PostgreSQL operating-system user can write to. Keep it outside the PostgreSQL data directory. If the data disk fails, an archive on the same disk may be lost too.
Set these parameters in postgresql.conf:
wal_level = replica
archive_mode = on
archive_command = 'test ! -f /mnt/archive/%f && cp %p /mnt/archive/%f'wal_level = replica includes enough WAL information for archiving and replication. archive_mode = on enables archiving. archive_command is a shell command that PostgreSQL runs for each completed WAL segment.
In the command:
%pis the path to the source WAL file.%fis the WAL file name.test ! -fprevents an existing archive file from being overwritten.- A successful command must return exit status zero.
archive_mode requires a server restart. wal_level and archive_command normally take effect after a reload, but follow the requirements of your PostgreSQL version and package.
Monitor failures and disk usage
If the command fails, PostgreSQL keeps the WAL segment in pg_wal and retries it. This protects recovery, but an archive outage can eventually fill the PostgreSQL data disk and stop normal operation.
Check the archiver statistics:
SELECT archived_count,
failed_count,
last_archived_wal,
last_archived_time,
last_failed_wal,
last_failed_time
FROM pg_stat_archiver;A growing failed_count, an old last_archived_time, or a full archive filesystem needs immediate attention. Do not delete archived WAL files casually. They may still be required by a base backup or a recovery process.
For stronger protection, copy archives to separate storage and use a backup tool that verifies checksums, manages retention, and handles retries. pgBackRest and Barman are common choices when the archive command becomes difficult to operate safely.
Exercise
Exercise
Why can a successful base backup still be insufficient for recovering the database to yesterday at 14:00?
Show answer
The base backup contains only the database state captured during that backup. Recovery to yesterday at 14:00 also requires every archived WAL segment from the backup through that target time.