Skip to content

Commit c4dbc61

Browse files
craig[bot]dt
andcommitted
Merge #155848
155848: sql: automatically cleanup automatic stats jobs r=michae2 a=dt Automatic stats jobs are frequent -- sometimes almost continious -- background process in many clusters, constantly being created and running as part of its normal operation. In some clusters these completed jobs have been observed to make up more than 90% of the retained content of the jobs system, but in most user-visible surfaces, they are usually filtered out or excluded as they have typically been found to be of low relevance or utility to users looking for more notable, discrete cluster events tied to less continious jobs. This change introduces automatic, immediate cleanup of automatic stats jobs so that they are eagerly removed rather than being retained for the full job retention period. This should dramatically reduce the number of these in the jobs system to just those are executing and could be relevant to the cluster's operation, and those which were manually run (and thus likely of concern to a user), as well as those which failed which could also be of interest. Release note (ops change): successfully completed automatic SQL stats collecton jobs now automatically purged rather than being retained for the full default job retention period. Epic: CRDB-55121. Co-authored-by: David Taylor <[email protected]>
2 parents cdcf76d + 427aee5 commit c4dbc61

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

pkg/sql/create_stats.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ var errorOnConcurrentCreateStats = settings.RegisterBoolSetting(
9191
false,
9292
settings.WithPublic)
9393

94+
var automaticStatsJobAutoCleanup = settings.RegisterBoolSetting(
95+
settings.ApplicationLevel,
96+
"sql.stats.automatic_stats_job_auto_cleanup.enabled",
97+
"set to true to enable automatic cleanup of completed AUTO CREATE STATISTICS jobs",
98+
true)
99+
94100
const nonIndexColHistogramBuckets = 2
95101

96102
// createStatsNode is a planNode implemented in terms of a function. The
@@ -203,6 +209,12 @@ func (n *createStatsNode) runJob(ctx context.Context) error {
203209
return nil
204210
}
205211
}
212+
} else if automaticStatsJobAutoCleanup.Get(n.p.ExecCfg().SV()) {
213+
if name := job.Details().(jobspb.CreateStatsDetails).Name; name == jobspb.AutoStatsName || name == jobspb.AutoPartialStatsName {
214+
if err := n.p.ExecCfg().JobRegistry.DeleteTerminalJobByID(ctx, job.ID()); err != nil {
215+
log.Dev.Warningf(ctx, "failed to auto-delete terminal automatic stats job: %v", err)
216+
}
217+
}
206218
}
207219
return err
208220
}

pkg/sql/create_stats_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ func TestAutoPartialStatsJobDescription(t *testing.T) {
303303
// Disable automatic statistics collection.
304304
sqlRunner.Exec(t, `SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;`)
305305
sqlRunner.Exec(t, `SET CLUSTER SETTING sql.stats.automatic_partial_collection.enabled = false;`)
306+
sqlRunner.Exec(t, `SET CLUSTER SETTING sql.stats.automatic_stats_job_auto_cleanup.enabled = false;`)
306307

307308
// Create a test table.
308309
sqlRunner.Exec(t, `CREATE TABLE test (id INT PRIMARY KEY, value INT);`)

0 commit comments

Comments
 (0)