7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -2339,19 +2339,27 @@ vc_updstats(Oid relid, int num_pages, int num_tuples, bool hasindex, VRelStats *
2339
2339
/* overwrite the existing statistics in the tuple */
2340
2340
if (VacAttrStatsEqValid (stats ))
2341
2341
{
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 */
2344
2345
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
+ }
2345
2354
else if (VacAttrStatsLtGtValid (stats ) && stats -> min_cnt + stats -> max_cnt == stats -> nonnull_cnt )
2346
2355
{
2347
2356
/* exact result when there are just 1 or 2 values... */
2348
2357
double min_cnt_d = stats -> min_cnt ,
2349
2358
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 ;
2352
2361
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 );
2355
2363
}
2356
2364
else
2357
2365
{
@@ -2362,7 +2370,7 @@ vc_updstats(Oid relid, int num_pages, int num_tuples, bool hasindex, VRelStats *
2362
2370
* we assume count of other values are 20% of best
2363
2371
* count in table
2364
2372
*/
2365
- selratio = (most * most + 0.20 * most * (total - most )) / total / total ;
2373
+ selratio = (most * most + 0.20 * most * (total - most )) / ( total * total ) ;
2366
2374
}
2367
2375
if (selratio < 0.0 )
2368
2376
selratio = 0.0 ;
0 commit comments