skip to content

With Patroni's patronictl, when do you run switchover instead of failover, and what does each command require and risk?

level: middleimportance: must knowfreq 55%

answer

  1. healthy cluster vs unhealthy cluster
  2. a clean demotion with a checkpoint
  3. one command insists on --candidate
  4. only one of them can be scheduled

basics

~20 s

patronictl switchover is for a healthy cluster: it cleanly demotes the current leader and promotes a chosen or best replica. patronictl failover is for an unhealthy one: it needs --candidate, works without a leader, and can lose data.

solid answer

~40 s

Use `patronictl switchover` for planned work on a healthy cluster: it needs a current leader (named with `--leader` or `--primary`, which replaced `--master` in 4.0), takes an optional `--candidate`, and can be timed with `--scheduled`. The old primary stops with a shutdown checkpoint and hands over the leader key once a replica has that WAL, so a switchover to a caught-up replica loses no commits. Use `patronictl failover` when the cluster is unhealthy — no leader, or no synchronous standby available: it insists on `--candidate`, cannot be scheduled, and promoting a lagging replica can lose data. Members tagged `nofailover` are never candidates; with `synchronous_mode: on` a switchover target must be a synchronous standby, and a failover asks before choosing an asynchronous one. Run `patronictl list` before and after to check roles, timeline and lag.

code

bash · 9 lines
bash
# Planned: move the payment gateway's primary from DC1 to DC2 at 02:00 UTC
patronictl -c /etc/patroni/patroni.yml switchover paygw \
  --leader pay-dc1-a --candidate pay-dc2-a --scheduled 2026-10-10T02:00+00:00

# Unplanned: no leader, promote a named replica without prompts
patronictl -c /etc/patroni/patroni.yml failover paygw --candidate pay-dc1-b --force

# Check roles, timeline and receive/replay lag afterwards
patronictl -c /etc/patroni/patroni.yml list paygw

go deeper

for a junior

Remember that switchover is the planned move on a healthy cluster and failover the emergency move, and that patronictl list shows who is leader.

for a middle

Explain the required options of each command, the graceful demotion with a shutdown checkpoint, and why a failover to a lagging replica can lose commits.

for a senior

Show the guard rails: nofailover tags, sync-standby-only switchovers, the async-candidate prompt, pause rules, and making CDC slots permanent so they survive.

for a principal

Decide which moves are automated, which need a human, and how runbooks and scripts are kept in step with Patroni majors such as the 4.0 rename.

