Skip to content

Commit 2cba92a

Browse files
committed
sql/stats: improve SHOW STATISTICS with merged and forecasted stats
This commit make `SHOW STATISTICS` compute merged stats unconditionally, and output them only if `WITH MERGE` is specified. Previously, we would compute merged stats only if `WITH MERGE` was specified, which could prevent us from seeing forecasted stats that depend on merged stats when specifying `WITH FORECAST` (without `MERGE`). This change is consistent with the stats cache, which computes merged stats unconditionally and uses them to create forecasts. Fixes: #155612 Release note (bug fix): Previously, we could have inconsistencies between the forecasted stats shown in `SHOW STATISTICS ... WITH FORECAST` and forecasted stats in the stats cache depending on whether `WITH MERGE` was also specified. We now correctly display all forecasted stats, regardless of `WITH MERGE`.
1 parent 3c76919 commit 2cba92a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pkg/sql/opt/exec/execbuilder/testdata/partial_stats

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,18 @@ WHERE stat->>'name' = '__forecast__';
431431
"row_count": 18
432432
}
433433

434+
# Verify that we display forecasted stats even when we don't show merged stats
435+
# that it is based on.
436+
query TTTIIII
437+
SELECT statistics_name, column_names, created, row_count, distinct_count, null_count, avg_size
438+
FROM [SHOW STATISTICS FOR TABLE g WITH FORECAST]
439+
ORDER BY created
440+
----
441+
__auto__ {b} 1988-08-05 00:00:00 +0000 UTC 3 3 0 1
442+
__auto__ {b} 1988-08-07 00:00:00 +0000 UTC 9 9 0 1
443+
partial {b} 1988-08-08 00:00:00 +0000 UTC 3 3 0 2
444+
__forecast__ {b} 1988-08-10 00:00:00 +0000 UTC 18 18 0 1
445+
434446
# Verify that when the partial histogram is empty
435447
# the returned statistic is the latest full statistic
436448
# renamed to __merged__ with the created_at time

pkg/sql/show_stats.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ func (p *planner) ShowTableStats(ctx context.Context, n *tree.ShowTableStats) (p
195195
statsList[i], statsList[j] = statsList[j], statsList[i]
196196
}
197197

198+
merged := stats.MergedStatistics(ctx, statsList, p.ExtendedEvalContext().Settings)
199+
statsList = append(merged, statsList...)
198200
if withMerge {
199-
merged := stats.MergedStatistics(ctx, statsList, p.ExtendedEvalContext().Settings)
200-
statsList = append(merged, statsList...)
201201
// Iterate in reverse order to match the ORDER BY "columnIDs".
202202
for i := len(merged) - 1; i >= 0; i-- {
203203
mergedRow, err := tableStatisticProtoToRow(&merged[i].TableStatisticProto)

0 commit comments

Comments
 (0)