Sub topic 14
WAL beyond crash recovery: archiving, PITR and replication
This chapter assumes basic SQL and psql, familiarity with transactions and ACID properties, and a basic understanding of PostgreSQL pages and configuration parameters.
Crash recovery uses WAL that still exists in the local pg_wal directory. Production systems often need more. You may need to recover a database to a specific time, rebuild a standby server, or keep a second server nearly current. PostgreSQL uses WAL archiving and replication for these jobs.
`wal_level` controls the information in WAL
wal_level is a server setting that controls how much information PostgreSQL writes to WAL.
minimalwrites enough for crash recovery. It cannot support archiving or replication.replicawrites enough for WAL archiving and physical replication.logicalwrites additional information needed by logical replication.
replica is the usual starting point for a server that needs backups, point-in-time recovery, or a physical standby. logical is required when changes must be decoded into row-level operations for another system.
Changing wal_level requires a server restart. The setting alone does not copy WAL anywhere.
WAL archiving and point-in-time recovery
WAL archiving copies completed WAL segment files from pg_wal to durable storage. A base backup provides a starting copy of the data directory. The archived WAL then replays changes made after that backup.
Point-in-time recovery, or PITR, restores the base backup and replays WAL only until a chosen target, such as a timestamp or transaction identifier.
A typical archive configuration looks like this:
ALTER SYSTEM SET wal_level = 'replica';
ALTER SYSTEM SET archive_mode = 'on';
ALTER SYSTEM SET archive_command =
'test -f /srv/postgres/archive/%f || cp %p /srv/postgres/archive/%f';archive_mode requires a restart. archive_command runs when PostgreSQL wants to archive a completed segment. %p means the source path, and %f means the WAL file name. The command must return success only after the file is safely stored. An incorrect success result can cause permanent data loss.
A backup tool should normally create the base backup. During recovery, restore_command retrieves archived files:
restore_command = 'cp /srv/postgres/archive/%f %p'
recovery_target_time = '2026-11-27 22:00:00+00'In current PostgreSQL versions, placing recovery.signal in the data directory starts archive recovery. A PITR plan must also include tested backups, archive monitoring, and enough storage for retained WAL.
Streaming replication
Streaming replication sends WAL records from a primary server to a standby as they are generated. The standby replays those records, so its data is usually close to the primary's state. A physical standby copies database changes at the page level and is suitable for failover and read-only queries.
A replication slot can prevent the primary from removing WAL before a standby has received it. This protects a disconnected standby, but an abandoned slot can fill the primary's disk.
Choosing between the mechanisms
| Need | Mechanism | Main risk |
|---|---|---|
| Recover after a crash | Local WAL replay | Lost or damaged local storage |
| Recover to an earlier time | Base backup plus WAL archive | Missing archive segments |
| Fast failover | Physical streaming standby | Replication lag |
| Send selected changes elsewhere | Logical replication | Schema and conflict management |
Archiving and streaming replication can run together. Streaming replication reduces failover time, while archiving provides older recovery points. Neither mechanism removes the need to monitor WAL volume, replication lag, archive failures, and disk usage.
Exercise
Why can a replication slot improve standby recovery but still threaten the primary server?
Show answer
It retains required WAL while the standby is behind, but an inactive or abandoned slot can retain enough WAL to exhaust the primary's disk.