I want to execute a this function on login as the postgres user
postgres@servcer:~/.bash.d/functions> cat check_pg-instances.func
#!/usr/bin/bash
# ##############################################################################
#
# function to detect all running postgres instances and check them with pg_isready
#
# ##############################################################################
is_primary() {
pg_port=$1
primary=$( psql -p "${pg_port}" -tc "SELECT state FROM pg_stat_replication ;" )
secondary=$(psql -p "${pg_port}" -tc "SELECT client_hostname FROM pg_stat_replication ;" \
| cut -d '.' -f1 | sed "s/^ //" )
}
is_replica() {
pg_port=$1
replica=$(psql -p "${pg_port}" -tc "SHOW primary_conninfo ;" | head -1 | sed "s/^ //" )
main=$( psql -p "${pg_port}" -tc "SHOW primary_conninfo ;" | head -1 | sed "s/^ //" \
| awk {'print $3'} | sed "s/'//g" | cut -d '=' -f 2 | cut -d '.' -f 1 )
}
replication_status() {
if [[ -n ${primary} ]] ; then
echo replicated by \> "${secondary}"
elif [[ -n ${replica} ]] ; then
echo replicating from \< "${main}"
else
echo STANDALONE
fi
}
function check_pg-instances() {
# make sure we are at the right location #################################
cd "$PGBIN" || exit
# variables ##############################################################
local instance_names=($(grep PGPORT= .pg*.env | sort -t '=' -k2,2 | cut -d '.' -f2 | sed "s/^pg-//" ))
local ports=($(grep PGPORT= .pg*.env | cut -d '=' -f2 | sort -t '=' -k2,2 | tr '\n' ' ' ))
local counter=$( echo "${instance_names[@]}" | wc -w )
local count=0
echo ; echo ================================================================================
while [[ "${counter}" -gt 0 ]] ; do
if $(psql -p ${ports[$count]} -l >/dev/null 2>&1) ; then
is_primary ${ports[$count]}
is_replica ${ports[$count]}
fi
echo -e "postgres $( pg_isready -p ${ports[$count]} | sed "s#/tmp:${ports[$count]} - ##" ) | port: ${ports[$count]} | ${instance_names[$count]} \
| $(replication_status)"
counter=$(( "${counter}" - 1 ))
count=$(( "${count}" + 1 ))
done
echo ================================================================================ ; echo
}
currently I handle this in ~/.bashrc
like
# User specific functions ######################################################
test -d ~/.bash.d/functions && . ~/.bash.d/functions/*.func || true
# check postgres instances #####################################################
check_pg-instances
Now my question is whether this is clever or maybe better to put all this (loading the functions/*.func
files and executing check_pg_instances
) into a script and execute that on startup.
I could imagine in the way I am doing it right now I might have that function(s) in memory during the entire session and not only when it is actually needed.
head -1 | sed "s/^ //" | awk {'print $3'} | sed "s/'//g" | cut -d '=' -f 2 | cut -d '.' -f 1
can be a single call to awk:awk 'NR==1{ split($3,f,/=/); $0=f[2]; gsub(/\047/,""); sub(/\..*/,""); print; exit }'
. There is almost certainly a simpler way to do that in awk, I was just translating your existing commands without knowing the input/output. Yoursed "s/^ //"
is doing nothing by the way, awk would ignore the leading blanks anyway when identifying$3
.