From 7250db8610d13543e3979259e4ad5771893b5a8f Mon Sep 17 00:00:00 2001 From: Eric Harmeling Date: Mon, 19 Sep 2022 19:38:37 +0000 Subject: [PATCH] sql: version gate idx recommendations in insert-stmt-stats This commit version gates index recommendation insert in insert-stmt-stats. Fixes #88140. Release note: None --- .../sqlstats/persistedsqlstats/BUILD.bazel | 1 + pkg/sql/sqlstats/persistedsqlstats/flush.go | 54 +++++++++++-------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/pkg/sql/sqlstats/persistedsqlstats/BUILD.bazel b/pkg/sql/sqlstats/persistedsqlstats/BUILD.bazel index bd86dc8808ea..0af9efcc0afe 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/kv", diff --git a/pkg/sql/sqlstats/persistedsqlstats/flush.go b/pkg/sql/sqlstats/persistedsqlstats/flush.go index 64d0045c4f53..38eccaa15236 100644 --- a/pkg/sql/sqlstats/persistedsqlstats/flush.go +++ b/pkg/sql/sqlstats/persistedsqlstats/flush.go @@ -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" @@ -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. @@ -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", @@ -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