Fix psql's single transaction mode on client-side errors with -c/-f switches
authorMichael Paquier <[email protected]>
Mon, 6 Jun 2022 02:07:39 +0000 (11:07 +0900)
committerMichael Paquier <[email protected]>
Mon, 6 Jun 2022 02:07:39 +0000 (11:07 +0900)
psql --single-transaction is able to handle multiple -c and -f switches
in a single transaction since d5563d7d, but this had the surprising
behavior of forcing a transaction COMMIT even if psql failed with an
error in the client (for example incorrect path given to \copy), which
would generate an error, but still commit any changes that were already
applied in the backend.  This commit makes the behavior more consistent,
by enforcing a transaction ROLLBACK if any commands fail, both
client-side and backend-side, so as no changes are applied if one error
happens in any of them.

Some tests are added on HEAD to provide some coverage about all that.
Backend-side errors are unreliable as IPC::Run can complain on SIGPIPE
if psql quits before reading a query result, but that should work
properly in the case where any errors come from psql itself, which is
what the original report is about.

Reported-by: Christoph Berg
Author: Kyotaro Horiguchi, Michael Paquier
Discussion: https://postgr.es/m/17504-76b68018e130415e@postgresql.org
Backpatch-through: 10

doc/src/sgml/ref/psql-ref.sgml
src/bin/psql/startup.c

index 844d7e276668e481e124c6db9c9526deaacaf14d..7b52ac1900216fea6033394649df35ab8446c28c 100644 (file)
@@ -580,8 +580,10 @@ EOF
         <application>psql</application> to issue a <command>BEGIN</> command
         before the first such option and a <command>COMMIT</> command after
         the last one, thereby wrapping all the commands into a single
-        transaction.  This ensures that either all the commands complete
-        successfully, or no changes are applied.
+        transaction. If any of the commands fails, a
+        <command>ROLLBACK</command> command is sent instead. This ensures that
+        either all the commands complete successfully, or no changes are
+        applied.
        </para>
 
        <para>
index 260b4ea55cde05f1df90c6476db22986b1c59b07..36e9432fc677b0136a37ec83bf61d94af83f7370 100644 (file)
@@ -371,7 +371,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)
                {