The hook you need is failover_command
.
All the scripts defined in PgPool's configuration - failover_command
, follow_master_command
, failback_command
, etc - are executed by the pgpool master process. You can check which one is master using pcp_watchdog_info
utility.
It means that if you want to execute commands on PostgreSQL node you need to setup password-less auth from pgpool into postgres nodes and use ssh inside the script. To give an example, in my failover.sh
I have this:
#!/bin/bash
node=$1
host=$2
port=$3
pgdata=$4
newmasternode=$5
newmasterhost=$6
oldmasternode=$7
oldprimarynode=$8
newmasterport=$9
newmasterpgdata=${10}
echo "$0 params: node=$node host=$host port=$port pgdata=$pgdata newmasternode=$newmasternode newmasterhost=$newmasterhost oldmasternode=$oldmasternode oldprimarynode=$oldprimarynode newmasterport=$newmasterport newmasterpgdata=$newmasterpgdata"
ssh_opts="-o BatchMode=yes -o StrictHostKeyChecking=no -o CheckHostIP=no -o ConnectTimeout=10"
echo "$0 Disabling postgresql on $host"
ssh $ssh_opts -l root $host 'systemctl mask postgresql --now'
if [ $newmasternode -ne $oldmasternode ]; then
echo "$0 Promoting postgresql on $newmasterhost"
ssh $ssh_opts -l root $newmasterhost 'pg_ctlcluster 10 main promote'
fi