Skip to content

Commit 6286526

Browse files
committed
Fix compute_scalar_stats() for case that all values exceed WIDTH_THRESHOLD.
The standard typanalyze functions skip over values whose detoasted size exceeds WIDTH_THRESHOLD (1024 bytes), so as to limit memory bloat during ANALYZE. However, we (I think I, actually :-() failed to consider the possibility that *every* non-null value in a column is too wide. While compute_minimal_stats() seems to behave reasonably anyway in such a case, compute_scalar_stats() just fell through and generated no pg_statistic entry at all. That's unnecessarily pessimistic: we can still produce valid stanullfrac and stawidth values in such cases, since we do include too-wide values in the average-width calculation. Furthermore, since the general assumption in this code is that too-wide values are probably all distinct from each other, it seems reasonable to set stadistinct to -1 ("all distinct"). Per complaint from Kadri Raudsepp. This has been like this since roughly neolithic times, so back-patch to all supported branches.
1 parent fd2ace8 commit 6286526

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/backend/commands/analyze.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2732,7 +2732,21 @@ compute_scalar_stats(VacAttrStatsP stats,
27322732
slot_idx++;
27332733
}
27342734
}
2735-
else if (nonnull_cnt == 0 && null_cnt > 0)
2735+
else if (nonnull_cnt > 0)
2736+
{
2737+
/* We found some non-null values, but they were all too wide */
2738+
Assert(nonnull_cnt == toowide_cnt);
2739+
stats->stats_valid = true;
2740+
/* Do the simple null-frac and width stats */
2741+
stats->stanullfrac = (double) null_cnt / (double) samplerows;
2742+
if (is_varwidth)
2743+
stats->stawidth = total_width / (double) nonnull_cnt;
2744+
else
2745+
stats->stawidth = stats->attrtype->typlen;
2746+
/* Assume all too-wide values are distinct, so it's a unique column */
2747+
stats->stadistinct = -1.0;
2748+
}
2749+
else if (null_cnt > 0)
27362750
{
27372751
/* We found only nulls; assume the column is entirely null */
27382752
stats->stats_valid = true;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy