Sub topic 3
Turning on Continuous WAL Archiving
Assume you can edit postgresql.conf, restart PostgreSQL, create directories, and inspect server logs. You also need a storage location that is separate from the PostgreSQL data directory. This chapter configures archiving; taking a base backup and restoring it are separate steps.
What continuous archiving does
PostgreSQL records changes in its write-ahead log (WAL). WAL is a sequence of files called segments. Before a changed data page is written to disk, the corresponding WAL record is written first.
Continuous archiving copies each completed WAL segment to another location. A base backup gives recovery a starting database state. The archived segments then replay changes made after that backup.
Configure the archiver
Set these parameters in postgresql.conf:
# postgresql.conf
wal_level = replica
archive_mode = on
archive_command = 'test ! -f /mnt/archive/%f && cp %p /mnt/archive/%f'
archive_timeout = 300The settings have different roles:
| Setting | Purpose | Change normally takes effect |
|---|---|---|
| `wal_level` | Includes enough WAL information for physical recovery | Server restart |
| `archive_mode` | Enables WAL archiving | Server restart |
| `archive_command` | Copies one completed segment to archive storage | Configuration reload |
| `archive_timeout` | Forces a segment switch after a period with activity | Configuration reload |
replica is the usual wal_level for physical backups and recovery. archive_mode must be enabled before archiving begins. Changing it requires a restart, not only a reload.
The command receives two PostgreSQL substitutions:
%pis the source path of the completed WAL segment.%fis the segment's file name.
The example first checks that the destination file does not exist, then copies the segment. The && operator means the copy runs only when the check succeeds.
Before starting PostgreSQL, create the destination and give the database operating-system user permission to write there:
sudo install -d -o postgres -g postgres -m 700 /mnt/archiveDo not place the only archive copy inside the data directory. A disk failure affecting the cluster could also destroy the archive.
Make failure visible
PostgreSQL treats an archive command that exits with status 0 as successful. Any nonzero status causes PostgreSQL to retry the same segment. This prevents a temporary storage or permission problem from silently discarding WAL.
Do not make the command always return success. For example, adding || true would tell PostgreSQL that a failed copy succeeded. That creates a recovery gap.
archive_timeout is a maximum wait before PostgreSQL switches to a new segment. It does not archive every transaction immediately, and it can create partially filled segments during quiet periods. Set it according to your recovery-point objective and storage costs.
After changing restart-required settings, restart PostgreSQL. Reload later changes with your platform's normal service command. Then inspect the server log and verify that new files appear in /mnt/archive. The next chapter covers base backups, which provide the starting point needed for replaying these files.
Exercise
Why does the sample archive_command test that the destination file does not already exist?
Show answer
It avoids overwriting an existing archived segment. An existing file may already be the correct immutable copy, or it may indicate a naming or storage problem that should be investigated.