Skip to content

Commit 20432f8

Browse files
committed
Rethink handling of [Prevent|Is]InTransactionBlock in pipeline mode.
Commits f929441 et al. made IsInTransactionBlock() set the XACT_FLAGS_NEEDIMMEDIATECOMMIT flag before returning "false", on the grounds that that kept its API promises equivalent to those of PreventInTransactionBlock(). This turns out to be a bad idea though, because it allows an ANALYZE in a pipelined series of commands to cause an immediate commit, which is unexpected. Furthermore, if we return "false" then we have another issue, which is that ANALYZE will decide it's allowed to do internal commit-and-start-transaction sequences, thus possibly unexpectedly committing the effects of previous commands in the pipeline. To fix the latter situation, invent another transaction state flag XACT_FLAGS_PIPELINING, which explicitly records the fact that we have executed some extended-protocol command and not yet seen a commit for it. Then, require that flag to not be set before allowing InTransactionBlock() to return "false". Having done that, we can remove its setting of NEEDIMMEDIATECOMMIT without fear of causing problems. This means that the API guarantees of IsInTransactionBlock now diverge from PreventInTransactionBlock, which is mildly annoying, but it seems OK given the very limited usage of IsInTransactionBlock. (In any case, a caller preferring the old behavior could always set NEEDIMMEDIATECOMMIT for itself.) For consistency also require XACT_FLAGS_PIPELINING to not be set in PreventInTransactionBlock. This too is meant to prevent commands such as CREATE DATABASE from silently committing previous commands in a pipeline. Per report from Peter Eisentraut. As before, back-patch to all supported branches (which sadly no longer includes v10). Discussion: https://postgr.es/m/65a899dd-aebc-f667-1d0a-abb89ff3abf8@enterprisedb.com
1 parent 4cb65e1 commit 20432f8

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

doc/src/sgml/libpq.sgml

+3-2
Original file line numberDiff line numberDiff line change
@@ -5055,10 +5055,11 @@ int PQflush(PGconn *conn);
50555055
</para>
50565056

50575057
<para>
5058-
While the pipeline API was introduced in
5058+
While <application>libpq</application>'s pipeline API was introduced in
50595059
<productname>PostgreSQL</productname> 14, it is a client-side feature
50605060
which doesn't require special server support and works on any server
5061-
that supports the v3 extended query protocol.
5061+
that supports the v3 extended query protocol. For more information see
5062+
<xref linkend="protocol-flow-pipelining"/>.
50625063
</para>
50635064

50645065
<sect2 id="libpq-pipeline-using">

doc/src/sgml/protocol.sgml

+5-4
Original file line numberDiff line numberDiff line change
@@ -1093,17 +1093,18 @@ SELCT 1/0;<!-- this typo is intentional -->
10931093
implicit <command>ROLLBACK</command> if they failed. However, there
10941094
are a few DDL commands (such as <command>CREATE DATABASE</command>)
10951095
that cannot be executed inside a transaction block. If one of
1096-
these is executed in a pipeline, it will, upon success, force an
1097-
immediate commit to preserve database consistency.
1098-
A Sync immediately following one of these has no effect except to
1096+
these is executed in a pipeline, it will fail unless it is the first
1097+
command in the pipeline. Furthermore, upon success it will force an
1098+
immediate commit to preserve database consistency. Thus a Sync
1099+
immediately following one of these commands has no effect except to
10991100
respond with ReadyForQuery.
11001101
</para>
11011102

11021103
<para>
11031104
When using this method, completion of the pipeline must be determined
11041105
by counting ReadyForQuery messages and waiting for that to reach the
11051106
number of Syncs sent. Counting command completion responses is
1106-
unreliable, since some of the commands may not be executed and thus not
1107+
unreliable, since some of the commands may be skipped and thus not
11071108
produce a completion message.
11081109
</para>
11091110
</sect2>

src/backend/access/transam/xact.c

