Make ON_ERROR_STOP stop on shell script failure

Started by bt22nakamoritover 3 years ago29 messageshackers
Jump to latest
#1bt22nakamorit
bt22nakamorit@oss.nttdata.com

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.

For example, suppose that below is an SQL file.
\set ON_ERROR_STOP on
SELECT 1;
\! false
SELECT 2;

The current design allows SELECT 2 even though the shell script returns
a value indicating a failure.

I thought that this action is rather unexpected since, based on the word
"""ON_ERROR_STOP""", ones may expect that failures of shell scripts
should halt the incoming instructions as well.
One clear solution is to let failures of shell script stop incoming
queries just like how errors of SQLs do currently. Thoughts?

Tatsu

Attachments:

stop_error.patchtext/x-diff; name=stop_error.patchDownload+4-0
#2Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: bt22nakamorit (#1)
Re: Make ON_ERROR_STOP stop on shell script failure

At Fri, 16 Sep 2022 15:55:33 +0900, bt22nakamorit <bt22nakamorit@oss.nttdata.com> wrote in

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.

For example, suppose that below is an SQL file.
\set ON_ERROR_STOP on
SELECT 1;
\! false
SELECT 2;

The current design allows SELECT 2 even though the shell script
returns a value indicating a failure.

Since the "false" command did not "error out"?

I thought that this action is rather unexpected since, based on the
word """ON_ERROR_STOP""", ones may expect that failures of shell
scripts should halt the incoming instructions as well.
One clear solution is to let failures of shell script stop incoming
queries just like how errors of SQLs do currently. Thoughts?

I'm not sure we want to regard any exit status from a succssful run as
a failure.

On the other hand, the proposed behavior seems useful to me.

