Sub topic 1
What you need to recover from: RPO, RTO, and failure scenarios
A backup plan starts with two questions: how much data can you afford to lose, and how long can you afford to be down? This chapter defines the two numbers that answer them, lists the failures a backup has to cover, and shows how those answers narrow the choice of backup method. Later chapters cover each method in detail.
Prerequisites
This chapter assumes you already know:
- Basic SQL and how to use
psql. - The Linux command line.
- What PostgreSQL roles, databases, and the data directory are.
- What a transaction is.
You do not need any prior backup experience. You don't need a running server to read this chapter, but the SQL checks near the end are easier to follow if you have one.
Why start with goals instead of tools
PostgreSQL ships several backup tools: pg_dump, pg_dumpall, pg_basebackup, and continuous WAL archiving. Each one makes different promises. A tool chosen before the goals are known is usually wrong in one of two ways. It may lose more data than the business accepts, or it may take far longer to restore than anyone can wait.
So write the goals down first. Two measurements do most of the work: RPO and RTO.
Recovery point objective (RPO)
The recovery point objective (RPO) is the maximum amount of data loss you accept, measured in time. It is the gap between the last change you can recover and the moment of the failure.
- An RPO of 24 hours means that losing a day of writes is acceptable.
- An RPO of 5 minutes means that at most five minutes of committed transactions may be lost.
- An RPO of zero means that no committed transaction may be lost.
In practice the RPO is set by how often you capture recoverable state:
- With nightly dumps, your worst-case data loss is the time since the last successful dump. A failure at 23:50 loses almost 24 hours, plus any failed runs in between.
- With WAL archiving, loss is bounded by how quickly completed WAL segments reach safe storage. A segment is archived only after it fills or is switched, so a quiet server can hold unarchived changes for a long time unless you set
archive_timeout. - With synchronous replication, a commit is not acknowledged until a standby has the WAL. That approaches an RPO of zero, but it is an availability feature, and the section on replicas below explains why it does not replace backups.
Always ask about the worst case, not the average. A nightly dump has an average loss of 12 hours, but the number that matters is 24.
Recovery time objective (RTO)
The recovery time objective (RTO) is the maximum time the service may be unavailable after a failure. The clock starts when the failure happens and stops when the database is serving correct data again.
The RTO includes much more than the restore command:
- Noticing the failure and deciding to restore.
- Finding the right backup and getting it onto a machine, which may mean downloading it from offsite storage.
- Restoring the data.
- Replaying WAL, if you are doing point-in-time recovery.
- Checking the result and pointing applications at the restored server.
Restore speed differs a lot between methods:
- A logical restore re-executes the data load and rebuilds every index and constraint. It is often the slowest approach for a large database.
- A physical restore copies files back, then replays WAL. The copy runs at disk or network speed, and the replay time depends on how much WAL was generated since the base backup.
- A standby server already holds a nearly current copy, so promoting it takes seconds to minutes.
If you have never timed a restore of your own data, you do not know your RTO. You only have a guess. The chapter on test restores returns to this.
How RPO and RTO relate
The two numbers are independent. A system can have a tight RPO and a loose RTO, such as continuous WAL archiving with a restore that takes hours. It can also have the opposite: a standby that promotes in seconds but is fed by hourly batch loads that can be lost.
Tighter targets cost more in both money and complexity. Cutting either number by a factor of ten usually means more infrastructure, more monitoring, and more things that can break. For that reason the targets should come from the people who own the data and understand what an outage or a lost hour costs, not from the DBA alone.
Failure scenarios
Different failures destroy different things. A method that covers one scenario well can be useless for another. The scenarios below are the ones most backup plans need to handle.
Human error and bad application changes
Someone runs DELETE without a WHERE clause, drops the wrong table, or deploys a migration that corrupts a column. The server is healthy, and it faithfully stores the damage. This is the most common reason to restore, and it needs a copy of the data from before the mistake. Ideally you can restore to a chosen moment, which is point-in-time recovery (PITR).
Hardware, operating system, and storage failure
A disk dies, a host will not boot, or a filesystem is corrupted. Local redundancy such as RAID helps with some of these, but not with a controller fault or a corrupted filesystem. You need a copy that lives on different hardware from the data directory.
Loss of a site
A fire, a power failure, a cloud region outage, or an account lockout takes out the machine and everything attached to it. A backup stored on the same host or in the same account does not survive this. Offsite copies are a requirement here, not an optional extra.
Silent corruption
Bad memory or a storage bug can write damaged pages that nobody notices for days. Every newer backup then contains the damage too, so you need older backups. This is the reason for retention policies, and checksums (initdb --data-checksums) help detect the problem.
Malicious deletion and ransomware
An attacker, or a compromised credential, deletes both the database and the backups. Backups need separate credentials and ideally storage the production host cannot overwrite or delete.
Failed upgrades and migrations
A major version upgrade or a large schema migration goes wrong. A fresh backup taken immediately before the change gives you a clean way back.
Replication is not a backup
A streaming replica, or hot standby, protects against the loss of one server. It does not protect against human error. A DROP TABLE on the primary is replicated to the standby within moments. Treat replicas as a high-availability tool and backups as a separate layer. One partial exception is a delayed replica, configured with recovery_min_apply_delay, which gives you a window to react before a bad change is applied. It is still no substitute for a real backup.
Mapping scenarios to methods
PostgreSQL's backups fall into two families. A logical backup exports the contents as SQL or an archive of objects. A physical backup copies the files of the cluster. The next chapter compares them in depth. This table gives the first-pass fit.
| Scenario | Logical dump | Base backup only | Base backup plus WAL archive |
|---|---|---|---|
| Dropped table, noticed quickly | Yes, to the last dump | Yes, to the backup time | Yes, to the exact moment before the mistake |
| Host or disk loss | Yes | Yes | Yes |
| Site loss | Yes, if copied offsite | Yes, if copied offsite | Yes, if both base backup and WAL are offsite |
| Move to another major version | Yes, designed for it | No | No |
| Restore one table | Yes | Awkward | Awkward |
| Typical RPO | Time since last dump | Time since last backup | Seconds to minutes |
| Typical restore speed on large data | Slow | Fast | Fast to moderate, depending on WAL volume |
A rough guide follows from the table:
- If hours of data loss are acceptable and the database is small to moderate, scheduled logical dumps are simple and portable.
- If you need an RPO of minutes, or you need to recover to the moment before a mistake, you need a base backup plus continuous WAL archiving.
- Many production systems use both. Physical backups with WAL cover disaster recovery, and occasional logical dumps cover version moves and single-table recovery.
Checking what you currently have
Before you design anything, find out what the server already does. These queries show the settings that matter for physical backups and whether archiving is running.
SHOW wal_level;
SHOW archive_mode;
SHOW archive_command;
SELECT pg_size_pretty(pg_database_size(current_database())) AS database_size;
SELECT archived_count, last_archived_time, failed_count, last_failed_time
FROM pg_stat_archiver;How to read the results:
wal_levelmust bereplicaor higher for WAL archiving and base backups to work.archive_modeofoffmeans no WAL is being archived, so no point-in-time recovery is possible yet.- A growing
failed_countor an oldlast_archived_timemeans archiving is broken. WAL then piles up inpg_waland can fill the disk. - The database size gives you a first estimate of backup storage and restore time.
Turning goals into a plan
Work through these steps in order:
- List the failure scenarios that matter for this system, using the list above.
- Set an RPO and an RTO for each critical database, agreed with whoever owns the data.
- Pick the simplest method that meets both numbers. Add complexity only when a number forces it.
- Decide how many backups to keep and for how long, and where the offsite copy lives.
- Time a real restore and compare it with the RTO. Change the plan if it misses.
Common mistakes
- Treating a replica as the only backup, and then replaying a destructive command onto it.
- Storing backups on the same disk, host, or cloud account as the database.
- Quoting the average data loss instead of the worst case.
- Estimating RTO from the size of the backup file and ignoring index rebuilds, download time, and WAL replay.
- Setting
archive_mode = onand never watchingpg_stat_archiver. - Setting one target for every database, even though a reporting copy and a payments database have different needs.
Exercise
A database takes a logical dump every night at 02:00, and each dump finishes by 02:40. The server's disk fails at 17:00 on Tuesday. The latest dump is intact, and restoring it takes 3 hours, including copying it from offsite storage. What is the data loss at the moment of failure, and what is the downtime if you start restoring at 17:30? What is the worst-case RPO of this schedule?
Show answer
The latest usable dump finished Tuesday at 02:40 and holds the state from when the dump began (about 02:00), so the data loss is about 15 hours (02:00 to 17:00). The downtime is 30 minutes of delay plus 3 hours of restore, so 3.5 hours (17:00 to 20:30), not counting verification. The worst-case RPO of a nightly schedule is about 24 hours plus the dump duration, because a failure just before the next dump completes loses nearly a full cycle, and a failed dump run would extend it further.
Exercise
A team runs a streaming replica in another availability zone and says they do not need backups because the replica is always current. Name two failure scenarios from this chapter that the replica does not protect against, and give a reason for each.
Show answer
Human error is one: a mistaken DROP TABLE or DELETE on the primary is replicated to the replica almost immediately, so both copies lose the data. Silent corruption or malicious deletion is another: bad data written by an application bug or an attacker with valid credentials is replicated like any other change, and the replica holds no older version to recover from. Loss of the whole site or cloud account is also a valid answer if the replica shares it.