PostgreSQL point-in-time recovery: from backup policy to timed restore drills
Replication protects against a failed server. It does not protect against a mistaken DELETE, a bad migration or ransomware — those are replicated within milliseconds. Point-in-time recovery is what lets you rewind to the moment before the mistake, and it only counts once you have actually done it under a stopwatch.
Contents
Key facts
- PostgreSQL point-in-time recovery combines a base backup with archived write-ahead log (WAL) segments replayed up to a chosen target.
- Recovery targets can be a timestamp, a transaction ID, a named restore point or an LSN.
- Replicas copy mistakes as faithfully as good data; they are not a substitute for backups.
- The achievable recovery point depends on how often WAL is archived; the achievable recovery time depends on backup size and how much WAL must be replayed.
Replicas are not backups
A streaming replica receives every change from the primary almost immediately. That is exactly what you want when the primary's disk fails, and exactly what you do not want when someone runs DELETE FROM invoices without a WHERE clause. The replica deletes the same rows a moment later.
Logical mistakes, faulty migrations, application bugs that corrupt data over hours, and attacks that encrypt or destroy data all need a copy from before the event. That is the job of backups — and for transactional databases, specifically of backups that can be rolled forward to an exact point in time.
How point-in-time recovery works
PostgreSQL writes every change to the write-ahead log before applying it to data files. PITR uses that log twice. A base backup captures the data files at some moment. WAL archiving copies each completed WAL segment to separate storage. To recover, you restore the base backup and replay archived WAL up to a target — for example, the timestamp just before the bad statement.
- archive_mode and archive_command (or an archive library) ship completed WAL segments to storage outside the database server.
- archive_timeout forces a segment switch after a set time on quiet systems, bounding how much recent change could be lost.
- Recovery targets —
recovery_target_time,recovery_target_xid,recovery_target_nameorrecovery_target_lsn— tell recovery where to stop. - Named restore points created with
pg_create_restore_point()before risky operations give you a precise, memorable target.
Tools such as pgBackRest or WAL-G wrap these mechanisms with compression, encryption, parallelism, retention management and verification, and are generally preferable to hand-written scripts.
Designing the backup policy
Start from objectives. The recovery point objective (RPO) is how much recent data the business can afford to lose; with continuous WAL archiving it can be a minute or less. The recovery time objective (RTO) is how long recovery may take; it is driven by restore speed and the amount of WAL to replay since the last base backup.
| Decision | Affects | Typical consideration |
|---|---|---|
| Base backup frequency | RTO (less WAL to replay) | Daily full or incremental backups for busy databases |
| WAL archive frequency | RPO | Continuous archiving plus archive_timeout |
| Retention period | How far back you can rewind | Long enough to detect slow corruption |
| Storage location | Survivability | Separate account or region, immutable if possible |
| Encryption | Confidentiality | Keys managed outside the database server |
Retention deserves special thought. Some problems — a bug that slowly corrupts records — are discovered days or weeks later. If backups and WAL are kept for only three days, the clean point in time may already be gone. Protect backup storage against deletion by the same credentials that can modify the database; ransomware increasingly targets backups first.
Restore drills: the only real test
A backup that has never been restored is a hypothesis. Restore drills turn it into a measured capability. Schedule them, automate as much as possible, and record the results.
- Pick a target time, ideally one that matches a realistic scenario.
- Provision a clean environment and restore the latest base backup before the target.
- Replay WAL to the target and promote the instance.
- Run validation queries: row counts, recent records, application smoke tests.
- Record elapsed time per step and compare the total with the RTO.
Automated nightly restores of the previous day's backup into a throwaway instance catch the most common failures — missing WAL segments, expired credentials for backup storage, incompatible versions — long before they matter. Quarterly drills with people following the runbook catch the rest.
Operating it day to day
Monitor the archive: age of the last archived segment, archive failures, and backup completion. A failing archive command is silent unless you watch it, and WAL accumulating on the primary can fill its disk. Alert when the newest archived WAL is older than your RPO.
Write the runbook for people under stress: where backups are, which credentials are needed, how to choose a target, how to point the application at the restored instance, and how to avoid overwriting the original database while investigating.
Logical backups still have a place
Physical backups and WAL archiving restore a whole cluster to a point in time. Logical backups taken with pg_dump capture individual databases or tables as SQL or archive files. They are slower for large databases and do not support point-in-time recovery, but they are portable across major versions and convenient for extracting a single table.
Many teams keep both: physical backups with WAL archiving for disaster recovery, and periodic logical dumps of critical tables for quick, targeted restores and long-term archives. Whatever the mix, each backup type needs its own restore test; a logical dump that fails to load because of a missing extension is as useless as a missing WAL segment.
Worked example: rewinding a bad migration
At 14:05 a migration with a faulty WHERE clause sets the status of all open orders to cancelled. The mistake is noticed at 14:20.
- Writes to the affected tables are paused through a feature flag to stop the damage spreading.
- A new instance is restored from the 02:00 base backup and WAL is replayed with
recovery_target_timeset to 14:04:30. - Replay of roughly twelve hours of WAL takes about 25 minutes in this environment, which matches the last drill.
- Instead of replacing production, the team exports the correct status values for affected orders from the restored instance and applies them to production, preserving orders created after 14:05.
- A post-incident action adds a named restore point to the migration tooling, created automatically before every migration.
Because the drill had measured restore speed, the team knew from the start how long the investigation would take and could communicate a realistic estimate.
Common pitfalls
- Treating replicas as backups.
- Never restoring, so missing WAL or broken credentials are discovered during an incident.
- Retention too short to recover from slowly discovered corruption.
- Backup storage deletable with the same credentials as the database.
- Ignoring archive failures until the primary's disk fills.
- Restoring over production instead of into a separate instance.
FAQ
Are snapshot-based backups from the cloud provider enough?
Snapshots help, but without WAL archiving they only restore to snapshot times. Managed services often provide PITR; confirm its retention and test restores as you would with your own setup.
How large should the RPO be?
As small as the business needs and the archive can support. With continuous archiving, losses of under a minute are typical; the drill tells you the real number.
Should we restore the whole cluster for a few bad rows?
Often it is better to restore into a separate instance and copy the correct data back, keeping changes made after the incident.
Checklist
- Base backups and continuous WAL archiving with a maintained tool.
- archive_timeout set so quiet periods do not stretch the RPO.
- Retention long enough to cover delayed discovery of corruption.
- Backup storage separated and protected against deletion.
- Automated restore tests plus periodic timed drills.
- Alerts on archive age and backup failures; runbook for restores.