Thread: Division by zero in selfuncs.c:estimate_hash_bucketsize()

Division by zero in selfuncs.c:estimate_hash_bucketsize()

From
Piotr Stefaniak
Date:
Hello,

the two asserts below will fail with this query (ran against the 
regression db):

SELECT 1
FROM (SELECT age FROM public.person LIMIT 1) s
INNER JOIN public.person USING (age);

diff --git a/src/backend/utils/adt/selfuncs.c 
b/src/backend/utils/adt/selfuncs.c
index 64b6ae4..56c65b3 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -3482,6 +3482,7 @@ estimate_hash_bucketsize(PlannerInfo *root, Node 
*hashkey, double nbuckets)                stanullfrac = 0.0;
        /* Compute avg freq of all distinct data values in raw relation */
+       Assert(ndistinct != 0);        avgfreq = (1.0 - stanullfrac) / ndistinct;
        /*
@@ -3502,8 +3503,10 @@ estimate_hash_bucketsize(PlannerInfo *root, Node 
*hashkey, double nbuckets)         */        if (ndistinct > nbuckets)                estfract = 1.0 / nbuckets;
-       else
+       else {
+               Assert(ndistinct != 0);                estfract = 1.0 / ndistinct;
+       }
        /*         * Look up the frequency of the most common value, if available.



Re: Division by zero in selfuncs.c:estimate_hash_bucketsize()

From
Tom Lane
Date:
Piotr Stefaniak <postgres@piotr-stefaniak.me> writes:
> the two asserts below will fail with this query (ran against the 
> regression db):

I've applied fixes for this and the other thing.  Thanks for the report!
        regards, tom lane