skip to content

In a planned PostgreSQL 18 switchover for a ticketing platform, in what order do you swap the primary and its streaming standby so nothing committed is lost and the old primary rejoins without pg_rewind?

level: seniorimportance: should knowfreq 38%

answer

  1. stop before you promote
  2. fast, never immediate
  3. shutdown checkpoint reaches the standby
  4. compare checkpoint location with replay LSN
  5. standby.signal on the old primary

basics

~20 s

Confirm the standby is streaming and caught up, stop the primary with a fast shutdown, check the standby replayed the primary's shutdown checkpoint, promote it, then start the old primary with standby.signal and primary_conninfo pointing at the new one.

solid answer

~50 s

Order is what makes it lossless. First check `pg_stat_replication` on the primary: the standby must be `streaming` with `replay_lsn` close to `sent_lsn`. Drain writes, then stop the primary with `pg_ctl stop -m fast`: after clients are gone the checkpointer writes a **shutdown checkpoint**, and each walsender sends all remaining WAL, that record included, and waits for its connected standby to confirm before exiting. Read `Latest checkpoint location` with `pg_controldata` on the old primary and verify `pg_last_wal_replay_lsn()` on the standby is past it. Only then promote with `pg_ctl promote` or `pg_promote()`. The old primary's WAL ends at that checkpoint, before the fork, so you add `standby.signal` and `primary_conninfo` pointing at the new primary, start it, and it follows the new timeline because `recovery_target_timeline` defaults to `latest`. Promoting first, or stopping with `-m immediate`, risks lost commits and a rewind.

code

bash · 13 lines
bash
# on pg-tix-a (primary): is pg-tix-b streaming and caught up?
psql -d tickets -c 'SELECT application_name, state, sent_lsn, replay_lsn FROM pg_stat_replication;'
# clean shutdown; walsenders ship the shutdown checkpoint first
pg_ctl stop -D /var/lib/postgresql/18/main -m fast
pg_controldata /var/lib/postgresql/18/main | grep -E 'cluster state|Latest checkpoint location'

# on pg-tix-b: replayed past that location? then promote
psql -d tickets -c 'SELECT pg_last_wal_replay_lsn();'
psql -d tickets -c 'SELECT pg_promote();'

# on pg-tix-a: set primary_conninfo to host=pg-tix-b in its config, then
touch /var/lib/postgresql/18/main/standby.signal
pg_ctl start -D /var/lib/postgresql/18/main

go deeper

for a junior

Remember the rule of thumb: stop the old primary cleanly before you promote the standby, never the other way round.

for a middle

Explain what a fast shutdown sends to connected standbys, how to compare the old primary's checkpoint location with the standby's replay LSN, and how the old primary rejoins.

for a senior

Run it as a checked procedure: drain writes, verify every step, know the abort path when the standby is behind, and repoint clients, other standbys and slots.

for a principal

Decide how switchovers are practised and who owns them, balancing a hand-run checklist against an HA manager, and how often to rehearse so a failover is not the first test.

