Manager Handbook for Centralized AntDB-T — P3
Published: 2023-08-14 07:16:18

Host/standby switchover
During the operation of the database, the database administrator may need to manually do a host/standby switch of the database nodes. For example, if the database node needs to restore the original host/standby role after the host/standby failover, or it is suspected that hardware failure requires manual host/standby switchover. You can achieve the host/standby switchover manually through switchover or failover.

Operation steps

Use the command patronictl -c /etc/patroni.yml switchover to manually switch between host and standby.

```
[adbpuq@host-10-19-28-129 ~]$ patronictl -c /etc/patroni.yml switchover
Master [pgnode02]: pgnode02                                                 #Input the current primary node.
Candidate ['pgnode01', 'pgnode03'] []: pgnode01                             #Input the current synchronous standby node.（Sync Standby）
When should the switchover take place (e.g. 2022-12-27T12:14 )  [now]:
Current cluster topology
+----------+--------------------+--------------+---------+-----+-----------+
| Member   | Host               | Role         | State   |  TL | Lag in MB |
+ Cluster: postgres-cluster (7171609184693121315) -------+-----+-----------+
| pgnode01 | 10.19.28.129:55551 | Sync Standby | running | 465 |         0 |
| pgnode02 | 10.19.36.206:55551 | Leader       | running | 465 |           |
| pgnode03 | 10.19.36.207:55551 | Replica      | running | 465 |         0 |
+----------+--------------------+--------------+---------+-----+-----------+
Are you sure you want to switchover cluster postgres-cluster, demoting current master pgnode02? [y/N]: y

#Check the result of the master/standby switch.：
[adbpuq@host-10-19-28-129 ~]$ patronictl -c /etc/patroni.yml list
+----------+--------------------+--------------+----------+-----+-----------+
| Member   | Host               | Role         | State    |  TL | Lag in MB |
+ Cluster: postgres-cluster (7171609184693121315) --------+-----+-----------+
| pgnode01 | 10.19.28.129:55551 | Sync Standby | running  | 465 |         0 |
| pgnode02 | 10.19.36.206:55551 | Leader       | stopping |     |           |
| pgnode03 | 10.19.36.207:55551 | Replica      | running  | 465 |         0 |
+----------+--------------------+--------------+----------+-----+-----------+

#The leader switched from "pgnode02" to "pgnode01".
[adbpuq@host-10-19-28-129 ~]$ patronictl -c /etc/patroni.yml list
+----------+--------------------+--------------+---------+-----+-----------+
| Member   | Host               | Role         | State   |  TL | Lag in MB |
+ Cluster: postgres-cluster (7171609184693121315) -------+-----+-----------+
| pgnode01 | 10.19.28.129:55551 | Leader       | running | 466 |           |
| pgnode02 | 10.19.36.206:55551 | Replica      | running | 466 |         0 |
| pgnode03 | 10.19.36.207:55551 | Sync Standby | running | 466 |         0 |
+----------+--------------------+--------------+---------+-----+-----------+
```
Use the command patronictl -c /etc/patroni.yml failover to manually switch between host and standby.

```
[adbpuq@host-10-19-28-129 ~]$ patronictl -c /etc/patroni.yml failover
Candidate ['pgnode02', 'pgnode03'] []: pgnode03            #Input the current synchronous standby node.（Sync Standby）              
Current cluster topology
+----------+--------------------+--------------+---------+-----+-----------+
| Member   | Host               | Role         | State   |  TL | Lag in MB |
+ Cluster: postgres-cluster (7171609184693121315) -------+-----+-----------+
| pgnode01 | 10.19.28.129:55551 | Leader       | running | 466 |           |
| pgnode02 | 10.19.36.206:55551 | Replica      | running | 466 |         0 |
| pgnode03 | 10.19.36.207:55551 | Sync Standby | running | 466 |         0 |
+----------+--------------------+--------------+---------+-----+-----------+
Are you sure you want to failover cluster postgres-cluster, demoting current master pgnode01? [y/N]: y

#Check the result of the master/standby switch:：
[adbpuq@host-10-19-28-129 ~]$ patronictl -c /etc/patroni.yml list
+----------+--------------------+--------------+----------+-----+-----------+
| Member   | Host               | Role         | State    |  TL | Lag in MB |
+ Cluster: postgres-cluster (7171609184693121315) --------+-----+-----------+
| pgnode01 | 10.19.28.129:55551 | Leader       | stopping |     |           |
| pgnode02 | 10.19.36.206:55551 | Replica      | running  | 466 |         0 |
| pgnode03 | 10.19.36.207:55551 | Sync Standby | running  | 466 |         0 |
+----------+--------------------+--------------+----------+-----+-----------+
[adbpuq@host-10-19-28-129 ~]$ patronictl -c /etc/patroni.yml list
+----------+--------------------+--------------+---------+-----+-----------+
| Member   | Host               | Role         | State   |  TL | Lag in MB |
+ Cluster: postgres-cluster (7171609184693121315) -------+-----+-----------+
| pgnode01 | 10.19.28.129:55551 | Replica      | stopped |     |   unknown |
| pgnode02 | 10.19.36.206:55551 | Sync Standby | running | 467 |         0 |
| pgnode03 | 10.19.36.207:55551 | Leader       | running | 467 |           |
+----------+--------------------+--------------+---------+-----+-----------+

#The leader switched from "pgnode01" to "pgnode03".
[adbpuq@host-10-19-28-129 ~]$ patronictl -c /etc/patroni.yml list
+----------+--------------------+--------------+---------+-----+-----------+
| Member   | Host               | Role         | State   |  TL | Lag in MB |
+ Cluster: postgres-cluster (7171609184693121315) -------+-----+-----------+
| pgnode01 | 10.19.28.129:55551 | Replica      | running | 467 |         0 |
| pgnode02 | 10.19.36.206:55551 | Sync Standby | running | 467 |         0 |
| pgnode03 | 10.19.36.207:55551 | Leader       | running | 467 |           |
+----------+--------------------+--------------+---------+-----+-----------+
```