From 5db380f33a3f94539a081e6c9d88f40205bb3aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanefi=20=C3=96nald=C4=B1?= Date: Fri, 30 Oct 2020 13:52:22 +0300 Subject: [PATCH] Prevent undistribute_table calls for foreign tables --- .../commands/create_distributed_table.c | 6 ++++++ src/test/regress/expected/undistribute_table.out | 16 ++++++++++++++++ src/test/regress/multi_schedule | 5 ++++- src/test/regress/sql/undistribute_table.sql | 11 +++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/backend/distributed/commands/create_distributed_table.c b/src/backend/distributed/commands/create_distributed_table.c index c8f3ac587..c7cb26887 100644 --- a/src/backend/distributed/commands/create_distributed_table.c +++ b/src/backend/distributed/commands/create_distributed_table.c @@ -1585,6 +1585,12 @@ UndistributeTable(Oid relationId) "because a foreign key references to it."))); } + char relationKind = get_rel_relkind(relationId); + if (relationKind == RELKIND_FOREIGN_TABLE) + { + ereport(ERROR, (errmsg("Cannot undistribute table " + "because it is a foreign table."))); + } List *preLoadCommands = GetPreLoadTableCreationCommands(relationId, true); List *postLoadCommands = GetPostLoadTableCreationCommands(relationId); diff --git a/src/test/regress/expected/undistribute_table.out b/src/test/regress/expected/undistribute_table.out index a15631efb..0d664d895 100644 --- a/src/test/regress/expected/undistribute_table.out +++ b/src/test/regress/expected/undistribute_table.out @@ -126,6 +126,22 @@ ERROR: Cannot undistribute table because a foreign key references to it. SELECT undistribute_table('referencing_table'); ERROR: Cannot undistribute table because it has a foreign key. DROP TABLE referenced_table, referencing_table; +-- test distributed foreign tables +-- we expect errors +CREATE FOREIGN TABLE foreign_table ( + id bigint not null, + full_name text not null default '' +) SERVER fake_fdw_server OPTIONS (encoding 'utf-8', compression 'true'); +SELECT create_distributed_table('foreign_table', 'id'); +NOTICE: foreign-data wrapper "fake_fdw" does not have an extension defined + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +SELECT undistribute_table('foreign_table'); +ERROR: Cannot undistribute table because it is a foreign table. +DROP FOREIGN TABLE foreign_table; -- test partitioned tables CREATE TABLE partitioned_table (id INT, a INT) PARTITION BY RANGE (id); CREATE TABLE partitioned_table_1_5 PARTITION OF partitioned_table FOR VALUES FROM (1) TO (5); diff --git a/src/test/regress/multi_schedule b/src/test/regress/multi_schedule index 57ac0f47a..43fc468d5 100644 --- a/src/test/regress/multi_schedule +++ b/src/test/regress/multi_schedule @@ -65,6 +65,10 @@ test: ensure_no_intermediate_data_leak # ---------- test: multi_partitioning_utils multi_partitioning replicated_partitioned_table +# ---------- +# Tests for foreign data wrapper support +# ---------- +test: multi_create_fdw # ---------- # Tests for recursive subquery planning @@ -199,7 +203,6 @@ test: multi_outer_join # Note that the order of the following tests are important. multi_complex_count_distinct # is independent from the rest of the group, it is added to increase parallelism. # --- -test: multi_create_fdw test: multi_complex_count_distinct multi_select_distinct test: multi_modifications test: multi_distribution_metadata diff --git a/src/test/regress/sql/undistribute_table.sql b/src/test/regress/sql/undistribute_table.sql index ca393c5d9..d019d89cc 100644 --- a/src/test/regress/sql/undistribute_table.sql +++ b/src/test/regress/sql/undistribute_table.sql @@ -52,6 +52,17 @@ SELECT undistribute_table('referencing_table'); DROP TABLE referenced_table, referencing_table; +-- test distributed foreign tables +-- we expect errors +CREATE FOREIGN TABLE foreign_table ( + id bigint not null, + full_name text not null default '' +) SERVER fake_fdw_server OPTIONS (encoding 'utf-8', compression 'true'); +SELECT create_distributed_table('foreign_table', 'id'); +SELECT undistribute_table('foreign_table'); + +DROP FOREIGN TABLE foreign_table; + -- test partitioned tables CREATE TABLE partitioned_table (id INT, a INT) PARTITION BY RANGE (id); CREATE TABLE partitioned_table_1_5 PARTITION OF partitioned_table FOR VALUES FROM (1) TO (5);