Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 157/ 160
08. Transactions, Security & Production SQL Administration

Point-in-Time Recovery (PITR) & Write-Ahead Log (WAL) Archiving

Recover to a specific timestamp using base backups plus WAL archives.

AdvancedPITR · WAL Archive3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Archiving settings (postgresql.conf)

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).

Key takeaway: Base backups + every WAL segment since = recover to any moment in between.

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.

Recovery settings

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.

Key takeaway: Recover to a new server, pause at the target, copy back only the damaged rows.
Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Setup

Archive WAL continuously

Enable archive_mode with a reliable archive command or tool, and alert if archiving fails.

Focus at this step

archive_mode = on · archive_timeout = 60

What to watch at this step
RPO
≈ archive_timeout
Tip: use ← → to move between steps

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

PITR (base backup + WAL)
Best for: Recovering from mistakes to the exact second

Strengths: Seconds of data loss; any moment in the retention window.

Watch out: Archive storage and replay time; needs regular testing.

Nightly logical dump
Best for: Small databases and long-term archives

Strengths: Portable; restore single tables easily.

Watch out: Up to a day of data loss; slow on big databases.

Replica / failover
Best for: Hardware or zone failures

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?
Interview tip: Tell the recovery as a story: base backup, WAL replay, target just before the mistake, restore to a separate server, copy rows back. Adding "and we rehearse it monthly" completes a senior answer.

Knowledge check

Loading quiz

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.

Open SQL Practice →SQL interview Q&A
Previous topic
Logical Backups (pg_dump, mysqldump) vs Physical Backups
Next topic
Read Replicas, High Availability & Failover Basics