Sub topic 8
Choosing Where Recovery Stops
Assume you understand PostgreSQL transactions, WAL (write-ahead logging), and the Linux recovery procedure. This chapter focuses on choosing a recovery target after you have restored a physical base backup and configured restore_command.
A recovery target tells PostgreSQL when to stop replaying archived WAL. You can stop at a clock time, transaction ID, WAL location, named restore point, or the earliest consistent state.
Recovery target types
Set one target in postgresql.conf or postgresql.auto.conf before starting recovery. Do not configure several target types at the same time.
| Target | Setting | Useful when |
|---|---|---|
| Time | `recovery_target_time` | You know approximately when unwanted data appeared |
| Transaction | `recovery_target_xid` | You have identified a transaction ID |
| WAL location | `recovery_target_lsn` | An investigation gives you an exact WAL position |
| Restore point | `recovery_target_name` | You created a named marker before a risky operation |
| Earliest consistent state | `recovery_target = 'immediate'` | You need the first usable state in the backup |
A time target uses a timestamp with a time zone. PostgreSQL replays WAL until it reaches that point. For example:
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_action = 'promote'The target time must be within the WAL available from the base backup onward. If the archive does not contain the required segments, recovery stops with an error before reaching the target.
Exactness and inclusiveness
Recovery targets can fall inside a transaction. The recovery_target_inclusive setting controls whether PostgreSQL includes the transaction that matches the target.
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_inclusive = falseWith false, PostgreSQL stops before the matching transaction when that distinction is meaningful. With true, it includes the matching transaction. The default is true.
For a time target, use a time just before the unwanted change when you want the database state before that change. Keep timestamps in UTC or include an explicit offset to avoid daylight-saving and server-time-zone mistakes.
LSN, transaction ID, and restore point targets
An LSN (log sequence number) identifies a position in the WAL stream. It is precise and useful when database logs or an audit investigation provide the position.
recovery_target_lsn = '0/50000A0'A transaction ID identifies a transaction in PostgreSQL's transaction sequence:
recovery_target_xid = '8421'Transaction IDs are less convenient for routine operations because you must first identify the relevant transaction, and transaction IDs eventually wrap around.
A named restore point is usually easier to use during planned maintenance. Create it before the operation:
SELECT pg_create_restore_point('before_schema_migration');Then use the name during recovery:
recovery_target_name = 'before_schema_migration'A restore point is recorded in WAL. Therefore, the WAL containing the restore point must be archived and available during recovery.
Immediate recovery
recovery_target = 'immediate' stops replay at the earliest consistent state after the base backup. This is useful when you only need to make a backup usable, but it does not select a business event or clock time.
After PostgreSQL reaches the target, recovery_target_action determines whether it pauses, promotes the server, or shuts it down. Check the server logs to confirm the target was reached rather than assuming that startup completed successfully.
Exercise
You need the database state immediately before a migration marked by the restore point before_schema_migration. Which recovery setting should you use?
Show answer
Set recovery_target_name = 'before_schema_migration' and set recovery_target_inclusive = false if the target transaction itself must be excluded.