Point-in-Time Recovery (PITR) & Write-Ahead Log (WAL) Archiving
Recover to a specific timestamp using base backups plus WAL archives.
In plain words
Start from an old copy of the database, replay every change in order, and stop just before the mistake.
Think of it like…
A video recording of a whiteboard: the base backup is a photo taken at 1 a.m., and the WAL is the recording of every change since. To see the board as it was at 2:32 p.m., you start from the photo and play the recording forward, pausing at exactly 2:32.
Where you’ve seen it
At 14:32:10 a support script runs DELETE FROM orders WHERE status = 'pending' without a date filter, removing 41,000 orders. The team restores the 01:00 base backup to a new server, sets recovery_target_time = '2026-11-20 14:32:00+05:30', replays WAL to that moment, copies the 41,000 rows from the recovered server back into production, and leaves every order created after 14:32 untouched.
Words you’ll hear
- Write-ahead log (WAL)
- A sequential record of every change, written before the data files are updated.
- Base backup
- A physical copy of the database files that recovery starts from (pg_basebackup).
- Recovery target
- Where replay stops: a time, transaction id, LSN or named restore point.
- RPO / RTO
- How much data you can lose / how long recovery may take.
Visual diagram
Theory & example queries
Setting up continuous archiving
Every change in PostgreSQL is first written to WAL in 16 MB segment files. With archive_mode = on and an archive_command (or archive_library), each completed segment is copied to durable storage such as object storage; archive_timeout forces a segment switch on quiet systems so the archive is never more than, say, 60 seconds behind. Take periodic base backups with pg_basebackup or a tool, and keep all WAL since the oldest base backup you might restore from.
In practice teams use pgBackRest, WAL-G or Barman, which handle compression, parallelism, retention, encryption and verification. Managed services do it for you: Amazon RDS and Aurora, Cloud SQL and Azure Database for PostgreSQL offer restore to any second within the retention window (up to 35 days on RDS), always into a new instance. MySQL achieves the same with a full backup plus binary log replay using mysqlbinlog --stop-datetime.
wal_level = replica archive_mode = on archive_command = 'pgbackrest --stanza=main archive-push %p' archive_timeout = 60 with a nightly pgbackrest --stanza=main --type=full backup (or incremental backups between fulls).
Recovering, and what to do after
Restore the base backup to a new data directory (never over the damaged production server), set restore_command to fetch archived WAL and recovery_target_time (or recovery_target_xid, recovery_target_lsn, or recovery_target_name for a restore point created with pg_create_restore_point), create a recovery.signal file (PostgreSQL 12+), and start the server. With recovery_target_action = 'pause', you can inspect the data before promoting it.
Usually you don't replace production with the recovered copy, since that would throw away everything after 14:32. Instead, extract the damaged rows from the recovered server (pg_dump of one table, or a query through postgres_fdw) and put them back. After promotion the recovered server starts a new timeline, so keep its WAL separate. And rehearse: a recovery you've never tested is a guess; time it to know your real RTO, and alert when archiving fails, because one missing segment makes everything after it unrecoverable.
restore_command = 'pgbackrest --stanza=main archive-get %f "%p"' recovery_target_time = '2026-11-20 14:32:00+05:30' recovery_target_action = 'pause' then touch recovery.signal and start PostgreSQL.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Archive WAL continuously
Enable archive_mode with a reliable archive command or tool, and alert if archiving fails.
archive_mode = on · archive_timeout = 60
When to use it & trade-offs
What it is
Point-in-time recovery (PITR) restores a database to its exact state at a chosen moment, such as 14:32:00, one second before someone ran a bad DELETE. It needs two things: a base backup (a physical copy of the data directory) and every write-ahead log (WAL) file written since, kept by continuous WAL archiving. Recovery restores the base backup, then replays WAL up to the recovery target and stops.
Why it matters: Nightly backups lose up to a day of data; PITR lets you lose seconds. The incidents it saves you from are usually human: a migration that drops the wrong column, an UPDATE without WHERE, a script run against production. Knowing how PITR works, what RPO and RTO it gives, and how to recover just the damaged rows is expected of senior backend, DBA and SRE candidates.
When to reach for it
- Undoing accidental DELETE, UPDATE or DROP
- Meeting low RPO requirements
- Creating a copy of the database as of a past moment for investigation
When to avoid or be careful
- Treating replicas as backups
- Restoring over the production server
- Archiving without alerts on failures
Recovery options
Strengths: Seconds of data loss; any moment in the retention window.
Watch out: Archive storage and replay time; needs regular testing.
Strengths: Portable; restore single tables easily.
Watch out: Up to a day of data loss; slow on big databases.
Strengths: Minutes or less of downtime.
Watch out: Replicates mistakes instantly; not a backup.
Interview signals & numbers to know
When this topic comes up
- “Someone deleted orders 10 minutes ago”
- “What's our RPO and RTO?”
- “How do backups work for Postgres?”
- WAL archiving
- Disaster recovery
Benchmarks & rules of thumb
- 16 MB
- default PostgreSQL WAL segment size
- 35 days
- maximum automated backup retention on Amazon RDS
- PostgreSQL 12
- recovery.signal replaced recovery.conf
Common mistakes & follow-ups
Common interview mistakes
- Relying on a streaming replica to recover from a DROP TABLE, which the replica applied a second later.
- Discovering during an incident that archiving had been failing for a week, so recovery stops at last Tuesday.
- Promoting the recovered server as the new production database and losing every order placed after the target time.
Likely interviewer follow-ups
- Walk me through recovering from an accidental DELETE an hour ago.
- Why isn't a read replica a backup?
- How would you know your backups actually work?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Point-in-Time Recovery (PITR) & Write-Ahead Log (WAL) Archiving with hands-on SQL practice and interview Q&A.