Sub topic 11
Promoting a Standby and Planning Failover
This chapter assumes you can configure PostgreSQL, use psql, understand WAL, and have a streaming primary and standby running. It also assumes you can stop a server and control client routing through DNS, a virtual IP, or a connection pool.
Promotion creates a new primary
A standby normally replays WAL and accepts read-only queries. Promotion ends recovery and makes the standby read-write:
-- Run on the standby
SELECT pg_promote();You can also promote it from the operating system:
pg_ctl -D /var/lib/postgresql/data promotePromotion does not copy data back to the old primary. PostgreSQL stops recovery, starts accepting writes, and creates a new timeline. A timeline identifies a branch of WAL history. The new primary's WAL belongs to the new timeline, even though it began with the old primary's data.
Check the result on the promoted server:
SELECT pg_is_in_recovery(); -- false means this server is primary
SELECT timeline_id, parent_timeline_id, switchpoint
FROM pg_control_checkpoint();The exact timeline details can also be inspected with pg_controldata from the PostgreSQL installation.
Prevent split brain
The most dangerous failover error is promoting a standby while the old primary can still accept writes. Both servers then behave as primaries. They create different changes that PostgreSQL cannot merge automatically. This condition is called split brain.
Fence the old primary before promotion. Fencing means making sure it cannot serve writes. Depending on your infrastructure, fencing may include:
- Stopping PostgreSQL on the old primary.
- Removing its network access.
- Revoking its storage or compute access.
- Moving a virtual IP or load-balancer target.
- Confirming that clients no longer connect to it.
A monitoring system should not promote a standby based only on a temporary network failure. The old primary might still be healthy and serving clients. Use an external consensus or fencing system when automatic failover is required. PostgreSQL itself does not elect a new primary.
A controlled failover sequence
A practical failover runbook has this order:
- Detect the failure and decide whether failover is necessary.
- Fence the old primary, or verify that it is definitely unavailable.
- Choose the standby with the most recent replayed WAL.
- Promote that standby.
- Move client traffic to the new primary.
- Verify that applications can write and that the new primary is healthy.
- Reconfigure remaining standbys to follow the new primary.
- Record the event and investigate the original failure.
Asynchronous replication can lose transactions that the standby had not replayed when the primary failed. Synchronous replication reduces that risk, but it can make commits wait for a standby and can reduce availability when the standby is unavailable.
| Replication mode | Main failover property |
|---|---|
| Asynchronous | Lower commit latency, but recent commits may be missing after failure |
| Synchronous | More durable remote commits, but commit availability depends on synchronous standbys |
Rejoining the old primary
After promotion, do not start the old primary as though nothing happened. Its data belongs to the previous timeline. Starting it independently can create split brain or cause it to diverge further.
If the old primary was cleanly stopped and the required WAL history is available, pg_rewind can often make it follow the new primary. Otherwise, take a fresh base backup from the new primary. Rejoining also requires new connection settings, a replication slot if you use one, and a standby.signal file.
Exercise
Why must you fence the old primary before promoting a standby?
Show answer
Without fencing, both servers can accept writes on separate timelines, creating split brain and changes that PostgreSQL cannot merge automatically.