From b5f432016406d149e21584ce1c5086dfb2452070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96nder=20Kalac=C4=B1?= Date: Wed, 17 Mar 2021 12:20:57 +0100 Subject: [PATCH] Make sure that single task local executions start coordinated transaction (#4831) With https://github.com/citusdata/citus/pull/4806 we enabled 2PC for any non-read-only local task. However, if the execution is a single task, enabling 2PC (CoordinatedTransactionShouldUse2PC) hits an assertion as we are not in a coordinated transaction. There is no downside of using a coordinated transaction for single task local queries. --- src/backend/distributed/executor/local_executor.c | 13 +++++++++++++ src/test/regress/expected/single_node.out | 6 +++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/backend/distributed/executor/local_executor.c b/src/backend/distributed/executor/local_executor.c index 0a6cfbbd4..786d5a29d 100644 --- a/src/backend/distributed/executor/local_executor.c +++ b/src/backend/distributed/executor/local_executor.c @@ -209,6 +209,19 @@ ExecuteLocalTaskListExtended(List *taskList, Oid *parameterTypes = NULL; uint64 totalRowsProcessed = 0; + /* + * Even if we are executing local tasks, we still enable + * coordinated transaction. This is because + * (a) we might be in a transaction, and the next commands may + * require coordinated transaction + * (b) we might be executing some tasks locally and the others + * via remote execution + * + * Also, there is no harm enabling coordinated transaction even if + * we only deal with local tasks in the transaction. + */ + UseCoordinatedTransaction(); + if (paramListInfo != NULL) { /* not used anywhere, so declare here */ diff --git a/src/test/regress/expected/single_node.out b/src/test/regress/expected/single_node.out index 599743550..4c9dd7589 100644 --- a/src/test/regress/expected/single_node.out +++ b/src/test/regress/expected/single_node.out @@ -1986,7 +1986,11 @@ ROLLBACK; WITH cte_1 AS (UPDATE another_schema_table SET b = b + 1 WHERE a = 1 RETURNING *) SELECT coordinated_transaction_should_use_2PC() FROM cte_1; NOTICE: executing the command locally: WITH cte_1 AS (UPDATE single_node.another_schema_table_90630511 another_schema_table SET b = (another_schema_table.b OPERATOR(pg_catalog.+) 1) WHERE (another_schema_table.a OPERATOR(pg_catalog.=) 1) RETURNING another_schema_table.a, another_schema_table.b) SELECT single_node.coordinated_transaction_should_use_2pc() AS coordinated_transaction_should_use_2pc FROM cte_1 -ERROR: The transaction is not a coordinated transaction + coordinated_transaction_should_use_2pc +--------------------------------------------------------------------- + t +(1 row) + -- if the local execution is disabled, we cannot failover to -- local execution and the queries would fail SET citus.enable_local_execution TO false;