Guard against zero vardata.rel->tuples in estimate_hash_bucketsize().
authorTom Lane
Sun, 27 Mar 2016 22:21:03 +0000 (18:21 -0400)
committerTom Lane
Sun, 27 Mar 2016 22:21:03 +0000 (18:21 -0400)
If the referenced rel was proven empty, we'd compute 0/0 here, which
results in the function returning NaN.  That's a bit more serious
than the other zero-divide case.  Still, it only seems to be possible
in HEAD, so no back-patch.

Per report from Piotr Stefaniak.  I looked through the rest of selfuncs.c
and found no other likely trouble spots.

src/backend/utils/adt/selfuncs.c

index 30751fc8ac6475b1e98e93a9e12ebbe4c9783c05..a6555e91b77bf62a5f166d1a863f3e0c242e7b8d 100644 (file)
@@ -3540,7 +3540,7 @@ estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets)
     * XXX Possibly better way, but much more expensive: multiply by
     * selectivity of rel's restriction clauses that mention the target Var.
     */
-   if (vardata.rel)
+   if (vardata.rel && vardata.rel->tuples > 0)
    {
        ndistinct *= vardata.rel->rows / vardata.rel->tuples;
        ndistinct = clamp_row_est(ndistinct);