Skip to content

Switching the LEADER node

In some maintenance cases, it may be useful to switch the PostgreSQL cluster LEADER node in order to perform operations on it.

Before you can change the LEADER node, please identify the current LEADER node of the PostgreSQL cluster and then connect to it as root.
Once connected as root to the LEADER node, run a command similar to the following to trigger an immediate switchover:

1
patronictl -c /etc/patroni/config.yml failover --candidate PSQL_NODE --force

Replace PSQL_NODE with the name of the node that will become LEADER.

Failure

If the command fails, it is possible that the following settings have not been applied: settings for the patronictl.

Example

For the following PostgreSQL cluster:

1
2
3
4
5
6
7
+ Cluster: 15-cleanroomvault5 ------+---------+---------+----+-----------+
| Member           | Host           | Role    | State   | TL | Lag in MB |
+------------------+----------------+---------+---------+----+-----------+
| PSQL_3           | psql_3         | Leader  | running | 2  |           |
| + PSQL_1         | psql_1         | Replica | running | 2  | 0         |
| + PSQL_2         | psql_2         | Replica | running | 2  | 0         |
+------------------+----------------+---------+---------+----+-----------+

L'objectif est de passer le nœud PSQL_1 en tant que LEADER.
Il est donc d'abord nécessaire de se connecter en tant que root au nœud PSQL_3 qui est l'actuel LEADER.
Une fois connecté, l'exécution de la commande suivante permet la bascule immédiate du LEADER vers le nœud PSQL_1 :

1
patronictl -c /etc/patroni/config.yml failover --candidate PSQL_1 --force

La modification de la topologie est ensuite visible et vérifiable :

1
2
3
4
5
6
7
+ Cluster: 15-cleanroomvault5 ------+---------+---------+----+-----------+
| Member           | Host           | Role    | State   | TL | Lag in MB |
+------------------+----------------+---------+---------+----+-----------+
| PSQL_1           | psql_1         | Leader  | running | 3  |           |
| + PSQL_3         | psql_3         | Replica | running | 3  | 0         |
| + PSQL_2         | psql_2         | Replica | running | 3  | 0         |
+------------------+----------------+---------+---------+----+-----------+