diff --git a/pkg/sql/sql_activity_update_job_test.go b/pkg/sql/sql_activity_update_job_test.go index 545fc46af1d1..355aaefc3764 100644 --- a/pkg/sql/sql_activity_update_job_test.go +++ b/pkg/sql/sql_activity_update_job_test.go @@ -186,6 +186,34 @@ func TestSqlActivityUpdateJob(t *testing.T) { err = row.Scan(&count) require.NoError(t, err) require.Equal(t, count, 1, "statement_activity after transfer: expect:1, actual:%d", count) + + // Reset the stats and verify it's empty + _, err = db.ExecContext(ctx, "SELECT crdb_internal.reset_sql_stats()") + require.NoError(t, err) + + row = db.QueryRowContext(ctx, "SELECT count_rows() "+ + "FROM system.public.transaction_activity") + err = row.Scan(&count) + require.NoError(t, err) + require.Zero(t, count, "transaction_activity after transfer: expect:0, actual:%d", count) + + row = db.QueryRowContext(ctx, "SELECT count_rows() "+ + "FROM system.public.statement_activity") + err = row.Scan(&count) + require.NoError(t, err) + require.Zero(t, count, "statement_activity after transfer: expect:0, actual:%d", count) + + row = db.QueryRowContext(ctx, "SELECT count_rows() "+ + "FROM crdb_internal.transaction_activity") + err = row.Scan(&count) + require.NoError(t, err) + require.Zero(t, count, "transaction_activity after transfer: expect:0, actual:%d", count) + + row = db.QueryRowContext(ctx, "SELECT count_rows() "+ + "FROM crdb_internal.statement_activity") + err = row.Scan(&count) + require.NoError(t, err) + require.Zero(t, count, "statement_activity after transfer: expect:0, actual:%d", count) } // TestSqlActivityUpdateJob verifies that the diff --git a/pkg/sql/sqlstats/persistedsqlstats/BUILD.bazel b/pkg/sql/sqlstats/persistedsqlstats/BUILD.bazel index 4aca0403cbc5..cdd63e2ab86a 100644 --- a/pkg/sql/sqlstats/persistedsqlstats/BUILD.bazel +++ b/pkg/sql/sqlstats/persistedsqlstats/BUILD.bazel @@ -24,6 +24,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/base", + "//pkg/clusterversion", "//pkg/jobs", "//pkg/jobs/jobspb", "//pkg/scheduledjobs", diff --git a/pkg/sql/sqlstats/persistedsqlstats/controller.go b/pkg/sql/sqlstats/persistedsqlstats/controller.go index 5c6a760ec86c..77d725496675 100644 --- a/pkg/sql/sqlstats/persistedsqlstats/controller.go +++ b/pkg/sql/sqlstats/persistedsqlstats/controller.go @@ -13,6 +13,7 @@ package persistedsqlstats import ( "context" + "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/server/serverpb" "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/isql" @@ -68,9 +69,22 @@ func (s *Controller) ResetClusterSQLStats(ctx context.Context) error { "TRUNCATE "+tableName) return err } + if err := resetSysTableStats("system.statement_statistics"); err != nil { return err } - return resetSysTableStats("system.transaction_statistics") + if err := resetSysTableStats("system.transaction_statistics"); err != nil { + return err + } + + if !s.st.Version.IsActive(ctx, clusterversion.V23_1CreateSystemActivityUpdateJob) { + return nil + } + + if err := resetSysTableStats("system.statement_activity"); err != nil { + return err + } + + return resetSysTableStats("system.transaction_activity") }