Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c3df4d5

Browse files
committed
Fix psql's single transaction mode on client-side errors with -c/-f switches
psql --single-transaction is able to handle multiple -c and -f switches in a single transaction since d5563d7, 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/[email protected] Backpatch-through: 10
1 parent 5033dbd commit c3df4d5

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,10 @@ EOF
580580
<application>psql</application> to issue a <command>BEGIN</> command
581581
before the first such option and a <command>COMMIT</> command after
582582
the last one, thereby wrapping all the commands into a single
583-
transaction. This ensures that either all the commands complete
584-
successfully, or no changes are applied.
583+
transaction. If any of the commands fails, a
584+
<command>ROLLBACK</command> command is sent instead. This ensures that
585+
either all the commands complete successfully, or no changes are
586+
applied.
585587
</para>
586588

587589
<para>

src/bin/psql/startup.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,9 @@ main(int argc, char *argv[])
371371

372372
if (options.single_txn)
373373
{
374-
if ((res = PSQLexec("COMMIT")) == NULL)
374+
res = PSQLexec((successResult == EXIT_SUCCESS) ?
375+
"COMMIT" : "ROLLBACK");
376+
if (res == NULL)
375377
{
376378
if (pset.on_error_stop)
377379
{

0 commit comments

Comments
 (0)