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.

Interactive How PostgreSQL Timelines Branch on PITR / Promotion
TIMELINE 1 (HEX: 00000001) · ORIGINAL HISTORY TIMELINE 2 (HEX: 00000002) · NEW HISTORY AFTER PROMOTION 00000001...0013 09:00 · Base backup 00000001...0014 09:55 · Good state 00000001...0015 (future) 00000001...0016 (future) 00000002.history (not created yet) 00000002...0014 (not created yet) 00000002...0015 (not created yet) 00000002...0016 (not created yet)

WAL Archive & .history

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:55 was slightly too early and wanted to try 09:58 instead, 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) and postgresql.conf (recovery_target_timeline = '46') expect decimal:

# Convert hex timeline 2E to decimal:
printf "%d\n" 0x2E
# Output: 46

The .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 TimeLineID

Choosing 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' or pgbackrest --target-timeline=46): Required when you intentionally want to restore back onto an older branch after a previous restore created a newer timeline.