WAL archiving

Also known as: continuous archiving

On this page 7

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.

ParameterTypical valueEffect
wal_levelreplica or logicalWrites enough detail for archiving and replicas
archive_modeonEnables the archiver process
archive_commandScript copying %p to storageDefines how each segment is stored
archive_timeout60 to 300 secondsForces segment switches on quiet systems
restore_commandScript fetching %f backRetrieves segments during recovery
recovery_target_timeA specific timestampStops replay at the chosen moment
Core PostgreSQL parameters for WAL archiving

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.

TechniqueRecovery granularityMain limitation
WAL archiving with base backupAny moment covered by the archiveReplay time grows with WAL volume
Logical dump such as pg_dumpOnly the moment of the dumpSlow restore for large databases
Storage snapshotOnly the moment of the snapshotUsually tied to one storage system
Streaming replicationCurrent state of the primaryReplicates mistakes immediately
Log shippingSegment by segment to a standbyStandby lags by up to one segment
WAL archiving compared with related techniques

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.

DecisionRecommended choiceReason
Archive locationDifferent host, region or object storageSurvives loss of the primary site
Write behaviorRefuse to overwrite existing segmentsProtects against split-brain corruption
Transfer methodDedicated backup toolAdds checksums, retries and parallelism
Low-traffic systemsSet archive_timeout or use pg_receivewalLimits data loss during quiet periods
MonitoringAlert on failed archiving and ready filesCatches gaps before they matter
Archive design decisions

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.