diff --git a/src/backend/distributed/commands/alter_table.c b/src/backend/distributed/commands/alter_table.c index e31498fe4..59a9d8bbc 100644 --- a/src/backend/distributed/commands/alter_table.c +++ b/src/backend/distributed/commands/alter_table.c @@ -640,6 +640,8 @@ ConvertTable(TableConversionState *con) Oid partitionRelationId = InvalidOid; foreach_oid(partitionRelationId, partitionList) { + char *tableQualifiedName = generate_qualified_relation_name( + partitionRelationId); char *detachPartitionCommand = GenerateDetachPartitionCommand( partitionRelationId); char *attachPartitionCommand = GenerateAlterTableAttachPartitionCommand( @@ -685,6 +687,19 @@ ConvertTable(TableConversionState *con) foreignKeyCommands = list_concat(foreignKeyCommands, partitionReturn->foreignKeyCommands); } + + + /* + * If we are altering a partitioned distributed table by + * colocateWith:none, we override con->colocationWith parameter + * with the first newly created partition table to share the + * same colocation group for rest of partitions and partitioned + * table. + */ + if (con->colocateWith != NULL && IsColocateWithNone(con->colocateWith)) + { + con->colocateWith = tableQualifiedName; + } } } diff --git a/src/test/regress/expected/alter_distributed_table.out b/src/test/regress/expected/alter_distributed_table.out index b29b10168..02c5b2ca6 100644 --- a/src/test/regress/expected/alter_distributed_table.out +++ b/src/test/regress/expected/alter_distributed_table.out @@ -321,6 +321,55 @@ SELECT * FROM partitioned_table_6_10 ORDER BY 1, 2; 7 | 2 (1 row) +-- test altering partitioned table colocate_with:none +CREATE TABLE foo (x int, y int, t timestamptz default now()) PARTITION BY RANGE (t); +CREATE TABLE foo_1 PARTITION of foo for VALUES FROM ('2022-01-01') TO ('2022-12-31'); +CREATE TABLE foo_2 PARTITION of foo for VALUES FROM ('2023-01-01') TO ('2023-12-31'); +SELECT create_distributed_table('foo','x'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +CREATE TABLE foo_bar (x int, y int, t timestamptz default now()) PARTITION BY RANGE (t); +CREATE TABLE foo_bar_1 PARTITION of foo_bar for VALUES FROM ('2022-01-01') TO ('2022-12-31'); +CREATE TABLE foo_bar_2 PARTITION of foo_bar for VALUES FROM ('2023-01-01') TO ('2023-12-31'); +SELECT create_distributed_table('foo_bar','x'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +SELECT COUNT(DISTINCT colocationid) FROM pg_dist_partition WHERE logicalrelid::regclass::text in ('foo', 'foo_1', 'foo_2', 'foo_bar', 'foo_bar_1', 'foo_bar_2'); + count +--------------------------------------------------------------------- + 1 +(1 row) + +SELECT alter_distributed_table('foo', colocate_with => 'none'); +NOTICE: converting the partitions of alter_distributed_table.foo +NOTICE: creating a new table for alter_distributed_table.foo_1 +NOTICE: moving the data of alter_distributed_table.foo_1 +NOTICE: dropping the old alter_distributed_table.foo_1 +NOTICE: renaming the new table to alter_distributed_table.foo_1 +NOTICE: creating a new table for alter_distributed_table.foo_2 +NOTICE: moving the data of alter_distributed_table.foo_2 +NOTICE: dropping the old alter_distributed_table.foo_2 +NOTICE: renaming the new table to alter_distributed_table.foo_2 +NOTICE: creating a new table for alter_distributed_table.foo +NOTICE: dropping the old alter_distributed_table.foo +NOTICE: renaming the new table to alter_distributed_table.foo + alter_distributed_table +--------------------------------------------------------------------- + +(1 row) + +SELECT COUNT(DISTINCT colocationid) FROM pg_dist_partition WHERE logicalrelid::regclass::text in ('foo', 'foo_1', 'foo_2', 'foo_bar', 'foo_bar_1', 'foo_bar_2'); + count +--------------------------------------------------------------------- + 2 +(1 row) + -- test references CREATE TABLE referenced_dist_table (a INT UNIQUE); CREATE TABLE referenced_ref_table (a INT UNIQUE); diff --git a/src/test/regress/sql/alter_distributed_table.sql b/src/test/regress/sql/alter_distributed_table.sql index bfedf83b4..06dfc1a9d 100644 --- a/src/test/regress/sql/alter_distributed_table.sql +++ b/src/test/regress/sql/alter_distributed_table.sql @@ -84,6 +84,24 @@ SELECT * FROM partitioned_table ORDER BY 1, 2; SELECT * FROM partitioned_table_1_5 ORDER BY 1, 2; SELECT * FROM partitioned_table_6_10 ORDER BY 1, 2; +-- test altering partitioned table colocate_with:none +CREATE TABLE foo (x int, y int, t timestamptz default now()) PARTITION BY RANGE (t); +CREATE TABLE foo_1 PARTITION of foo for VALUES FROM ('2022-01-01') TO ('2022-12-31'); +CREATE TABLE foo_2 PARTITION of foo for VALUES FROM ('2023-01-01') TO ('2023-12-31'); + +SELECT create_distributed_table('foo','x'); + +CREATE TABLE foo_bar (x int, y int, t timestamptz default now()) PARTITION BY RANGE (t); +CREATE TABLE foo_bar_1 PARTITION of foo_bar for VALUES FROM ('2022-01-01') TO ('2022-12-31'); +CREATE TABLE foo_bar_2 PARTITION of foo_bar for VALUES FROM ('2023-01-01') TO ('2023-12-31'); + +SELECT create_distributed_table('foo_bar','x'); + +SELECT COUNT(DISTINCT colocationid) FROM pg_dist_partition WHERE logicalrelid::regclass::text in ('foo', 'foo_1', 'foo_2', 'foo_bar', 'foo_bar_1', 'foo_bar_2'); + +SELECT alter_distributed_table('foo', colocate_with => 'none'); + +SELECT COUNT(DISTINCT colocationid) FROM pg_dist_partition WHERE logicalrelid::regclass::text in ('foo', 'foo_1', 'foo_2', 'foo_bar', 'foo_bar_1', 'foo_bar_2'); -- test references CREATE TABLE referenced_dist_table (a INT UNIQUE);