Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u6kYO-0043UE-J3 for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Apr 2025 06:22:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1u6kYM-001wZB-IC for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Apr 2025 06:22:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u6kYL-001wXX-DW for pgsql-hackers@lists.postgresql.org; Mon, 21 Apr 2025 06:22:51 +0000 Received: from fout-a2-smtp.messagingengine.com ([103.168.172.145]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u6kYI-001B6b-0b for pgsql-hackers@postgresql.org; Mon, 21 Apr 2025 06:22:49 +0000 Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfout.phl.internal (Postfix) with ESMTP id 9DA6913801BA; Mon, 21 Apr 2025 02:22:44 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-09.internal (MEProxy); Mon, 21 Apr 2025 02:22:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1745216564; x=1745302964; bh=gUVVJeVhVk 1d+2RwcqmQJOiU1Lg4TnBEnnIQOmzcxrY=; b=jfc/h34JPV1yADl6IK+SIxR82Y JnAOqL7qT+sFjhMc1AZsRSNmGUV+9fyEWfQu+tQkGPo21sLUWtJURqEhAKXoxh6y Vwa/G2+p2/8JzQ9Rve76DjXyxEjXkm6rwC5gVyr92yjwM+HeVt5DLBwEq2aORlDY /hD1GtaTOsG8MNiwO0iJ9PMsPBoJATTCzgjFgXrG+PGMfwRHEEjCB1QAQjCbVz2C G7LGYbL0qgmFJo9X+ccHK1JQcfJLgfeUyt9KM1QsTSkDc9oAWQQV2Ab/Njst19Ff RF5rmkHTUxsZBc2E83WmBlPJDHa03t94nRNETwiGKpQ0qaOsl3eykY+3F3vA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1745216564; x=1745302964; bh=gUVVJeVhVk1d+2RwcqmQJOiU1Lg4TnBEnnI QOmzcxrY=; b=R9feHukxlj/zvEl3vjIEHGZOMHByknS+fwQl6H4/WWE3gCSGVYv KEYochmV0lB8RVmiQHN95g4/uVQOXytypKWsve8242fxap04CEOVBO8J2r1HLlBL KEW/LpZOSvBP3QS6G5FHKg5KGpBc4lHdaQad/hF+p7I7FFYmXBuRglYgU1vCccuX dIHNu5ExlPHaFVttGyWzurcYazPTd528vYQVOgH9WP36YW5CVPthLIwN/CQZzeaX 1xHFq1+dE3mgyzBstqtDGUn7OnVl+wjhyDNe4OCh2JbAW9Rembt9gEcZopttlXgR HcxkxrOdDd0nN+NVMFbhBNKFQusC1onepLA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvgedtudduucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnegfrhhlucfvnfffucdljedtmdenucfjughrpeffhffvvefu kfhfgggtuggjsehgtderredttddunecuhfhrohhmpefoihgthhgrvghlucfrrghquhhivg hruceomhhitghhrggvlhesphgrqhhuihgvrhdrgiihiieqnecuggftrfgrthhtvghrnhep gfehhefgudefudfgfeelgeeiffegfefhkeefffdvheefuddtheetgeelteevkeetnecuve hluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepmhhitghhrggv lhesphgrqhhuihgvrhdrgiihiidpnhgspghrtghpthhtohephedpmhhouggvpehsmhhtph houhhtpdhrtghpthhtoheprgdrkhhoiihhvghmhigrkhhinhesphhoshhtghhrvghsphhr ohdrrhhupdhrtghpthhtohepuggrnhhivghlsehmrghnihhtohhuqdhmrghilhdrohhrgh dprhgtphhtthhopegrnhhthhhonhhinhdrsghonhhnvghfohihsegurghtrgguohhghhhq rdgtohhmpdhrtghpthhtohepphhoshhtghhrvghssehjvghlthgvfhdrnhhlpdhrtghpth htohepphhgshhqlhdqhhgrtghkvghrshesphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 21 Apr 2025 02:22:41 -0400 (EDT) Date: Mon, 21 Apr 2025 15:22:28 +0900 From: Michael Paquier To: "a.kozhemyakin" Cc: Daniel Verite , Anthonin Bonnefoy , Jelte Fennema-Nio , PostgreSQL Hackers Subject: Re: Add Pipelining support in psql Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="Ve7KesJZF/ELFuMh" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Ve7KesJZF/ELFuMh Content-Type: multipart/mixed; boundary="tPTN7wbpNh+FjgHs" Content-Disposition: inline --tPTN7wbpNh+FjgHs Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Apr 16, 2025 at 09:18:01AM -0700, Michael Paquier wrote: > On Wed, Apr 16, 2025 at 09:31:59PM +0700, a.kozhemyakin wrote: >> After commit 2cce0fe on master >>=20 >> ERROR:=A0 unexpected message type 0x50 during COPY from stdin >> CONTEXT:=A0 COPY psql_pipeline, line 1 >> Pipeline aborted, command did not run >> psql: common.c:1510: discardAbortedPipelineResults: Assertion `res =3D= =3D ((void >> *)0) || result_status =3D=3D PGRES_PIPELINE_ABORTED' failed. >> Aborted (core dumped) >=20 > Reproduced here, thanks for the report. I'll look at that at the > beginning of next week, adding an open item for now. The failure is not related to 2cce0fe. The following sequence fails as well, as long as we have one SELECT after the COPY to mess up with the \getresults that causes a PGRES_FATAL_ERROR combined with a "terminating connection because protocol synchronization was lost" on the backend side, because the server expects some data while the client does not send it but psql is not able to cope with this state: \startpipeline COPY psql_pipeline FROM STDIN \bind \sendpipeline SELECT $1 \bind 'val1' \sendpipeline \syncpipeline \getresults \endpipeline It's actually nice that we are able to emulate such query patterns with psql using all these meta-commands, I don't think we have any coverage for the backend synchronization loss case yet like this one? 2cce0fe makes that easier to reach by allowing more command patterns, but it's the mix of COPY followed by a SELECT that causes psql to be confused. All the tests that we have don't check this kind of scenarios, for COPY TO/FROM, with always use a flush or a sync followed quickly by \getresults, but we don't have tests where we mix things. Anyway, I don't think that there is much we can do under a PGRES_FATAL_ERROR in this code path when discarding the pipe results. As far as I can tell, the server has failed the query suddenly and the whole pipeline flow is borked. The best thing that I can think of is to discard all the results while decrementing the counters, then let psql complain about that like in the attached. I've added two tests in TAP, as these trigger a FATAL in the backend so we cannot use the normal SQL route, so as we have some coverage. @Anthonin: Any thoughts or comments, perhaps? A second opinion would be welcome here. -- Michael --tPTN7wbpNh+FjgHs Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-psql-Fix-assertion-failure-with-pipeline-mode.patch" Content-Transfer-Encoding: quoted-printable =46rom cec3613b43f536ec1f91a75a034264ed304cd628 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Mon, 21 Apr 2025 15:17:43 +0900 Subject: [PATCH] psql: Fix assertion failure with pipeline mode --- src/bin/psql/common.c | 20 ++++++++++++++++++++ src/bin/psql/t/001_basic.pl | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 21d660a8961a..6eb56d7bb66e 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1478,6 +1478,26 @@ discardAbortedPipelineResults(void) */ return res; } + else if (result_status =3D=3D PGRES_FATAL_ERROR) + { + /* + * Fatal error found from a query execution failure, there is not + * much that can be done, so give up and clear all the results + * available. There may be more than one result stacked. + */ + PQclear(res); + + if (pset.available_results > 0) + pset.available_results--; + if (pset.requested_results > 0) + pset.requested_results--; + + /* leave if no more results */ + if (pset.requested_results =3D=3D 0) + return NULL; + else + continue; + } else if (res =3D=3D NULL) { /* A query was processed, decrement the counters */ diff --git a/src/bin/psql/t/001_basic.pl b/src/bin/psql/t/001_basic.pl index 739cb4397087..91c56cd7c08c 100644 --- a/src/bin/psql/t/001_basic.pl +++ b/src/bin/psql/t/001_basic.pl @@ -34,11 +34,13 @@ sub psql_fails_like { local $Test::Builder::Level =3D $Test::Builder::Level + 1; =20 - my ($node, $sql, $expected_stderr, $test_name) =3D @_; + my ($node, $sql, $expected_stderr, $test_name, $replication) =3D @_; + + # Use the context of a WAL sender, if requested by the caller. + $replication =3D '' unless defined($replication); =20 - # Use the context of a WAL sender, some of the tests rely on that. my ($ret, $stdout, $stderr) =3D - $node->psql('postgres', $sql, replication =3D> 'database'); + $node->psql('postgres', $sql, replication =3D> $replication); =20 isnt($ret, 0, "$test_name: exit code not 0"); like($stderr, $expected_stderr, "$test_name: matches"); @@ -79,7 +81,8 @@ psql_fails_like( $node, 'START_REPLICATION 0/0', qr/unexpected PQresultStatus: 8$/, - 'handling of unexpected PQresultStatus'); + 'handling of unexpected PQresultStatus', + 'database'); =20 # test \timing psql_like( @@ -481,4 +484,28 @@ psql_like($node, "copy (values ('foo'),('bar')) to std= out \\g | $pipe_cmd", my $c4 =3D slurp_file($g_file); like($c4, qr/foo.*bar/s); =20 +# Tests with pipelines. These trigger FATAL failures in the backend, +# so they cannot be tested through the SQL regression tests. +$node->safe_psql('postgres', 'CREATE TABLE psql_pipeline()'); +psql_fails_like( + $node, + qq{\\startpipeline +COPY psql_pipeline FROM STDIN; +SELECT 'val1'; +\\syncpipeline +\\getresults +\\endpipeline}, + qr/unexpected message type 0x50 during COPY from stdin/, + 'handling of protocol synchronization loss with pipelines'); +psql_fails_like( + $node, + qq{\\startpipeline +COPY psql_pipeline FROM STDIN \\bind \\sendpipeline +SELECT 'val1' \\bind \\sendpipeline +\\syncpipeline +\\getresults +\\endpipeline}, + qr/unexpected message type 0x50 during COPY from stdin/, + 'handling of protocol synchronization loss with pipelines'); + done_testing(); --=20 2.49.0 --tPTN7wbpNh+FjgHs-- --Ve7KesJZF/ELFuMh Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmgF5CQACgkQnvQgOdby QH1uyg//VLEhBZoakgt9Z3oZGRmsRPW6Q4gSMBFXGTZDmTq2DZetQwTv+/cPnON8 d1xVqLVkTu+H91D1KDyYm2P8lvq7zjlIHTPJtTEqsDZ1TQSSc1fNm03F5aIVURKJ M2GE1EQTIn87AIUh+acrJHSz/TI5dgIVMQA4iZDoXP0dtOE3bJ1oYz89Tgy2/xWw fmkxh4Z1du0NrYm4Xuohnhz5S6IWjTb7AjRBRwEjUA77ts0rO84VqZ8vaLWvBrAn 9zwtKbhOELsH8X/VGlVBb8hPGPzvzhZ8e4+st+bieUkrZCtAJlMn6iHcqOnIneNd TeelYtQF+rUK7xN+TVmtXksm9iU/HrPiqBPTmbHZzJKeM44msmf/Mt0nk/5lSRtp AfmH4rNdBjwyztxrBuVUKORrkddL/Lyom/+zokMjBy39Pb+/SBbjAGtm+gijuKM2 u+ZaVJm7rXa3C7QTd6jJZbe+MVtpCqJBE2VOYuqfpMcye8SOshIBUuMDtlViSCEq sfV1mZtm57KSwxKM67BAXc14jwnY9dvVr+VT3450Buz8/VTScq0qqBQT0jHVw7e0 smQijF6rWUX2MFxsS7pXzcG0tabGbgip4f6ojNNUsubBAldHSBHP51GomxQ/1bcz k/DV6k9ntkJR5M/RFGTuBGyW9B6nPZjotmCRM5KYDJXWQh/QO8w= =gXWo -----END PGP SIGNATURE----- --Ve7KesJZF/ELFuMh--