Sub topic 1
What You Need to Recover From: RPO, RTO, and Failure Scenarios
Prerequisites
This chapter assumes that you know basic SQL and psql, can use the Linux command line, and understand that PostgreSQL roles, databases, and the data directory are different objects. You should also know that a transaction groups related database changes.
A backup plan starts with the failure, not with a command. A useful plan answers two questions:
- How much recent data can the business afford to lose?
- How long can the service remain unavailable?
These questions are measured by the recovery point objective and recovery time objective.
RPO: How Much Data Can You Lose?
The recovery point objective (RPO) is the maximum acceptable amount of recent data loss, measured in time. An RPO of one hour means that, after a disaster, the organization accepts losing at most one hour of committed changes.
RPO describes the gap between the latest recoverable copy and the failure:
- A nightly logical dump may have an RPO close to 24 hours.
- A base backup combined with continuous write-ahead log (WAL) archiving can reduce the RPO to the time since the last archived WAL record.
- Synchronous replication can reduce data loss further, but it is a high-availability feature rather than a replacement for backups.
RPO does not mean that every backup is usable. A corrupted database can produce a consistently backed-up but unusable dataset. Verification remains necessary.
RTO: How Long Can Recovery Take?
The recovery time objective (RTO) is the maximum acceptable time from an outage to restored service. An RTO of 30 minutes requires more than having a backup file. You must include detection, provisioning, copying files, replaying WAL, restoring permissions, checking the application, and switching traffic.
Restoring a large logical dump can take hours because PostgreSQL must recreate objects and insert rows. A physical base backup can usually bring back a whole cluster faster, especially when the required WAL is available. However, physical recovery restores the cluster layout and database versions more closely, while a logical restore offers more control over selected databases and objects.
| Recovery need | Common approach | Main trade-off |
|---|---|---|
| Occasional recovery of selected tables | Logical dump with `pg_dump` | Flexible, but often slower for large databases |
| Whole-cluster disaster recovery | Base backup plus WAL archiving | Fast and complete, but needs cluster-level planning |
| Recovery to just before an operator error | Point-in-time recovery | Precise, but requires a valid base backup and continuous WAL |
Match the Failure to the Recovery Method
Different failures require different recovery actions. A dropped table may need point-in-time recovery or a logical dump from another environment. A damaged server disk usually requires a physical base backup and WAL. A mistaken DELETE committed at 10:15 requires recovery to a time before that transaction, followed by careful extraction or promotion.
Consider these questions:
- Is the failure inside one database or across the whole cluster?
- Do you need one object, one database, or every database and role?
- Do you know when the bad change occurred?
- Are the backup files and archived WAL stored outside the failed server?
- Can the restored system meet the required RTO?
The last question prevents a common mistake: calling a backup strategy successful because it creates files. A strategy is successful only when it restores the required data within the required time.
Build a Recovery Target
Write down the target before choosing tools. For example: “We can lose at most five minutes of committed data, and service must return within one hour after losing the database host.” This target points toward a physical base-backup process, frequent or continuous WAL archiving, offsite copies, and a tested restore procedure.
Also record what the target excludes. Roles, passwords, tablespaces, extensions, application configuration, and encryption keys may not all be contained in a database dump. A recovery runbook must identify these dependencies.
Exercise
A team takes one pg_dump every night. Its database host fails at 16:00, and the latest dump finished at 02:00. What is the approximate best-case RPO, and what additional requirement is needed to judge the RTO?
Show answer
The best-case RPO is about 14 hours of changes. To judge the RTO, the team must measure how long it takes to provision a target, restore the dump, recreate required cluster objects, validate the application, and switch service.