WAL archiving is the practice of copying every completed write-ahead log (WAL) segment from a database server to separate, durable storage, so that the full history of changes survives the loss of the server. In backup & recovery, it is the foundation of point-in-time recovery (PITR): a physical base backup combined with an unbroken WAL archive can rebuild the database to any moment between the backup and the latest archived segment.
The term is most closely associated with PostgreSQL, where it is also called continuous archiving, but the same principle exists in other engines as archived redo logs, binary log backups or transaction log backups.
How WAL archiving works
Every change in PostgreSQL is first written to the write-ahead log before data files are modified, which is what allows crash recovery. The WAL is split into segment files of 16 MB by default, and when a segment is full or forcibly switched, the server marks it as ready for archiving.
The archiver process then calls the configured archive_command or, since PostgreSQL 15, an archive_library module. Only when that call returns success is the segment considered archived and allowed to be recycled; a failing command makes the server retry indefinitely and retain WAL locally.
The table below lists the main configuration parameters that control archiving and restoring.
| Parameter | Typical value | Effect |
|---|---|---|
| wal_level | replica or logical | Writes enough detail for archiving and replicas |
| archive_mode | on | Enables the archiver process |
| archive_command | Script copying %p to storage | Defines how each segment is stored |
| archive_timeout | 60 to 300 seconds | Forces segment switches on quiet systems |
| restore_command | Script fetching %f back | Retrieves segments during recovery |
| recovery_target_time | A specific timestamp | Stops replay at the chosen moment |
WAL archiving and base backups together
A WAL archive alone cannot restore a database, because it contains changes, not a starting state. Recovery always begins from a physical base backup, typically taken with pg_basebackup or a backup tool, and then replays archived WAL from the backup start point forward.
This makes the oldest retained base backup the practical limit of recovery: WAL older than that backup is useless and can be deleted. Retention policies are therefore expressed as a number of base backups plus all WAL needed to make each of them consistent and to roll it forward.
The following table compares WAL archiving with related protection methods in the same Topic.
| Technique | Recovery granularity | Main limitation |
|---|---|---|
| WAL archiving with base backup | Any moment covered by the archive | Replay time grows with WAL volume |
| Logical dump such as pg_dump | Only the moment of the dump | Slow restore for large databases |
| Storage snapshot | Only the moment of the snapshot | Usually tied to one storage system |
| Streaming replication | Current state of the primary | Replicates mistakes immediately |
| Log shipping | Segment by segment to a standby | Standby lags by up to one segment |
Designing a reliable archive
A reliable archive must be complete, durable and separate from the primary server. A single missing segment breaks the chain, and recovery cannot proceed beyond that gap, so archiving scripts must never overwrite existing files and must fsync or verify uploads before reporting success.
Hand-written commands such as a simple cp are acceptable for learning but fragile in production. Dedicated tools such as pgBackRest, Barman and WAL-G add parallel transfer, compression, encryption, checksums and retention management, and they handle object storage correctly.
| Decision | Recommended choice | Reason |
|---|---|---|
| Archive location | Different host, region or object storage | Survives loss of the primary site |
| Write behavior | Refuse to overwrite existing segments | Protects against split-brain corruption |
| Transfer method | Dedicated backup tool | Adds checksums, retries and parallelism |
| Low-traffic systems | Set archive_timeout or use pg_receivewal | Limits data loss during quiet periods |
| Monitoring | Alert on failed archiving and ready files | Catches gaps before they matter |
Recovery objectives and timelines
The recovery point objective (RPO) of WAL archiving equals the data in segments not yet archived. On a busy server this is seconds, but on a quiet one it can reach the full archive_timeout interval. Streaming WAL with pg_receivewal reduces the gap to near zero without waiting for segment switches.
The recovery time objective (RTO) depends on base backup size and on how much WAL must be replayed. Replaying a week of heavy write traffic can take hours, which is why teams take base backups often enough to keep replay within their target window.
After a point-in-time recovery, PostgreSQL starts a new timeline and writes a history file to the archive. Timelines prevent WAL from the abandoned history from mixing with new changes, but they also mean the archive must keep these small history files for later recoveries.
Common failure modes
Most archiving incidents are discovered only during a restore, which is why regular restore tests are as important as the archive itself. The following problems appear repeatedly in production environments.
- Disk filling with pg_wal because the archive command fails silently and segments accumulate.
- Archive command returning success too early, before data has reached durable storage.
- Deleting WAL still needed by the oldest base backup under a careless cleanup job.
- Never testing restore_command, so recovery fails on permissions or paths.
- Keeping the archive on the same storage as the database, which defeats its purpose.
In everyday language
Think of a database as a large ledger. Its write-ahead log is a diary of every change. WAL archiving copies each finished diary page to a safe place, far from the ledger.
Suppose the ledger is destroyed or someone makes a serious mistake. You take an older photocopy of the ledger. Then you replay the diary entries up to the exact minute you choose. The key takeaway is that an unbroken diary plus one good copy lets you rebuild the past precisely.
Seen in the real world
An online ticketing company runs a 900 GB PostgreSQL database. It takes a nightly base backup with pgBackRest and archives WAL continuously to object storage in another region, with archive_timeout set to 60 seconds and alerts on any failed archive attempt.
One afternoon at 14:32, a developer runs an UPDATE without a WHERE clause and overwrites the prices of every upcoming event. Streaming replicas copy the error within a second, so they cannot help. The team restores the latest base backup to a new server and sets recovery_target_time to 14:31:50.
After about 40 minutes of restore and WAL replay, the recovered server contains correct prices, and the team copies the affected table back to production. The incident costs less than one hour of effort and no lost orders, because the WAL archive had no gaps.