Skip to content

Commit c70f0a8

Browse files
huansongavamingli
authored andcommitted
Support ALTER TABLE SET DISTRIBUTED BY for external tables
The external tables have distribution policy but it does not dictate actual data distribution. It is only used when unloading data, to compare with source table's distribution policy. Therefore, when supporting ALTER TABLE SET DISTRIBUTED BY for external tables, we don't really need to re-organize the table but just need to make sure the catalog change happens.
1 parent c53d3db commit c70f0a8

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/backend/commands/tablecmds.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5563,7 +5563,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
55635563
pass = AT_PASS_MISC;
55645564
break;
55655565
}
5566-
ATSimplePermissions(rel, ATT_TABLE | ATT_DIRECTORY_TABLE);
5566+
ATSimplePermissions(rel, ATT_TABLE | ATT_DIRECTORY_TABLE | ATT_FOREIGN_TABLE);
55675567

55685568
if (!recursing) /* MPP-5772, MPP-5784 */
55695569
{
@@ -18620,6 +18620,12 @@ ATExecSetDistributedBy(Relation rel, Node *node, AlterTableCmd *cmd)
1862018620

1862118621
lwith = nlist;
1862218622
}
18623+
/* External tables cannot really be re-organized. Error out if we are instructed to do so.*/
18624+
if (force_reorg && rel_is_external_table(RelationGetRelid(rel)))
18625+
ereport(ERROR,
18626+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
18627+
errmsg("cannot reorganize external table \"%s\"",
18628+
RelationGetRelationName(rel))));
1862318629

1862418630
if (ldistro)
1862518631
change_policy = true;
@@ -18853,7 +18859,8 @@ ATExecSetDistributedBy(Relation rel, Node *node, AlterTableCmd *cmd)
1885318859
{
1885418860
need_reorg = true;
1885518861
}
18856-
else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
18862+
else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ||
18863+
rel_is_external_table(RelationGetRelid(rel)))
1885718864
need_reorg = false;
1885818865
else
1885918866
elog(ERROR, "unexpected relkind '%c'", rel->rd_rel->relkind);

src/test/regress/input/external_table.source

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,9 +3614,6 @@ ALTER TABLE ext_w_dist SET DISTRIBUTED BY (b);
36143614
SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 'ext_w_dist'::regclass;
36153615
ALTER TABLE ext_w_dist SET DISTRIBUTED RANDOMLY;
36163616
SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 'ext_w_dist'::regclass;
3617-
CREATE EXTERNAL WEB TABLE ext_r_dist(a int) EXECUTE 'printf ${GP_SEGMENT_ID}' FORMAT 'TEXT' DISTRIBUTED BY (a);
3618-
CREATE EXTERNAL WEB TABLE ext_r_dist(a int) EXECUTE 'printf ${GP_SEGMENT_ID}' FORMAT 'TEXT';
3619-
ALTER TABLE ext_r_dist SET DISTRIBUTED BY (a); -- should error out altering readable external tables' distribution policy
36203617

36213618
-- Testing external table as the partition child.
36223619
CREATE TABLE part_root(a int) PARTITION BY RANGE(a);
@@ -3629,7 +3626,7 @@ ALTER TABLE part_root ATTACH PARTITION part_ext_w FOR VALUES FROM (10) TO (20);
36293626
ALTER TABLE part_root ADD COLUMN b int;
36303627
INSERT INTO part_root SELECT i,i FROM generate_series(1,19)i;
36313628

3632-
-- altering distribution policy should work fine
3629+
-- altering distribution policy should work fine
36333630
SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 'part_ext_w'::regclass;
36343631
ALTER TABLE part_root SET DISTRIBUTED BY (b);
36353632
SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 'part_ext_w'::regclass;

src/test/regress/output/external_table.source

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4916,34 +4916,27 @@ DROP TABLE test_part_integrity;
49164916
-- Testing altering the distribution policy of external tables.
49174917
CREATE WRITABLE EXTERNAL WEB TABLE ext_w_dist(a int, b int) EXECUTE 'cat > @abs_srcdir@/data/ext_w_dist.tbl' FORMAT 'TEXT' (DELIMITER AS '|' NULL AS 'null' ESCAPE AS ' ') DISTRIBUTED BY (a);
49184918
ALTER TABLE ext_w_dist SET WITH (reorganize=true); -- should error out if forcing reorganize
4919-
ERROR: "ext_w_dist" is not a table or directory table
4919+
ERROR: cannot reorganize external table "ext_w_dist"
49204920
SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 'ext_w_dist'::regclass;
49214921
policytype | distkey
49224922
------------+---------
49234923
p | 1
49244924
(1 row)
49254925

49264926
ALTER TABLE ext_w_dist SET DISTRIBUTED BY (b);
4927-
ERROR: "ext_w_dist" is not a table or directory table
49284927
SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 'ext_w_dist'::regclass;
49294928
policytype | distkey
49304929
------------+---------
4931-
p | 1
4930+
p | 2
49324931
(1 row)
49334932

49344933
ALTER TABLE ext_w_dist SET DISTRIBUTED RANDOMLY;
4935-
ERROR: "ext_w_dist" is not a table or directory table
49364934
SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 'ext_w_dist'::regclass;
49374935
policytype | distkey
49384936
------------+---------
4939-
p | 1
4937+
p |
49404938
(1 row)
49414939

4942-
CREATE EXTERNAL WEB TABLE ext_r_dist(a int) EXECUTE 'printf ${GP_SEGMENT_ID}' FORMAT 'TEXT' DISTRIBUTED BY (a);
4943-
ERROR: readable external tables can't specify a DISTRIBUTED BY clause
4944-
CREATE EXTERNAL WEB TABLE ext_r_dist(a int) EXECUTE 'printf ${GP_SEGMENT_ID}' FORMAT 'TEXT';
4945-
ALTER TABLE ext_r_dist SET DISTRIBUTED BY (a); -- should error out altering readable external tables' distribution policy
4946-
ERROR: "ext_r_dist" is not a table or directory table
49474940
-- Testing external table as the partition child.
49484941
CREATE TABLE part_root(a int) PARTITION BY RANGE(a);
49494942
CREATE TABLE part_child (LIKE part_root);
@@ -4953,19 +4946,18 @@ ALTER TABLE part_root ATTACH PARTITION part_ext_w FOR VALUES FROM (10) TO (20);
49534946
-- Adding column should work fine
49544947
ALTER TABLE part_root ADD COLUMN b int;
49554948
INSERT INTO part_root SELECT i,i FROM generate_series(1,19)i;
4956-
-- altering distribution policy should work fine
4949+
-- altering distribution policy should work fine
49574950
SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 'part_ext_w'::regclass;
49584951
policytype | distkey
49594952
------------+---------
49604953
p | 1
49614954
(1 row)
49624955

49634956
ALTER TABLE part_root SET DISTRIBUTED BY (b);
4964-
ERROR: "part_ext_w" is not a table or directory table
49654957
SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 'part_ext_w'::regclass;
49664958
policytype | distkey
49674959
------------+---------
4968-
p | 1
4960+
p | 2
49694961
(1 row)
49704962

49714963
DROP TABLE part_root;

0 commit comments

Comments
 (0)