postgrespro/testgres

PostgresNode.safe_psql(filename) does not detect syntax errors

cbandy opened this issue · 6 comments

It took me some time to discover that a syntax error was being not being reported by node.safe_psql(filename='some.sql'). I tracked it down to behavior in psql. It seems worth raising, though I'm not certain it is something that testgres is necessarily responsible for handling.


$ psql --version
psql (PostgreSQL) 9.6.4

and/or

$ psql --version
psql (PostgreSQL) 10.4

psql reports errors differently depending on how input is passed:

  1. $ psql -h 127.0.0.1 -U cbandy -X -A -t -q -c 'whoops' -p 34751 postgres ; echo $?
    ERROR:  syntax error at or near "whoops"
    LINE 1: whoops
            ^
    1
    
  2. N.B. exit code is zero here:

    $ psql -h 127.0.0.1 -U cbandy -X -A -t -q -f <( echo 'whoops' ) -p 34751 postgres ; echo $?
    psql:/dev/fd/63:1: ERROR:  syntax error at or near "whoops"
    LINE 1: whoops
            ^
    0
    
  3. $ psql -h 127.0.0.1 -U cbandy -X -A -t -q -v ON_ERROR_STOP=1 -f <( echo 'whoops' ) -p 34751 
    postgres ; echo $?
    psql:/dev/fd/63:1: ERROR:  syntax error at or near "whoops"
    LINE 1: whoops
            ^
    3
    

Hi Chris,

Thanks for the report! I'll definitely fix this.

This is really odd, but we should provide a workaround nevertheless. I guess it's worth starting a thread in pgsql-hackers.

ON_ERROR_STOP=1

Now the question is whether we should add this option to psql's args by default. I'd like to, but this might break some other user's expectations.

EDIT: at first I didn't notice you mentioning ON_ERROR_STOP.

ildus commented

I suggest to change environment in safe_psql and keep old behavior in psql.

Fixed in 1.8.2.

Thank you for such a quick response!