Division by zero in selfuncs.c:estimate_hash_bucketsize() - Mailing list pgsql-hackers

From Piotr Stefaniak
Subject Division by zero in selfuncs.c:estimate_hash_bucketsize()
Date
Msg-id BLU436-SMTP6793A5675100602B2012D6F28B0@phx.gbl
Whole thread Raw
Responses Re: Division by zero in selfuncs.c:estimate_hash_bucketsize()
List pgsql-hackers
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.



pgsql-hackers by date:

Previous
From: Beena Emerson
Date:
Subject: Re: Support for N synchronous standby servers - take 2
Next
From: Corey Huinker
Date:
Subject: Re: dblink: add polymorphic functions.