1

postgres 9.6 Debian GNU/Linux 9

is use following simplyfied bash script wich gets called from a users crontab, myid is a passed parameter:

#!/bin/bash
...
PGPASSFILE="/home/user/.pgpass"
PGCONNECT=" psql -U myuser -w -h 000.000.000.000 -d mydatabase -p 5432"
PGCOMMAND1="SELECT something, somemmore FROM schema.table WHERE id= ${myid}"
PGRESULT=`$PGCONNECT -A -F ";" -t -c "$PGCOMMAND1"`
RETCODE1=$?
...
PGCOMMAND2="SELECT schema.functionname(${myid});"
PGRESULT=`$PGCONNECT -A -F ";" -t -c "$PGCOMMAND2"`
RETCODE2=$?

if [ $RETCODE1 -eq 0 ] &&  [ $RETCODE2 -eq 0 ]; then
          mailx -s sendme_asuccessemail
else
          echo -e "ret1: $RETCODE1\n" >> $MAIL_FILE
          echo -e "ret2: $RETCODE2\n" >> $MAIL_FILE
          mailx -s sendme_errormailwith retcode1=2
fi

and most of the time all is ok ( the script runs 60 times a day to different times ). once per week or less i get a errormail with psql exit status 2 in RETCODE1 as a result of the select.

from the manpage:

 2 if the connection to the server went bad and the session was not
       interactive

it may happen, this script is called per usercrontab, that it gets executeted twice or more on the same time with different myid. The second command always gets called, which does the real work.

How can i narrow down the error ? What does the error realy mean ? Can i ignore it ?

FatFreddy
  • 125
  • 1
  • 5
  • This would happen when the PostgreSQL server is forced to quit (OOM killer, restart...) or one backend otherwise crashed. You want to see what's in the PostgreSQL server logs when the problem occurs. – Daniel Vérité Sep 07 '19 at 09:40
  • @Daniel Vérité well, thats the problem i dont see any message or errors on the server. Memory is satisfying, 128GB, 7GB used, swap not used at all. – FatFreddy Sep 10 '19 at 06:15

1 Answers1

1

psql exit code 2 can be ignored if it is not too frequent, reason could be network or db health. To dig more you can add debug steps to script which shows network connectivity and db health(server is busy and loaded with queries) before doing real work.

asktyagi
  • 2,860
  • 2
  • 8
  • 25
  • not realy the answer i hoped to get, after some testing with stderr und stout it seems that, well, something got lost. i solved it something like that, if there is a retcode = 2 , sleep 2 secs and do the select again, if theres still a retcode 2, well, ignore it. thank you – FatFreddy Sep 05 '19 at 12:32