From 8544346a78fb3853fc06122e9db9343b8266e12c Mon Sep 17 00:00:00 2001 From: Marco Slot Date: Fri, 16 Sep 2022 10:55:02 +0200 Subject: [PATCH] Allow create_distributed_table_concurrently on an empty node (#6353) Co-authored-by: Marco Slot --- .../citus_add_local_table_to_metadata.c | 3 + .../commands/create_distributed_table.c | 1 - src/test/regress/expected/multi_extension.out | 60 +++++++++++++++++++ src/test/regress/sql/multi_extension.sql | 34 +++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/backend/distributed/commands/citus_add_local_table_to_metadata.c b/src/backend/distributed/commands/citus_add_local_table_to_metadata.c index 0e2bd0ecd..83f542492 100644 --- a/src/backend/distributed/commands/citus_add_local_table_to_metadata.c +++ b/src/backend/distributed/commands/citus_add_local_table_to_metadata.c @@ -128,6 +128,9 @@ citus_add_local_table_to_metadata_internal(Oid relationId, bool cascadeViaForeig { CheckCitusVersion(ERROR); + /* enable citus_add_local_table_to_metadata on an empty node */ + InsertCoordinatorIfClusterEmpty(); + bool autoConverted = false; CreateCitusLocalTable(relationId, cascadeViaForeignKeys, autoConverted); } diff --git a/src/backend/distributed/commands/create_distributed_table.c b/src/backend/distributed/commands/create_distributed_table.c index 128fb16b7..236e505ef 100644 --- a/src/backend/distributed/commands/create_distributed_table.c +++ b/src/backend/distributed/commands/create_distributed_table.c @@ -382,7 +382,6 @@ CreateDistributedTableConcurrently(Oid relationId, char *distributionColumnName, "citus.shard_replication_factor > 1"))); } - EnsureCoordinatorIsInMetadata(); EnsureCitusTableCanBeCreated(relationId); EnsureValidDistributionColumn(relationId, distributionColumnName); diff --git a/src/test/regress/expected/multi_extension.out b/src/test/regress/expected/multi_extension.out index 4ae526935..c26a65497 100644 --- a/src/test/regress/expected/multi_extension.out +++ b/src/test/regress/expected/multi_extension.out @@ -1521,6 +1521,66 @@ SELECT count(*) FROM pg_stat_activity WHERE application_name = 'Citus Maintenanc 1 (1 row) +-- confirm that we can create a distributed table concurrently on an empty node +DROP EXTENSION citus; +CREATE EXTENSION citus; +CREATE TABLE test (x int, y int); +INSERT INTO test VALUES (1,2); +SET citus.shard_replication_factor TO 1; +SET citus.defer_drop_after_shard_split TO off; +SELECT create_distributed_table_concurrently('test','x'); +NOTICE: relation test does not have a REPLICA IDENTITY or PRIMARY KEY +DETAIL: UPDATE and DELETE commands on the relation will error out during create_distributed_table_concurrently unless there is a REPLICA IDENTITY or PRIMARY KEY. INSERT commands will still work. + create_distributed_table_concurrently +--------------------------------------------------------------------- + +(1 row) + +DROP TABLE test; +TRUNCATE pg_dist_node; +-- confirm that we can create a distributed table on an empty node +CREATE TABLE test (x int, y int); +INSERT INTO test VALUES (1,2); +SET citus.shard_replication_factor TO 1; +SELECT create_distributed_table('test','x'); +NOTICE: Copying data from local table... +NOTICE: copying the data has completed +DETAIL: The local data in the table is no longer visible, but is still on disk. +HINT: To remove the local data, run: SELECT truncate_local_data_after_distributing_table($$public.test$$) + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +DROP TABLE test; +TRUNCATE pg_dist_node; +-- confirm that we can create a reference table on an empty node +CREATE TABLE test (x int, y int); +INSERT INTO test VALUES (1,2); +SELECT create_reference_table('test'); +NOTICE: Copying data from local table... +NOTICE: copying the data has completed +DETAIL: The local data in the table is no longer visible, but is still on disk. +HINT: To remove the local data, run: SELECT truncate_local_data_after_distributing_table($$public.test$$) + create_reference_table +--------------------------------------------------------------------- + +(1 row) + +DROP TABLE test; +TRUNCATE pg_dist_node; +-- confirm that we can create a local table on an empty node +CREATE TABLE test (x int, y int); +INSERT INTO test VALUES (1,2); +SELECT citus_add_local_table_to_metadata('test'); + citus_add_local_table_to_metadata +--------------------------------------------------------------------- + +(1 row) + +DROP TABLE test; +DROP EXTENSION citus; +CREATE EXTENSION citus; DROP TABLE version_mismatch_table; DROP SCHEMA multi_extension; ERROR: cannot drop schema multi_extension because other objects depend on it diff --git a/src/test/regress/sql/multi_extension.sql b/src/test/regress/sql/multi_extension.sql index cfac9397c..a911542b2 100644 --- a/src/test/regress/sql/multi_extension.sql +++ b/src/test/regress/sql/multi_extension.sql @@ -795,5 +795,39 @@ FROM test.maintenance_worker(); -- confirm that there is only one maintenance daemon SELECT count(*) FROM pg_stat_activity WHERE application_name = 'Citus Maintenance Daemon'; +-- confirm that we can create a distributed table concurrently on an empty node +DROP EXTENSION citus; +CREATE EXTENSION citus; +CREATE TABLE test (x int, y int); +INSERT INTO test VALUES (1,2); +SET citus.shard_replication_factor TO 1; +SET citus.defer_drop_after_shard_split TO off; +SELECT create_distributed_table_concurrently('test','x'); +DROP TABLE test; +TRUNCATE pg_dist_node; + +-- confirm that we can create a distributed table on an empty node +CREATE TABLE test (x int, y int); +INSERT INTO test VALUES (1,2); +SET citus.shard_replication_factor TO 1; +SELECT create_distributed_table('test','x'); +DROP TABLE test; +TRUNCATE pg_dist_node; + +-- confirm that we can create a reference table on an empty node +CREATE TABLE test (x int, y int); +INSERT INTO test VALUES (1,2); +SELECT create_reference_table('test'); +DROP TABLE test; +TRUNCATE pg_dist_node; + +-- confirm that we can create a local table on an empty node +CREATE TABLE test (x int, y int); +INSERT INTO test VALUES (1,2); +SELECT citus_add_local_table_to_metadata('test'); +DROP TABLE test; +DROP EXTENSION citus; +CREATE EXTENSION citus; + DROP TABLE version_mismatch_table; DROP SCHEMA multi_extension;