Skip to content

Commit 49bbc71

Browse files
craig[bot]Uzair5162
andcommitted
Merge #155615
155615: sql/stats: improve SHOW STATISTICS with merged and forecasted stats r=Uzair5162 a=Uzair5162 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`. Co-authored-by: Uzair Ahmad <uzair.ahmad@cockroachlabs.com>
2 parents e6d3d9d + 2cba92a commit 49bbc71

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
@@ -419,6 +419,18 @@ WHERE stat->>'name' = '__forecast__';
419419
"row_count": 18
420420
}
421421

422+
# Verify that we display forecasted stats even when we don't show merged stats
423+
# that it is based on.
424+
query TTTIIII
425+
SELECT statistics_name, column_names, created, row_count, distinct_count, null_count, avg_size
426+
FROM [SHOW STATISTICS FOR TABLE g WITH FORECAST]
427+
ORDER BY created
428+
----
429+
__auto__ {b} 1988-08-05 00:00:00 +0000 UTC 3 3 0 1
430+
__auto__ {b} 1988-08-07 00:00:00 +0000 UTC 9 9 0 1
431+
partial {b} 1988-08-08 00:00:00 +0000 UTC 3 3 0 2
432+
__forecast__ {b} 1988-08-10 00:00:00 +0000 UTC 18 18 0 1
433+
422434
# Verify that when the partial histogram is empty
423435
# the returned statistic is the latest full statistic
424436
# 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)