skip to content

After a PostgreSQL failover the old primary cannot follow the promoted standby's new timeline; how do you rejoin it with pg_rewind, and what must already be configured?

level: seniorimportance: must knowfreq 55%

answer

  1. it wrote past the fork
  2. copy only changed blocks
  3. hint bits leave no WAL
  4. checksums or wal_log_hints
  5. --source-server plus -R

basics

~20 s

With the old primary shut down, run pg_rewind against its data directory with --source-server pointing at the new primary and -R, then start it as a standby. It needs data checksums, on by default in initdb since 18, or wal_log_hints = on.

solid answer

~50 s

The old primary wrote WAL on the old timeline after the point where the new one forked, so recovery refuses to follow and logs that the new timeline forked off before its current recovery point. `pg_rewind` fixes that without a full copy: it compares both servers' timeline history, scans the target's WAL from the last common checkpoint, copies only the blocks changed since the divergence plus every other file in full, and writes a `backup_label` so the next start replays the new primary's WAL. Run it with `--target-pgdata`, `--source-server` (a normal connection; superuser or a role granted `pg_ls_dir`, `pg_stat_file` and `pg_read_binary_file`) and `-R` to write `standby.signal` and `primary_conninfo`. Prerequisites: data checksums (initdb's default in PostgreSQL 18) or `wal_log_hints = on`, which must already have been on; `full_page_writes` on the source; the target's WAL back to the divergence. Commits the old primary made after the fork are discarded.

code

bash · 11 lines
bash
# 1. confirm the target qualifies (run against the stopped old primary)
pg_controldata /var/lib/postgresql/18/main | grep -E 'checksum version|wal_log_hints'

# 2. dry run first: reports the divergence point without changing files
pg_rewind --target-pgdata=/var/lib/postgresql/18/main \
  --source-server='host=pg-new user=rewind_user dbname=postgres' --dry-run

# 3. real run; -R writes standby.signal and primary_conninfo
pg_rewind --target-pgdata=/var/lib/postgresql/18/main \
  --source-server='host=pg-new user=rewind_user dbname=postgres' -R --progress
pg_ctl start -D /var/lib/postgresql/18/main

go deeper

for a junior

Remember that a failed-over old primary cannot just be restarted as a standby, and that pg_rewind is the PostgreSQL tool that brings it back without a full copy.

for a middle

Explain the steps: timeline histories, the WAL scan from the last common checkpoint, copying changed blocks, and -R writing standby.signal and primary_conninfo.

for a senior

Show the production judgement: verify checksums or wal_log_hints before any failover happens, accept that post-fork commits are lost, and know when a rebuild beats a rewind.

for a principal

Weigh rewind against rebuild in the HA design: checksum policy at initdb, WAL retention for rewinds, and how lost post-fork commits are reconciled with the business.

