diff --git a/pkg/ccl/logictestccl/testdata/logic_test/secondary_region b/pkg/ccl/logictestccl/testdata/logic_test/secondary_region index 1c79b165b4bb..400c2686e33e 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/secondary_region +++ b/pkg/ccl/logictestccl/testdata/logic_test/secondary_region @@ -469,6 +469,9 @@ ALTER DATABASE mr3 ADD SUPER REGION "test1" VALUES "us-east-1", "us-west-1" statement ok ALTER DATABASE mr3 ADD SUPER REGION "test2" VALUES "us-central-1", "ca-central-1" +statement error pq: duplicate region us-central-1 found in super region test3 +ALTER DATABASE mr3 ADD SUPER REGION "test3" VALUES "us-central-1", "ca-central-1", "us-central-1" + statement error pq: the secondary region must be in the same super region as the current primary region ALTER DATABASE mr3 SET SECONDARY REGION "us-east-1" diff --git a/pkg/sql/alter_database.go b/pkg/sql/alter_database.go index 3c61a6c107e0..d54a0c8a4596 100644 --- a/pkg/sql/alter_database.go +++ b/pkg/sql/alter_database.go @@ -1548,6 +1548,16 @@ func (p *planner) AlterDatabaseAddSuperRegion( return nil, err } + // Validate no duplicate regions exist. + existingRegionNames := make(map[tree.Name]struct{}) + for _, region := range n.Regions { + if _, found := existingRegionNames[region]; found { + return nil, pgerror.Newf(pgcode.DuplicateObject, + "duplicate region %s found in super region %s", region, n.SuperRegionName) + } + existingRegionNames[region] = struct{}{} + } + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.DatabaseName)) if err != nil { return nil, err