Re: BUG #17504: psql --single-transaction -vON_ERROR_STOP=1 still commits after client-side error

Michael Paquier <michael@paquier.xyz>

From: Michael Paquier <michael@paquier.xyz>
To: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Cc: christoph.berg@credativ.de, pgsql-bugs@lists.postgresql.org
Date: 2022-06-02T05:39:41Z
Lists: pgsql-bugs
On Tue, May 31, 2022 at 11:39:27AM +0900, Kyotaro Horiguchi wrote:
> The code looks like just a thinko that "COMMIT" works fine even if the
> given commands have ended in failure. But actually it doesn't for
> client-side failure.
> 
> diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
> index ddff903915..2261f78f81 100644
> --- a/src/bin/psql/startup.c
> +++ b/src/bin/psql/startup.c
> @@ -426,7 +426,9 @@ main(int argc, char *argv[])
>  
>                 if (options.single_txn)
>                 {
> -                       if ((res = PSQLexec("COMMIT")) == NULL)
> +                       res = PSQLexec(successResult == EXIT_SUCCESS ?
> +                                                  "COMMIT" : "ROLLBACK");
> +                       if (res == NULL)
>                         {
>                                 if (pset.on_error_stop)
>                                 {

Yeah, it seems a bit strange to commit the changes if an error happens
on the client side, and the docs are a bit blurry about that because
it has never been considered, I guess.  This would not happen with a
failure in the backend as COMMIT would just map to a ROLLBACK
automatically.

The change that you are sending would enforce this policy as Christoph
would like.  Some tests would be nice to check such behaviors, say in
001_basic.pl, but we also need to be careful when sending down queries
with psql expected to fail because of SIGPIPE (c757a3d, 6d41dd0).  The
docs need a refresh, they mention now that COMMIT is sent after the
last command but that would not be the case anymore with this patch if
there is a client-side error.
--
Michael

Commits

  1. Tweak behavior of psql --single-transaction depending on ON_ERROR_STOP

  2. Doc: remove a04ccf6df from release notes, now that it's reverted.

  3. Revert "Fix psql's single transaction mode on client-side errors with -c/-f switches".

  4. Fix psql's single transaction mode on client-side errors with -c/-f switches