Sub topic 14
Timelines and Rejoining the Old Primary with `pg_rewind`
Assume that you understand WAL, physical streaming replication, pg_basebackup, and how to start a hot standby. This chapter focuses on what happens after a standby is promoted and how to reuse the former primary safely.
Why failover creates a new timeline
A PostgreSQL timeline identifies a branch of WAL history. A log sequence number (LSN) alone is not always enough to identify a WAL record because two branches can use the same LSN range after a promotion.
Suppose server A is the primary on timeline 1. Server B receives its WAL and is then promoted after A fails. B starts writing on timeline 2. PostgreSQL records this change in a timeline history file such as 00000002.history.
The old primary contains WAL that does not exist on timeline 2. Starting it as a writable server would create a second branch and could cause split-brain writes. A failover procedure must fence, or isolate, the old primary before promoting another server.
| State | Timeline | Safe role |
|---|---|---|
| Former primary after failover | 1 | Stopped until rewound |
| Promoted standby | 2 | Read-write primary |
| Rejoined former primary | 2 | Standby |
Requirements for `pg_rewind`
pg_rewind makes the old primary look like a standby of the new primary. It copies changed blocks and removes the old branch's divergent changes instead of copying the entire database again.
The target server must meet these conditions:
- It is stopped before
pg_rewindruns. - It shares a common checkpoint history with the new primary.
full_page_writeswas enabled when the divergent changes were made.- Data checksums were enabled, or
wal_log_hintswas enabled on the target. - Required WAL and timeline history are still available, either locally or through WAL archiving.
Check the target's configuration before a planned failover:
SHOW full_page_writes;
SHOW wal_log_hints;Data checksums are normally chosen when the cluster is initialized. They cannot be enabled later by changing a configuration setting.
Running `pg_rewind`
After stopping and fencing the old primary, run pg_rewind against its data directory. The source is the new primary. Store credentials in a protected .pgpass file rather than placing a password in the command line.
# Run on the stopped former primary.
export PGDATA=/var/lib/postgresql/data
pg_rewind \
--target-pgdata="$PGDATA" \
--source-server="host=new-primary.example.com port=5432 user=rewind_user dbname=postgres application_name=rewind" \
--progress \
--write-recovery-confThe --write-recovery-conf option, also written as -R, creates standby.signal and writes connection settings so the target can follow the source after it starts. Review the generated settings. Confirm that the connection points to the promoted primary and that any replication slot name is correct.
If required WAL has been archived, add --restore-target-wal and ensure the target's restore_command can retrieve it:
pg_rewind \
--target-pgdata="$PGDATA" \
--source-server="host=new-primary.example.com user=rewind_user dbname=postgres" \
--restore-target-wal \
--progress \
--write-recovery-confStart the rewound server as a standby, then verify that it enters recovery:
pg_ctl -D "$PGDATA" start
psql -d postgres -c "SELECT pg_is_in_recovery();"The result should be t. Monitor the new primary's pg_stat_replication and confirm that the rejoined server appears with the expected application_name.
Exercise
Why must the old primary be stopped before pg_rewind runs?
Show answer
It must not accept new writes while its divergent timeline is being removed. Otherwise it could continue creating a conflicting branch and cause split-brain behavior.