Sub topic 1
What point-in-time recovery gives you
Point-in-time recovery (PITR) lets you rebuild a PostgreSQL cluster as it was at a chosen moment in the past. Suppose someone runs DELETE FROM orders without a WHERE clause at 14:31. With PITR you restore the cluster to 14:30:59 and lose almost nothing. A nightly dump would return you to the previous night.
This chapter explains what PITR is, which pieces it needs, what it cannot do, and how it differs from other protection mechanisms. Later chapters configure and run each piece.
Prerequisites
This chapter assumes you already have:
- Basic PostgreSQL administration skills: starting and stopping the server and editing
postgresql.conf. - Comfort with the Linux shell and file permissions.
- Familiarity with the data directory layout (
PGDATA, withbase/,pg_wal/andpostgresql.conf). - A high-level understanding of transactions and durability: a committed transaction must survive a crash.
You do not need to know WAL internals yet. The next chapter covers them.
The problem PITR solves
Most data loss is not a dead disk. It is a human or application mistake that the database carries out faithfully:
- A migration drops the wrong table.
- A bug in a batch job overwrites a column for every row.
- A script runs against production instead of staging.
Replication does not help here. A streaming replica applies the bad DELETE within milliseconds, so every copy is wrong at once. A backup that was valid last night does not help much either, because it discards all the legitimate work done since.
PITR gives you a time machine. You pick a target moment just before the mistake. PostgreSQL rebuilds the cluster to that moment and stops.
The two ingredients
PITR is built from two things that must be kept together.
- A base backup. This is a physical copy of the whole data directory taken at some moment. It is the starting point.
- A continuous archive of WAL. The write-ahead log (WAL) is the sequential record of every change PostgreSQL makes. Archiving means copying each completed WAL segment file to storage outside the server, as it fills.
To recover, you restore the base backup, then let PostgreSQL replay archived WAL on top of it. Replay applies every change in order, so the cluster moves forward through time. You tell PostgreSQL where to stop. That stopping point is the recovery target.
The base backup and the WAL are two halves of one thing. A base backup without the WAL written after it can only restore the moment the backup ended. WAL without a base backup has nothing to apply to.
What you can recover to
A target can be expressed in several ways. You choose one per recovery.
| Target type | Setting | Typical use |
|---|---|---|
| Timestamp | `recovery_target_time` | "Just before 14:31 today" |
| Transaction ID | `recovery_target_xid` | You know the bad transaction's ID |
| WAL position | `recovery_target_lsn` | Exact byte position in the log |
| Named restore point | `recovery_target_name` | A marker you created before a risky change |
| End of base backup | `recovery_target = 'immediate'` | Restore a consistent copy as of backup end |
If you set no target, replay continues until the archive runs out. That recovers to the latest archived moment, which is what you want after a disk failure and not after a logical mistake.
The reachable range has fixed bounds:
- Earliest: the moment the base backup finished. You cannot go to a time before the backup ended, because the backup is the starting point. To reach earlier moments you need an older base backup.
- Latest: the end of the last WAL segment you archived. Anything after that is gone unless it is still in the server's own
pg_wal.
What PITR costs you
PITR is not free. Plan for these costs before relying on it.
- Storage. You keep base backups plus every WAL segment since the oldest backup you retain. A write-heavy system can generate many gigabytes of WAL per day.
- Operational discipline. Archiving must run continuously. A gap in the WAL sequence makes later recovery impossible past the gap.
- Recovery time. Replay is not instant. If you restore a backup from last Sunday and the target is Friday, PostgreSQL must replay five days of WAL. Frequent base backups shorten the replay.
- Data loss window. PostgreSQL archives a WAL segment only when it is complete (16 MB by default) or when a forced switch occurs. Changes in the segment currently being written are not yet in the archive. The
archive_timeoutsetting bounds how long that window can last. Streaming WAL continuously can shrink it further, as tools such aspg_receivewaldo.
Settings you will meet
You will configure these in the following chapters. This query shows their current values on any server, so you can see where a system stands today.
SELECT name, setting
FROM pg_settings
WHERE name IN ('wal_level', 'archive_mode', 'archive_command', 'archive_timeout')
ORDER BY name;On a default installation archive_mode is off, and archive_command is empty. In that state PITR is impossible, whatever base backups you have taken. wal_level must be replica or higher. The minimal level does not write enough information for archiving or base backups.
How PITR compares with other protections
Each mechanism covers a different failure. They complement each other.
| Mechanism | Protects against | Does not protect against |
|---|---|---|
| `pg_dump` (logical dump) | Loss of a database, one table, or one schema, restorable across major versions | Anything since the dump. Large restores are slow. |
| Streaming replica | Server or disk failure, with fast failover | Logical errors, which replicate immediately |
| Delayed replica | Logical errors within the delay window | Errors noticed after the delay. It holds one point, not a range. |
| Base backup only | Total loss, back to the backup moment | Work since the backup |
| PITR (base backup + WAL archive) | Both total loss and logical errors, to any point in the range | Cluster-level mistakes outside PostgreSQL, such as losing the archive itself |
Two properties of PITR matter when choosing it:
- It works on the whole cluster. You cannot restore one table or one database to a past moment. Recovery brings back every database in the cluster. To recover one dropped table, restore the whole cluster to a temporary server, then extract the table with
pg_dumpand load it into production. - It is physical. The restored cluster must run the same major PostgreSQL version and a compatible platform (CPU architecture and similar) as the source. A base backup from version 15 cannot be replayed on 16.
Common misconceptions and errors
- "We have replicas, so we have backups." Replicas copy mistakes as quickly as they copy data. Replication is for availability. PITR is for undoing history.
- Archiving that silently fails. If
archive_commandreturns a non-zero exit status, PostgreSQL keeps the segment and retries, andpg_walgrows until the disk fills. Chapter 6 shows how to watch for this withpg_stat_archiver. - Treating the base backup as enough. A base backup alone restores only to its own end. Its value comes from the WAL that follows.
- Never testing a restore. Many teams find out that their archive has a gap only during an emergency. A backup you have not restored is an assumption, not a backup.
- Expecting an exact timestamp to be a moment between commits. A time target stops at commit boundaries. Recovery replays whole transactions, and a transaction either committed before the target or it did not. Pick a time just before the damaging statement was committed.
The lifecycle at a glance
The rest of this tutorial walks through these stages in order:
- Understand how WAL makes replay possible.
- Turn on continuous archiving and make the archive command safe.
- Take a base backup and monitor that archiving works.
- When something goes wrong, fetch WAL with
restore_commandand choose a recovery target. - Run the recovery, decide what happens at the target, and read the new timeline.
- Define retention, test restores regularly, and decide whether a tool such as pgBackRest, Barman or WAL-G should do the work for you.
Exercise
A developer runs an UPDATE at 10:15 that corrupts every row in one table of a cluster that has three databases. You have a streaming replica, a nightly pg_dump, and a base backup from 02:00 with continuous WAL archiving. Which mechanism lets you return to 10:14 with the least data loss, and what must you do about the other two databases?
Show answer
PITR, using the 02:00 base backup and archived WAL with a recovery target just before 10:15. The replica already applied the bad UPDATE, and the pgdump only holds the previous night. PITR restores the whole cluster, so all three databases return to 10:14. Restore into a separate temporary server, then copy only the damaged table's data into production with pgdump and psql. This avoids rolling back legitimate changes in the other databases.