Sub topic 12
Timelines After Recovery
Prerequisites: you should understand WAL archiving, physical base backups, restore_command, recovery targets, and PostgreSQL data directory permissions. This chapter explains what changes after a recovery stops and the server becomes writable.
What a timeline is
A timeline is a numbered history of WAL. PostgreSQL creates a new timeline when recovery ends and the server starts accepting writes. The new timeline prevents the recovered server from accidentally joining an old history branch.
For example, suppose a database has this history:
Timeline 1: --- base backup --- A --- B --- mistake
\
Timeline 2: recovery target --- new writesTimeline 2 contains the WAL created after recovery. Timeline 1 still contains the original history, including the mistake. The two timelines share WAL up to their fork, but they must not be combined after that point.
Each timeline has a numeric identifier. PostgreSQL also writes a timeline history file, such as 00000002.history, to describe where timeline 2 branched from timeline 1. Archive storage must retain these history files along with the WAL segments.
Choosing a recovery timeline
The recovery_target_timeline setting tells PostgreSQL which timeline to follow while replaying archived WAL.
| Setting | Meaning | Typical use |
|---|---|---|
| `latest` | Follow the newest timeline available in the archive | Normal recovery after several recovery operations |
| `current` | Stay on the timeline recorded by the base backup | Recover only along the original branch |
| A timeline number | Follow a specific numeric timeline | Controlled recovery to a known branch |
latest is usually the safest choice for a normal restore because it lets PostgreSQL follow later timeline history files. Use a specific number when you need a particular branch and have verified that the required history and WAL files exist.
For PostgreSQL 12 and later, place the setting in the recovery configuration before starting the restored server:
# 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_timeline = 'latest'
recovery_target_action = 'promote'PostgreSQL reads the timeline history files from the archive through restore_command. The command must return a failure status when a requested file does not exist. Do not make it return success for missing files, because recovery could then continue with incomplete WAL.
Promotion and timeline history
When recovery_target_action = 'promote' reaches the target, PostgreSQL promotes the server and creates a new timeline. The server writes the new timeline history file and begins generating WAL on that branch.
Check the server log after promotion. It should identify the timeline switch and the new timeline number. Also verify that the archive process stores the new history file and subsequent WAL segments.
A recovered server should become the new source for future base backups and WAL archiving. Do not resume backups from an old primary unless you intentionally manage both branches. A standby following the old timeline may need to be rebuilt or configured to follow the new one.
Exercise
Why does PostgreSQL create a new timeline when a point-in-time recovery is promoted?
Show answer
It separates new writes from the original WAL branch, so the recovered server cannot later mix its history with WAL created after the recovery point on the old branch.