Sub topic 8
Choosing where recovery stops
This chapter assumes you can edit postgresql.conf, that you have a base backup and a working WAL archive, and that restore_command is already set. It covers how to tell PostgreSQL where to stop replaying WAL. The settings are called recovery targets.
What a recovery target does
By default, recovery replays every WAL segment it can fetch from the archive, until none is left. That restores the cluster to the latest archived state. This is the right choice after a disk failure. It is the wrong choice after a bad DELETE or DROP TABLE, because replaying everything replays the mistake too.
A recovery target tells PostgreSQL to stop replay at a chosen position. The base backup supplies the starting point. The archived WAL supplies every change after it. The target decides how far to replay.
The target types
PostgreSQL offers five ways to say where to stop. Each one is a parameter in postgresql.conf or postgresql.auto.conf.
| Parameter | Stops at | Typical use |
|---|---|---|
| `recovery_target_time` | A timestamp | You know roughly when the damage happened |
| `recovery_target_xid` | A transaction ID | You identified the bad transaction |
| `recovery_target_lsn` | A WAL position (log sequence number) | You have an exact position from logs or `pg_waldump` |
| `recovery_target_name` | A named restore point | You created a marker before risky work |
| `recovery_target = 'immediate'` | The first consistent state | You want the backup as it was, with only the WAL needed to make it consistent |
Time
recovery_target_time = '2026-11-27 14:30:00+00'Always include a time zone offset. Without one, PostgreSQL interprets the value using the server's timezone setting, which may not be what you expect. Recovery stops at the first commit whose timestamp is later than the target (or equal to it, if you set recovery_target_inclusive = on, the default).
A time target is the easiest to reason about, but it depends on the clock at the moment of each commit. If you only know the damage happened "around 14:30", pick a time a little earlier, recover, and check the data. You can repeat the recovery with a later time if you stopped too soon.
Transaction ID
recovery_target_xid = '1234567'This is precise when you know the transaction. You can find candidates by looking at application logs or by using track_commit_timestamp data, if it was enabled before the incident. Transaction IDs are assigned when a transaction starts, but recovery stops based on commit order. Two transactions can commit in a different order than their IDs. Stopping at one XID may therefore include or exclude other transactions with lower or higher IDs in ways that surprise you.
LSN
recovery_target_lsn = '0/3000148'An LSN is a position in the WAL stream. It is the most exact target. You usually obtain one from pg_waldump output or from pg_current_wal_lsn() captured earlier. Recovery stops at the first record at or after this position, depending on recovery_target_inclusive.
Named restore point
recovery_target_name = 'before_schema_migration'This stops at a marker you created earlier with pg_create_restore_point(). It avoids guessing a time. The next chapter covers how to create these markers.
Immediate
recovery_target = 'immediate'This stops as soon as the database reaches a consistent state, which is the end of the backup itself. It is useful when you want to restore exactly what the base backup contained, for example to check that a backup is usable.
Rules for setting targets
- Set only one of the
recovery_target*parameters. If you set more than one, the server refuses to start with a configuration error. recovery_target_time,_xid,_lsnand_nameeach need a value.recovery_targetaccepts only the stringimmediate.- The target must be later than the end of the base backup. If the target is earlier than the point where the backup became consistent, recovery fails with an error such as "recovery ended before configured recovery target was reached". Use an older base backup in that case.
- The WAL needed to reach the target must exist in the archive. If a segment is missing, recovery stops early and reports that the target was not reached.
Inclusive or exclusive
recovery_target_inclusive controls whether recovery stops just after the target or just before it.
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_inclusive = offWith the default on, the transaction at the target is replayed. With off, recovery stops just before it. This setting applies to time, xid and LSN targets. It has no effect on named restore points or immediate.
A common case is a known bad transaction. If you know its XID and want to keep everything before it, set recovery_target_xid to that XID and recovery_target_inclusive = off.
Which timeline to follow
After a recovery, PostgreSQL starts a new timeline. When you recover a second time from the same archive, it matters which timeline you follow. The parameter recovery_target_timeline controls this. Since PostgreSQL 12 the default is latest, which follows the newest timeline in the archive. For a recovery that must follow the path of the original base backup, you can give a specific timeline ID or current. Timelines are covered in detail in a later chapter, so treat the default as sufficient for now.
A working example
Suppose a migration ran at 14:31 and dropped a table by mistake. The base backup is at /backups/base/20261127 and the archive is at /mnt/archive. The settings below stop replay one minute before the migration.
# postgresql.auto.conf (in the restored data directory)
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_inclusive = on
recovery_target_action = 'pause'Use pause while you are investigating. The server finishes replay, then stays in read-only recovery mode. You can connect and check the data.
SELECT pg_is_in_recovery();
SELECT pg_get_wal_replay_pause_state();
SELECT count(*) FROM orders;If the table is present and the data looks right, run SELECT pg_wal_replay_resume(); to end recovery and promote. If you stopped too early or too late, shut the server down, adjust the target, and run the recovery again from the base backup. Moving a target later is possible without restoring the base backup again only if the server has not been promoted. After promotion, the data directory has moved to a new timeline and you must restore the base backup again.
Common errors
- The server starts and ends recovery immediately. You may have set
recovery_target = 'immediate', or the target was already reached by the first consistent point. - "recovery ended before configured recovery target was reached". The archive lacks WAL up to the target, or the target is later than the last archived record. Check
restore_command, the archive contents, andpg_stat_archiveron the original server. - "requested recovery stop point is before consistent recovery point". The target lies before the end of the base backup. Restore an older base backup.
- A time target without an offset stops at the wrong moment. The time was parsed in the server's time zone. Always write an explicit offset.
- Several targets set at once. The server refuses to start. Comment out all but one.
- The target XID was never committed. Recovery cannot stop at a transaction that aborted. Choose a different target.
Choosing a target
- If you created a named restore point before the risky change, use
recovery_target_name. It needs no guessing. - If you know the exact transaction, use
recovery_target_xidwithrecovery_target_inclusive = off. - If you know the time, use
recovery_target_timewith an explicit offset, and begin withpause. - If you have an LSN from logs or tools, use
recovery_target_lsnfor the most exact stop. - If you only want to verify a backup, use
recovery_target = 'immediate'.
Exercise
A developer ran a destructive UPDATE in a transaction with XID 8812040. You want the database exactly as it was before that transaction, and you are not sure the stop point is right yet. Which recovery settings do you use?
Show answer
Set recovery_target_xid = '8812040', recovery_target_inclusive = off so that the transaction itself is not replayed, and recovery_target_action = 'pause' so you can inspect the data before promoting. Keep restore_command set and create recovery.signal. If the data is right, run pg_wal_replay_resume(). If not, restore the base backup again with a different target.
Exercise
You set both recovery_target_time and recovery_target_name in postgresql.conf and start the server. What happens, and how do you fix it?
Show answer
The server refuses to start, because only one recovery_target* setting may be active. Comment out one of them, keep the one you want, and start the server again.