Re: Make ON_ERROR_STOP stop on shell script failure - Mailing list pgsql-hackers

From Justin Pryzby
Subject Re: Make ON_ERROR_STOP stop on shell script failure
Date
Msg-id 20221102115801.GG16921@telsasoft.com
Whole thread Raw
In response to Make ON_ERROR_STOP stop on shell script failure  (bt22nakamorit <bt22nakamorit@oss.nttdata.com>)
Responses Re: Make ON_ERROR_STOP stop on shell script failure
List pgsql-hackers
On Fri, Sep 16, 2022 at 03:55:33PM +0900, bt22nakamorit wrote:
> Hi,
> 
> """\set ON_ERROR_STOP on""" stops any subsequent incoming query that comes
> after an error of an SQL, but does not stop after a shell script ran by
> """\! <some command>""" returning values other than 0, -1, or 127, which
> suggests a failure in the result of the shell script.

Actually, I think this could be described as a wider problem (not just
ON_ERROR_STOP).  The shell's exit status is being ignored (except for -1
and 127).

Shouldn't the user be able to do something with the exit status ?
Right now, it seems like they'd need to wrap the shellscript with
"if ! ...; then echo failed; fi"
and then \gset and compare with "failed"

I think it'd be a lot better to expose the script status to psql.
(without having to write "foo; echo status=$?").

Another consideration is that shellscripts can exit with a nonzero
status due to the most recent conditional (like: if || &&).

For example, consider shell command like:
"if foo; then bar; fi" or "foo && bar"

If foo has nonzero status, then bar isn't run.

If that's the entire shell script, the shell will *also* exit with foo's
nonzero status.  (That's the reason why people write "exit 0" as the
last line of a shell script.  It's easy to believe that it was going to
"exit 0" in any case; but, what it was actually going to do was to "exit
$?", and $? can be nonzero after conditionals, even in "set -e" mode).

So a psql script like this would start to report as a failure any time
"foo" was false, even if that's the normal/typical case.

-- 
Justin



pgsql-hackers by date:

Previous
From: Greg Stark
Date:
Subject: Re: Commit fest 2022-11
Next
From: Jehan-Guillaume de Rorthais
Date:
Subject: Re: psql: Add command to use extended query protocol