From 714d2b29b37fdbe9d84784adce827adcd7eacbc3 Mon Sep 17 00:00:00 2001
From: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Date: Wed, 30 Oct 2024 09:11:30 +0100
Subject: Consider pipeline implicit transaction as a transaction block

When using pipeline with implicit transaction, a transaction will
start from the first command and be committed with the Sync message.
Functions like IsInTransactionBlock and PreventInTransactionBlock
already assimilate this implicit transaction as a transaction block.

However, this is not the case in CheckTransactionBlock. This function is
used for things like warning when a local GUC is set outside of a
transaction block.

This patch adds the detection of implicit transactions started inside by
a pipeline in CheckTransactionBlock, avoiding warning when commands like
`set local` are called within a pipeline and making the detection of
transaction block coherent with what's done in IsInTransactionBlock and
PreventInTransactionBlock.
---
 src/backend/access/transam/xact.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 004f7e10e55..8a8c586e0ee 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -3744,6 +3744,12 @@ CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
 	if (IsSubTransaction())
 		return;
 
+	/*
+	 * inside a pipeline that has started an implicit transaction?
+	 */
+	if (MyXactFlags & XACT_FLAGS_PIPELINING)
+		return;
+
 	/*
 	 * inside a function call?
 	 */
-- 
2.39.5 (Apple Git-154)

