Commit 7231d573 authored by Rohan Yadav's avatar Rohan Yadav

sql: disallow creation of schemas with the "pg_" prefix

This is disallowed by Postgres, and will be useful when integrating user
defined schema resolution with the temporary schema (which isn't backed
by a descriptor).

Release note: None
parent 08f1c789
......@@ -12,6 +12,7 @@ package sql
import (
"context"
"strings"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
......@@ -19,7 +20,9 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/errors"
)
type createSchemaNode struct {
......@@ -80,6 +83,13 @@ func (p *planner) createUserDefinedSchema(params runParams, n *tree.CreateSchema
return pgerror.Newf(pgcode.DuplicateSchema, "schema %q already exists", n.Schema)
}
// Schemas starting with "pg_" are not allowed.
if strings.HasPrefix(n.Schema, sessiondata.PgSchemaPrefix) {
err := pgerror.Newf(pgcode.ReservedName, "unacceptable schema name %q", n.Schema)
err = errors.WithDetail(err, `The prefix "pg_" is reserved for system schemas.`)
return err
}
// Ensure that the cluster version is high enough to create the schema.
if !params.p.ExecCfg().Settings.Version.IsActive(params.ctx, clusterversion.VersionUserDefinedSchemas) {
return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
......
......@@ -33,3 +33,6 @@ CREATE SCHEMA pg_catalog
statement error schema .* already exists
CREATE SCHEMA information_schema
statement error pq: unacceptable schema name \"pg_temp\"
CREATE SCHEMA pg_temp
......@@ -29,6 +29,10 @@ const InformationSchemaName = "information_schema"
// CRDBInternalSchemaName is the name of the crdb_internal system schema.
const CRDBInternalSchemaName = "crdb_internal"
// PgSchemaPrefix is a prefix for Postgres system schemas. Users cannot
// create schemas with this prefix.
const PgSchemaPrefix = "pg_"
// PgTempSchemaName is the alias for temporary schemas across sessions.
const PgTempSchemaName = "pg_temp"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment