From 33ec33c5b31072bd8a386a6f381d48cf0a07c4c7 Mon Sep 17 00:00:00 2001 From: Onder Kalaci Date: Fri, 15 Sep 2017 02:52:30 +0300 Subject: [PATCH] Ensure schema exists on reference table creation If the schema doesn't exists on the workers, create it. --- .../commands/create_distributed_table.c | 8 +++ .../regress/expected/multi_create_table.out | 55 +++++++++++++++++++ src/test/regress/sql/multi_create_table.sql | 36 ++++++++++++ 3 files changed, 99 insertions(+) diff --git a/src/backend/distributed/commands/create_distributed_table.c b/src/backend/distributed/commands/create_distributed_table.c index 247e41277..1e3d1da9f 100644 --- a/src/backend/distributed/commands/create_distributed_table.c +++ b/src/backend/distributed/commands/create_distributed_table.c @@ -236,6 +236,14 @@ create_reference_table(PG_FUNCTION_ARGS) EnsureCoordinator(); CheckCitusVersion(ERROR); + /* + * Ensure schema exists on each worker node. We can not run this function + * transactionally, since we may create shards over separate sessions and + * shard creation depends on the schema being present and visible from all + * sessions. + */ + EnsureSchemaExistsOnAllNodes(relationId); + /* * Lock target relation with an exclusive lock - there's no way to make * sense of this table until we've committed, and we don't want multiple diff --git a/src/test/regress/expected/multi_create_table.out b/src/test/regress/expected/multi_create_table.out index bba39e136..f498325a5 100644 --- a/src/test/regress/expected/multi_create_table.out +++ b/src/test/regress/expected/multi_create_table.out @@ -873,5 +873,60 @@ SELECT count(*) FROM tt1; 6 (1 row) +-- the goal of the following test is to make sure that +-- both create_reference_table and create_distributed_table +-- calls creates the schemas without leading to any deadlocks +-- first create reference table, then hash distributed table +BEGIN; +CREATE SCHEMA sc; +CREATE TABLE sc.ref(a int); +insert into sc.ref SELECT s FROM generate_series(0, 100) s; +SELECT create_reference_table('sc.ref'); +NOTICE: Copying data from local table... + create_reference_table +------------------------ + +(1 row) + +CREATE TABLE sc.hash(a int); +insert into sc.hash SELECT s FROM generate_series(0, 100) s; +SELECT create_distributed_table('sc.hash', 'a'); +NOTICE: Copying data from local table... + create_distributed_table +-------------------------- + +(1 row) + +COMMIT; +-- first create hash distributed table, then reference table +BEGIN; +CREATE SCHEMA sc2; +CREATE TABLE sc2.hash(a int); +insert into sc2.hash SELECT s FROM generate_series(0, 100) s; +SELECT create_distributed_table('sc2.hash', 'a'); +NOTICE: Copying data from local table... + create_distributed_table +-------------------------- + +(1 row) + +CREATE TABLE sc2.ref(a int); +insert into sc2.ref SELECT s FROM generate_series(0, 100) s; +SELECT create_reference_table('sc2.ref'); +NOTICE: Copying data from local table... + create_reference_table +------------------------ + +(1 row) + +COMMIT; DROP TABLE tt1; DROP TABLE tt2; +DROP SCHEMA sc CASCADE; +NOTICE: drop cascades to 2 other objects +DETAIL: drop cascades to table sc.ref +drop cascades to table sc.hash +DROP SCHEMA sc2 CASCADE; +NOTICE: drop cascades to 2 other objects +DETAIL: drop cascades to table sc2.hash +drop cascades to table sc2.ref diff --git a/src/test/regress/sql/multi_create_table.sql b/src/test/regress/sql/multi_create_table.sql index 531f337ea..d054eb314 100644 --- a/src/test/regress/sql/multi_create_table.sql +++ b/src/test/regress/sql/multi_create_table.sql @@ -482,5 +482,41 @@ END; SELECT count(*) FROM tt1; + +-- the goal of the following test is to make sure that +-- both create_reference_table and create_distributed_table +-- calls creates the schemas without leading to any deadlocks + +-- first create reference table, then hash distributed table +BEGIN; + +CREATE SCHEMA sc; +CREATE TABLE sc.ref(a int); +insert into sc.ref SELECT s FROM generate_series(0, 100) s; +SELECT create_reference_table('sc.ref'); + +CREATE TABLE sc.hash(a int); +insert into sc.hash SELECT s FROM generate_series(0, 100) s; +SELECT create_distributed_table('sc.hash', 'a'); + +COMMIT; + + +-- first create hash distributed table, then reference table +BEGIN; + +CREATE SCHEMA sc2; +CREATE TABLE sc2.hash(a int); +insert into sc2.hash SELECT s FROM generate_series(0, 100) s; +SELECT create_distributed_table('sc2.hash', 'a'); + +CREATE TABLE sc2.ref(a int); +insert into sc2.ref SELECT s FROM generate_series(0, 100) s; +SELECT create_reference_table('sc2.ref'); + +COMMIT; + DROP TABLE tt1; DROP TABLE tt2; +DROP SCHEMA sc CASCADE; +DROP SCHEMA sc2 CASCADE;