From cec90eb9bf1fdecb4f8ce7c4f4eefcc25a2fe3f9 Mon Sep 17 00:00:00 2001 From: Adam Storm Date: Wed, 27 Jan 2021 13:17:47 -0500 Subject: [PATCH] sql: Block adding a primary region to the system database Prevent users from adding a primary region to the system database, thus preventing the system database from becoming a multi-region database. Release note: None --- pkg/sql/alter_database.go | 10 ++++++++++ pkg/sql/logictest/testdata/logic_test/multiregion | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/pkg/sql/alter_database.go b/pkg/sql/alter_database.go index d511eba54bce..f1c53d9ba692 100644 --- a/pkg/sql/alter_database.go +++ b/pkg/sql/alter_database.go @@ -15,6 +15,7 @@ import ( "fmt" "sort" + "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv" @@ -393,6 +394,15 @@ func (n *alterDatabasePrimaryRegionNode) startExec(params runParams) error { return err } + // Block adding a primary region to the system database. This ensures that the system + // database can never be made into a multi-region database. + if n.desc.GetID() == keys.SystemDatabaseID { + return pgerror.Newf( + pgcode.FeatureNotSupported, + "adding a primary region to the system database is not supported", + ) + } + // There are two paths to consider here: either this is the first setting of the // primary region, OR we're updating the primary region. In the case where this // is the first setting of the primary region, the call will turn the database into diff --git a/pkg/sql/logictest/testdata/logic_test/multiregion b/pkg/sql/logictest/testdata/logic_test/multiregion index a6530ed77b49..ef6c8052996c 100644 --- a/pkg/sql/logictest/testdata/logic_test/multiregion +++ b/pkg/sql/logictest/testdata/logic_test/multiregion @@ -329,6 +329,10 @@ USE new_db statement error database new_db is not multi-region enabled, but table cannot_create_table_no_multiregion has locality GLOBAL set CREATE TABLE cannot_create_table_no_multiregion (a int) LOCALITY GLOBAL +# Test adding a primary region to the system database. +statement error adding a primary region to the system database is not supported +ALTER DATABASE system PRIMARY REGION "ap-southeast-2" + statement ok CREATE DATABASE alter_test_db primary region "ca-central-1"