skip to content

backups and point-in-time recovery

pg_dump vs base backups, WAL archiving, and restoring to 14:31:59, the second before the mistake.

address
everything.wal.backups_and_pitr
part
part 16, WAL and recovery
chapter
chapter 9 of 11
reading time
9 min

pg_dump writes out a snapshot as SQL: you can restore exactly that moment, and nothing after it. a base backup copies the data files while the server runs, and with WAL archiving every segment after it is kept too. restore the base backup, replay the archived WAL, and stop wherever you like: that's point-in-time recovery, down to the second before the mistake. the restored server starts a new timeline. postgres 17 added incremental base backups.

the picture

you know how someone runs DELETE FROM accounts without the WHERE at 14:32, and the nightly dump is from 02:00? restoring the dump loses twelve and a half hours of everyone else's work. what you want is the database as it was at 14:31:59.

think of a photo album and a diary (a base backup and the WAL archive). the photo is from last Sunday. the diary has every change since, in order, timestamped. to see the house as it was on Wednesday at 14:31:59, start from Sunday's photo and act out the diary until the entry just before 14:32. you can't do that with a photo alone, and you can't do it with a diary alone: you need the starting point and every entry after it.

hover a stage.

point-in-time recovery, end to end. hover a stage. the first two run all the time; the rest only on the day you need them.
pg_dumpbase backup + WAL archive
what it isSQL (or a custom-format file) that recreates the dataa copy of the data files, plus every WAL segment after
restore tothe moment the dump startedany moment after the base backup
granularityone database, or chosen tablesthe whole cluster
works across versionsyes, it's how you move to a new majorno, same major version only
restore timerebuilds every table and indexcopy files, replay WAL

they answer different questions, and most production setups run both: continuous archiving for "the second before the mistake", an occasional dump for "give me that one table from last month".

the setup

archiving is two settings and a restart: archive_mode = on, and archive_command, a shell command postgres runs for each finished segment (%p is its path, %f its file name). the browser engine has neither archiving nor a second machine, so it shows the settings switched off:

sqldb=lab
SELECT name, setting FROM pg_settings
WHERE name IN ('archive_mode', 'archive_command', 'wal_level', 'summarize_wal', 'restore_command')
ORDER BY name;
SELECT pg_walfile_name(pg_current_wal_lsn()) AS current_segment;
output, from postgres 18.6
#1 · SELECT 5
nametextsettingtext
archive_command(disabled)
archive_modeoff
restore_command''
summarize_waloff
wal_levelreplica
(5 rows)
#2 · SELECT 1
current_segmenttext
000000010000000000000001
(1 row)

on a real server, this run used archive_command = 'cp %p /archive/%f' (fine for a demo; production wants something that refuses to overwrite and fsyncs, like pgBackRest, barman or WAL-G), a table of 1,000 accounts, and then a base backup:

terminal
$ pg_basebackup -h /tmp/w17prim -p 5493 -U postgres -D full --checkpoint=fast --progress
waiting for checkpoint
24045/24045 kB (100%), 0/1 tablespace
24045/24045 kB (100%), 1/1 tablespace
$ head -c 600 full/backup_label
START WAL LOCATION: 0/2000028 (file 000000010000000000000002)
CHECKPOINT LOCATION: 0/2000080
BACKUP METHOD: streamed
BACKUP FROM: primary
START TIME: 2026-11-04 15:09:01 IST
LABEL: pg_basebackup base backup
START TIMELINE: 1
recorded: pg_basebackup 18.6 against a throwaway postgres 18.6 cluster with archiving on, on this mac

the copy is taken while writes continue, so the files in it are not consistent with each other: some pages are older than others. that's fine, for the same reason crash recovery is fine. backup_label says where replay has to start (the backup's own checkpoint, 0/2000028), and replaying WAL from there to the end of the backup makes it consistent (crash recovery is the same loop).

the mistake, and the way back

a day of work goes by: 100 balances updated, 1,000 more accounts. then:

psql
SELECT count(*), sum(balance) FROM accounts;
 count |    sum
-------+-----------
  2000 | 200100.00
(1 row)
 
SELECT now() AS before_the_mistake;
      before_the_mistake
-------------------------------
 2026-11-04 09:39:01.804859+00
(1 row)
 
DELETE FROM accounts;
DELETE 2000
recorded: psql 18.6 on the archiving cluster; the pg_sleep(1.5) between the two statements is left out

the restore goes into a fresh directory: the base backup (here, the full one combined with an incremental, see below), three settings, and a signal file:

