Skip to content

Commit

Permalink
sql: version gate idx recommendations in insert-stmt-stats
Browse files Browse the repository at this point in the history
This commit version gates index recommendation insert in insert-stmt-stats.

Fixes #88140.

Release note: None
  • Loading branch information
ericharmeling committed Sep 19, 2022
1 parent 9c5a69d commit 7250db8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
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/kv",
Expand Down
54 changes: 32 additions & 22 deletions pkg/sql/sqlstats/persistedsqlstats/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ package persistedsqlstats

import (
"context"
"fmt"
"time"

"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security/username"
Expand Down Expand Up @@ -472,13 +474,7 @@ func (s *PersistedSQLStats) insertStatementStats(
serializedPlanHash []byte,
stats *roachpb.CollectedStatementStatistics,
) (rowsAffected int, err error) {
insertStmt := `
INSERT INTO system.statement_statistics
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
ON CONFLICT (crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_plan_hash_transaction_fingerprint_id_shard_8,
aggregated_ts, fingerprint_id, transaction_fingerprint_id, app_name, plan_hash, node_id)
DO NOTHING
`

aggInterval := s.GetAggregationInterval()

// Prepare data for insertion.
Expand All @@ -495,13 +491,37 @@ DO NOTHING
statistics := tree.NewDJSON(statisticsJSON)

plan := tree.NewDJSON(sqlstatsutil.ExplainTreePlanNodeToJSON(&stats.Stats.SensitiveInfo.MostRecentPlanDescription))
indexRecommendations := tree.NewDArray(types.String)
for _, recommendation := range stats.Stats.IndexRecommendations {
if err := indexRecommendations.Append(tree.NewDString(recommendation)); err != nil {
return 0, err
values := "$1 ,$2, $3, $4, $5, $6, $7, $8, $9, $10"
args := append(make([]interface{}, 0, 11),
aggregatedTs, // aggregated_ts
serializedFingerprintID, // fingerprint_id
serializedTransactionFingerprintID, // transaction_fingerprint_id
serializedPlanHash, // plan_hash
stats.Key.App, // app_name
s.cfg.SQLIDContainer.SQLInstanceID(), // node_id
aggInterval, // agg_interval
metadata, // metadata
statistics, // statistics
plan, // plan
)
if s.cfg.Settings.Version.IsActive(ctx, clusterversion.AlterSystemStatementStatisticsAddIndexRecommendations) {
values = values + ", $11"
indexRecommendations := tree.NewDArray(types.String)
for _, recommendation := range stats.Stats.IndexRecommendations {
if err := indexRecommendations.Append(tree.NewDString(recommendation)); err != nil {
return 0, err
}
}
args = append(args, indexRecommendations)
}

insertStmt := fmt.Sprintf(`
INSERT INTO system.statement_statistics
VALUES (%s)
ON CONFLICT (crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_plan_hash_transaction_fingerprint_id_shard_8,
aggregated_ts, fingerprint_id, transaction_fingerprint_id, app_name, plan_hash, node_id)
DO NOTHING
`, values)
rowsAffected, err = s.cfg.InternalExecutor.ExecEx(
ctx,
"insert-stmt-stats",
Expand All @@ -510,17 +530,7 @@ DO NOTHING
User: username.NodeUserName(),
},
insertStmt,
aggregatedTs, // aggregated_ts
serializedFingerprintID, // fingerprint_id
serializedTransactionFingerprintID, // transaction_fingerprint_id
serializedPlanHash, // plan_hash
stats.Key.App, // app_name
s.cfg.SQLIDContainer.SQLInstanceID(), // node_id
aggInterval, // agg_interval
metadata, // metadata
statistics, // statistics
plan, // plan
indexRecommendations, // index_recommendations
args...,
)

return rowsAffected, err
Expand Down

0 comments on commit 7250db8

Please sign in to comment.