Skip to content

Commit 5c30672

Browse files
committed
Handle better implicit transaction state of pipeline mode
When using a pipeline, a transaction starts from the first command and is committed with a Sync message or when the pipeline ends. Functions like IsInTransactionBlock() or PreventInTransactionBlock() were already able to understand a pipeline as being in a transaction block, but it was not the case of CheckTransactionBlock(). This function is called for example to generate a WARNING for SET LOCAL, complaining that it is used outside of a transaction block. The current state of the code caused multiple problems, like: - SET LOCAL executed at any stage of a pipeline issued a WARNING, even if the command was at least second in line where the pipeline is in a transaction state. - LOCK TABLE failed when invoked at any step of a pipeline, even if it should be able to work within a transaction block. The pipeline protocol assumes that the first command of a pipeline is not part of a transaction block, and that any follow-up commands is considered as within a transaction block. This commit changes the backend so as an implicit transaction block is started each time the first Execute message of a pipeline has finished processing, with this implicit transaction block ended once a sync is processed. The checks based on XACT_FLAGS_PIPELINING in the routines checking if we are in a transaction block are not necessary: it is enough to rely on the existing ones. Some tests are added to pgbench, that can be backpatched down to v17 when \syncpipeline is involved and down to v14 where \startpipeline and \endpipeline are available. This is unfortunately limited regarding the error patterns that can be checked, but it provides coverage for various pipeline combinations to check if these succeed or fail. These tests are able to capture the case of SET LOCAL's WARNING. The author has proposed a different feature to improve the coverage by adding similar meta-commands to psql where error messages could be checked, something more useful for the cases where commands cannot be used in transaction blocks, like REINDEX CONCURRENTLY or VACUUM. This is considered as future work for v18~. Author: Anthonin Bonnefoy Reviewed-by: Jelte Fennema-Nio, Michael Paquier Discussion: https://postgr.es/m/CAO6_XqrWO8uNBQrSu5r6jh+vTGi5Oiyk4y8yXDORdE2jbzw8xw@mail.gmail.com Backpatch-through: 13
1 parent 48a6cd1 commit 5c30672

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

doc/src/sgml/protocol.sgml

+11-10
Original file line numberDiff line numberDiff line change
@@ -1088,16 +1088,17 @@ SELCT 1/0;<!-- this typo is intentional -->
10881088

10891089
<para>
10901090
If the client has not issued an explicit <command>BEGIN</command>,
1091-
then each Sync ordinarily causes an implicit <command>COMMIT</command>
1092-
if the preceding step(s) succeeded, or an
1093-
implicit <command>ROLLBACK</command> if they failed. However, there
1094-
are a few DDL commands (such as <command>CREATE DATABASE</command>)
1095-
that cannot be executed inside a transaction block. If one of
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
1100-
respond with ReadyForQuery.
1091+
then an implicit transaction block is started and each Sync ordinarily
1092+
causes an implicit <command>COMMIT</command> if the preceding step(s)
1093+
succeeded, or an implicit <command>ROLLBACK</command> if they failed.
1094+
This implicit transaction block will only be detected by the server
1095+
when the first command ends without a sync. There are a few DDL
1096+
commands (such as <command>CREATE DATABASE</command>) that cannot be
1097+
executed inside a transaction block. If one of these is executed in a
1098+
pipeline, it will fail unless it is the first command after a Sync.
1099+
Furthermore, upon success it will force an immediate commit to preserve
1100+
database consistency. Thus a Sync immediately following one of these
1101+
commands has no effect except to respond with ReadyForQuery.
11011102
</para>
11021103

11031104
<para>

src/backend/access/transam/xact.c

-13
Original file line numberDiff line numberDiff line change
@@ -3405,16 +3405,6 @@ PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
34053405
errmsg("%s cannot run inside a subtransaction",
34063406
stmtType)));
34073407

3408-
/*
3409-
* inside a pipeline that has started an implicit transaction?
3410-
*/
3411-
if (MyXactFlags & XACT_FLAGS_PIPELINING)
3412-
ereport(ERROR,
3413-
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3414-
/* translator: %s represents an SQL statement name */
3415-
errmsg("%s cannot be executed within a pipeline",
3416-
stmtType)));
3417-
34183408
/*
34193409
* inside a function call?
34203410
*/
@@ -3526,9 +3516,6 @@ IsInTransactionBlock(bool isTopLevel)
35263516
if (IsSubTransaction())
35273517
return true;
35283518

3529-
if (MyXactFlags & XACT_FLAGS_PIPELINING)
3530-
return true;
3531-
35323519
if (!isTopLevel)
35333520
return true;
35343521

src/backend/tcop/postgres.c

+18
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,17 @@ start_xact_command(void)
26562656

26572657
xact_started = true;
26582658
}
2659+
else if (MyXactFlags & XACT_FLAGS_PIPELINING)
2660+
{
2661+
/*
2662+
* When the first Execute message is completed, following commands
2663+
* will be done in an implicit transaction block created via
2664+
* pipelining. The transaction state needs to be updated to an
2665+
* implicit block if we're not already in a transaction block (like
2666+
* one started by an explicit BEGIN).
2667+
*/
2668+
BeginImplicitTransactionBlock();
2669+
}
26592670

26602671
/*
26612672
* Start statement timeout if necessary. Note that this'll intentionally
@@ -4605,6 +4616,13 @@ PostgresMain(int argc, char *argv[],
46054616

46064617
case 'S': /* sync */
46074618
pq_getmsgend(&input_message);
4619+
4620+
/*
4621+
* If pipelining was used, we may be in an implicit
4622+
* transaction block. Close it before calling
4623+
* finish_xact_command.
4624+
*/
4625+
EndImplicitTransactionBlock();
46084626
finish_xact_command();
46094627
send_ready_for_query = true;
46104628
break;

0 commit comments

Comments
 (0)