From 2641356ffacc0c69779a6252a8072f41521c0f3f Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Wed, 23 Apr 2025 15:52:24 +0900 Subject: [PATCH v3] psql: Fix assertion failure with pipeline mode --- src/bin/psql/common.c | 17 +++++++++++++ src/bin/psql/t/001_basic.pl | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 21d660a8961a..0aab02ee32e6 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1478,6 +1478,23 @@ discardAbortedPipelineResults(void) */ return res; } + else if (res != NULL && result_status == PGRES_FATAL_ERROR) + { + /* + * We have a fatal error sent by the backend and we can't recover + * from this state. Instead, return the last fatal error and let + * the outer loop handle it. + */ + PGresult *fatal_res PG_USED_FOR_ASSERTS_ONLY; + + /* + * Fetch result to consume the end of the current query being + * processed. + */ + fatal_res = PQgetResult(pset.db); + Assert(fatal_res == NULL); + return res; + } else if (res == 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 b0e4919d4d71..3cada3ba959b 100644 --- a/src/bin/psql/t/001_basic.pl +++ b/src/bin/psql/t/001_basic.pl @@ -483,4 +483,53 @@ psql_like($node, "copy (values ('foo'),('bar')) to stdout \\g | $pipe_cmd", my $c4 = slurp_file($g_file); like($c4, qr/foo.*bar/s); +# 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()'); +my $log_location = -s $node->logfile; +psql_fails_like( + $node, + qq{\\startpipeline +COPY psql_pipeline FROM STDIN; +SELECT 'val1'; +\\syncpipeline +\\getresults +\\endpipeline}, + qr/server closed the connection unexpectedly/, + 'protocol sync loss in pipeline: direct COPY, SELECT, sync and getresult' +); +$node->wait_for_log( + qr/FATAL: .*terminating connection because protocol synchronization was lost/, + $log_location); + +$log_location = -s $node->logfile; +psql_fails_like( + $node, + qq{\\startpipeline +COPY psql_pipeline FROM STDIN \\bind \\sendpipeline +SELECT 'val1' \\bind \\sendpipeline +\\syncpipeline +\\getresults +\\endpipeline}, + qr/server closed the connection unexpectedly/, + 'protocol sync loss in pipeline: bind COPY, SELECT, sync and getresult'); +$node->wait_for_log( + qr/FATAL: .*terminating connection because protocol synchronization was lost/, + $log_location); + +# This time, test without the \getresults. +$log_location = -s $node->logfile; +psql_fails_like( + $node, + qq{\\startpipeline +COPY psql_pipeline FROM STDIN; +SELECT 'val1'; +\\syncpipeline +\\endpipeline}, + qr/server closed the connection unexpectedly/, + 'protocol sync loss in pipeline: COPY, SELECT and sync'); +$node->wait_for_log( + qr/FATAL: .*terminating connection because protocol synchronization was lost/, + $log_location); + done_testing(); -- 2.49.0