terminal
$ cp -R restored $PGDATA
$ cat >> $PGDATA/postgresql.conf
restore_command = 'cp /archive/%f %p'
recovery_target_time = '2026-11-04 09:39:01.804859+00'
recovery_target_action = 'promote'
$ touch $PGDATA/recovery.signal
$ pg_ctl start
recorded: the shell, same run
server log
  1. LOG: database system was interrupted; last known up at 2026-11-04 15:09:01 IST
  2. LOG: starting backup recovery with redo LSN 0/4000028, checkpoint LSN 0/4000080, on timeline ID 1
  3. LOG: restored log file "000000010000000000000004" from archive
  4. LOG: starting point-in-time recovery to 2026-11-04 09:39:01.804859+00
  5. LOG: redo starts at 0/4000028
  6. LOG: restored log file "000000010000000000000005" from archive
  7. LOG: completed backup recovery with redo LSN 0/4000028 and end LSN 0/4000120
  8. LOG: consistent recovery state reached at 0/4000120
  9. LOG: database system is ready to accept read-only connections
  10. cp: /archive/000000010000000000000006: No such file or directory
  11. LOG: recovery stopping before commit of transaction 756, time 2026-11-04 09:39:03.306749+00
  12. LOG: redo done at 0/50366F8 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.03 s
  13. LOG: selected new timeline ID: 2
  14. LOG: archive recovery complete
  15. LOG: checkpoint starting: end-of-recovery immediate wait
  16. LOG: database system is ready to accept connections
recorded: the restored cluster's log, same run; postgres's own lines about missing .history files are left out
the restored server's log. it fetches segments from the archive, replays them, and stops in front of the first commit after the target: transaction 756, the DELETE.

"recovery stopping before commit of transaction 756, time ... 09:39:03.306749". the DELETE committed a second and a half after the target, so its commit record is where replay stops. and then:

psql
SELECT count(*), sum(balance) FROM accounts;
 count |    sum
-------+-----------
  2000 | 200100.00
(1 row)
 
SELECT pg_is_in_recovery(), timeline_id FROM pg_control_checkpoint();
 pg_is_in_recovery | timeline_id
-------------------+-------------
 f                 |           2
(1 row)
recorded: psql 18.6 on the restored cluster, same run

all 2,000 accounts, every balance. the recovery target can also be a transaction id (recovery_target_xid), an LSN (recovery_target_lsn), or a name you set earlier with pg_create_restore_point(). a time is what you usually have, from the person who ran the DELETE.

predict the output
you PITR the cluster to 14:31:59 to get back a table someone dropped at 14:32. what happens to an order another app committed at 14:31:30 in a different database?
sql
-- 14:31:30 shop db: INSERT INTO orders ... COMMIT
-- 14:32:00 crm db:  DROP TABLE customers;
-- restore the cluster with recovery_target_time = '14:31:59'

timelines

once the restored server promotes, there are two futures after 0/50366F8: the original server's, where the DELETE happened, and this one's, where it didn't. postgres keeps them apart with a timeline id, the first eight hex digits of every WAL file name. the restored server picked timeline 2 and wrote a history file saying where it branched:

terminal
$ cat $PGDATA/pg_wal/00000002.history
1	0/50366F8	before 2026-11-04 09:39:03.306749+00
recorded: the restored cluster's pg_wal, same run
  • the original server
  • ├── base backup at 0/2000028
  • ├── a day of work
  • ├── the last thing replayed
  • │ └── DELETE FROM accounts, xid 756only on timeline 1
  • └── the restored server, from 0/50366F8 on
  • └── new writes, in 000000020000000000000005 and up
drawn by hand · from the history file above
two timelines. new WAL on the restored server goes into files named 00000002..., so it can never be confused with timeline 1's segments in the same archive.

timelines are what let you try a restore, decide it was the wrong target, and try again from the same archive. recovery_target_timeline (default latest) says which branch to follow; streaming replication uses the same mechanism when a standby is promoted.

incremental backups

a full base backup of a big cluster every night is a lot of copying. postgres 17 added incremental ones: with summarize_wal = on, a background process writes summaries of which blocks each stretch of WAL changed, and pg_basebackup --incremental=<previous backup's manifest> copies only those blocks. pg_combinebackup stitches a full backup and its chain of incrementals into one directory you can start, which is what the restore above used:

terminal
$ pg_basebackup -h /tmp/w17prim -p 5493 -U postgres -D incr1 --checkpoint=fast --incremental=full/backup_manifest
$ du -sk full incr1 full/base incr1/base full/pg_wal incr1/pg_wal
40108	full
22308	incr1
22976	full/base
5408	incr1/base
16384	full/pg_wal
16384	incr1/pg_wal
$ pg_combinebackup full incr1 -o restored
recorded: pg_basebackup and pg_combinebackup 18.6, same run
full: base/22,976 KB
incremental: base/5,408 KB
recorded: du -sk on the two backup directories, same run
size of the copied data files (base/) in each backup, in KB. both also carry one 16 MB WAL segment, the WAL needed to make the copy consistent.

after a day that touched 100 rows and added 1,000 more, the incremental copied a quarter of what the full one did. on a big, mostly cold database the ratio gets much better, and the WAL segment stops mattering.

keylines

  • pg_dump restores one moment. base backup + archived WAL restores any moment after the backup.
  • archive_command copies each finished WAL segment. restore_command fetches them back.
  • recovery stops before the first commit past recovery_target_time, xid or lsn.
  • PITR rewinds the whole cluster. restore elsewhere, copy the lost data back.
  • a promoted restore starts a new timeline, recorded in a .history file.
  • postgres 17+: incremental backups with summarize_wal, --incremental and pg_combinebackup.