So +1 from me to the proposal, assuming the corresponding edit of the
documentation happens.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#3bt22nakamorit
bt22nakamorit@oss.nttdata.com
In reply to: Kyotaro Horiguchi (#2)
Re: Make ON_ERROR_STOP stop on shell script failure

2022-09-16 17:30 に Kyotaro Horiguchi さんは書きました:

At Fri, 16 Sep 2022 15:55:33 +0900, bt22nakamorit
<bt22nakamorit@oss.nttdata.com> wrote in

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.

For example, suppose that below is an SQL file.
\set ON_ERROR_STOP on
SELECT 1;
\! false
SELECT 2;

The current design allows SELECT 2 even though the shell script
returns a value indicating a failure.

Since the "false" command did not "error out"?

I thought that this action is rather unexpected since, based on the
word """ON_ERROR_STOP""", ones may expect that failures of shell
scripts should halt the incoming instructions as well.
One clear solution is to let failures of shell script stop incoming
queries just like how errors of SQLs do currently. Thoughts?

I'm not sure we want to regard any exit status from a succssful run as
a failure.

On the other hand, the proposed behavior seems useful to me.

So +1 from me to the proposal, assuming the corresponding edit of the
documentation happens.

regards.

Since the "false" command did not "error out"?

"false" command returns 1 which is an exit status code that indicates
failure, but not error.
I think it does not "error out" if that is what you mean.

So +1 from me to the proposal, assuming the corresponding edit of the
documentation happens.

I will work on editing the document and share further updates.

Thank you!
Tatsu

#4bt22nakamorit
bt22nakamorit@oss.nttdata.com
In reply to: bt22nakamorit (#3)
Re: Make ON_ERROR_STOP stop on shell script failure

2022-09-17 09:44 に bt22nakamorit さんは書きました:

2022-09-16 17:30 に Kyotaro Horiguchi さんは書きました:

At Fri, 16 Sep 2022 15:55:33 +0900, bt22nakamorit
<bt22nakamorit@oss.nttdata.com> wrote in

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.

For example, suppose that below is an SQL file.
\set ON_ERROR_STOP on
SELECT 1;
\! false
SELECT 2;

The current design allows SELECT 2 even though the shell script
returns a value indicating a failure.

Since the "false" command did not "error out"?

I thought that this action is rather unexpected since, based on the
word """ON_ERROR_STOP""", ones may expect that failures of shell
scripts should halt the incoming instructions as well.
One clear solution is to let failures of shell script stop incoming
queries just like how errors of SQLs do currently. Thoughts?

I'm not sure we want to regard any exit status from a succssful run as
a failure.

On the other hand, the proposed behavior seems useful to me.

So +1 from me to the proposal, assuming the corresponding edit of the
documentation happens.

regards.

Since the "false" command did not "error out"?

"false" command returns 1 which is an exit status code that indicates
failure, but not error.
I think it does not "error out" if that is what you mean.

So +1 from me to the proposal, assuming the corresponding edit of the
documentation happens.

I will work on editing the document and share further updates.

Thank you!
Tatsu

I edited the documentation for ON_ERROR_STOP.
Any other suggestions?

Tatsu

Attachments:

stop_error.patchtext/x-diff; name=stop_error.patchDownload+7-1
#5Fujii Masao
masao.fujii@gmail.com
In reply to: bt22nakamorit (#4)
Re: Make ON_ERROR_STOP stop on shell script failure

On 2022/09/20 15:15, bt22nakamorit wrote:

I thought that this action is rather unexpected since, based on the
word """ON_ERROR_STOP""", ones may expect that failures of shell
scripts should halt the incoming instructions as well.
One clear solution is to let failures of shell script stop incoming
queries just like how errors of SQLs do currently. Thoughts?

+1

I edited the documentation for ON_ERROR_STOP.
Any other suggestions?

Thanks for the patch!
Could you add it to the next CommitFest so that we don't forget it?

We can execute the shell commands via psql in various ways
other than \! meta-command. For example,

* `command`
* \g | command
* \gx | command
* \o | command
* \w | command
* \copy ... program 'command'

ON_ERROR_STOP should handle not only \! but also all the above in the same way?

One concern about this patch is that some applications already depend on
the current behavior of ON_ERROR_STOP, i.e., psql doesn't stop even when
the shell command returns non-zero exit code. If so, we might need to
extend ON_ERROR_STOP so that it accepts the following setting values.

* off - don't stop even when either sql or shell fails (same as the current behavior)
* on or sql - stop only whensql fails (same as the current behavior)
* shell - stop only when shell fails
* all - stop when either sql or shell fails

Thought?

Regards,

--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION

#6Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Fujii Masao (#5)
Re: Make ON_ERROR_STOP stop on shell script failure

At Wed, 21 Sep 2022 11:45:07 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2022/09/20 15:15, bt22nakamorit wrote:

I thought that this action is rather unexpected since, based on the
word """ON_ERROR_STOP""", ones may expect that failures of shell
scripts should halt the incoming instructions as well.
One clear solution is to let failures of shell script stop incoming
queries just like how errors of SQLs do currently. Thoughts?

+1

I edited the documentation for ON_ERROR_STOP.
Any other suggestions?

Thanks for the patch!
Could you add it to the next CommitFest so that we don't forget it?

We can execute the shell commands via psql in various ways
other than \! meta-command. For example,

* `command`
* \g | command
* \gx | command
* \o | command
* \w | command
* \copy ... program 'command'

ON_ERROR_STOP should handle not only \! but also all the above in the
same way?

+1

One concern about this patch is that some applications already depend
on
the current behavior of ON_ERROR_STOP, i.e., psql doesn't stop even
when
the shell command returns non-zero exit code. If so, we might need to
extend ON_ERROR_STOP so that it accepts the following setting values.

* off - don't stop even when either sql or shell fails (same as the
* current behavior)
* on or sql - stop only whensql fails (same as the current behavior)
* shell - stop only when shell fails
* all - stop when either sql or shell fails

Thought?

+1

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#7Wolfgang Walther
walther@technowledgy.de
In reply to: Fujii Masao (#5)
Re: Make ON_ERROR_STOP stop on shell script failure

Fujii Masao:

One concern about this patch is that some applications already depend on
the current behavior of ON_ERROR_STOP, i.e., psql doesn't stop even when
the shell command returns non-zero exit code. If so, we might need to
extend ON_ERROR_STOP so that it accepts the following setting values.

I just got bitten by this and I definitely consider this a bug. I expect
psql to stop when a shell script fails and I have ON_ERROR_STOP set. I
don't think this should be made more complicated with different settings.

If someone needs to have ON_ERROR_STOP set, but continue execution after
a certain shell command, they could still do something like this:

\! might_fail || true

Best

Wolfgang

#8torikoshia
torikoshia@oss.nttdata.com
In reply to: bt22nakamorit (#4)
Re: Make ON_ERROR_STOP stop on shell script failure

On 2022-09-20 15:15, bt22nakamorit wrote:

I edited the documentation for ON_ERROR_STOP.
Any other suggestions?

Thanks for the patch!

if (result == 127 || result == -1)
{
pg_log_error("\\!: failed");
return false;
}
else if (result != 0) {
pg_log_error("command failed");
return false;

Since it would be hard to understand the cause of failures from these
two messages, it might be better to clarify them in the messages.

The former comes from failures of child process creation or execution on
it and the latter occurs when child process creation and execution
succeeded but the return code is not 0, doesn't it?

I also felt it'd be natural that the latter message also begins with
"\\!" since both message concerns with \!.

How do you think?

--
Regards,

--
Atsushi Torikoshi
NTT DATA CORPORATION

#9bt22nakamorit
bt22nakamorit@oss.nttdata.com
In reply to: torikoshia (#8)
Re: Make ON_ERROR_STOP stop on shell script failure

2022-09-28 21:49 に torikoshia さんは書きました:

if (result == 127 || result == -1)
{
pg_log_error("\\!: failed");
return false;
}
else if (result != 0) {
pg_log_error("command failed");
return false;

Since it would be hard to understand the cause of failures from these
two messages, it might be better to clarify them in the messages.

The former comes from failures of child process creation or execution
on it and the latter occurs when child process creation and execution
succeeded but the return code is not 0, doesn't it?

I also felt it'd be natural that the latter message also begins with
"\\!" since both message concerns with \!.

How do you think?

Thank you for the feedback!
I agree that the messages should be more clear.
\\!: command was not executed
\\!: command failed
Would these two messages be enough to describe the two cases?

Tatsu

#10Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: bt22nakamorit (#9)
Re: Make ON_ERROR_STOP stop on shell script failure

At Thu, 29 Sep 2022 11:29:40 +0900, bt22nakamorit <bt22nakamorit@oss.nttdata.com> wrote in

2022-09-28 21:49 に torikoshia さんは書きました:

if (result == 127 || result == -1)
{
pg_log_error("\\!: failed");
return false;
}
else if (result != 0) {
pg_log_error("command failed");
return false;

Since it would be hard to understand the cause of failures from these
two messages, it might be better to clarify them in the messages.
The former comes from failures of child process creation or execution
on it and the latter occurs when child process creation and execution
succeeded but the return code is not 0, doesn't it?
I also felt it'd be natural that the latter message also begins with
"\\!" since both message concerns with \!.
How do you think?

Thank you for the feedback!
I agree that the messages should be more clear.
\\!: command was not executed
\\!: command failed
Would these two messages be enough to describe the two cases?

FWIW, I would spell these as something like this:

\\!: command execution failure: %m
\\!: command returned failure status: %d

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#11Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Kyotaro Horiguchi (#10)
Re: Make ON_ERROR_STOP stop on shell script failure

At Thu, 29 Sep 2022 12:35:04 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in

Thank you for the feedback!
I agree that the messages should be more clear.
\\!: command was not executed
\\!: command failed
Would these two messages be enough to describe the two cases?

FWIW, I would spell these as something like this:

\\!: command execution failure: %m

The following might be more complient to our policy.

\\!: failed to execute command \"%s\": %m

\\!: command returned failure status: %d

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#12bt22nakamorit
bt22nakamorit@oss.nttdata.com
In reply to: Fujii Masao (#5)
Re: Make ON_ERROR_STOP stop on shell script failure

2022-09-21 11:45 に Fujii Masao wrote:

We can execute the shell commands via psql in various ways
other than \! meta-command. For example,

* `command`
* \g | command
* \gx | command
* \o | command
* \w | command
* \copy ... program 'command'

ON_ERROR_STOP should handle not only \! but also all the above in the
same way?

One concern about this patch is that some applications already depend
on
the current behavior of ON_ERROR_STOP, i.e., psql doesn't stop even
when
the shell command returns non-zero exit code. If so, we might need to
extend ON_ERROR_STOP so that it accepts the following setting values.

* off - don't stop even when either sql or shell fails (same as the
current behavior)
* on or sql - stop only whensql fails (same as the current behavior)
* shell - stop only when shell fails
* all - stop when either sql or shell fails

Thought?

Regards,

I agree that some applications may depend on the behavior of previous
ON_ERROR_STOP.
I created a patch that implements off, on, shell, and all option for
ON_ERROR_STOP.
I also edited the code for \g, \o, \w, and \set in addition to \! to
return exit status of shell commands for ON_ERROR_STOP.

There were discussions regarding the error messages for when shell
command fails.
I have found that \copy already handles exit status of shell commands
when it executes one, so I copied the messages from there.
More specifically, I referred to """bool do_copy(const char *args)""" in
src/bin/psql/copy.c

Any feedback would be very much appreciated.

Tatsu

Attachments:

stop_error2.patchtext/x-diff; name=stop_error2.patchDownload+136-19
#13Fujii Masao
masao.fujii@gmail.com
In reply to: bt22nakamorit (#12)
Re: Make ON_ERROR_STOP stop on shell script failure

On 2022/09/30 16:54, bt22nakamorit wrote:

2022-09-21 11:45 に Fujii Masao wrote:

We can execute the shell commands via psql in various ways
other than \! meta-command. For example,

* `command`
* \g | command
* \gx | command
* \o | command
* \w | command
* \copy ... program 'command'

ON_ERROR_STOP should handle not only \! but also all the above in the same way?

One concern about this patch is that some applications already depend on
the current behavior of ON_ERROR_STOP, i.e., psql doesn't stop even when
the shell command returns non-zero exit code. If so, we might need to
extend ON_ERROR_STOP so that it accepts the following setting values.

* off - don't stop even when either sql or shell fails (same as the
current behavior)
* on or sql - stop only whensql fails (same as the current behavior)
* shell - stop only when shell fails
* all - stop when either sql or shell fails

Thought?

Regards,

I agree that some applications may depend on the behavior of previous ON_ERROR_STOP.
I created a patch that implements off, on, shell, and all option for ON_ERROR_STOP.
I also edited the code for \g, \o, \w, and \set in addition to \! to return exit status of shell commands for ON_ERROR_STOP.

There were discussions regarding the error messages for when shell command fails.
I have found that \copy already handles exit status of shell commands when it executes one, so I copied the messages from there.
More specifically, I referred to """bool do_copy(const char *args)""" in src/bin/psql/copy.c

Any feedback would be very much appreciated.

Thanks for updating the patch!

The patch failed to be applied into the master cleanly. Could you rebase it?

patching file src/bin/psql/common.c
Hunk #1 succeeded at 94 (offset 4 lines).
Hunk #2 succeeded at 104 (offset 4 lines).
Hunk #3 succeeded at 133 (offset 4 lines).
Hunk #4 succeeded at 1869 with fuzz 1 (offset 1162 lines).
Hunk #5 FAILED at 2624.
1 out of 5 hunks FAILED -- saving rejects to file src/bin/psql/common.c.rej

Regards,

--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION

#14bt22nakamorit
bt22nakamorit@oss.nttdata.com
In reply to: Fujii Masao (#13)
Re: Make ON_ERROR_STOP stop on shell script failure

2022-10-07 17:16 Fujii Masao wrote:

The patch failed to be applied into the master cleanly. Could you
rebase it?

patching file src/bin/psql/common.c
Hunk #1 succeeded at 94 (offset 4 lines).
Hunk #2 succeeded at 104 (offset 4 lines).
Hunk #3 succeeded at 133 (offset 4 lines).
Hunk #4 succeeded at 1869 with fuzz 1 (offset 1162 lines).
Hunk #5 FAILED at 2624.
1 out of 5 hunks FAILED -- saving rejects to file
src/bin/psql/common.c.rej

Thank you for checking.
I edited the patch so that it would apply to the latest master branch.
Please mention if there are any other problems.

Best,
Tatsuhiro Nakamori

Attachments:

stop_error3.patchtext/x-diff; name=stop_error3.patchDownload+152-20
#15bt22nakamorit
bt22nakamorit@oss.nttdata.com
In reply to: bt22nakamorit (#14)
Re: Make ON_ERROR_STOP stop on shell script failure

There was a mistake in the error message for \! so I updated the patch.

Best,
Tatsuhiro Nakamori

Attachments:

stop_error4.patchtext/x-diff; name=stop_error4.patchDownload+152-20
#16Justin Pryzby
pryzby@telsasoft.com
In reply to: bt22nakamorit (#1)
Re: Make ON_ERROR_STOP stop on shell script failure

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

#17Corey Huinker
corey.huinker@gmail.com
In reply to: Justin Pryzby (#16)
Re: Make ON_ERROR_STOP stop on shell script failure

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

I agree, and I hacked up a proof of concept, but started another thread at
/messages/by-id/CADkLM=cWao2x2f+UDw15W1JkVFr_bsxfstw=NGea7r9m4j-7rQ@mail.gmail.com
so as not to clutter up this one.

#18Matheus Alcantara
mths.dev@pm.me
In reply to: bt22nakamorit (#15)
Re: Make ON_ERROR_STOP stop on shell script failure

------- Original Message -------
On Tuesday, November 22nd, 2022 at 20:10, bt22nakamorit <bt22nakamorit@oss.nttdata.com> wrote:

There was a mistake in the error message for \! so I updated the patch.

Best,
Tatsuhiro Nakamori

Hi

I was checking your patch and seems that it failed to be applied into the
master cleanly. Could you please rebase it?

--
Matheus Alcantara

#19Corey Huinker
corey.huinker@gmail.com
In reply to: Matheus Alcantara (#18)
Re: Make ON_ERROR_STOP stop on shell script failure

On Tue, Nov 22, 2022 at 6:16 PM Matheus Alcantara <mths.dev@pm.me> wrote:

------- Original Message -------
On Tuesday, November 22nd, 2022 at 20:10, bt22nakamorit <
bt22nakamorit@oss.nttdata.com> wrote:

There was a mistake in the error message for \! so I updated the patch.

Best,
Tatsuhiro Nakamori

Hi

I was checking your patch and seems that it failed to be applied into the
master cleanly. Could you please rebase it?

Yes. My apologies, I had several life events get in the way.

#20David Zhang
david.zhang@highgo.ca
In reply to: bt22nakamorit (#15)
Re: Make ON_ERROR_STOP stop on shell script failure

On 2022-10-12 2:13 a.m., bt22nakamorit wrote:

There was a mistake in the error message for \! so I updated the patch.

Tried to apply this patch to the master branch, but got the error like
below.
$ git apply --check patch-view.diff
error: patch failed: src/bin/psql/command.c:2693
error: src/bin/psql/command.c: patch does not apply

I think there are some tests related with "ON_ERROR_STOP" in
src/bin/psql/t/001_basic.pl, and we should consider to add corresponding
test cases for "on/off/shell/all" to this patch.

Best regards,

David

#21Andreas 'ads' Scherbaum
adsmail@wars-nicht.de
In reply to: Matheus Alcantara (#18)
#22Andreas 'ads' Scherbaum
adsmail@wars-nicht.de
In reply to: Andreas 'ads' Scherbaum (#21)
#23Bruce Momjian
bruce@momjian.us
In reply to: Andreas 'ads' Scherbaum (#21)
#24Gregory Stark (as CFM)
stark.cfm@gmail.com
In reply to: Bruce Momjian (#23)
#25Gregory Stark (as CFM)
stark.cfm@gmail.com
In reply to: Gregory Stark (as CFM) (#24)
#26Peter Eisentraut
peter_e@gmx.net
In reply to: Bruce Momjian (#23)
#27Corey Huinker
corey.huinker@gmail.com
In reply to: Peter Eisentraut (#26)
#28Corey Huinker
corey.huinker@gmail.com
In reply to: Corey Huinker (#27)
#29Daniel Gustafsson
daniel@yesql.se
In reply to: Corey Huinker (#28)