Skip to content

Commit

Permalink
sql: fix reset_sql_stats to truncate activity tables
Browse files Browse the repository at this point in the history
Previously:
The reset stats on the ui and crdb_internal.reset_sql_stats() would
only reset the statement_statistics and transaction_statics tables.
This would leave the sql_activity table with old data. The reset stats
now truncates the sql_activity table as well.

Fixes: #104321
Epic: none

Release note (sql change): Fix crdb_internal.reset_sql_stats() to
 cleanup the sql_activity table which work as a cache for the stats.
  • Loading branch information
j82w committed Jun 5, 2023
1 parent d71019d commit fb5f2ff
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 fb5f2ff

Please sign in to comment.