Skip to content

Commit

Permalink
sql: return pgerror for duplicate regions in super region
Browse files Browse the repository at this point in the history
Previously, when duplicate regions would exist within
a super region an internal assertion would be generated.
This was inadequate since an assertion failure would
lead to a stack dump. To address this, this patch
will generate a proper pgerror.

Fixes: cockroachdb#108556

Release note: None
  • Loading branch information
fqazi committed Sep 6, 2023
1 parent c18029b commit 2de4afc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/secondary_region
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
10 changes: 10 additions & 0 deletions pkg/sql/alter_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2de4afc

Please sign in to comment.