From a1ce9e4289e0a45e77901499dbeadc2848e8320a Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Fri, 27 Nov 2026 19:16:34 -0500 Subject: [PATCH] Fix lost transaction with simple Query or FunctionCall in a pipeline They ran in the pipeline's implicit block and reset xact_started without committing, so the Sync did nothing. Reported-by: MD. Ibrahim Discussion: https://postgr.es/m/19941-cb6bf76c6239186f@postgresql.org --- src/backend/tcop/postgres.c | 8 + src/test/modules/test_misc/meson.build | 1 + .../t/017_simple_query_in_pipeline.pl | 153 ++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 src/test/modules/test_misc/t/017_simple_query_in_pipeline.pl diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index afb77548dd1..c66c64b67da 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -2831,6 +2831,14 @@ finish_xact_command(void) if (xact_started) { + /* + * Outside the extended protocol, nothing else will end the implicit + * block that pipelining may have opened, so close it here. + */ + if (!doing_extended_query_message && + (MyXactFlags & XACT_FLAGS_PIPELINING)) + EndImplicitTransactionBlock(); + CommitTransactionCommand(); #ifdef MEMORY_CONTEXT_CHECKING diff --git a/src/test/modules/test_misc/meson.build b/src/test/modules/test_misc/meson.build index 65bfb6ecd11..f702ab5013b 100644 --- a/src/test/modules/test_misc/meson.build +++ b/src/test/modules/test_misc/meson.build @@ -23,6 +23,7 @@ tests += { 't/013_temp_obj_multisession.pl', 't/015_temp_schema_exit_deferrable.pl', 't/016_heap_scan_stream.pl', + 't/017_simple_query_in_pipeline.pl', ], # The injection points are cluster-wide, so disable installcheck 'runningcheck': false, diff --git a/src/test/modules/test_misc/t/017_simple_query_in_pipeline.pl b/src/test/modules/test_misc/t/017_simple_query_in_pipeline.pl new file mode 100644 index 00000000000..99d84873785 --- /dev/null +++ b/src/test/modules/test_misc/t/017_simple_query_in_pipeline.pl @@ -0,0 +1,153 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Test a simple Query or FunctionCall message in an extended-protocol +# pipeline, after an Execute and before the Sync. + +use strict; +use warnings FATAL => 'all'; +use IO::Socket::INET; +use IO::Socket::UNIX; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $node = PostgreSQL::Test::Cluster->new('main'); +$node->init; +$node->start; +$node->safe_psql('postgres', 'CREATE TABLE pipe_t (i int)'); + +sub msg +{ + my ($type, $body) = @_; + $body = '' unless defined $body; + return $type . pack('N', length($body) + 4) . $body; +} + +# Read messages until $n ReadyForQuery messages were seen. Return the +# ReadyForQuery status bytes, with 'E' prepended if an error was seen. +sub read_until +{ + my ($sock, $n) = @_; + my @res; + my $seen = 0; + + while ($seen < $n) + { + my ($hdr, $body); + $sock->read($hdr, 5) == 5 or die "short read"; + my ($type, $len) = unpack('a1 N', $hdr); + $sock->read($body, $len - 4) if $len > 4; + push @res, 'E' if $type eq 'E'; + if ($type eq 'Z') + { + push @res, $body; + $seen++; + } + } + return join('', @res); +} + +sub connect_raw +{ + my $host = $node->host; + my $sock; + + if ($host =~ m{^/}) + { + $sock = IO::Socket::UNIX->new( + Peer => "$host/.s.PGSQL." . $node->port, + Type => SOCK_STREAM) or die "connect: $!"; + } + else + { + $sock = IO::Socket::INET->new( + PeerAddr => $host, + PeerPort => $node->port, + Proto => 'tcp') or die "connect: $!"; + } + $sock->autoflush(1); + + my $user = $node->safe_psql('postgres', 'select current_user'); + my $body = pack('N', 196608) . "user\0$user\0database\0postgres\0\0"; + $sock->print(pack('N', length($body) + 4) . $body); + read_until($sock, 1); + return $sock; +} + +my $sock = connect_raw(); + +# Parse, Bind and Execute of an INSERT into pipe_t. +my $exec_insert = + msg('P', "\0INSERT INTO pipe_t VALUES (1)\0" . pack('n', 0)) + . msg('B', "\0\0" . pack('nnn', 0, 0, 0)) + . msg('E', "\0" . pack('N', 0)); + +# FunctionCall of pg_backend_pid(), with no arguments. +my $fn_oid = + $node->safe_psql('postgres', "SELECT 'pg_backend_pid'::regproc::oid"); +my $fn_call = msg('F', pack('N n n n', $fn_oid, 0, 0, 0)); + +my $expected_rows = 0; + +# Send $batch, then check the ReadyForQuery states, that the session is +# idle, and that the new rows are committed. +sub check_batch +{ + my ($name, $batch, $n_rfq, $expected, $new_rows) = @_; + + $sock->print($batch); + is(read_until($sock, $n_rfq), $expected, "$name: ReadyForQuery states"); + + $sock->print(msg('Q', "SELECT 1\0")); + is(read_until($sock, 1), 'I', "$name: next command succeeds"); + + $expected_rows += $new_rows; + is($node->safe_psql('postgres', 'SELECT count(*) FROM pipe_t'), + $expected_rows, "$name: committed rows visible to others"); +} + +check_batch('single-statement Query', + $exec_insert . msg('Q', "SELECT 2\0") . msg('S'), + 2, 'II', 1); + +check_batch('empty Query', $exec_insert . msg('Q', "\0") . msg('S'), + 2, 'II', 1); + +check_batch('multi-statement Query', + $exec_insert . msg('Q', "SELECT 2; SELECT 3\0") . msg('S'), + 2, 'II', 1); + +check_batch('FunctionCall', $exec_insert . $fn_call . msg('S'), + 2, 'II', 1); + +# The implicit block is already open when the Query arrives. +check_batch('two Executes then Query', + $exec_insert . $exec_insert . msg('Q', "SELECT 2\0") . msg('S'), + 2, 'II', 2); + +check_batch('two Executes then FunctionCall', + $exec_insert . $exec_insert . $fn_call . msg('S'), + 2, 'II', 2); + +check_batch('Execute after Query', + $exec_insert . msg('Q', "SELECT 2\0") . $exec_insert . msg('S'), + 2, 'II', 2); + +# An explicit BEGIN keeps the transaction open, as it should. +check_batch( + 'Query BEGIN', + $exec_insert + . msg('Q', "BEGIN\0") + . msg('S') + . msg('Q', "COMMIT\0"), + 3, 'TTI', 1); + +# An error in the Query rolls back the earlier Execute too. +check_batch('Query with error', + $exec_insert . msg('Q', "SELECT 1/0\0") . msg('S'), + 2, 'EII', 0); + +$sock->print(msg('X')); +$sock->close; + +done_testing(); -- 2.37.1 (Apple Git-137.1)