## Why the old primary cannot simply follow After a failover the promoted standby writes on a new **timeline** that forked from the old one at a **switch point**: the end of the WAL it had replayed. With asynchronous replication the old primary usually wrote more WAL on the old timeline after that point, both commits the standby never received and anything that happened while it was isolated. If you add `standby.signal` and start it, recovery finds that the new timeline forked off before its own position, logs something like `new timeline 2 forked off current database system timeline 1 before current recovery point 0/7000060`, and never catches up. Its data files contain changes the new primary does not have. The two ways back are a fresh base backup of the new primary, which copies every file, or **`pg_rewind`**, which copies only what differs. ## What pg_rewind does 1. It reads the **timeline history** of source and target, finds where they diverged, and the last checkpoint on the target before that point. 2. It **scans the target's WAL** from that checkpoint and lists every data block the old primary changed after the divergence. 3. It copies those blocks from the source, either over a normal libpq connection to the running new primary (`--source-server`) or by file access to a cleanly stopped one (`--source-pgdata`). 4. It copies all other files **in full**: new relation files, WAL segments, `pg_xact`, configuration files. A few, such as `postmaster.pid` and `pg_internal.init`, are skipped. 5. It writes a `backup_label` so the next start replays the source's WAL from that checkpoint; with `-R` (`--write-recovery-conf`) it also creates `standby.signal` and appends `primary_conninfo` to `postgresql.auto.conf`. When the target starts it replays to a consistent state and streams from the new primary like any standby. Everything the old primary committed after the fork is **gone**: the rewind makes the target match the source and merges nothing back. If the target's WAL ends at or before the fork, pg_rewind reports `no rewind required`, which is the normal outcome after a clean switchover. ## The prerequisites | Requirement | Why | How to check | |---|---|---| | Data checksums or `wal_log_hints = on` on the target | setting a hint bit changes a page without WAL unless one of these forces a full-page image, so the WAL scan would miss that page | `pg_controldata`: `Data page checksum version` and `wal_log_hints setting` | | `full_page_writes = on` on the source | protects against torn pages; it is the default | `SHOW full_page_writes;` | | Target WAL back to the divergence | the list of changed blocks comes from it | copy segments from the archive into `pg_wal`, or pass `-c` to use the target's `restore_command` | | Target not running | its data directory is rewritten | a crashed target is first taken through single-user crash recovery, unless `--no-ensure-shutdown` | | Access to the source | files are read through SQL functions | superuser, or `EXECUTE` on `pg_ls_dir`, `pg_stat_file` and `pg_read_binary_file` | The first row is the one that bites. `wal_log_hints` is off by default, takes effect only at server start, and must have been on **while the divergent WAL was written**, so it cannot be fixed after the failover. In PostgreSQL 18 `initdb` enables data checksums by default, so a cluster initialised on 18 qualifies; a cluster created on 17 or earlier without `--data-checksums` needs `wal_log_hints = on` set in advance. Without either, pg_rewind stops with `target server needs to use either data checksums or "wal_log_hints = on"`. ## A rejoin, step by step ```bash # on the old primary host, as the postgres OS user; the server must not be running pg_rewind --target-pgdata=/var/lib/postgresql/18/main \ --source-server='host=pg-new user=rewind_user dbname=postgres' \ --write-recovery-conf --progress pg_ctl start -D /var/lib/postgresql/18/main ``` ## Traps - **Configuration is overwritten.** `postgresql.conf`, `pg_hba.conf` and the other config files come from the source; check host-specific settings before starting. - **A failed rewind leaves an unusable directory.** If pg_rewind stops part-way, take a new base backup instead of retrying on top. - **Read-only files abort it.** SSL keys or certificates the target cannot write make it fail at once; move them aside first. - **Starting without recovery configuration** makes the target diverge again; use `-R` or add `standby.signal` and `primary_conninfo` yourself. - **The lost commits are real.** If the business must know what the old primary accepted after the fork, keep a copy of its data directory before rewinding. ## When to rebuild instead A base backup is the right call when the target's WAL since the divergence is gone and was not archived, when the target has neither checksums nor `wal_log_hints`, or when a previous rewind failed.

  • pg_rewind prints 'no rewind required'. What does that mean for the old PostgreSQL primary?
    The target's WAL ends at or before the point where the source's timeline forked, so it wrote nothing that conflicts. It only needs to start as a standby; with `-R` pg_rewind still writes `standby.signal` and `primary_conninfo`. This is the usual result after a clean switchover, where the old primary's last record was the shutdown checkpoint the standby had already received.
  • Why does pg_rewind insist on data checksums or wal_log_hints rather than just reading the WAL?
    It finds changed blocks by scanning WAL, but setting a hint bit modifies a page without writing WAL. A page changed only that way after the fork would be missed and keep hint bits recording the old primary's commit outcomes. Checksums or `wal_log_hints` force a full-page image on the first such change after each checkpoint, so every changed page shows up.
  • What do you do when the old PostgreSQL primary's WAL back to the divergence has already been recycled?
    pg_rewind needs it to list changed blocks. Pass `-c` (`--restore-target-wal`) so it fetches missing segments with the target's `restore_command`, or copy them from the archive into `pg_wal` yourself. If they were never archived, rebuild the node from a base backup instead.

  • pg_rewind merges the old primary's extra transactions into the new primary.
  • pg_rewind works on any cluster as long as full_page_writes is on.
  • You can turn on wal_log_hints with a reload just before running pg_rewind.
  • pg_rewind copies the whole data directory, so it is no faster than a base backup.
  • After pg_rewind the old primary can start without standby.signal and will follow.

Assumes PostgreSQL 18, where initdb enables data checksums by default; clusters created on 17 or earlier without checksums need wal_log_hints = on before the failover.