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.
pull/6976/head^2
Ahmet Gedemenli 2023-06-07 11:02:53 +03:00 committed by GitHub
parent 3f7bc0cbf5
commit 8d8968ae63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 0 deletions

View File

@ -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)));
}
}

View File

@ -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);

View File

@ -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 */

View File

@ -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');

View File

@ -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