Skip to content

Commit acf8f76

Browse files
authored
Merge pull request #152019 from cockroachdb/blathers/backport-release-25.3-151870
release-25.3: sql,multiregion: prevent DROP REGION in user DB from cleaning up `system.sql_instances`
2 parents 373ff5a + 1afc9a5 commit acf8f76

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

pkg/ccl/multiregionccl/multiregion_system_table_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,45 @@ func TestMrSystemDatabaseUpgrade(t *testing.T) {
598598
{"ALTER PARTITION \"us-east3\" OF INDEX system.public.lease@primary CONFIGURE ZONE USING\n\tnum_voters = 3,\n\tvoter_constraints = '[+region=us-east3]',\n\tlease_preferences = '[[+region=us-east3]]'"},
599599
})
600600
}
601+
602+
// TestDropRegionFromUserDatabaseCleansUpSystemTables verifies that
603+
// dropping a region from a user database doesn't remove rows
604+
// from the system.sql_instances table.
605+
func TestDropRegionFromUserDatabaseCleansUpSystemTables(t *testing.T) {
606+
defer leaktest.AfterTest(t)()
607+
defer log.Scope(t).Close(t)
608+
609+
ctx := context.Background()
610+
611+
makeSettings := func() *cluster.Settings {
612+
cs := cluster.MakeTestingClusterSettings()
613+
instancestorage.ReclaimLoopInterval.Override(ctx, &cs.SV, 150*time.Millisecond)
614+
return cs
615+
}
616+
617+
_, systemSQL, cleanup := multiregionccltestutils.TestingCreateMultiRegionCluster(t, 3,
618+
base.TestingKnobs{},
619+
multiregionccltestutils.WithSettings(makeSettings()))
620+
defer cleanup()
621+
622+
sDB := sqlutils.MakeSQLRunner(systemSQL)
623+
624+
// Create a user database with multiple regions
625+
sDB.Exec(t, `CREATE DATABASE userdb PRIMARY REGION "us-east1" REGIONS "us-east2", "us-east3" SURVIVE ZONE FAILURE`)
626+
627+
// Verify sql_instances has data before the operation
628+
initialCount := sDB.QueryStr(t, `SELECT count(*) FROM system.sql_instances`)
629+
require.NotEmpty(t, initialCount)
630+
require.Equal(t, "13", initialCount[0][0], "sql_instances should have data initially")
631+
632+
// Drop a region from the USER database (not system database)
633+
sDB.Exec(t, `ALTER DATABASE userdb DROP REGION "us-east2"`)
634+
635+
// Verify that dropping a region from a user database doesn't affect system.sql_instances.
636+
// The region still exists in the system database, so instances should remain unchanged.
637+
finalCount := sDB.QueryStr(t, `SELECT count(*) FROM system.sql_instances`)
638+
require.NotEmpty(t, finalCount)
639+
// The count should remain the same since we only dropped from userdb, not system.
640+
require.Equal(t, initialCount[0][0], finalCount[0][0],
641+
"sql_instances count should not change when dropping region from user database")
642+
}

pkg/sql/type_change.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func (t *typeSchemaChanger) exec(ctx context.Context) error {
407407
var idsToRemove []int
408408
populateIDsToRemove := func(holder context.Context, txn descs.Txn) error {
409409
typeDesc, err := txn.Descriptors().MutableByID(txn.KV()).Type(ctx, t.typeID)
410-
if err != nil {
410+
if err != nil || typeDesc.GetParentID() != keys.SystemDatabaseID {
411411
return err
412412
}
413413
for _, member := range typeDesc.EnumMembers {

0 commit comments

Comments
 (0)