Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nx36Q-00083e-SU for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jun 2022 08:56:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nx36O-0006xJ-8E for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jun 2022 08:56:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nx36N-0006xA-LA for pgsql-bugs@lists.postgresql.org; Fri, 03 Jun 2022 08:56:15 +0000 Received: from mail-pf1-x429.google.com ([2607:f8b0:4864:20::429]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nx36G-0005rK-R9 for pgsql-bugs@lists.postgresql.org; Fri, 03 Jun 2022 08:56:14 +0000 Received: by mail-pf1-x429.google.com with SMTP id 15so6748950pfy.3 for ; Fri, 03 Jun 2022 01:56:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:message-id:to:cc:subject:from:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=z+KqPimfb7WY+mEsMdUT9hyXw8ROwWYulu8ZOt1rJDk=; b=lhFp4d/sZH5+m6a4scPkeTyl+zUDyK7uvy+fUru31MWMy7mJxDraxrQ0SPszvddX9g oM+UcN1L14Yp3Z7JX0JCbUEQf81YmXX5ZnWE1ocnUn0yKxz5PloJXI0EmkEu8RqvnPjM FVRJm4AKrITyGjpmz8EauvBe6ylyhSnsgfSMYSjgujQjS7ZyCZCKWrd9+4DIz162EoIl 8uwdWz4Dbwruwe8sR2ArMHaaP+M6c8f2Og45qe7ymd51coQNnZw/jsEkldLmuuZYNNef nTuUIZyV6Xo7j6vulHHvIrotVS+8wV0hyqmgUazVOass57C0+A7smz1MXeSqLqZTQbcD aCMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:to:cc:subject:from:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=z+KqPimfb7WY+mEsMdUT9hyXw8ROwWYulu8ZOt1rJDk=; b=U5cH5CiMlrPmkwnQjsc2HTFmUnnAGxKkpJRj9RWVC4lChO7mudgg8F80ii3jMAsl11 FGtOYfJu6u3fLOfTfzgzhf0NsVaVYwWM8XiXjl3hxjwVUwZaH8/xZlhS/xgXil0t/PZf qM+Jj+rcY/ZZGU4VueKgRjljm7bQgrRMds7fjJL/O3nRwZ15uxNRLhxfNOw2AU5Mc3Xs kcclWcgvHguvkVmDjAFac+naCcdRyyu3owygu4sId5RKUsVpGw9V7UB7eyS5WczaWSFQ 8ZBh8nJ3ji6qK6cwaFDoNST/SrfBlgP3CNGoo2fNJW/PoxSGsA5OTrVz6S+hiDzps7xw INhg== X-Gm-Message-State: AOAM532zs1tuF3cW3aSdNqafC3F/+e1v562Cv9nkvnMROfC5oNuXdqpN sjUHPsQoPMfey352eo2wUPk= X-Google-Smtp-Source: ABdhPJyVGHIOxnTPEwmvPcsciyVGbVzXsZCimiEL/rA2L8hVECoCIsZY5Hp5MT6CXa0ZWeDC4y+LGQ== X-Received: by 2002:a63:f156:0:b0:3ab:ada6:b463 with SMTP id o22-20020a63f156000000b003abada6b463mr7888953pgk.462.1654246567225; Fri, 03 Jun 2022 01:56:07 -0700 (PDT) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id l187-20020a6225c4000000b0051b97714703sm4880636pfl.187.2022.06.03.01.56.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 03 Jun 2022 01:56:05 -0700 (PDT) Date: Fri, 03 Jun 2022 17:56:03 +0900 (JST) Message-Id: <20220603.175603.682874096867945240.horikyota.ntt@gmail.com> To: michael@paquier.xyz Cc: christoph.berg@credativ.de, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17504: psql --single-transaction -vON_ERROR_STOP=1 still commits after client-side error From: Kyotaro Horiguchi In-Reply-To: References: <20220531.113927.640026319524742851.horikyota.ntt@gmail.com> User-Agent: Mew version 6.8 on Emacs 26.1 Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Fri_Jun__3_17_56_03_2022_900)--" Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ----Next_Part(Fri_Jun__3_17_56_03_2022_900)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Thanks for the suggestions! At Thu, 2 Jun 2022 14:39:41 +0900, Michael Paquier wrote in > 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 Added a test. > 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 This does not need to send a byte after the client-side failure. The connection is rather living after failure in this case. > 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. Agreed. Does the following work? > It causes psql to issue a BEGIN command before the first such option > and a COMMIT command after the last one, thereby wrapping all the > commands into a single transaction. +> If any of the commands fails, a ROLLBACK command is sent instead. > This ensures that either all the commands complete successfully, or > no changes are applied. regards. -- Kyotaro Horiguchi NTT Open Source Software Center ----Next_Part(Fri_Jun__3_17_56_03_2022_900)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="do_not_commit_after_client-side_failure.patch" diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 47bf3342a5..6f5476d99e 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -584,8 +584,10 @@ EOF psql to issue a BEGIN command before the first such option and a 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 ROLLBACK command is sent instead. This ensures + that either all the commands complete successfully, or no changes are + applied. 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) { diff --git a/src/bin/psql/t/001_basic.pl b/src/bin/psql/t/001_basic.pl index c3ed18e84d..0cae05ca4b 100644 --- a/src/bin/psql/t/001_basic.pl +++ b/src/bin/psql/t/001_basic.pl @@ -198,4 +198,14 @@ like( ^LOCATION: .*$/m, '\errverbose after \gdesc with error'); +# Check if client-side error causes the whole transaction rolled-back +$node->safe_psql('postgres', "CREATE TABLE t AS SELECT 1 AS a"); + +$node->command_fails(['psql', '--single-transaction', '-v', 'ON_ERROR_STOP=1', + '-c', 'DELETE FROM t', + '-c', "\\copy t FROM '/nonexistent'"]); + +is($node->safe_psql('postgres', 'SELECT count(*) FROM t'), 1, + 'Non-server error when ON_ERROR_STOP rolls back the transaction'); + done_testing(); ----Next_Part(Fri_Jun__3_17_56_03_2022_900)----