diff --git a/src/backend/distributed/commands/alter_table.c b/src/backend/distributed/commands/alter_table.c index 3728e7470..40324cfcb 100644 --- a/src/backend/distributed/commands/alter_table.c +++ b/src/backend/distributed/commands/alter_table.c @@ -196,6 +196,7 @@ static void EnsureTableNotReferencing(Oid relationId, char conversionType); static void EnsureTableNotReferenced(Oid relationId, char conversionType); static void EnsureTableNotForeign(Oid relationId); static void EnsureTableNotPartition(Oid relationId); +static void ErrorIfColocateWithTenantTable(char *colocateWith); static TableConversionState * CreateTableConversion(TableConversionParameters *params); static void CreateDistributedTableLike(TableConversionState *con); static void CreateCitusTableLike(TableConversionState *con); @@ -437,6 +438,9 @@ AlterDistributedTable(TableConversionParameters *params) "is not distributed"))); } + ErrorIfTenantTable(params->relationId, "alter_distributed_table"); + ErrorIfColocateWithTenantTable(params->colocateWith); + EnsureTableNotForeign(params->relationId); EnsureTableNotPartition(params->relationId); EnsureHashDistributedTable(params->relationId); @@ -1182,6 +1186,24 @@ EnsureTableNotPartition(Oid relationId) } +/* + * ErrorIfColocateWithTenantTable errors out if given colocateWith text refers to + * a tenant table. + */ +void +ErrorIfColocateWithTenantTable(char *colocateWith) +{ + if (colocateWith != NULL && + !IsColocateWithDefault(colocateWith) && + !IsColocateWithNone(colocateWith)) + { + text *colocateWithTableNameText = cstring_to_text(colocateWith); + Oid colocateWithTableId = ResolveRelationId(colocateWithTableNameText, false); + ErrorIfTenantTable(colocateWithTableId, "colocate_with"); + } +} + + TableConversionState * CreateTableConversion(TableConversionParameters *params) { diff --git a/src/test/regress/expected/schema_based_sharding.out b/src/test/regress/expected/schema_based_sharding.out index 0a529398a..84ca562e3 100644 --- a/src/test/regress/expected/schema_based_sharding.out +++ b/src/test/regress/expected/schema_based_sharding.out @@ -83,6 +83,12 @@ ERROR: test_table is not allowed for colocate_with because it is a tenant table -- verify we don't allow undistribute_table for tenant tables SELECT undistribute_table('tenant_2.test_table'); ERROR: test_table is not allowed for undistribute_table because it is a tenant table +-- verify we don't allow alter_distributed_table for tenant tables +SELECT alter_distributed_table('tenant_2.test_table', colocate_with => 'none'); +ERROR: test_table is not allowed for alter_distributed_table because it is a tenant table +-- verify we also don't allow colocate_with a tenant table +SELECT alter_distributed_table('regular_schema.test_table', colocate_with => 'tenant_2.test_table'); +ERROR: test_table is not allowed for colocate_with because it is a tenant table -- (on coordinator) verify that colocation id is set for empty tenants too SELECT colocationid > 0 FROM pg_dist_tenant_schema WHERE schemaid::regnamespace::text IN ('tenant_1', 'tenant_3'); diff --git a/src/test/regress/sql/schema_based_sharding.sql b/src/test/regress/sql/schema_based_sharding.sql index 469145a91..f24628731 100644 --- a/src/test/regress/sql/schema_based_sharding.sql +++ b/src/test/regress/sql/schema_based_sharding.sql @@ -61,6 +61,10 @@ SELECT update_distributed_table_colocation('tenant_2.test_table', colocate_with SELECT update_distributed_table_colocation('regular_schema.test_table', colocate_with => 'tenant_2.test_table'); -- verify we don't allow undistribute_table for tenant tables SELECT undistribute_table('tenant_2.test_table'); +-- verify we don't allow alter_distributed_table for tenant tables +SELECT alter_distributed_table('tenant_2.test_table', colocate_with => 'none'); +-- verify we also don't allow colocate_with a tenant table +SELECT alter_distributed_table('regular_schema.test_table', colocate_with => 'tenant_2.test_table'); -- (on coordinator) verify that colocation id is set for empty tenants too SELECT colocationid > 0 FROM pg_dist_tenant_schema