Sub topic 1
What Point-in-Time Recovery Gives You
Point-in-time recovery (PITR) rebuilds a PostgreSQL cluster to a chosen moment in the past. It combines a physical base backup with archived write-ahead log (WAL) files.
This chapter assumes you can start and stop PostgreSQL, edit postgresql.conf, use the Linux shell, and recognize the PostgreSQL data directory. You should also understand that a committed transaction is intended to survive a crash.
Prerequisites
You need:
- Basic PostgreSQL administration skills.
- Comfort with shell commands and file permissions.
- Familiarity with the PostgreSQL data directory.
- A basic understanding of transactions and durability.
The Recovery Model
A base backup is a complete physical copy of a PostgreSQL cluster at one starting point. WAL records describe changes made after that copy. PostgreSQL can restore the base backup and replay the archived WAL in order.
For example, suppose you take a base backup at 01:00. PostgreSQL archives every WAL segment generated afterward. If an operator deletes important rows at 14:30, you can restore the base backup and replay WAL until just before the deletion.
Base backup at 01:00
|
v
WAL replay: 01:00 -> 14:29:59
|
v
Recovered database before the accidental deletionThe recovery process cannot normally go earlier than the base backup. It also cannot pass a missing or damaged WAL segment. The base backup and every required WAL segment must therefore be available and readable.
When PITR Helps
PITR is useful when the current database contains unwanted changes but the underlying storage is still available. Common examples include:
- An accidental
DELETEorUPDATE. - A failed schema migration.
- An application bug that wrote incorrect data.
- Corruption discovered after a known safe time.
- A server failure where the latest base backup is still usable.
PITR is different from a logical export such as pg_dump. A logical export contains database objects and rows in a portable format. A physical backup copies the cluster files and works with WAL replay. PITR normally restores the whole cluster, not one table or one row.
| Need | Typical approach |
|---|---|
| Recover the whole cluster to an earlier moment | Physical base backup plus archived WAL |
| Copy selected tables into another database | Logical dump or application-level export |
| Recover after losing the entire server | Base backup and WAL stored separately |
Recovery Limits and Planning Terms
Two planning measures matter:
- Recovery point objective (RPO) is the maximum amount of data you can lose, measured in time. If WAL is archived every minute, the practical RPO may be close to one minute, provided the archive is healthy.
- Recovery time objective (RTO) is how long restoring service may take. A large base backup, slow storage, or many WAL segments can increase the RTO.
PITR does not replace testing. A backup job can report success while the destination is full, inaccessible, incomplete, or missing a required WAL segment. A restore test proves that the files can actually rebuild a working cluster.
Exercise
A base backup was taken at 10:00, and an accidental change occurred at 09:30. Can that backup alone recover the database to 09:29?
Show answer
No. Recovery cannot normally go earlier than the base backup. You need an older base backup, or another recovery source that contains the state before 09:30.