diff --git a/src/backend/distributed/commands/create_citus_local_table.c b/src/backend/distributed/commands/create_citus_local_table.c index 98a23e08e..264cfd324 100644 --- a/src/backend/distributed/commands/create_citus_local_table.c +++ b/src/backend/distributed/commands/create_citus_local_table.c @@ -25,6 +25,7 @@ #include "distributed/commands/sequence.h" #include "distributed/commands/utility_hook.h" #include "distributed/listutils.h" +#include "distributed/local_executor.h" #include "distributed/metadata_sync.h" #include "distributed/multi_partitioning_utils.h" #include "distributed/namespace_utils.h" @@ -113,6 +114,14 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys) /* enable create_citus_local_table on an empty node */ InsertCoordinatorIfClusterEmpty(); + /* + * Creating Citus local tables relies on functions that accesses + * shards locally (e.g., ExecuteAndLogDDLCommand()). As long as + * we don't teach those functions to access shards remotely, we + * cannot relax this check. + */ + SetLocalExecutionStatus(LOCAL_EXECUTION_REQUIRED); + /* * Lock target relation with an AccessExclusiveLock as we don't want * multiple backends manipulating this relation. We could actually simply diff --git a/src/test/regress/expected/ref_citus_local_fkeys.out b/src/test/regress/expected/ref_citus_local_fkeys.out index 44d723404..d8eaaf318 100644 --- a/src/test/regress/expected/ref_citus_local_fkeys.out +++ b/src/test/regress/expected/ref_citus_local_fkeys.out @@ -221,6 +221,19 @@ BEGIN; ALTER TABLE citus_local_table_1 ADD CONSTRAINT multi_fkey FOREIGN KEY (a, b) REFERENCES citus_local_table_2(a, b); NOTICE: executing the command locally: SELECT worker_apply_inter_shard_ddl_command (1506004, 'ref_citus_local_fkeys', 1506005, 'ref_citus_local_fkeys', 'ALTER TABLE citus_local_table_1 ADD CONSTRAINT multi_fkey FOREIGN KEY (a, b) REFERENCES citus_local_table_2(a, b);') COMMIT; +-- when local execution is disabled, citus local table cannot be created +BEGIN; + SET citus.enable_local_execution TO false; + CREATE TABLE referenced_table(id int primary key); + SELECT create_reference_table('referenced_table'); + create_reference_table +--------------------------------------------------------------------- + +(1 row) + + CREATE TABLE referencing_table(id int, ref_id int, FOREIGN KEY(ref_id) REFERENCES referenced_table(id) ON DELETE SET DEFAULT); +ERROR: cannot switch local execution status from local execution disabled to local execution enabled since it can cause visibility problems in the current transaction +ROLLBACK; -- cleanup at exit DROP SCHEMA ref_citus_local_fkeys CASCADE; NOTICE: drop cascades to 6 other objects diff --git a/src/test/regress/sql/ref_citus_local_fkeys.sql b/src/test/regress/sql/ref_citus_local_fkeys.sql index 6a23e43ef..18f2a1d5a 100644 --- a/src/test/regress/sql/ref_citus_local_fkeys.sql +++ b/src/test/regress/sql/ref_citus_local_fkeys.sql @@ -148,5 +148,13 @@ BEGIN; ALTER TABLE citus_local_table_1 ADD CONSTRAINT multi_fkey FOREIGN KEY (a, b) REFERENCES citus_local_table_2(a, b); COMMIT; +-- when local execution is disabled, citus local table cannot be created +BEGIN; + SET citus.enable_local_execution TO false; + CREATE TABLE referenced_table(id int primary key); + SELECT create_reference_table('referenced_table'); + CREATE TABLE referencing_table(id int, ref_id int, FOREIGN KEY(ref_id) REFERENCES referenced_table(id) ON DELETE SET DEFAULT); +ROLLBACK; + -- cleanup at exit DROP SCHEMA ref_citus_local_fkeys CASCADE;