Search code examples
bashpostgresqlshellpartitioningzabbix

Zabbix Partition Cleanup Script with PostgreSQL returns blank line after checking for being in recovery


Ok, so to begin with I must point out that I'm using community work here, so forgive any misunderstanding. That said, we have a database server with PostgreSQL 9.6 and a Zabbix server running 3.2 that writes to that database. We've setup partitioning based on the following documentation: https://www.zabbix.org/wiki/Higher_performant_partitioning_in_PostgreSQL

That's all well and good, the data is writing to the correct tables and all tables are created. We're now at the point where we're about to reach our drop period and we're trying to get the maintenance script setup and running. I use the script (with a couple alterations due to errors) from that page. The script is setup as such to make sure that the DB isn't in recovery mode, so it executes a proc that returns t/f. The proc works, if I execute it from shell manually I get a f as I would expect. This is where the problem comes in, it also returns a blank line after the line with the f. The script is the following on my system:

#!/bin/bash
# . ~/lib/pgIsInRecovery.func
pgIsInRecovery () {
local IN_RECOVERY=$(/usr/edb/as9.6/bin/psql -d zabbix -U zabuser -c 'SELECT pg_is_in_recovery()')
#local IN_RECOVERY=$(psql -t -c zabbix -U zabuser 'SELECT pg_is_in_recovery()')
  case ${IN_RECOVERY// /} in
  ( t ) return 0 ;;
  ( f ) return 1 ;;
  esac
}
# . ~/lib/printUsage.func
printUsage () {
  cat <<- EOT
        Usage: ${0} runme
EOT
}

printSql () {
  cat <<- SQL
        \set VERBOSITY 'terse'

        SELECT zabbix_partition_maintenance('1 week', 4, 52, 1);

        \quit
SQL
}

main () {
  case $1 in
  ( runme )
    if ! pgIsInRecovery
    then
      printSql | /usr/bin/psql -U zabuser zabbix
    else
      echo $pgIsInRecovery
      echo "PostgreSQL cluster is in recovery. Exiting."
    fi
    ;;
  ( * )
    printUsage
    return 1
    ;;
  esac
}

main "$@"
exit $?

If I execute the following manually I get the result with the extra line after: /usr/edb/as9.6/bin/psql -d zabbix -U zabuser -c 'SELECT pg_is_in_recovery()'

-bash-4.2$ psql -t -U zabuser -d zabbix -c 'SELECT pg_is_in_recovery()'
 f

-bash-4.2$

Any idea why the blank line is returned at the end, and any idea of a way to work around this with this script, we definitely don't want to be performing our DB maintenance if it happens to be in recovery so I am quite hesitant to remove that validation. I do know that if I execute that proc from in psql I just get the one line result as I would expect.

Thanks in advance for any assistance.

ONE EDIT FOR CLARITY: Just to clarify, this matters because due to the condition check it's not returning not in recovery so the script never proceeds to the step where it would perform the maintenance.


Solution

  • Needed to add -t flag to the psql command to return only tuple.