Timelines
Think of PostgreSQL timelines like Git branches for your WAL archive. Whenever you complete a Point-In-Time Recovery (PITR) or promote a standby to primary, PostgreSQL increments the Timeline ID so new WAL files never collide with or overwrite the original archived WAL files.
Why does the timeline change on every restore or failover?#
Without timelines, restoring to 09:55 and continuing to write would generate a brand-new WAL segment with the exact same filename (000000010000000000000015) as the old archived segment from 10:00:
- Archiving would either fail or overwrite the old WAL segment, corrupting your archive.
- If you realized
09:55was slightly too early and wanted to try09:58instead, the original history would be gone.
By branching to Timeline 2 (00000002), both histories coexist cleanly in your WAL archive.
Reading a WAL filename#
Every WAL segment name is 24 hex characters split into three 8-character parts:
0000002E 00000119 0000004F
└──┬───┘ └──┬───┘ └──┬───┘
Timeline Log ID Segment
(hex)
Watch out for Hex vs Decimal! WAL filenames encode the timeline in hexadecimal, while tools like
pgBackRest(--target-timeline=46) andpostgresql.conf(recovery_target_timeline = '46') expect decimal:
# Convert hex timeline 2E to decimal:
printf "%d\n" 0x2E
# Output: 46The .history file#
When PostgreSQL switches to a new timeline (e.g., Timeline 2), it creates a tiny 00000002.history file in pg_wal/ (and archives it) recording the exact branch point:
# parent_timeline switchpoint_lsn reason
1 0/14000098 before 2025-06-17 09:55:00+00
If you later restore to Timeline 2, PostgreSQL reads 00000002.history so it knows to pull segments up to 0/14000098 from Timeline 1 and everything after from Timeline 2.
Checking the current timeline#
From SQL:
SELECT timeline_id, redo_lsn
FROM pg_control_checkpoint();
Or from the OS with pg_controldata:
pg_controldata "$PGDATA" | grep TimeLineIDChoosing recovery_target_timeline during PITR#
latest(default in modern PostgreSQL): Follows the newest timeline available in the archive.current: Stays strictly on the timeline of the base backup.- Explicit decimal ID (e.g.,
recovery_target_timeline = '1'orpgbackrest --target-timeline=46): Required when you intentionally want to restore back onto an older branch after a previous restore created a newer timeline.