Sub topic 9
Marking safe points with named restore points
This chapter covers named restore points: how to create one, how to recover to it, and where it fails. It assumes you can edit postgresql.conf and run SQL as a superuser. You also need to know that WAL archiving is already running, as set up in the earlier chapters on archiving and restore_command.
Why name a point in time
A time-based target such as recovery_target_time = '2026-11-27 14:30:00+00' depends on a clock. Before a risky change, you may not know the exact moment things will go wrong. You may also not trust the server clock, or you may not want to work out afterwards which timestamp was safe.
A named restore point fixes this. It is a label written into the WAL stream at the moment you create it. Recovery can later stop at that label. You choose the name, and the position is exact, with no guessing from timestamps.
Typical uses:
- Just before a schema migration or a bulk data change.
- Just before a deployment that touches many tables.
- At the start and end of a batch job, so you can undo the whole job.
- Before manual maintenance that you will perform by hand, such as running
DELETEstatements on a production table.
Creating a restore point
Use the function pg_create_restore_point(name). It returns the WAL location (an LSN, a log sequence number) where the record was written.
SELECT pg_create_restore_point('before_schema_migration');Example output:
pg_create_restore_point
-------------------------
0/5000A28Rules and limits:
- You must be a superuser, or have been granted
EXECUTEon the function. - The name can be at most 63 characters. Longer names are rejected with an error.
- The server must run with
wal_levelset toreplicaor higher. The default on current versions isreplica, which is enough. - You cannot call it on a standby server that is in recovery. It must run on a primary.
- Creating a restore point does not lock tables or block other sessions. It writes one small record to the WAL.
The restore point records a position in WAL. It does not copy any data. Without a base backup and the WAL archive, the label is useless.
Making sure the label reaches the archive
The restore point record lives in the current WAL segment. Archiving only copies a segment once it is complete. If the server crashes and the disk is lost before that segment is archived, the label goes with it.
To close the segment and push it to the archive straight away, switch the segment and then check the archiver:
SELECT pg_create_restore_point('before_schema_migration');
SELECT pg_switch_wal();
SELECT archived_count, last_archived_wal, failed_count, last_failed_wal
FROM pg_stat_archiver;pg_switch_wal() forces the current segment to be completed so the archiver can pick it up. Archiving is asynchronous, so the segment may not be archived the instant the function returns. Run the pg_stat_archiver query again after a few seconds. You want last_archived_wal to advance and failed_count to stay unchanged.
If you skip the switch, archive_timeout will eventually do it for you (300 seconds in our configuration). Doing it by hand is better when you are about to start a risky operation, because you want the safe point in the archive before you begin.
The safe-point workflow
The order of steps matters. Create the restore point first, then run the risky change.
The diagram below shows the sequence.
Recovering to a restore point
To stop recovery at a restore point, set recovery_target_name in the recovery settings. This works together with the files and steps from the recovery procedure: a restored base backup, recovery.signal, and a restore_command.
# recovery settings (PostgreSQL 12 and later)
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_name = 'before_schema_migration'
recovery_target_action = 'promote'Then create recovery.signal and start the server, as in the end-to-end procedure. The server replays WAL from the base backup. When it reaches the record named before_schema_migration, it stops.
Two points about where it stops:
- Recovery stops just before nothing else is replayed after the restore point record. In effect, the database state is exactly as it was when you called
pg_create_restore_point. - Only one recovery target may be set at a time. If you set
recovery_target_nametogether withrecovery_target_time, the server refuses to start and logs an error. Comment out the others.
The log shows the result. Look for lines like these:
LOG: recovery stopping at restore point "before_schema_migration", time 2026-11-27 14:29:41.532+00
LOG: pausing at the end of recoveryWith recovery_target_action = 'promote' the server then opens for read/write connections. The next chapters cover the other actions and the new timeline.
Choosing a name
Names are free text, so make them unambiguous. A name that appears twice in the WAL is a problem.
If the same name appears more than once in the replayed WAL, recovery stops at the first occurrence it meets. PostgreSQL does not check that names are unique when you create them. So adopt a convention that includes a date or a ticket number:
| Poor name | Better name |
|---|---|
| `before_change` | `before_migration_2026_11_27_rel42` |
| `test` | `pre_cleanup_orders_2026_11_27` |
| `backup` | `batch_nightly_start_2026_11_27` |
You can also record each name outside the database, for example in the change ticket or in a deployment log, along with the LSN that the function returned. The LSN is useful because it gives you a second way to recover to the same point, using recovery_target_lsn.
Restore points and base backups
A restore point must come after the base backup you recover from. Recovery starts at the base backup and replays forward. If the label sits earlier in the WAL than the start of the backup, recovery never encounters it.
What happens then depends on the situation:
- If recovery reaches the end of the available WAL without finding the target, it does not stop at the end silently. With a target set, the server fails at the end of recovery with an error such as
recovery ended before configured recovery target was reached. Fix the settings or choose an earlier base backup, then try again. - If the name is misspelled, the result is the same error, but only after the whole archive has been replayed. A typo can therefore cost you a long wait.
Because of this, always check that the base backup you intend to use was taken before the restore point. The backup directory name or the backup_label file shows the start time and start LSN.
grep -E 'START (TIME|WAL LOCATION)' /backups/base/20261127/backup_labelCommon mistakes
- Not archiving the segment. The label exists only in the live WAL, so a total loss of the server also loses the label. Switch the segment and check
pg_stat_archiver. - Creating the point after the change. The label then marks a state that already includes the change. Create it first.
- Reusing a name. Recovery stops at the first match, which may be an old point.
- Setting several targets. Only one of
recovery_target_name,recovery_target_time,recovery_target_xid,recovery_target_lsnandrecovery_target = 'immediate'can be active. - Expecting a restore point to protect against a bad backup. It marks a position only. It does not verify that your base backup or archive is complete.
Comparing the target types
| Target | Setting | Best when |
|---|---|---|
| Named restore point | `recovery_target_name` | You prepared a safe point before the change |
| Time | `recovery_target_time` | You know roughly when the mistake happened |
| Transaction ID | `recovery_target_xid` | You can identify the bad transaction |
| LSN | `recovery_target_lsn` | You have an exact WAL position, such as one returned by the function |
| Immediate | `recovery_target = 'immediate'` | You want the backup to be consistent and nothing more |
Named points are the only option that you must plan in advance. All the others can be chosen after the incident. That is the trade-off: a bit of planning in exchange for an exact, easy-to-explain target.
Exercise
You run pgcreaterestorepoint('beforecleanup'), then immediately drop a table by mistake. Your server's disk fails 30 seconds later, before the current WAL segment was archived and before archivetimeout fired. Can you recover to beforecleanup? Why, and what step would have prevented the problem?
Show answer
No. The restore point record was in the current WAL segment, which was never copied to the archive, so it is lost with the disk. You can only recover to the last archived WAL, which may be before the label. Running pgswitchwal() right after creating the restore point, then confirming in pgstatarchiver that lastarchivedwal advanced, would have put the label in the archive before the risky change.
Exercise
Your recovery settings contain recoverytargetname = 'beforemigration' and recoverytarget_time = '2026-11-27 14:30:00+00'. What happens when you start the server, and how do you fix it?
Show answer
The server refuses to start, because only one recovery target can be set at a time. Remove or comment out one of the two settings, keep recovery.signal in place, and start the server again.