## Why the order matters A planned **switchover** swaps roles on purpose: the standby becomes the primary and the old primary becomes its standby, typically so a host can be patched or resized. Unlike a failover, both servers are healthy, so the bar is higher: **zero lost commits**, and an old primary that rejoins without `pg_rewind` or a rebuild. Core PostgreSQL has no single switchover command, so the guarantee comes from running a few ordinary commands in the right order. Promote first, or stop the primary abruptly, and you get a window with two writable servers or a standby missing the last commits. ## The sequence 1. **Check the standby is streaming and caught up.** On the primary, `pg_stat_replication` should show it with `state = streaming` and `replay_lsn` close to `sent_lsn`. A standby that is disconnected when the primary stops will not receive the final WAL. 2. **Drain writes.** Pause the ticket-sales workers or put the application into maintenance, so buyers do not see errors mid-checkout and clients stop hammering a server that is going away. 3. **Stop the primary with a fast shutdown**, `pg_ctl stop -m fast` (fast is the default mode). Once regular sessions are gone the checkpointer writes a **shutdown checkpoint**; each walsender then sends all outstanding WAL, that record included, and waits for its connected standby to confirm before exiting. 4. **Prove the standby has everything.** On the stopped primary, `pg_controldata` should show `Database cluster state: shut down` and a `Latest checkpoint location`. On the standby, `pg_last_wal_replay_lsn()` must be past that location. 5. **Promote the standby** with `pg_ctl promote` or `SELECT pg_promote();`. It starts a new timeline whose switch point lies just after the old primary's last record. 6. **Rejoin the old primary as a standby.** Create `standby.signal` in its data directory, set `primary_conninfo` to the new primary, and start it. Its WAL ends exactly at the shutdown checkpoint, before the fork, so it fetches the new history file and follows the new timeline under the default `recovery_target_timeline = 'latest'`. 7. **Repoint clients and other standbys.** Applications with a libpq multi-host string and `target_session_attrs=read-write` find the new primary on reconnect; any other standby needs `primary_conninfo` changed to the new primary and a reload. ## The checks, side by side | Check | Where | Good result | |---|---|---| | Standby streaming | `pg_stat_replication` on the primary | `state = streaming`, small gap between `sent_lsn` and `replay_lsn` | | Primary stopped cleanly | `pg_controldata` on the old primary | cluster state `shut down` | | Nothing missing | `pg_last_wal_replay_lsn()` on the standby | past the old primary's `Latest checkpoint location` | | Promotion done | `pg_is_in_recovery()` on the new primary | `false` | | Old primary following | `pg_stat_replication` on the new primary | the old primary listed as `streaming` | ## Why fast and not smart or immediate - **Smart** waits for every client session to disconnect by itself, which can stall a maintenance window indefinitely. - **Fast** rolls back open transactions, disconnects clients and shuts down cleanly with a shutdown checkpoint that the walsenders deliver to connected standbys. - **Immediate** aborts every process with no shutdown checkpoint and no final WAL send; the next start runs crash recovery. The last commits may never reach the standby, and the old primary's WAL can extend past what the standby replayed, which is exactly the divergence that forces `pg_rewind`. ## What can still go wrong - **The standby was disconnected** when the primary stopped, so step 4 fails. Do not promote: start the old primary again as the primary, repair replication and retry. - **Promoting before stopping the primary** leaves both writable for a moment; any write to the old one diverges and needs a rewind later. - **Rejoining without `standby.signal`** starts the old primary as a second primary on the old timeline. - **Replication slots do not follow promotion.** A physical slot the old primary kept for the standby stays behind; if you use slots, create one on the new primary for the rejoined node (slots have their own leaf). Patroni automates this same order with its own leader lock and checks; that belongs to the Patroni tree. Moving a VIP or proxy during the switch belongs to the connection-routing concept leaf.

  • Why not stop the PostgreSQL primary with pg_ctl stop -m immediate to make the switchover faster?
    Immediate aborts the server without a shutdown checkpoint and without the walsenders' final send. The standby can miss the last commits, and the old primary's WAL may run past what the standby replayed, so after promotion it must be rewound instead of simply rejoining. The seconds saved cost a lossless switchover.
  • On the standby, pg_last_wal_replay_lsn() is behind the old primary's Latest checkpoint location. What now?
    Do not promote. The standby is missing WAL, usually because it was disconnected at shutdown. Start the old primary again as the primary, find why replication stalled in `pg_stat_replication` and the standby's log, and retry once it streams. Promoting anyway loses those commits and forces a rewind of the old primary.
  • During a PostgreSQL switchover, what must you do about a second standby that streamed from the old primary?
    Change its `primary_conninfo` to the new primary and reload; the parameter is reloadable. If it was connected when the primary stopped, it received the same WAL up to the shutdown checkpoint; since `recovery_target_timeline` defaults to `latest`, it fetches the new history file and follows the new timeline without a rebuild.

  • Promote the standby first, then stop the old primary.
  • pg_ctl stop -m immediate is the safest way to stop the primary before promotion.
  • After any switchover the old primary always needs pg_rewind before it can follow.
  • pg_ctl has a switchover mode that swaps primary and standby roles.
  • Only a smart shutdown lets the standby receive the primary's final WAL.