@@ -1165,6 +1165,9 @@ func TestApproxMaxSchemaObjects(t *testing.T) {
1165
1165
defer leaktest .AfterTest (t )()
1166
1166
defer log .Scope (t ).Close (t )
1167
1167
1168
+ skip .UnderDuress (t , "slow test, requires polling to wait for auto stats job" )
1169
+ skip .UnderShort (t , "slow test, requires polling to wait for auto stats job" )
1170
+
1168
1171
ctx := context .Background ()
1169
1172
1170
1173
// Test with both declarative schema changer modes.
@@ -1174,40 +1177,41 @@ func TestApproxMaxSchemaObjects(t *testing.T) {
1174
1177
defer s .Stopper ().Stop (ctx )
1175
1178
1176
1179
tdb := sqlutils .MakeSQLRunner (sqlDB )
1180
+ tdb .Exec (t , "SET CLUSTER SETTING sql.stats.automatic_collection.min_stale_rows = 1" )
1177
1181
1178
1182
// Configure the declarative schema changer mode.
1179
1183
if useDeclarative {
1184
+ tdb .Exec (t , `SET sql.defaults.use_declarative_schema_changer = 'on'` )
1180
1185
tdb .Exec (t , `SET use_declarative_schema_changer = 'on'` )
1181
1186
} else {
1187
+ tdb .Exec (t , `SET sql.defaults.use_declarative_schema_changer = 'off'` )
1182
1188
tdb .Exec (t , `SET use_declarative_schema_changer = 'off'` )
1183
1189
}
1184
1190
1185
- // Get the current count of descriptors to set a realistic limit.
1186
- var currentCount int
1187
- tdb .QueryRow (t , `SELECT count(*) FROM system.descriptor` ).Scan (& currentCount )
1191
+ var maxObjects int
1192
+ updateMaxObjects := func () {
1193
+ // Manually refresh stats.
1194
+ tdb .Exec (t , "ANALYZE system.public.descriptor" )
1188
1195
1189
- // Set the limit to be slightly more than current count .
1190
- maxObjects := currentCount + 5
1191
- tdb .Exec (t , fmt . Sprintf ( `SET CLUSTER SETTING sql.schema.approx_max_object_count = %d` , maxObjects ) )
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 )
1192
1199
1193
- // Create a test database and use it.
1194
- tdb .Exec (t , `CREATE DATABASE testdb` )
1195
- tdb .Exec (t , `USE testdb` )
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
+ }
1204
+ updateMaxObjects ()
1196
1205
1197
1206
// Test that different object types are subject to the limit.
1198
1207
objectTypes := []string {"table" , "database" , "schema" , "type" , "function" }
1199
1208
for _ , objectType := range objectTypes {
1200
1209
t .Run (objectType , func (t * testing.T ) {
1201
1210
// Increase the limit before each subtest to avoid interference.
1202
- maxObjects += 10
1203
- tdb .Exec (t , fmt .Sprintf (`SET CLUSTER SETTING sql.schema.approx_max_object_count = %d` , maxObjects ))
1204
-
1205
- // Try to create objects until we hit the limit.
1206
- // ANALYZE before starting to ensure stats are fresh.
1207
- tdb .Exec (t , `ANALYZE system.descriptor` )
1211
+ updateMaxObjects ()
1208
1212
1209
1213
objNum := 0
1210
- for {
1214
+ testutils . SucceedsWithin ( t , func () error {
1211
1215
var createStmt string
1212
1216
switch objectType {
1213
1217
case "table" :
@@ -1224,30 +1228,22 @@ func TestApproxMaxSchemaObjects(t *testing.T) {
1224
1228
1225
1229
_ , err := sqlDB .Exec (createStmt )
1226
1230
if err != nil {
1227
- // Check if we got the expected error.
1231
+ // Check if we got the expected error and message .
1228
1232
if pqErr := (* pq .Error )(nil ); errors .As (err , & pqErr ) {
1229
1233
if string (pqErr .Code ) == pgcode .ConfigurationLimitExceeded .String () {
1230
- // Verify the error message mentions " approximate maximum".
1231
- testutils . IsError ( err , "would exceed approximate maximum" )
1232
- break
1234
+ if testutils . IsError ( err , "would exceed approximate maximum") {
1235
+ return nil
1236
+ }
1233
1237
}
1234
1238
}
1235
1239
// Some other error occurred.
1236
- t . Fatal ( err )
1240
+ return err
1237
1241
}
1238
1242
objNum ++
1239
1243
1240
- // Re-analyze periodically to update stats.
1241
- if objNum % 2 == 0 {
1242
- tdb .Exec (t , `ANALYZE system.descriptor` )
1243
- }
1244
-
1245
- // Safety check: if we created way more objects than expected,
1246
- // something is wrong.
1247
- if objNum > 30 {
1248
- t .Fatalf ("created %d %ss without hitting limit (max=%d)" , objNum , objectType , maxObjects )
1249
- }
1250
- }
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 )
1251
1247
})
1252
1248
}
1253
1249
})
0 commit comments