Sub topic 12
Timelines after recovery
This chapter covers what PostgreSQL does at the end of a point-in-time recovery to keep the restored database's WAL separate from the original's. It assumes you have already run a recovery with restore_command, recovery.signal and a recovery target, and that you can read WAL segment file names. It also assumes you know that pg_basebackup plus archived WAL is your backup.
Why timelines exist
Suppose your cluster runs until 16:00. At 14:30 someone dropped a table, so you recover to 14:29 and open the database for writes. The archive now holds WAL from the original history for 14:29 to 16:00. The restored cluster is about to generate different WAL from the same starting position.
Without a rule, the new WAL would collide with the old WAL, and a later recovery could not tell which history to replay. PostgreSQL solves this with a timeline. A timeline is a numbered branch of WAL history. Every cluster starts on timeline 1. When recovery finishes and the server is promoted to accept writes, PostgreSQL starts a new timeline with the next unused number. All WAL generated afterwards belongs to that new timeline.
The old timeline is never rewritten. Its WAL stays in the archive, so you can still recover to any point on it, including points later than the one you chose the first time.
Where the timeline ID shows up
The timeline ID is part of every WAL file name. A segment name is 24 hex digits: 8 for the timeline, 8 for the log number, 8 for the segment number.
000000010000000000000005
^^^^^^^^ timeline 1
^^^^^^^^ log 0
^^^^^^^^ segment 5After the recovery above, the archive can contain both of these:
000000010000000000000005
000000010000000000000006
000000020000000000000005
00000002.historySegment 5 exists on both timelines. Timeline 1 continues into segment 6 and beyond. Timeline 2 branches off inside segment 5 and continues from there. Because the names differ, the test ! -f guard in your archive_command does not block the new timeline's segments.
You can check the current timeline from SQL:
SELECT timeline_id FROM pg_control_checkpoint();
SELECT pg_walfile_name(pg_current_wal_lsn());The first digits of the file name returned by the second query match the first query's result.
The timeline history file
When a new timeline is created, PostgreSQL writes a history file named <new timeline ID>.history, such as 00000002.history. It lists the ancestry of the timeline, one line per ancestor. Each line holds three fields separated by whitespace: the parent timeline ID, the LSN where the branch happened, and a comment saying why.
1 0/5000A58 before 2026-11-27 14:30:00.000000+00This file is what lets recovery follow a path across branches. If timeline 3 descended from timeline 2, which descended from timeline 1, the file 00000003.history lists both switch points. Recovery reads it to know where to stop replaying one timeline and continue on the next.
PostgreSQL archives the history file immediately, without waiting for a segment to fill. The file is tiny. Keep it for as long as any WAL from the timelines it describes.
Replace nothing: the diagram follows.
When the switch happens
The switch occurs at the end of recovery, not at its start. What triggers it depends on recovery_target_action:
promote: PostgreSQL ends recovery and creates the new timeline immediately.pause: Recovery stops at the target and no timeline is created yet. You can inspect the data. If you runSELECT pg_wal_replay_resume();, recovery ends and the new timeline is created. If the data is wrong, shut down and choose a different target without having consumed a timeline number.shutdown: The server stops at the target. No new timeline exists until you start it again without a stopping target, and recovery then completes.
Promoting a standby also creates a new timeline. A failover therefore produces the same kind of history file you see after PITR.
Choosing which timeline to recover along
The setting recovery_target_timeline tells recovery which branch to follow when replaying archived WAL.
| Value | Meaning |
|---|---|
| `latest` | Follow the newest timeline found in the archive. This is the default from PostgreSQL 12. |
| `current` | Stay on the timeline of the base backup. This was the default before PostgreSQL 12. |
| A number such as `2` | Follow that specific timeline ID. |
The default of latest suits the common case: you restore a base backup, and you want everything up to the most recent state, or a target that happens to lie on the newest branch.
It is wrong in one common case. Say you recover to 14:00, promote, and find you picked a time too early. The data you wanted was at 14:29. Timeline 2 branched at 14:00 and contains only the writes made since promotion. A target of 14:29 does not exist on timeline 2's path from the branch point, because that path no longer follows the original history past 14:00.
To reach 14:29 on the original history, you must tell recovery to stay on timeline 1:
# recovery settings: second attempt, original history
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:29:00+00'
recovery_target_timeline = '1'
recovery_target_action = 'promote'Restore the base backup again, create recovery.signal, and start the server. This recovery creates timeline 3, because timeline 2 is already taken. Its history file reads 1 <LSN> before 2026-11-27 14:29:00..., so timeline 3 is a child of timeline 1, a sibling of timeline 2.
A worked inspection
After a recovery, check the archive and the cluster to confirm what happened.
ls /mnt/archive | grep history
sudo -u postgres psql -Atc "SELECT timeline_id FROM pg_control_checkpoint();"
cat /mnt/archive/00000002.historyThe first command lists 00000002.history, and 00000003.history if you made a second attempt. The second command prints the timeline the running cluster is on. The third shows the switch point and reason.
The server log also reports the switch. Look for a line such as selected new timeline ID: 2 near the end of recovery, followed by archive recovery complete.
Common errors and gotchas
Missing history file. If the archive lacks the history file for the timeline you request, recovery fails with an error such as requested timeline 2 is not a child of this server's history. This happens when archiving of history files was not set up, or someone deleted them during cleanup. Keep history files with the WAL.
Expected log noise. During recovery, PostgreSQL asks restore_command for files that may not exist. It probes for 00000002.history, 00000003.history, and so on to find the newest timeline. A cp that fails with "No such file or directory" is therefore normal. It is not a sign of damage. Your restore_command must return a nonzero exit code for a missing file and must not return zero for an empty or partial copy.
Partial segment at the branch. The final segment of the old timeline and the first segment of the new one share a number. On the new timeline, PostgreSQL copies the content up to the switch point into the new segment, so both exist. Do not delete the old one, because recovery along timeline 1 needs it.
Shared archive between clusters. If a standby and a restored copy both promote and archive into the same directory, they can pick the same new timeline ID and write conflicting files. The test ! -f guard then makes the second archiver fail and pg_stat_archiver.failed_count grows. Give every independent cluster, including test restores, its own archive, or disable archiving on the test copy.
Old primary after failover. An old primary that kept running past the promotion point sits on a diverged timeline. It cannot simply follow the new primary. Resolve this with pg_rewind or by re-cloning it, and do not point its WAL at the shared archive.
Retention. WAL on timelines you may still want to recover along must be kept. When you prune, a base backup on timeline 1 needs timeline 1's WAL from the backup start onward, and also the history files of every timeline you may follow.
Exercise
You restored a base backup, set recoverytargettime to 14:00, used the default settings, and promoted. The cluster is on timeline 2. You then realize the correct time was 14:29. What do you set, and which timeline will the second recovery create?
Show answer
Restore the base backup into a clean data directory again and create recovery.signal. Set recoverytargettime = '2026-11-27 14:29:00+00' and recoverytargettimeline = '1' (or 'current', since the backup is on timeline 1), because the default 'latest' would follow timeline 2, which branched at 14:00 and does not contain the original history after that point. After promotion, the cluster creates timeline 3 with a history file 00000003.history whose parent is timeline 1.
Exercise
The archive contains 000000010000000000000005, 000000010000000000000006 and 000000020000000000000005. Which timeline branched in which segment, and why is there no name collision for segment 5?
Show answer
Timeline 2 branched from timeline 1 inside segment 5. The first 8 hex digits of each file name are the timeline ID, so segment 5 on timeline 1 and segment 5 on timeline 2 have different names. Timeline 1 carries on into segment 6, while timeline 2 continues from its own copy of segment 5.