Skip to content

Commit

Permalink
Merge #98000
Browse files Browse the repository at this point in the history
98000: sql: allow ALTER SCHEMA public OWNER TO new_owner r=ajwerner a=rafiss

fixes #97994

Release note (bug fix): The owner of the public schema can now be changed. Use `ALTER SCHEMA public OWNER TO new_owner`.

Co-authored-by: Rafi Shamim <[email protected]>
  • Loading branch information
craig[bot] and rafiss committed Mar 6, 2023
2 parents 353006b + d290440 commit f959dd6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
9 changes: 6 additions & 3 deletions pkg/sql/alter_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ func (p *planner) AlterSchema(ctx context.Context, n *tree.AlterSchema) (planNod
if err != nil {
return nil, err
}
// Explicitly disallow modifying Public schema in 22.1.
if schema.GetName() == tree.PublicSchema {
return nil, pgerror.Newf(pgcode.InvalidSchemaName, "cannot modify schema %q", n.Schema.String())
// Explicitly disallow renaming public schema. In the future, we may want
// to support this. In order to support it, we have to remove the logic that
// automatically re-creates the public schema if it doesn't exist.
_, isRename := n.Cmd.(*tree.AlterSchemaRename)
if schema.GetName() == tree.PublicSchema && isRename {
return nil, pgerror.Newf(pgcode.InvalidSchemaName, "cannot rename schema %q", n.Schema.String())
}
switch schema.SchemaKind() {
case catalog.SchemaPublic, catalog.SchemaVirtual, catalog.SchemaTemporary:
Expand Down
24 changes: 19 additions & 5 deletions pkg/sql/logictest/testdata/logic_test/schema
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,35 @@ SELECT * FROM myschema.tb2
statement ok
SET search_path TO public

# We should be able to alter the public schema owner.
statement ok
ALTER SCHEMA public OWNER TO testuser

query TT
SELECT schema_name, owner FROM [SHOW SCHEMAS] WHERE schema_name = 'public'
----
public testuser

# Try to create a temp table in a user defined schema.
statement error pq: cannot create temporary relation in non-temporary schema
statement error pgcode 42P16 pq: cannot create temporary relation in non-temporary schema
CREATE TEMP TABLE myschema.tmp (x int)

# We should error out trying to modify any virtual schemas.
statement error pq: schema cannot be modified: "pg_catalog"
statement error pgcode 42501 pq: schema cannot be modified: "pg_catalog"
CREATE TABLE pg_catalog.bad (x int)

# We shouldn't be able to alter virtual or public schemas.
statement error pq: cannot modify schema "public"
# We shouldn't be able to rename the public schema. In the future, we may
# want to support this.
statement error pgcode 3F000 pq: cannot rename schema "public"
ALTER SCHEMA public RENAME TO private

statement error pq: cannot modify schema "pg_catalog"
# We shouldn't be able to alter virtual schemas.
statement error pgcode 3F000 pq: cannot modify schema "pg_catalog"
ALTER SCHEMA pg_catalog RENAME TO mysql_catalog

statement error pgcode 3F000 pq: cannot modify schema "pg_catalog"
ALTER SCHEMA pg_catalog OWNER TO root

# We can't rename a schema to a pg_temp prefixed name.
statement error pq: unacceptable schema name "pg_temp_not_temp"
ALTER SCHEMA myschema RENAME TO pg_temp_not_temp
Expand Down

0 comments on commit f959dd6

Please sign in to comment.