Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix reset_sql_stats to truncate activity tables #104337

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}