Skip to content

Commit 0766904

Browse files
committed
Fix booltestsel() for case where we have NULL stats but not MCV stats.
In a boolean column that contains mostly nulls, ANALYZE might not find enough non-null values to populate the most-common-values stats, but it would still create a pg_statistic entry with stanullfrac set. The logic in booltestsel() for this situation did the wrong thing for "col IS NOT TRUE" and "col IS NOT FALSE" tests, forgetting that null values would satisfy these tests (so that the true selectivity would be close to one, not close to zero). Per bug #8274. Fix by Andrew Gierth, some comment-smithing by me.
1 parent 0883c85 commit 0766904

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,31 +1430,29 @@ booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
14301430
/*
14311431
* No most-common-value info available. Still have null fraction
14321432
* information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
1433-
* for null fraction and assume an even split for boolean tests.
1433+
* for null fraction and assume a 50-50 split of TRUE and FALSE.
14341434
*/
14351435
switch (booltesttype)
14361436
{
14371437
case IS_UNKNOWN:
1438-
1439-
/*
1440-
* Use freq_null directly.
1441-
*/
1438+
/* select only NULL values */
14421439
selec = freq_null;
14431440
break;
14441441
case IS_NOT_UNKNOWN:
1445-
1446-
/*
1447-
* Select not unknown (not null) values. Calculate from
1448-
* freq_null.
1449-
*/
1442+
/* select non-NULL values */
14501443
selec = 1.0 - freq_null;
14511444
break;
14521445
case IS_TRUE:
1453-
case IS_NOT_TRUE:
14541446
case IS_FALSE:
1455-
case IS_NOT_FALSE:
1447+
/* Assume we select half of the non-NULL values */
14561448
selec = (1.0 - freq_null) / 2.0;
14571449
break;
1450+
case IS_NOT_TRUE:
1451+
case IS_NOT_FALSE:
1452+
/* Assume we select NULLs plus half of the non-NULLs */
1453+
/* equiv. to freq_null + (1.0 - freq_null) / 2.0 */
1454+
selec = (freq_null + 1.0) / 2.0;
1455+
break;
14581456
default:
14591457
elog(ERROR, "unrecognized booltesttype: %d",
14601458
(int) booltesttype);

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