@@ -1159,93 +1159,99 @@ CREATE TABLE other_schema.t1(n int REFERENCES complex_drop_schema.t1(n));
11591159 `cannot create "complex_drop_schema.sc1" because the target database or schema does not exist` )
11601160}
11611161
1162- // TestApproxMaxSchemaObjects tests that the approximate max schema objects
1163- // guardrail works correctly with both the legacy and declarative schema changers.
1164- func TestApproxMaxSchemaObjects (t * testing.T ) {
1165- defer leaktest .AfterTest (t )()
1162+ func testApproxMaxSchemaObjectsImpl (t * testing.T , useDeclarative bool ) {
11661163 defer log .Scope (t ).Close (t )
11671164
11681165 skip .UnderDuress (t , "slow test, requires polling to wait for auto stats job" )
11691166 skip .UnderShort (t , "slow test, requires polling to wait for auto stats job" )
11701167
11711168 ctx := context .Background ()
11721169
1173- // Test with both declarative schema changer modes.
1174- for _ , useDeclarative := range []bool {false , true } {
1175- t .Run (fmt .Sprintf ("declarative=%t" , useDeclarative ), func (t * testing.T ) {
1176- s , sqlDB , _ := serverutils .StartServer (t , base.TestServerArgs {})
1177- defer s .Stopper ().Stop (ctx )
1170+ s , sqlDB , _ := serverutils .StartServer (t , base.TestServerArgs {})
1171+ defer s .Stopper ().Stop (ctx )
11781172
1179- tdb := sqlutils .MakeSQLRunner (sqlDB )
1180- tdb .Exec (t , "SET CLUSTER SETTING sql.stats.automatic_collection.min_stale_rows = 1" )
1173+ tdb := sqlutils .MakeSQLRunner (sqlDB )
1174+ tdb .Exec (t , "SET CLUSTER SETTING sql.stats.automatic_collection.min_stale_rows = 1" )
1175+
1176+ // Configure the declarative schema changer mode.
1177+ if useDeclarative {
1178+ tdb .Exec (t , `SET sql.defaults.use_declarative_schema_changer = 'on'` )
1179+ tdb .Exec (t , `SET use_declarative_schema_changer = 'on'` )
1180+ } else {
1181+ tdb .Exec (t , `SET sql.defaults.use_declarative_schema_changer = 'off'` )
1182+ tdb .Exec (t , `SET use_declarative_schema_changer = 'off'` )
1183+ }
11811184
1182- // Configure the declarative schema changer mode.
1183- if useDeclarative {
1184- tdb .Exec (t , `SET sql.defaults.use_declarative_schema_changer = 'on'` )
1185- tdb .Exec (t , `SET use_declarative_schema_changer = 'on'` )
1186- } else {
1187- tdb .Exec (t , `SET sql.defaults.use_declarative_schema_changer = 'off'` )
1188- tdb .Exec (t , `SET use_declarative_schema_changer = 'off'` )
1189- }
1185+ var maxObjects int
1186+ updateMaxObjects := func () {
1187+ // Manually refresh stats.
1188+ tdb .Exec (t , "ANALYZE system.public.descriptor" )
11901189
1191- var maxObjects int
1192- updateMaxObjects := func () {
1193- // Manually refresh stats.
1194- tdb .Exec (t , "ANALYZE system.public.descriptor" )
1190+ // Get the current count of descriptors to set a realistic limit.
1191+ var currentCount int
1192+ tdb .QueryRow (t , `SELECT count(*) FROM system.descriptor` ).Scan (& currentCount )
11951193
1196- // Get the current count of descriptors to set a realistic limit.
1197- var currentCount int
1198- tdb .QueryRow (t , `SELECT count(*) FROM system.descriptor` ).Scan (& currentCount )
1194+ // Set the limit to be slightly more than current count.
1195+ maxObjects = currentCount + 1
1196+ tdb .Exec (t , fmt .Sprintf (`SET CLUSTER SETTING sql.schema.approx_max_object_count = %d` , maxObjects ))
1197+ }
1198+ updateMaxObjects ()
11991199
1200- // Set the limit to be slightly more than current count.
1201- maxObjects = currentCount + 1
1202- tdb .Exec (t , fmt .Sprintf (`SET CLUSTER SETTING sql.schema.approx_max_object_count = %d` , maxObjects ))
1203- }
1200+ // Test that different object types are subject to the limit.
1201+ objectTypes := []string {"table" , "database" , "schema" , "type" , "function" }
1202+ for _ , objectType := range objectTypes {
1203+ t .Run (objectType , func (t * testing.T ) {
1204+ // Increase the limit before each subtest to avoid interference.
12041205 updateMaxObjects ()
12051206
1206- // Test that different object types are subject to the limit.
1207- objectTypes := []string {"table" , "database" , "schema" , "type" , "function" }
1208- for _ , objectType := range objectTypes {
1209- t .Run (objectType , func (t * testing.T ) {
1210- // Increase the limit before each subtest to avoid interference.
1211- updateMaxObjects ()
1212-
1213- objNum := 0
1214- testutils .SucceedsWithin (t , func () error {
1215- var createStmt string
1216- switch objectType {
1217- case "table" :
1218- createStmt = fmt .Sprintf (`CREATE TABLE t%d (id INT PRIMARY KEY)` , objNum )
1219- case "database" :
1220- createStmt = fmt .Sprintf (`CREATE DATABASE db%d` , objNum )
1221- case "schema" :
1222- createStmt = fmt .Sprintf (`CREATE SCHEMA sc%d` , objNum )
1223- case "type" :
1224- createStmt = fmt .Sprintf (`CREATE TYPE enum%d AS ENUM ('a', 'b', 'c')` , objNum )
1225- case "function" :
1226- createStmt = fmt .Sprintf (`CREATE FUNCTION f%d() RETURNS INT LANGUAGE SQL AS $$ SELECT 1 $$` , objNum )
1227- }
1207+ objNum := 0
1208+ testutils .SucceedsWithin (t , func () error {
1209+ var createStmt string
1210+ switch objectType {
1211+ case "table" :
1212+ createStmt = fmt .Sprintf (`CREATE TABLE t%d (id INT PRIMARY KEY)` , objNum )
1213+ case "database" :
1214+ createStmt = fmt .Sprintf (`CREATE DATABASE db%d` , objNum )
1215+ case "schema" :
1216+ createStmt = fmt .Sprintf (`CREATE SCHEMA sc%d` , objNum )
1217+ case "type" :
1218+ createStmt = fmt .Sprintf (`CREATE TYPE enum%d AS ENUM ('a', 'b', 'c')` , objNum )
1219+ case "function" :
1220+ createStmt = fmt .Sprintf (`CREATE FUNCTION f%d() RETURNS INT LANGUAGE SQL AS $$ SELECT 1 $$` , objNum )
1221+ }
12281222
1229- _ , err := sqlDB .Exec (createStmt )
1230- if err != nil {
1231- // Check if we got the expected error and message.
1232- if pqErr := (* pq .Error )(nil ); errors .As (err , & pqErr ) {
1233- if string (pqErr .Code ) == pgcode .ConfigurationLimitExceeded .String () {
1234- if testutils .IsError (err , "would exceed approximate maximum" ) {
1235- return nil
1236- }
1237- }
1223+ _ , err := sqlDB .Exec (createStmt )
1224+ if err != nil {
1225+ // Check if we got the expected error and message.
1226+ if pqErr := (* pq .Error )(nil ); errors .As (err , & pqErr ) {
1227+ if string (pqErr .Code ) == pgcode .ConfigurationLimitExceeded .String () {
1228+ if testutils .IsError (err , "would exceed approximate maximum" ) {
1229+ return nil
12381230 }
1239- // Some other error occurred.
1240- return err
12411231 }
1242- objNum ++
1232+ }
1233+ // Some other error occurred.
1234+ return err
1235+ }
1236+ objNum ++
12431237
1244- // Haven't hit the limit yet, keep trying.
1245- return errors .Errorf ("created %d %ss without hitting limit (max=%d)" , objNum , objectType , maxObjects )
1246- }, 5 * time .Minute )
1247- })
1248- }
1238+ // Haven't hit the limit yet, keep trying.
1239+ return errors .Errorf ("created %d %ss without hitting limit (max=%d)" , objNum , objectType , maxObjects )
1240+ }, 5 * time .Minute )
12491241 })
12501242 }
12511243}
1244+
1245+ // TestApproxMaxSchemaObjects tests that the approximate max schema objects
1246+ // guardrail works correctly with the declarative schema changer.
1247+ func TestApproxMaxSchemaObjectsDeclarative (t * testing.T ) {
1248+ defer leaktest .AfterTest (t )()
1249+ testApproxMaxSchemaObjectsImpl (t , true )
1250+ }
1251+
1252+ // TestApproxMaxSchemaObjects tests that the approximate max schema objects
1253+ // guardrail works correctly with the legacy schema changer.
1254+ func TestApproxMaxSchemaObjectsLegacy (t * testing.T ) {
1255+ defer leaktest .AfterTest (t )()
1256+ testApproxMaxSchemaObjectsImpl (t , false )
1257+ }
0 commit comments