-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stats: automatically delete stats for dropped tables
This commit adds another task to the stats refresher that periodically runs a query to delete stats for dropped tables from the system table. By default, this query runs once an hour, but this can be configured via a cluster setting (which also exposes a way to disable this new "stats garbage collector" when it is set to 0). The query also limits the number of dropped tables to process at once to 1000 by default (controled via another cluster setting). The rationale for introducing the limit is to prevent a huge DELETE when the cluster that has been running for long time with many dropped tables has just upgraded to the binary with this fix. Release note (bug fix): CockroachDB now automatically deletes statistics for dropped tables from `system.table_statistics` table.
- Loading branch information
1 parent
320e861
commit 874a00e
Showing
6 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package stats_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql" | ||
"github.com/cockroachdb/cockroach/pkg/sql/tests" | ||
"github.com/cockroachdb/cockroach/pkg/testutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// TestStatsAreDeletedForDroppedTables ensures that statistics for dropped | ||
// tables are automatically deleted. | ||
func TestStatsAreDeletedForDroppedTables(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
params, _ := tests.CreateTestServerParams() | ||
params.Knobs.GCJob = &sql.GCJobTestingKnobs{ | ||
// We disable the usage of DeleteRange primitive in order to speed up | ||
// the test. | ||
DisableDelRange: true, | ||
} | ||
s, sqlDB, _ := serverutils.StartServer(t, params) | ||
defer s.Stopper().Stop(context.Background()) | ||
runner := sqlutils.MakeSQLRunner(sqlDB) | ||
|
||
// Disable auto stats so that it doesn't interfere. | ||
runner.Exec(t, "SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;") | ||
// Lower the garbage collection interval to speed up the test. | ||
runner.Exec(t, "SET CLUSTER SETTING sql.stats.garbage_collection_interval = '1s';") | ||
|
||
// Create a table with short TTL and collect stats on it. | ||
runner.Exec(t, "CREATE TABLE t (k PRIMARY KEY) AS SELECT 1;") | ||
runner.Exec(t, "ALTER TABLE t CONFIGURE ZONE USING gc.ttlseconds = 1;") | ||
runner.Exec(t, "ANALYZE t;") | ||
|
||
r := runner.QueryRow(t, "SELECT 't'::regclass::oid") | ||
var tableID int | ||
r.Scan(&tableID) | ||
|
||
// Ensure that we see a single statistic for the table. | ||
var count int | ||
runner.QueryRow(t, `SELECT count(*) FROM system.table_statistics WHERE "tableID" = $1;`, tableID).Scan(&count) | ||
if count != 1 { | ||
t.Fatalf("expected a single statistic for table 't', found %d", count) | ||
} | ||
|
||
// Now drop the table and make sure that the table statistic is deleted | ||
// promptly. | ||
runner.Exec(t, "DROP TABLE t;") | ||
testutils.SucceedsSoon(t, func() error { | ||
runner.QueryRow(t, `SELECT count(*) FROM system.table_statistics WHERE "tableID" = $1;`, tableID).Scan(&count) | ||
if count != 0 { | ||
return errors.Newf("expected no stats for the dropped table, found %d statistics", count) | ||
} | ||
return nil | ||
}) | ||
} |