Skip to content

Commit fb491a5

Browse files
committed
For a unique-key attribute (no duplicate values), vacuum analyze
was recording a disbursion of 0, not the correct value 1/numberOfRows.
1 parent a76ad50 commit fb491a5

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/backend/commands/vacuum.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.116 1999/08/01 04:54:24 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.117 1999/08/08 17:13:10 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -2339,19 +2339,27 @@ vc_updstats(Oid relid, int num_pages, int num_tuples, bool hasindex, VRelStats *
23392339
/* overwrite the existing statistics in the tuple */
23402340
if (VacAttrStatsEqValid(stats))
23412341
{
2342-
if (stats->nonnull_cnt + stats->null_cnt == 0 ||
2343-
(stats->null_cnt <= 1 && stats->best_cnt == 1))
2342+
if (stats->nonnull_cnt == 0 && stats->null_cnt == 0)
2343+
{
2344+
/* empty relation, so put a dummy value in attdisbursion */
23442345
selratio = 0;
2346+
}
2347+
else if (stats->null_cnt <= 1 && stats->best_cnt == 1)
2348+
{
2349+
/* looks like we have a unique-key attribute */
2350+
double total = ((double) stats->nonnull_cnt) + ((double) stats->null_cnt);
2351+
2352+
selratio = 1.0 / total;
2353+
}
23452354
else if (VacAttrStatsLtGtValid(stats) && stats->min_cnt + stats->max_cnt == stats->nonnull_cnt)
23462355
{
23472356
/* exact result when there are just 1 or 2 values... */
23482357
double min_cnt_d = stats->min_cnt,
23492358
max_cnt_d = stats->max_cnt,
2350-
null_cnt_d = stats->null_cnt,
2351-
nonnull_cnt_d = stats->nonnull_cnt; /* prevent overflow */
2359+
null_cnt_d = stats->null_cnt;
2360+
double total = ((double) stats->nonnull_cnt) + null_cnt_d;
23522361

2353-
selratio = (min_cnt_d * min_cnt_d + max_cnt_d * max_cnt_d + null_cnt_d * null_cnt_d) /
2354-
(nonnull_cnt_d + null_cnt_d) / (nonnull_cnt_d + null_cnt_d);
2362+
selratio = (min_cnt_d * min_cnt_d + max_cnt_d * max_cnt_d + null_cnt_d * null_cnt_d) / (total * total);
23552363
}
23562364
else
23572365
{
@@ -2362,7 +2370,7 @@ vc_updstats(Oid relid, int num_pages, int num_tuples, bool hasindex, VRelStats *
23622370
* we assume count of other values are 20% of best
23632371
* count in table
23642372
*/
2365-
selratio = (most * most + 0.20 * most * (total - most)) / total / total;
2373+
selratio = (most * most + 0.20 * most * (total - most)) / (total * total);
23662374
}
23672375
if (selratio < 0.0)
23682376
selratio = 0.0;

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