Sub topic 7
Telling PostgreSQL How to Fetch Archived WAL
This chapter assumes you can edit postgresql.conf, stop and start PostgreSQL, and identify the cluster's data directory. You should also understand that a physical base backup is a copy of the whole cluster, while WAL records describe changes made after that copy.
The recovery input
A point-in-time restore needs two inputs:
- A physical base backup.
- Every archived WAL segment generated after that backup, up to the desired recovery point.
PostgreSQL first reads the base backup. During recovery, it requests WAL segments in sequence. The restore_command setting tells PostgreSQL how to obtain each requested segment from the archive.
The command receives two substitution values:
| Value | Meaning |
|---|---|
| `%f` | Requested WAL file name, such as `00000001000000000000002A` |
| `%p` | Destination path where PostgreSQL expects the file |
A command should copy %f from the archive to %p. PostgreSQL considers the segment available when the command exits with status zero.
Configuring `restore_command`
For an archive stored at /mnt/archive, a simple local configuration is:
# PostgreSQL 12 and later
restore_command = 'cp /mnt/archive/%f "%p"'The archive file name is inserted for %f. PostgreSQL supplies the destination path for %p. Quoting %p protects paths that contain spaces or shell-special characters.
PostgreSQL calls the command repeatedly. If the requested segment is not present yet, the command should return a nonzero status. A missing file is expected when recovery has reached the end of the available archive. PostgreSQL can then try other WAL sources, such as WAL files included in the base backup or a configured streaming source.
Do not make a missing-file command report success. For example, cp returns a failure status when the source file does not exist. If a wrapper script hides that status, PostgreSQL may treat an incomplete recovery step as successful.
Starting archive recovery
For PostgreSQL 12 and later, create an empty recovery.signal file in the data directory:
sudo -u postgres touch /var/lib/postgresql/16/main/recovery.signalAdd the recovery settings to postgresql.conf or postgresql.auto.conf, then start the server. The presence of recovery.signal tells PostgreSQL to enter archive recovery during startup.
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_action = 'promote'The target settings determine where replay stops. If no target is configured, PostgreSQL replays all WAL it can obtain. Recovery behavior after reaching a target is controlled by recovery_target_action, which can pause, promote the server, or shut it down.
Check the recovery result
Watch the PostgreSQL log while the server starts. Look for messages showing that archive recovery began, WAL files were restored, and the recovery target was reached. After promotion, PostgreSQL accepts read/write connections and creates a new timeline.
Before allowing application traffic, verify the recovered timestamp and important records. Also confirm that the original damaged data directory remains separate until the restore has been checked.
Exercise
What should restore_command return when PostgreSQL requests a WAL segment that is not yet in the archive?
Show answer
It should return a nonzero exit status, allowing PostgreSQL to treat the segment as unavailable rather than as a successful restore.