0

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.

2
  • You should copy/paste your code into shellcheck.net and fix the issues it will tell you about. It also has issues that shellcheck won't tell you about so after fixing those it might be worth posting your code in codereview.stackexchange.com
    – Ed Morton
    Commented Oct 23, 2024 at 10:48
  • For example, 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. Your sed "s/^ //" is doing nothing by the way, awk would ignore the leading blanks anyway when identifying $3.
    – Ed Morton
    Commented Oct 23, 2024 at 10:55

1 Answer 1

3

Yes, you will have all 2 kB of these functions in memory.

Worrying about whether bash uses 2 kB of RAM, i.e., less than a memory page, on a database server, is very silly.

Declare these functions wherever convenient.

As usual for any performance considerations, the null hypothesis is "it doesn't matter". You can assume that safely, unless it uses a considerable amount of your overall RAM, or if it happens many thousand times per second. These shell functions are orders of magnitude below either criterion, so they don't matter.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.