In PostgreSQL, what is a timeline ID, what does a file such as 00000002.history contain, and why does promotion create one?
answer
- branches of WAL history
- first eight hex digits
- parent, switch point, reason
- same file names, different contents
basics
~20 sA timeline ID numbers one branch of WAL history and forms the first eight hex digits of every WAL file name. Promotion starts a new timeline and writes a .history file listing each ancestor timeline and the LSN where it branched off.
solid answer
~50 sA **timeline** is one branch of WAL history; a new cluster writes on timeline 1, and every end of archive recovery, promotion included, starts a new one. The ID is the first eight hex digits of each segment name, so `000000020000000A0000003C` is on timeline 2. Promotion takes the highest timeline it can find in `pg_wal` or the archive plus one, and writes `00000002.history`: one line per ancestor with the parent timeline, the LSN where the branch forked, and a reason such as `no recovery target specified`. The point is that the old primary may keep writing on timeline 1 past that LSN; without a new ID both servers would produce different segments under identical names in a shared archive. History files are tiny and should be archived and kept. Other standbys move to the new timeline because `recovery_target_timeline` defaults to `latest`, unless they already replayed past the fork.
go deeper
Recall that a timeline is a numbered branch of WAL history, that the number starts every WAL file name, and that promotion moves the server to a new one.
Explain what a history file line holds, how the new ID is chosen, and why reusing timeline 1 would let two servers write different WAL under the same file names.
Show you can read timelines in pg_controldata and pg_stat_wal_receiver, and predict which standbys can follow a promotion and which have replayed past the fork.
Explain why archive retention must keep every history file and .partial segment, and how timelines make repeated restores and failovers auditable.
## What a timeline is A PostgreSQL **timeline** is one branch of WAL history. A freshly initialised cluster writes on timeline 1. Every time an **archive recovery** ends, whether a standby is promoted or a point-in-time restore finishes, the server starts writing on a new, higher timeline. The old branch is not erased; this server simply stops extending it. The manual compares it to time-travel stories: after a branch, two futures exist from one past, and the timeline ID says which future a piece of WAL belongs to. ## Where the timeline ID appears | Place | How it looks | |---|---| | WAL segment file name | first eight hex digits: `000000020000000A0000003C` is timeline 2 | | Timeline history file | `00000002.history`, in `pg_wal` and in the archive | | `pg_controldata` | `Latest checkpoint's TimeLineID` and `Latest checkpoint's PrevTimeLineID` | | SQL | the `timeline_id` column of `pg_control_checkpoint()` | | A standby | `received_tli` in `pg_stat_wal_receiver` | | Server log | printed in **decimal**, unlike the hex in file names | Because the ID is part of every file name, WAL from two branches can sit in the same archive without overwriting each other. ## What a history file holds A history file is a small text file listing the ancestry of its timeline, one line per earlier branch: the **parent timeline**, the **LSN** where the child forked off it (the switch point), and a **reason**. After a first promotion `00000002.history` looks like this: ``` 1 0/5000148 no recovery target specified ``` When timeline 3 later forks from timeline 2, its file copies the parent's lines and appends its own, so every history file carries the whole chain back to timeline 1. Recovery uses these files to decide which segment to read for any LSN: before the switch point it reads timeline 1's segments, after it timeline 2's. They are archived like WAL segments and, since they are tiny, should be kept indefinitely; you may even add comments explaining why a timeline was created. ## How promotion creates one 1. The standby replays the WAL it already has; the end of that WAL becomes the **switch point**. 2. It looks for the highest timeline that already has a history file in `pg_wal` or the archive and takes the next number, so it never reuses an ID that another branch, perhaps an abandoned restore, already took. 3. It writes the new history file and continues writing WAL into a segment named with the new ID. 4. If archiving is on, the last, partly filled segment of the old timeline is renamed with a `.partial` suffix before it is archived, so it cannot clash with the complete copy the old primary may still archive. Recovery never reads `.partial` files on its own. ## Why the branch is needed At the moment of promotion the old primary may still be running, or may come back, and keep writing WAL on timeline 1 beyond the switch point. Without a new ID, both servers would produce **different contents under identical segment names**, and whichever archived second would overwrite the other or be rejected. With a new ID the two histories stay distinct, and tools can compare them: `pg_rewind` reads both servers' history to find exactly where they diverged. ## Checking timelines on a live system - **On the new primary**, `SELECT pg_walfile_name(pg_current_wal_lsn());` shows the current segment name, whose first eight digits are the new timeline. - `pg_control_checkpoint()` and `pg_controldata` report the timeline of the **latest checkpoint**, so straight after promotion they can still show the old ID until the checkpoint requested at promotion completes. - **On a standby**, `received_tli` in `pg_stat_wal_receiver` shows the timeline it is currently streaming. - **On disk**, `ls pg_wal/*.history` lists every timeline this server knows about; the same files should be in the archive. ## How other servers follow - **Other standbys** pointed at the promoted server switch to the new timeline on their own: the walreceiver fetches the history file from its upstream, and the recovery parameter `recovery_target_timeline` defaults to `latest`. Picking a different timeline on purpose is point-in-time recovery's business. - A standby that already replayed WAL **past the switch point** on the old timeline cannot follow. Its log reports that the new timeline forked off its current timeline before its current recovery point, and it keeps waiting. - The **old primary** is in the same position whenever it wrote anything after the fork, which is why it usually needs `pg_rewind` before it can become a standby.
- Why should PostgreSQL .history files be kept in the WAL archive even after old segments are purged?Recovery reads them to know which timeline's segment holds each LSN, so restoring a later base backup or bringing up a standby that follows `latest` needs every history file in the chain. They are a few bytes each, so the manual recommends keeping them indefinitely; purging them with old segments can make recovery onto later timelines fail.
- What is a .partial file that appears in the WAL archive after a PostgreSQL promotion?It is the last, partly filled segment of the old timeline, archived under a `.partial` suffix by the newly promoted server. The suffix avoids colliding with the full segment the old primary may still archive. Recovery ignores `.partial` files; an administrator can copy one into `pg_wal` without the suffix for an unusual point-in-time restore.
- Each timeline gets its own pg_wal directory on disk.
- Promotion keeps writing on timeline 1; only PITR changes the timeline.
- History files are large and should be purged along with old WAL segments.
- Server log messages print timeline IDs in hex, just like WAL file names.
- Cascaded standbys must be rebuilt after their upstream is promoted.