From 2e42e582dbb45f4b2d45f8e22f4777882127b9f9 Mon Sep 17 00:00:00 2001 From: Marylia Gutierrez Date: Thu, 11 Aug 2022 15:12:22 -0400 Subject: [PATCH] sql: fix aggregation of statistics Previously, because we were using a join, we were double counting statistics when we had the same fingerprint in memory and persisted. This commit adds a `DISTINCT` so we only count them once. Fixes #85958 Release note: None --- pkg/sql/crdb_internal.go | 2 +- pkg/sql/logictest/testdata/logic_test/create_statements | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/sql/crdb_internal.go b/pkg/sql/crdb_internal.go index 129a456edbea..9827b26634f1 100644 --- a/pkg/sql/crdb_internal.go +++ b/pkg/sql/crdb_internal.go @@ -5571,7 +5571,7 @@ SELECT plan_hash, app_name, max(metadata) as metadata, - crdb_internal.merge_statement_stats(array_agg(statistics)), + crdb_internal.merge_statement_stats(array_agg(DISTINCT statistics)), max(sampled_plan), aggregation_interval, array_remove(array_agg(index_rec), NULL) AS index_recommendations diff --git a/pkg/sql/logictest/testdata/logic_test/create_statements b/pkg/sql/logictest/testdata/logic_test/create_statements index 543ad9394a3e..ab9260c2eb0a 100644 --- a/pkg/sql/logictest/testdata/logic_test/create_statements +++ b/pkg/sql/logictest/testdata/logic_test/create_statements @@ -1512,7 +1512,7 @@ CREATE VIEW crdb_internal.statement_statistics ( plan_hash, app_name, max(metadata) AS metadata, - crdb_internal.merge_statement_stats(array_agg(statistics)), + crdb_internal.merge_statement_stats(array_agg(DISTINCT statistics)), max(sampled_plan), aggregation_interval, array_remove(array_agg(index_rec), NULL) AS index_recommendations @@ -1571,7 +1571,7 @@ CREATE VIEW crdb_internal.statement_statistics ( plan_hash, app_name, max(metadata) AS metadata, - crdb_internal.merge_statement_stats(array_agg(statistics)), + crdb_internal.merge_statement_stats(array_agg(DISTINCT statistics)), max(sampled_plan), aggregation_interval, array_remove(array_agg(index_rec), NULL) AS index_recommendations