Switchover from leader to replica in ADPG

As mentioned in ADPG architecture overview, ADPG uses Patroni to build a cluster based on the PostgreSQL streaming replication and implement High Availability (HA). If you need to switchover from the leader node of an ADPG cluster to a replica, you can do it manually via the patronictl switchover command.

This command can only be used in a healthy cluster that has:

  • leader node;

  • synchronized replica.

Follow the steps below to switchover a leader:

  1. Run the Check cluster action to ensure that the cluster is healthy. If no errors are found, you can proceed to the steps below.

  2. Connect to one of the ADPG service nodes and execute the patronictl list command to display cluster members:

    $ sudo patronictl -c /etc/patroni/patroni.yml list

    where /etc/patroni/patroni.yml is the path to the Patroni configuration file.

    Result:

    + Cluster: test_adpg_cluster (7397708120470393282) ---------+---------+----+-----------+
    | Member                           | Host         | Role    | State   | TL | Lag in MB |
    +----------------------------------+--------------+---------+---------+----+-----------+
    | test_adpg_cluster_eek-adpg-adpg1 | 10.92.40.49  | Leader  | running | 10 |           |
    | test_adpg_cluster_eek-adpg-adpg2 | 10.92.42.110 | Replica | running | 10 |         0 |
    | test_adpg_cluster_eek-adpg-adpg3 | 10.92.42.217 | Replica | running | 10 |         0 |
    +----------------------------------+--------------+---------+---------+----+-----------+
  3. Run the patronictl switchover command. Note that the master option is used because the current Patroni version in ADPG is 2.1.3. In later Patroni versions, the parameter name was changed to primary/leader.

    $ sudo patronictl -c <path to patroni.yml> switchover --master <current leader> --candidate <new leader>

    Use the names of the cluster members from the list above as parameters:

    $ sudo patronictl -c /etc/patroni/patroni.yml switchover --master test_adpg_cluster_eek-adpg-adpg1 --candidate test_adpg_cluster_eek-adpg-adpg3

    The following output will be displayed:

    When should the switchover take place (e.g. 2024-08-02T15:28 )  [now]:

    Press Enter to confirm that the leader should be switched immediately.

    Type y in response to the following message:

    Are you sure you want to switchover cluster test_adpg_cluster, demoting current master test_adpg_cluster_eek-adpg-adpg1? [y/N]:

    Result:

    2024-08-02 14:29:02.22919 Successfully switched over to "test_adpg_cluster_eek-adpg-adpg3"
    + Cluster: test_adpg_cluster (7397708120470393282) ---------+---------+----+-----------+
    | Member                           | Host         | Role    | State   | TL | Lag in MB |
    +----------------------------------+--------------+---------+---------+----+-----------+
    | test_adpg_cluster_eek-adpg-adpg1 | 10.92.40.49  | Replica | stopped |    |   unknown |
    | test_adpg_cluster_eek-adpg-adpg2 | 10.92.42.110 | Replica | running | 10 |         0 |
    | test_adpg_cluster_eek-adpg-adpg3 | 10.92.42.217 | Leader  | running | 10 |           |
    +----------------------------------+--------------+---------+---------+----+-----------+
  4. Run the following command to make sure that the leader has been changed:

    $ sudo patronictl -c /etc/patroni/patroni.yml list

    The Role column displays if a node is a leader or a replica:

    + Cluster: test_adpg_cluster (7397708120470393282) ---------+---------+----+-----------+
    | Member                           | Host         | Role    | State   | TL | Lag in MB |
    +----------------------------------+--------------+---------+---------+----+-----------+
    | test_adpg_cluster_eek-adpg-adpg1 | 10.92.40.49  | Replica | running | 11 |         0 |
    | test_adpg_cluster_eek-adpg-adpg2 | 10.92.42.110 | Replica | running | 11 |         0 |
    | test_adpg_cluster_eek-adpg-adpg3 | 10.92.42.217 | Leader  | running | 11 |           |
    +----------------------------------+--------------+---------+---------+----+-----------+

    When the switchover command is executed, the timeline (the TL column) grows. Its increase shows that the operation was successful.

  5. Run the Check cluster action to ensure that the cluster works correctly.

Found a mistake? Seleсt text and press Ctrl+Enter to report it