10000 psql: Fix assertion failures with pipeline mode · petergeoghegan/postgres@3631612 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3631612

Browse files
committed
psql: Fix assertion failures with pipeline mode
A correct cocktail of COPY FROM, SELECT and/or DML queries and \syncpipeline was able to break the logic in charge of discarding results of a pipeline, done in discardAbortedPipelineResults(). Such sequence make the backend generate a FATAL, due to a protocol synchronization loss. This problem comes down to the fact that we did not consider the case of libpq returning a PGRES_FATAL_ERROR when discarding the results of an aborted pipeline. The discarding code is changed so as this result status is handled as a special case, with the caller of discardAbortedPipelineResults() being responsible for consuming the result. A couple of tests are added to cover the problems reported, bringing an interesting gain in coverage as there were no tests in the tree covering the case of protocol synchronization loss. Issue introduced by 41625ab. Reported-by: Alexander Kozhemyakin <a.kozhemyakin@postgrespro.ru> Author: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com> Co-authored-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/ebf6ce77-b180-4d6b-8eab-71f641499ddf@postgrespro.ru
1 parent 923ae50 commit 3631612

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/bin/psql/common.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,23 @@ discardAbortedPipelineResults(void)
14781478
*/
14791479
return res;
14801480
}
1481+
else if (res != NULL && result_status == PGRES_FATAL_ERROR)
1482+
{
1483+
/*
1484+
* Found a FATAL error sent by the backend, and we cannot recover
1485+
* from this state. Instead, return the last result and let the
1486+
* outer loop handle it.
1487+
*/
1488+
PGresult *fatal_res PG_USED_FOR_ASSERTS_ONLY;
1489+
1490+
/*
1491+
* Fetch result to consume the end of the current query being
1492+
* processed.
1493+
*/
1494+
fatal_res = PQgetResult(pset.db);
1495+
Assert(fatal_res == NULL);
1496+
return res;
1497+
}
14811498
else if (res == NULL)
14821499
{
14831500
/* A query was processed, decrement the counters */

src/bin/psql/t/001_basic.pl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,45 @@ sub psql_fails_like
483483
my $c4 = slurp_file($g_file);
484484
like($c4, qr/foo.*bar/s);
485485

486+
# Tests with pipelines. These trigger FATAL failures in the backend,
487+
# so they cannot be tested via SQL.
488+
$node->safe_psql('postgres', 'CREATE TABLE psql_pipeline()');
489+
my $log_location = -s $node->logfile;
490+
psql_fails_like(
491+
$node,
492+
qq{\\startpipeline
493+
COPY psql_pipeline FROM STDIN;
494+
SELECT 'val1';
495+
\\syncpipeline
496+
\\getresults
497+
\\endpipeline},
498+
qr/server closed the connection unexpectedly/,
499+
'protocol sync loss in pipeline: direct COPY, SELECT, sync and getresult'
500+
);
501+
$node->wait_for_log(
502+
qr/FATAL: .*terminating connection because protocol synchronization was lost/,
503+
$log_location);
504+
505+
psql_fails_like(
506+
$node,
507+
qq{\\startpipeline
508+
COPY psql_pipeline FROM STDIN \\bind \\sendpipeline
509+
SELECT 'val1' \\bind \\sendpipeline
510+
\\syncpipeline
511+
\\getresults
512+
\\endpipeline},
513+
qr/server closed the connection unexpectedly/,
514+
'protocol sync loss in pipeline: bind COPY, SELECT, sync and getresult');
515+
516+
# This time, test without the \getresults.
517+
psql_fails_like(
518+
$node,
519+
qq{\\startpipeline
520+
COPY psql_pipeline FROM STDIN;
521+
SELECT 'val1';
522+
\\syncpipeline
523+
\\endpipeline},
524+
qr/server closed the connection unexpectedly/,
525+
'protocol sync loss in pipeline: COPY, SELECT and sync');
526+
486527
done_testing();

0 commit comments

Comments
 (0)
0