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.
| pg_dump | base backup + WAL archive | |
|---|---|---|
| what it is | SQL (or a custom-format file) that recreates the data | a copy of the data files, plus every WAL segment after |
| restore to | the moment the dump started | any moment after the base backup |
| granularity | one database, or chosen tables | the whole cluster |
| works across versions | yes, it's how you move to a new major | no, same major version only |
| restore time | rebuilds every table and index | copy 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:
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;| nametext | settingtext |
|---|---|
| archive_command | (disabled) |
| archive_mode | off |
| restore_command | '' |
| summarize_wal | off |
| wal_level | replica |
| current_segmenttext |
|---|
| 000000010000000000000001 |
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:
$ 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: 1the 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:
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 2000the 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:
$ 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- LOG: database system was interrupted; last known up at 2026-11-04 15:09:01 IST
- LOG: starting backup recovery with redo LSN 0/4000028, checkpoint LSN 0/4000080, on timeline ID 1
- LOG: restored log file "000000010000000000000004" from archive
- LOG: starting point-in-time recovery to 2026-11-04 09:39:01.804859+00
- LOG: redo starts at 0/4000028
- LOG: restored log file "000000010000000000000005" from archive
- LOG: completed backup recovery with redo LSN 0/4000028 and end LSN 0/4000120
- LOG: consistent recovery state reached at 0/4000120
- LOG: database system is ready to accept read-only connections
- cp: /archive/000000010000000000000006: No such file or directory
- LOG: recovery stopping before commit of transaction 756, time 2026-11-04 09:39:03.306749+00
- LOG: redo done at 0/50366F8 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.03 s
- LOG: selected new timeline ID: 2
- LOG: archive recovery complete
- LOG: checkpoint starting: end-of-recovery immediate wait
- LOG: database system is ready to accept connections
"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:
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)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.
-- 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:
$ cat $PGDATA/pg_wal/00000002.history
1 0/50366F8 before 2026-11-04 09:39:03.306749+00- 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
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:
$ 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 restoredafter 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.