Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Explain how point-in-time recovery works in PostgreSQL. What must you keep, and how would you restore the database to 14:32 yesterday just before a bad DELETE?

Point-in-time recovery restores a physical base backup and then replays archived WAL up to a chosen moment. So we must keep a base backup from before the target time, taken with pg_basebackup, pgBackRest or Barman, plus every WAL segment since, archived continuously with archive_command or archive_library. To recover to 14:32 yesterday, we restore that backup on a separate server, set restore_command and recovery_target_time to the exact timestamp, create recovery.signal and start it; PostgreSQL replays to the target and then pauses or promotes according to recovery_target_action, and we copy the deleted rows back to production. A pg_dump cannot do this, because it is a single snapshot.

Why this question is asked

Backups that can only bring the database back to last night are of little use when someone ran a bad DELETE at 14:32 and the business carried on writing until 15:10. Point-in-time recovery, PITR, lets us go back to any second, which is what turns a disaster into an inconvenience. Interviewers ask it because many teams believe they have it and discover during the incident that a WAL segment is missing or that they only have dumps. In this answer, we will recap the building blocks, set up what we must keep, perform the restore to 14:32 step by step, see why we restore to a separate instance, and explain why logical dumps cannot do this.

1A Quick Recap of Base Backups and WAL

PostgreSQL writes every change to the write-ahead log, the WAL, before changing data files. The WAL is split into segment files, 16 MB each by default. If we have a copy of the data files at some moment, and every WAL segment written after it, we can replay those changes and arrive at any later moment.

That copy of the data files is a base backup: a physical copy of the whole cluster, taken while the database runs. Because it is taken while writes continue, it is not consistent on its own; it becomes consistent once the WAL written during the backup has been replayed. So a base backup is always used together with WAL.

2What We Must Keep

We need two things, continuously. Base backups, taken regularly, for example nightly, with pg_basebackup or a backup tool such as pgBackRest or Barman, which add compression, incremental backups, retention and verification. And WAL archiving: every completed WAL segment is copied to safe storage by archive_command, a shell command, or archive_library, a loadable module, available since PostgreSQL 15.

Java
# postgresql.conf on the primary
wal_level = replica
archive_mode = on
archive_command = 'pgbackrest --stanza=shop archive-push %p' # %p is the segment's path
archive_timeout = 60 # force a segment switch at least every 60 s on quiet systems
-- check that archiving keeps up
SELECT archived_count, last_archived_wal, failed_count, last_failed_time FROM pg_stat_archiver;
-- 18234 | 00000001000004A2000000C7 | 0 |

Notice the retention rule this implies: to reach any moment in the last seven days, we must keep the newest base backup taken before that moment plus all WAL from then on. A single missing segment breaks every recovery that needs to pass it, which is why failed_count must be monitored.

3Restoring to 14:32 Yesterday

First, we find the exact time. The DELETE might have run at 14:32:17, so we check the application logs or the database log with log_statement and choose a target just before it, say 14:32:00 in a clearly stated time zone. A target given only as a time of day is ambiguous; we give a full timestamp. Then we restore the newest base backup taken before that time onto a separate server.

Java
# 1. restore the base backup from before the target onto a new server
pgbackrest --stanza=shop --set=20261104-010002F restore --pg1-path=/srv/pitr
# 2. postgresql.conf (or postgresql.auto.conf) on the restored copy
restore_command = 'pgbackrest --stanza=shop archive-get %f "%p"'
recovery_target_time = '2026-11-04 14:32:00+00'
recovery_target_action = 'pause' # stop there so we can inspect
# 3. tell PostgreSQL to start in targeted recovery, then start it
touch /srv/pitr/recovery.signal
pg_ctl -D /srv/pitr start

Since PostgreSQL 12, recovery settings live in the normal configuration file, and the file recovery.signal tells the server to perform targeted recovery; the old recovery.conf no longer exists. pgBackRest can write these settings for us with its --type=time and --target options, but it is worth knowing what they mean.

4What PostgreSQL Does During Recovery

On start, the server sees recovery.signal, opens the base backup and calls restore_command for each WAL segment it needs, in order, replaying every change. Before applying each commit, it checks the commit's timestamp against recovery_target_time. When it reaches the first commit after the target, it stops without applying it.

Step throughRecovery up to a target time
  1. Start with recovery.signalbase backup from 01:00 yesterday
  2. Fetch next WAL segmentvia restore_command
  3. Replay its recordscommit by commit
  4. Next commit after 14:32:00?
    YesStop before itNoFetch the next segment
  5. Apply recovery_target_actionpause, promote or shutdown

