Skip to content

Commit

Permalink
Merge pull request #104361 from cockroachdb/blathers/backport-release…
Browse files Browse the repository at this point in the history
…-23.1-104337

release-23.1: sql: fix reset_sql_stats to truncate activity tables
  • Loading branch information
j82w authored Jun 6, 2023
2 parents 9dfe6f7 + fb5f2ff commit e47b24c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
28 changes: 28 additions & 0 deletions pkg/sql/sql_activity_update_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/sqlstats/persistedsqlstats/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/base",
"//pkg/clusterversion",
"//pkg/jobs",
"//pkg/jobs/jobspb",
"//pkg/scheduledjobs",
Expand Down
16 changes: 15 additions & 1 deletion pkg/sql/sqlstats/persistedsqlstats/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

0 comments on commit e47b24c

Please sign in to comment.