Sub topic 11
What happens when the target is reached
This chapter assumes you can already run a point-in-time recovery: you have a base backup, a working restore_command, a recovery.signal file and a recovery target such as recovery_target_time. Here we cover the last decision in that setup. When replay reaches the target, what should the server do next?
The setting that decides the outcome
The recovery_target_action parameter tells PostgreSQL what to do once replay reaches the configured target. It accepts three values:
| Value | Server state after the target | Accepts writes | Typical use |
|---|---|---|---|
| `pause` (default) | Recovery stops replaying. The server stays up in recovery. | No, read-only | Inspect the data and decide whether the target was right |
| `promote` | Recovery ends and the server becomes a normal primary. | Yes | Unattended or scripted restores where the target is known to be right |
| `shutdown` | The server stops. | Not applicable | Take a copy of the data directory at exactly the target, or restart under your own control |
The setting only applies when a recovery target is set. If you set no target, recovery replays all available WAL and then ends as a normal recovery. If you choose recovery_target = 'immediate', the action applies once the backup is consistent.
The default is pause on purpose. Promotion is a one-way step. After promotion the new primary begins writing WAL on a new timeline, and you cannot move the cluster to an earlier or later target. You would have to restore the base backup again. Pausing first lets you look before you commit.
What the server does at each stage
Replay ends exactly where the target says. With recovery_target_inclusive = on (the default), the transaction or time that matches the target is included. With off, replay stops just before it. This matters most for recovery_target_xid and recovery_target_time, where "just before the bad transaction" is often what you want.
Option 1: pause and inspect
With pause, the server stays in recovery and accepts read-only connections. This requires hot_standby = on, which is the default in current releases. If hot_standby is off, pause behaves like shutdown, and the log warns you about it.
Settings for a recovery that stops and waits for you:
# postgresql.conf (or postgresql.auto.conf)
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_action = 'pause'After you start the server, the log shows a line similar to recovery stopping before commit of transaction ... or recovery stopping at ..., followed by a note that recovery is paused. Connect and check the state:
SELECT pg_is_in_recovery() AS in_recovery,
pg_get_wal_replay_pause_state() AS pause_state,
pg_last_xact_replay_timestamp() AS last_replayed_commit;pg_get_wal_replay_pause_state() is available from PostgreSQL 14. It returns not paused, pause requested or paused. On older versions, use pg_is_wal_replay_paused(), which returns a boolean. Once the state is paused, the target has been reached and replay has stopped.
Now check that the data is what you expected. For example, if you recovered to just before an accidental DROP TABLE, confirm the table exists and has the rows you need:
SELECT count(*) FROM orders;
SELECT max(created_at) FROM orders;Writes fail while the server is in recovery. An INSERT or CREATE TABLE produces an error such as cannot execute INSERT in a read-only transaction. This is expected and protects the recovery state.
Choosing what to do after you look
You have three paths from the paused state.
- The state is right. End recovery and promote:
``sql SELECT pg_wal_replay_resume(); ``
When recovery is paused at a target, resuming ends recovery and promotes the server. You can also call pg_promote(), which waits for the promotion to finish by default:
``sql SELECT pg_promote(); ``
- The target was too early. Stop the server, set a later target, and start again. Keep
recovery.signalin place:
```bash sudo systemctl stop postgresql
edit recovery_target_time to a later value in the config
sudo systemctl start postgresql ```
Recovery continues from the base backup and replays up to the new target. The new target must be later than the point already reached. Recovery cannot go backward, because the server has already replayed past that point.
- The target was too late. The data you wanted to avoid is already applied. Stop the server, restore the base backup again into an empty data directory, and start over with an earlier target. This is why a copy of the base backup and the archive should stay untouched during a recovery.
Each stop and restart replays the WAL again from the base backup's start point, so the cost grows with the amount of WAL between the backup and the target. Use named restore points or xid targets when you can, so you need fewer attempts.
Option 2: promote automatically
Use promote when you know the target is correct. It fits scripted restores, test environments and rehearsals.
recovery_target_name = 'before_schema_migration'
recovery_target_action = 'promote'When the target is reached, the server finishes recovery, removes recovery.signal, creates a new timeline, and accepts writes. The log reports that the selected new timeline ID and that the database system is ready to accept connections. There is no pause where you can change your mind.
Option 3: shut down at the target
With shutdown, the server stops once it reaches the target. The data directory is left exactly at that point.
recovery_target_xid = '1234567'
recovery_target_action = 'shutdown'Use this when you want to copy or snapshot the recovered data directory before it changes, or when another tool controls the next start. The server does not promote. The next start continues recovery, because recovery.signal is still present and the target is still configured. If you start it without changing anything, it replays up to the same target and stops again.
To bring the cluster up after a shutdown, either remove the target settings and let recovery finish, or change the action to promote and start again.
Cleaning up after promotion
After promotion, PostgreSQL removes recovery.signal itself. It does not remove your recovery settings from postgresql.conf. Leaving recovery_target_time and restore_command in place causes trouble at the next restart or when someone creates recovery.signal again. Comment them out or delete them. If you used ALTER SYSTEM, reset them:
ALTER SYSTEM RESET recovery_target_time;
ALTER SYSTEM RESET recovery_target_action;
SELECT pg_reload_conf();Also review the archiving settings. If archive_mode = on, the promoted server starts archiving WAL on its new timeline. WAL file names include the timeline ID, so they do not collide with the old files, and a guard such as test ! -f in archive_command keeps working. Make sure the archive location has space and that the old and new histories are not mixed up in a retention script. The next chapter covers timelines in detail.
Failure modes
- The target is never reached. If the archive ends before the target, for example a
recovery_target_timelater than the last archived WAL, PostgreSQL 13 and later stops with a FATAL error saying recovery ended before the configured target was reached. Earlier releases promoted silently, so check the log on older versions. Fix it by choosing an earlier target or by restoring the missing WAL. - A missing segment in the middle. Replay stops with an error when
restore_commandcannot fetch a required file. The server does not skip gaps. - The target is older than the base backup. The backup's consistent point must come before the target. Otherwise recovery fails with a message about the target being before the consistent recovery point. Use an earlier base backup.
- A paused server that nobody resumes. A paused server holds the cluster in read-only mode. If you forget about it, applications fail on writes. Add a note to your runbook for who decides to resume or abandon.
- Promoting from a script too early. A script that checks
pg_is_in_recovery()and seestruemay conclude that recovery is still running. Withpause, the server stays in recovery by design. Check the pause state as well.
Exercise
You restored a cluster with recoverytargettime = '2026-11-27 14:30:00+00' and the default recoverytargetaction. The server is up, and a query shows that the table you wanted still contains a row deleted at 14:20. What state is the server in, and what should you do to reach a time before the delete?
Show answer
The default action is pause, so the server is in recovery and read-only, paused at 14:30. It has already replayed past 14:20, and recovery cannot go backward. Stop the server, restore the base backup into a fresh data directory (the earlier damaged directory should remain set aside), keep recovery.signal, set recoverytargettime to a time before 14:20 (for example '2026-11-27 14:19:00+00'), and start again. Do not promote the paused server, because the data would then include the delete.