+18-10
Original file line numberDiff line numberDiff line change
@@ -3488,6 +3488,16 @@ PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
34883488
errmsg("%s cannot run inside a subtransaction",
34893489
stmtType)));
34903490

3491+
/*
3492+
* inside a pipeline that has started an implicit transaction?
3493+
*/
3494+
if (MyXactFlags & XACT_FLAGS_PIPELINING)
3495+
ereport(ERROR,
3496+
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3497+
/* translator: %s represents an SQL statement name */
3498+
errmsg("%s cannot be executed within a pipeline",
3499+
stmtType)));
3500+
34913501
/*
34923502
* inside a function call?
34933503
*/
@@ -3577,9 +3587,11 @@ CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
35773587
* a transaction block than when running as single commands. ANALYZE is
35783588
* currently the only example.
35793589
*
3580-
* If this routine returns "false", then the calling statement is
3581-
* guaranteed that if it completes without error, its results will be
3582-
* committed immediately.
3590+
* If this routine returns "false", then the calling statement is allowed
3591+
* to perform internal transaction-commit-and-start cycles; there is not a
3592+
* risk of messing up any transaction already in progress. (Note that this
3593+
* is not the identical guarantee provided by PreventInTransactionBlock,
3594+
* since we will not force a post-statement commit.)
35833595
*
35843596
* isTopLevel: passed down from ProcessUtility to determine whether we are
35853597
* inside a function.
@@ -3597,20 +3609,16 @@ IsInTransactionBlock(bool isTopLevel)
35973609
if (IsSubTransaction())
35983610
return true;
35993611

3612+
if (MyXactFlags & XACT_FLAGS_PIPELINING)
3613+
return true;
3614+
36003615
if (!isTopLevel)
36013616
return true;
36023617

36033618
if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
36043619
CurrentTransactionState->blockState != TBLOCK_STARTED)
36053620
return true;
36063621

3607-
/*
3608-
* If we tell the caller we're not in a transaction block, then inform
3609-
* postgres.c that it had better commit when the statement is done.
3610-
* Otherwise our report could be a lie.
3611-
*/
3612-
MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
3613-
36143622
return false;
36153623
}
36163624

src/backend/tcop/postgres.c

+12
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,12 @@ exec_execute_message(const char *portal_name, long max_rows)
22282228
*/
22292229
CommandCounterIncrement();
22302230

2231+
/*
2232+
* Set XACT_FLAGS_PIPELINING whenever we complete an Execute
2233+
* message without immediately committing the transaction.
2234+
*/
2235+
MyXactFlags |= XACT_FLAGS_PIPELINING;
2236+
22312237
/*
22322238
* Disable statement timeout whenever we complete an Execute
22332239
* message. The next protocol message will start a fresh timeout.
@@ -2243,6 +2249,12 @@ exec_execute_message(const char *portal_name, long max_rows)
22432249
/* Portal run not complete, so send PortalSuspended */
22442250
if (whereToSendOutput == DestRemote)
22452251
pq_putemptymessage('s');
2252+
2253+
/*
2254+
* Set XACT_FLAGS_PIPELINING whenever we suspend an Execute message,
2255+
* too.
2256+
*/
2257+
MyXactFlags |= XACT_FLAGS_PIPELINING;
22462258
}
22472259

22482260
/*

src/include/access/xact.h

+7
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ extern PGDLLIMPORT int MyXactFlags;
113113
*/
114114
#define XACT_FLAGS_NEEDIMMEDIATECOMMIT (1U << 2)
115115

116+
/*
117+
* XACT_FLAGS_PIPELINING - set when we complete an extended-query-protocol
118+
* Execute message. This is useful for detecting that an implicit transaction
119+
* block has been created via pipelining.
120+
*/
121+
#define XACT_FLAGS_PIPELINING (1U << 3)
122+
116123
/*
117124
* start- and end-of-transaction callbacks for dynamically loaded modules
118125
*/

0 commit comments

Comments
 (0)