Rewind a database to the minute before the mistake, using a backup plus the log.
Point-in-time recovery (PITR) lets you rewind a database to any moment inside a time window, for example to one minute before someone ran a bad command.
Most databases write every change to a log before they apply it. The PostgreSQL write-ahead log (WAL) and the MySQL binary log are two examples. This log is an exact, ordered diary of everything that changed.
If you keep a base backup and all the log written after it, you can rebuild the database as it was at any moment after that backup. Restore the backup, then replay the log forward until the time you want, and stop there.
The usual reason is a human or software mistake: a DELETE without a WHERE, a bad deploy that corrupted rows, a dropped table. A replica will not save you here, because replication copies the mistake to every copy within seconds.
| Target | What it means | When to use it |
|---|---|---|
| A time | Stop at a given date and time | You know roughly when the mistake happened. |
| A log position | Stop at an exact point in the log (an LSN or binlog position) | You found the bad statement in the log and want to stop just before it. |
| A named restore point | Stop at a label you created before a risky change | Before a big migration or deploy, mark a point you can come back to. |
| The end of the log | Replay everything, to the latest change | The disk or the machine was lost, not a mistake. |
| Backups more often | Backups less often |
|---|---|
| Less log to replay, so a faster recovery | More log to replay, so a slower recovery |
| More backup storage and more backup work | Cheaper and lighter |
| Fewer log files needed to restore | A long chain of log files that must all be intact |
A simplified model with fixed numbers (no random values). The write rate, the 15 minutes to copy a backup back and the log replaying 30 times faster than real time are illustrative, not measurements. Real recovery time depends on database size, hardware and how busy the log was.
# 1. restore the base backup into a new, empty data directory # 2. tell the server where the archived log files are restore_command = 'copy-from-archive %f %p' # 3. tell it where to stop recovery_target_time = '2026-03-14 15:06:00' # 4. start the server: it replays the log up to that time
| Term | Meaning in easy words |
|---|---|
| Base backup | A full copy of the database taken at one moment |
| Transaction log | The ordered record of every change (WAL in PostgreSQL, binary log in MySQL) |
| Log archiving | Copying finished log files to safe storage as they are produced |
| Replay (redo) | Applying logged changes again, in order, to rebuild a state |
| Recovery target | The time, position or named point where replay stops |
| Recovery window | How far back you can recover, set by how long you keep backups and log |
| RPO | Recovery Point Objective: how much recent data you can afford to lose |
| RTO | Recovery Time Objective: how long you can afford to be down while recovering |
| Restore point | A named marker you create in the log before a risky change |
| Snapshot | A fast, frozen copy of a disk or database, often used as a base backup |
| Log gap | A missing log file in the chain, which stops replay at that point |
| Choice | We gain | We lose |
|---|---|---|
| Frequent base backups | A faster recovery, a shorter log chain | More storage and backup work |
| Rare base backups | Cheaper | A long replay, so a long wait to recover |
| Long retention window | You can go back further | More storage cost, more data to protect |
| Short retention window | Cheaper and smaller | A mistake found late cannot be undone |
| Restore to a new instance | Safe, and you can compare | Takes time and extra capacity |
| Restore in place | No extra machine | Risky: a wrong target destroys the evidence |