## Two commands for two situations `patronictl` is Patroni's command-line client. It reads the DCS and talks to each member's REST API. Both commands move the primary role, but they are built for different cluster states: | | `patronictl switchover` | `patronictl failover` | |---|---|---| | Intended for | healthy cluster with a leader | unhealthy cluster: no leader, or no synchronous standby | | Needs a current leader | yes ('This cluster has no leader' otherwise) | no | | `--candidate` | optional; any healthy replica may win if omitted | required ('Failover could be performed only to a specific candidate') | | `--leader` / `--primary` | names the leader to demote; must match | not accepted since 4.0 | | `--scheduled` | ISO 8601 timestamp or `now`; not while paused | not possible | | Data-loss risk | none for a caught-up target | depends on how far the candidate lags | The documentation is explicit: nothing stops you running `failover` on a healthy cluster, but `switchover` is the command for that case. ## What a switchover does 1. `patronictl` prints the current topology and validates the request: the named leader is the real one, the candidate exists and is not tagged `nofailover`, and under `synchronous_mode: on` the candidate is a synchronous standby (otherwise the API answers 'candidate name does not match with sync_standby'). 2. It sends `POST /switchover` to the leader's REST API; if the API cannot be reached it falls back to writing the request into the DCS `/failover` key. 3. The leader performs a **graceful demotion**: a fast shutdown with a checkpoint. Once a replica has received the shutdown checkpoint, the leader releases the key. 4. The candidate takes the key and promotes, the timeline (`TL`) goes up by one, and the other members follow it. The old primary rejoins as a replica; whether it needs `pg_rewind` is a core PostgreSQL topic. ## Guard rails worth knowing - **Tags:** `nofailover: true`, or `failover_priority` of 0 or less, removes a member from every candidate list. - **Synchronous mode:** a failover to an asynchronous node prompts 'Are you sure you want to failover to the asynchronous node …?'; `--force` skips that prompt and every other one, which is why scripts must choose the candidate carefully. - **Maintenance mode:** after `patronictl pause`, a switchover must name an explicit candidate and cannot be scheduled. - **Quorum mode:** the 'must be a sync standby' check does not apply; Patroni instead verifies the candidate is not behind the quorum. ## Reading patronictl list `patronictl list paygw` shows one row per member with `Member`, `Host`, `Role` (`Leader`, `Standby Leader`, `Sync Standby`, `Replica`), `State` (`running`, `streaming`, `stopped`, `crashed` …) and `TL`. Since 4.1 it also shows **Receive LSN**, **Replay LSN** and the lag of each, which is what you check before choosing a failover candidate. `-e` adds pending-restart and tags columns, and `-W` refreshes every two seconds during the operation. After the move, `patronictl history` lists each timeline switch with its LSN and timestamp, which is the record to attach to a change ticket. A short pre-switchover checklist for the payment cluster: - the candidate shows `streaming` with zero or near-zero receive and replay lag; - no member shows a pending restart that the switchover would trigger at a bad moment; - the load balancer's write check follows `/primary`, so clients move with the role. ## What survives the move: replication slots - With `use_slots` on (the default), Patroni keeps a **physical slot for each member** on every node that could become leader, so replicas can resume streaming from the new primary. When a member's key disappears, its slot is kept for `member_slots_ttl` (default `30min`, added in 4.0). - A **logical slot** that a CDC tool created by hand exists only on the old primary. Worse, Patroni drops inactive slots it does not know about unless `ignore_slots` matches them. - Declare such a slot under the `slots` dynamic setting as a **permanent logical slot**: Patroni creates it, copies it to replicas and advances it every `loop_wait`, so it is there after a switchover. The copy may trail slightly, so a consumer can receive some changes twice; it should track `confirmed_flush_lsn`. - Patroni 4.1 leaves alone slots created with `failover = true` on PostgreSQL 17 and later, so PostgreSQL's own slot synchronisation can manage them. ## The 4.0 rename Patroni 4.0 finished removing 'master': `patronictl` no longer accepts `--master` (use `--leader` or `--primary`), the REST API rejects `role=master` on `/switchover`, `/failover` and `/restart`, the role is reported and written to the DCS as `primary`, and callbacks receive `role=primary`. Runbooks and scripts written for Patroni 3 need those edits before an upgrade.

  • What happens to a CDC consumer's logical replication slot when a Patroni switchover moves the primary?
    If the slot was created by hand it exists only on the old primary, and Patroni drops inactive slots it does not know about unless `ignore_slots` matches. Declared under `slots` as a permanent logical slot, it is copied to replicas and advanced every `loop_wait`, so it exists on the new primary; the consumer should expect a few repeated changes and deduplicate by LSN.
  • A switchover in a cluster with synchronous_mode: on is rejected with 'candidate name does not match with sync_standby'. What does that mean?
    The chosen candidate is not listed as a synchronous standby in the DCS `/sync` key. In that mode Patroni only lets a synchronous standby take over in a switchover, because only it is known to hold every acknowledged commit. Pick the member `patronictl list` shows as `Sync Standby`, or omit `--candidate`; quorum mode does not apply this check.

  • patronictl failover is the right command for planned maintenance on a healthy cluster.
  • patronictl switchover --master pay-dc1-a still works in Patroni 4.
  • A switchover without --candidate fails because Patroni cannot choose a target.
  • A failover can be scheduled for a maintenance window with --scheduled.
  • Logical slots created by a CDC tool follow the primary automatically after a switchover.

Assumes Patroni 4.1: 4.0 removed patronictl's --master option (use --leader or --primary) and 4.1 added receive and replay LSN and lag columns to patronictl list.