5 steps in order. Play them, or step through them one at a time.

recovery_target_action decides what happens then. With pause, the default, the server stays read-only at the target so we can check the data; if hot_standby is on we can query it, and if it is too early or too late we can adjust the target and restart. With promote, it ends recovery and becomes a normal read-write server on a new timeline. With shutdown, it stops. If we choose pause and the data looks right, SELECT pg_wal_replay_resume() promotes it.

5Restore to the Side, Then Copy the Rows Back

Why not recover production itself to 14:32? Because that would also throw away every legitimate order, payment and update between 14:32 and now. In most incidents, only the rows hit by the bad DELETE are wrong. So we recover a separate instance, check the deleted rows exist there, and copy just those back into production, for example with a COPY export and import or through postgres_fdw.

SQL
-- on the recovered instance, paused at 14:32:00
SELECT count(*) FROM customer_notes WHERE account_id = 4711; -- 1832, the rows that were deleted
\copy (SELECT * FROM customer_notes WHERE account_id = 4711) TO 'notes_4711.csv' CSV
-- on production
\copy customer_notes FROM 'notes_4711.csv' CSV
SELECT count(*) FROM customer_notes WHERE account_id = 4711; -- 1832

Restoring the whole production database to the target is reserved for cases where everything after the target is unusable, for example widespread corruption.

6Why pg_dump Cannot Do This

pg_dump produces a logical snapshot: SQL or an archive representing the database at the moment the dump started. It contains no WAL and has no relationship with the WAL, so there is nothing to replay on top of it. A nightly dump can bring us back to 01:00, never to 14:32. Dumps remain useful for moving data between versions or keeping a portable copy of a table, but they are not a PITR strategy.

7Follow-up questions

What if the DELETE happened during a long transaction?

Recovery targets work on commits. If the DELETE committed at 14:32:17 as part of a larger transaction, the whole transaction is either applied or not. Targeting just before its commit time excludes all of it, including any good changes inside it, which we may need to redo by hand.

Can we target something other than a time?

Yes. recovery_target_xid stops at a transaction id, recovery_target_lsn at a WAL position, and recovery_target_name at a named restore point created earlier with pg_create_restore_point(), useful before risky migrations. recovery_target_inclusive decides whether the target itself is applied.

How long does such a restore take?

Restoring the base backup is limited by storage throughput, and replaying WAL by the volume of changes since that backup. For a 1 TB database with a nightly backup, an hour or two in total is common; we only know our number by measuring it in restore drills.

8Conclusion

In this answer, we saw that PITR combines a physical base backup with continuously archived WAL. To return to 14:32 yesterday, we restore a backup from before that time on a separate server, set restore_command, a full recovery_target_time timestamp and recovery_target_action, create recovery.signal, and let PostgreSQL replay up to the target and pause. We then copy the lost rows back to production rather than rewinding it, and we saw why pg_dump snapshots cannot do this.

What to say in the interviewAbout 90 seconds aloud

Open with

Point-in-time recovery in PostgreSQL combines two things you must keep: a physical base backup, taken with pg_basebackup or a tool like pgBackRest or Barman, and every WAL segment written since, archived continuously with archive_command or archive_library.

Then, if they want more

Because the base backup is a copy of the data files and the WAL contains every change, PostgreSQL can replay up to any moment. For the bad DELETE, I would first find the exact time from the logs and pick a full timestamp just before it, with the time zone.

Then I would restore the newest base backup taken before that time onto a separate server, set restore_command so it can fetch archived WAL, set recovery_target_time to that timestamp and recovery_target_action to pause, create recovery.signal and start it. PostgreSQL replays WAL commit by commit and stops before the first commit after the target.

Paused there, I can query it and check the deleted rows are present, and if the target was slightly wrong, adjust and restart. Then I would copy just those rows back to production, because rewinding production would also throw away all the good writes since 14:32.

And I would point out that a pg_dump cannot do this: it is one logical snapshot with no WAL, so it only gets you back to when the dump ran. We also need to monitor pg_stat_archiver, because one missing segment breaks the chain.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 12, PostgreSQL 15
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain how point-in-time recovery works in PostgreSQL. What must you keep, and how...": https://scrivio-interview-library.netlify.app/databases/replication-and-ha/783e1426ff/