Skip to content

Commit 1cca4a7

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 afd096e commit 1cca4a7

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

doc/src/sgml/protocol.sgml

Lines changed: 5 additions & 4 deletions
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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,6 +3378,16 @@ PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
33783378
errmsg("%s cannot run inside a subtransaction",
33793379
stmtType)));
33803380

3381+
/*
3382+
* inside a pipeline that has started an implicit transaction?
3383+
*/
3384+
if (MyXactFlags & XACT_FLAGS_PIPELINING)
3385+
ereport(ERROR,
3386+
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3387+
/* translator: %s represents an SQL statement name */
3388+
errmsg("%s cannot be executed within a pipeline",
3389+
stmtType)));
3390+
33813391
/*
33823392
* inside a function call?
33833393
*/
@@ -3468,9 +3478,11 @@ CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
34683478
* a transaction block than when running as single commands. ANALYZE is
34693479
* currently the only example.
34703480
*
3471-
* If this routine returns "false", then the calling statement is
3472-
* guaranteed that if it completes without error, its results will be
3473-
* committed immediately.
3481+
* If this routine returns "false", then the calling statement is allowed
3482+
* to perform internal transaction-commit-and-start cycles; there is not a
3483+
* risk of messing up any transaction already in progress. (Note that this
3484+
* is not the identical guarantee provided by PreventInTransactionBlock,
3485+
* since we will not force a post-statement commit.)
34743486
*
34753487
* isTopLevel: passed down from ProcessUtility to determine whether we are
34763488
* inside a function.
@@ -3488,20 +3500,16 @@ IsInTransactionBlock(bool isTopLevel)
34883500
if (IsSubTransaction())
34893501
return true;
34903502

3503+
if (MyXactFlags & XACT_FLAGS_PIPELINING)
3504+
return true;
3505+
34913506
if (!isTopLevel)
34923507
return true;
34933508

34943509
if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
34953510
CurrentTransactionState->blockState != TBLOCK_STARTED)
34963511
return true;
34973512

3498-
/*
3499-
* If we tell the caller we're not in a transaction block, then inform
3500-
* postgres.c that it had better commit when the statement is done.
3501-
* Otherwise our report could be a lie.
3502-
*/
3503-
MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
3504-
35053513
return false;
35063514
}
35073515

src/backend/tcop/postgres.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,12 @@ exec_execute_message(const char *portal_name, long max_rows)
21162116
*/
21172117
CommandCounterIncrement();
21182118

2119+
/*
2120+
* Set XACT_FLAGS_PIPELINING whenever we complete an Execute
2121+
* message without immediately committing the transaction.
2122+
*/
2123+
MyXactFlags |= XACT_FLAGS_PIPELINING;
2124+
21192125
/* full command has been executed, reset timeout */
21202126
disable_statement_timeout();
21212127
}
@@ -2128,6 +2134,12 @@ exec_execute_message(const char *portal_name, long max_rows)
21282134
/* Portal run not complete, so send PortalSuspended */
21292135
if (whereToSendOutput == DestRemote)
21302136
pq_putemptymessage('s');
2137+
2138+
/*
2139+
* Set XACT_FLAGS_PIPELINING whenever we suspend an Execute message,
2140+
* too.
2141+
*/
2142+
MyXactFlags |= XACT_FLAGS_PIPELINING;
21312143
}
21322144

21332145
/*

src/include/access/xact.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ extern int MyXactFlags;
109109
*/
110110
#define XACT_FLAGS_NEEDIMMEDIATECOMMIT (1U << 2)
111111

112+
/*
113+
* XACT_FLAGS_PIPELINING - set when we complete an extended-query-protocol
114+
* Execute message. This is useful for detecting that an implicit transaction
115+
* block has been created via pipelining.
116+
*/
117+
#define XACT_FLAGS_PIPELINING (1U << 3)
118+
112119
/*
113120
* start- and end-of-transaction callbacks for dynamically loaded modules
114121
*/

0 commit comments

Comments
 (0)