Sub topic 12
Combining Streaming Replication with WAL Archiving
Assume you already understand PostgreSQL configuration files, pg_basebackup, physical standbys, and basic Linux file permissions. This chapter focuses on using WAL archiving as a complement to streaming replication.
Why use both methods?
Streaming replication sends write-ahead log (WAL) records directly from the primary to a connected standby. It usually provides low replication delay. However, streaming depends on a working network connection and on the primary retaining the WAL that the standby still needs.
WAL archiving copies completed WAL segment files to separate storage. The storage might be another server, an object store, or a mounted backup volume. A standby can use these archived files when streaming is temporarily unavailable.
The two methods provide different protection:
- Streaming supports near-real-time replication.
- Archiving lets a standby catch up after a longer outage.
- Archiving supports point-in-time recovery from a base backup.
- Separate archive storage protects against loss of the primary's local disk.
Archiving does not replace a base backup. A recoverable backup needs a base backup plus every WAL segment required after that backup.
Configure archiving on the primary
The primary must have WAL archiving enabled. The archive_command runs after PostgreSQL completes a WAL segment. The command must copy the segment to durable archive storage and return a zero exit status only when the copy succeeds.
# postgresql.conf on the primary
wal_level = replica
archive_mode = on
archive_command = 'test -f /var/lib/postgresql/wal_archive/%f || cp %p /var/lib/postgresql/wal_archive/%f'In this command, %p is the source path of the completed WAL file and %f is its file name. The test prevents an existing archived file from being overwritten. Create the archive directory first and give the PostgreSQL service account permission to write there.
For production systems, replace a local directory with a reliable backup or object-storage process. A local directory on the same disk as the database does not protect against disk failure.
You can check archiving progress on the primary:
SELECT archived_count, last_archived_wal, last_archived_time,
failed_count, last_failed_wal, last_failed_time
FROM pg_stat_archiver;A growing failed_count means the archive is incomplete, even if streaming replication still appears healthy.
Let a standby read the archive
Configure restore_command on the standby. PostgreSQL uses this command to fetch an archived WAL file when it needs one during recovery.
# postgresql.conf on the standby
restore_command = 'cp /var/lib/postgresql/wal_archive/%f %p'The command must return a nonzero status when the requested file is not available. PostgreSQL then continues trying other recovery sources, including streaming from the primary. The exact precedence and behavior can vary by recovery state, but the important model is that streaming handles the normal path while the archive fills gaps.
The standby needs access to the same archived files through a mounted directory, file transfer, or an archive-specific retrieval command. Do not assume that a path on the primary exists on the standby.
Common failure modes
If archive_command repeatedly fails, the primary keeps completed WAL files in its local pg_wal directory until the archive succeeds. This can fill the disk.
If the archive loses a required WAL segment, a standby may stop because it cannot continue recovery. Streaming can repair only a gap that the primary still retains. A replication slot can help retain streamed WAL, but it cannot recreate a segment that was deleted or never archived.
For point-in-time recovery, keep a tested base backup and a continuous, ordered WAL archive. Test restoring both instead of treating successful file copies as proof of recoverability.
Exercise
Why does WAL archiving still matter when a standby normally streams WAL directly?
Show answer
It provides a fallback when streaming is interrupted and supplies the WAL history needed for point-in-time recovery.