From a49ebf71af32fb838bb45dfa17c0c07a099bbff5 Mon Sep 17 00:00:00 2001 From: Monty Date: Mon, 16 Oct 2023 17:14:24 +0300 Subject: [PATCH] Fixed memory leak when using histograms This was introduced in last merge with 10.6 The reason is that 10.6 does not need anything special to free histograms as everything is allocated on a memroot. In 10.10 histograms is using the vector class, which has some problems: - No automatic free - No memory usage accounting (we should at some point remove vector usage because of the above problem) Fixed by expliciting freeing histograms when freeing TABLE_STATISTICS objects. --- sql/sql_statistics.cc | 13 +++++++++++++ sql/sql_statistics.h | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc index b47db263ce9..8c129857095 100644 --- a/sql/sql_statistics.cc +++ b/sql/sql_statistics.cc @@ -91,7 +91,19 @@ TABLE_STATISTICS_CB::TABLE_STATISTICS_CB(): TABLE_STATISTICS_CB::~TABLE_STATISTICS_CB() { + Column_statistics *column_stats= table_stats->column_stats; + Column_statistics *column_stats_end= column_stats + table_stats->columns; DBUG_ASSERT(usage_count == 0); + + /* Free json histograms */ + for (; column_stats < column_stats_end ; column_stats++) + { + delete column_stats->histogram; + /* + Protect against possible other free in free_statistics_for_table() + */ + column_stats->histogram= 0; + } free_root(&mem_root, MYF(0)); } @@ -2381,6 +2393,7 @@ alloc_engine_independent_statistics(THD *thd, const TABLE_SHARE *table_share, bzero(idx_avg_frequency, sizeof(idx_avg_frequency) * key_parts); stats_cb->table_stats= table_stats; + table_stats->columns= table_share->fields; table_stats->column_stats= column_stats; table_stats->index_stats= index_stats; table_stats->idx_avg_frequency= idx_avg_frequency; diff --git a/sql/sql_statistics.h b/sql/sql_statistics.h index e8da71252e2..cf4fc4a4094 100644 --- a/sql/sql_statistics.h +++ b/sql/sql_statistics.h @@ -432,9 +432,9 @@ class Index_statistics; class Table_statistics { - public: my_bool cardinality_is_null; /* TRUE if the cardinality is unknown */ + uint columns; /* Number of columns in table */ ha_rows cardinality; /* Number of rows in the table */ uchar *min_max_record_buffers; /* Record buffers for min/max values */ Column_statistics *column_stats; /* Array of statistical data for columns */