From 8d8968ae630ad846e6cde2cb1bbe2abd89a9adb9 Mon Sep 17 00:00:00 2001 From: Ahmet Gedemenli Date: Wed, 7 Jun 2023 11:02:53 +0300 Subject: [PATCH] Disable ALTER TABLE .. SET SCHEMA for tenant tables (#6973) Disables `ALTER TABLE .. SET SCHEMA` for tenant tables. Disables `ALTER TABLE .. SET SCHEMA` for tenant schemas. --- .../commands/schema_based_sharding.c | 17 +++++++++++++++++ src/backend/distributed/commands/table.c | 4 ++++ src/include/distributed/commands.h | 1 + .../regress/expected/schema_based_sharding.out | 9 +++++++++ src/test/regress/sql/schema_based_sharding.sql | 6 ++++++ 5 files changed, 37 insertions(+) diff --git a/src/backend/distributed/commands/schema_based_sharding.c b/src/backend/distributed/commands/schema_based_sharding.c index b7f1ba70a..77daaa629 100644 --- a/src/backend/distributed/commands/schema_based_sharding.c +++ b/src/backend/distributed/commands/schema_based_sharding.c @@ -258,3 +258,20 @@ ErrorIfTenantTable(Oid relationId, char *operationName) operationName))); } } + + +/* + * ErrorIfTenantSchema errors out with the given operation name, + * if the given schema is a tenant schema. + */ +void +ErrorIfTenantSchema(Oid nspOid, char *operationName) +{ + if (IsTenantSchema(nspOid)) + { + ereport(ERROR, (errmsg( + "%s is not allowed for %s because it is a distributed schema", + get_namespace_name(nspOid), + operationName))); + } +} diff --git a/src/backend/distributed/commands/table.c b/src/backend/distributed/commands/table.c index 2286c292a..df0aa757b 100644 --- a/src/backend/distributed/commands/table.c +++ b/src/backend/distributed/commands/table.c @@ -2293,6 +2293,10 @@ PreprocessAlterTableSchemaStmt(Node *node, const char *queryString, return NIL; } + ErrorIfTenantTable(relationId, "ALTER TABLE SET SCHEMA"); + ErrorIfTenantSchema(get_namespace_oid(stmt->newschema, false), + "ALTER TABLE SET SCHEMA"); + DDLJob *ddlJob = palloc0(sizeof(DDLJob)); QualifyTreeNode((Node *) stmt); ObjectAddressSet(ddlJob->targetObjectAddress, RelationRelationId, relationId); diff --git a/src/include/distributed/commands.h b/src/include/distributed/commands.h index 3064f83cd..f5dc6e898 100644 --- a/src/include/distributed/commands.h +++ b/src/include/distributed/commands.h @@ -790,5 +790,6 @@ extern void ErrorIfIllegalPartitioningInTenantSchema(Oid parentRelationId, Oid partitionRelationId); extern void CreateTenantSchemaTable(Oid relationId); extern void ErrorIfTenantTable(Oid relationId, char *operationName); +extern void ErrorIfTenantSchema(Oid nspOid, char *operationName); #endif /*CITUS_COMMANDS_H */ diff --git a/src/test/regress/expected/schema_based_sharding.out b/src/test/regress/expected/schema_based_sharding.out index 7c17e5bb8..ba3018b15 100644 --- a/src/test/regress/expected/schema_based_sharding.out +++ b/src/test/regress/expected/schema_based_sharding.out @@ -89,6 +89,15 @@ ERROR: tenant_2.test_table is not allowed for alter_distributed_table because i -- 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: tenant_2.test_table is not allowed for colocate_with because it is a tenant table +-- verify we don't allow ALTER TABLE SET SCHEMA for tenant tables +ALTER TABLE tenant_2.test_table SET SCHEMA regular_schema; +ERROR: tenant_2.test_table is not allowed for ALTER TABLE SET SCHEMA because it is a tenant table +-- verify we don't allow ALTER TABLE SET SCHEMA for tenant schemas +ALTER TABLE regular_schema.test_table SET SCHEMA tenant_2; +ERROR: tenant_2 is not allowed for ALTER TABLE SET SCHEMA because it is a distributed schema +-- the same, from tenant schema to tenant schema +ALTER TABLE tenant_2.test_table SET SCHEMA tenant_3; +ERROR: tenant_2.test_table is not allowed for ALTER TABLE SET SCHEMA 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 27741473b..309797a74 100644 --- a/src/test/regress/sql/schema_based_sharding.sql +++ b/src/test/regress/sql/schema_based_sharding.sql @@ -65,6 +65,12 @@ SELECT undistribute_table('tenant_2.test_table'); 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'); +-- verify we don't allow ALTER TABLE SET SCHEMA for tenant tables +ALTER TABLE tenant_2.test_table SET SCHEMA regular_schema; +-- verify we don't allow ALTER TABLE SET SCHEMA for tenant schemas +ALTER TABLE regular_schema.test_table SET SCHEMA tenant_2; +-- the same, from tenant schema to tenant schema +ALTER TABLE tenant_2.test_table SET SCHEMA tenant_3; -- (on coordinator) verify that colocation id is set for empty tenants too SELECT colocationid > 0 FROM pg_dist_tenant_schema