Fix hash table size estimation error in choose_hashed_distinct().
authorTom Lane <[email protected]>
Wed, 21 Aug 2013 17:38:16 +0000 (13:38 -0400)
committerTom Lane <[email protected]>
Wed, 21 Aug 2013 17:38:34 +0000 (13:38 -0400)
We should account for the per-group hashtable entry overhead when
considering whether to use a hash aggregate to implement DISTINCT.  The
comparable logic in choose_hashed_grouping() gets this right, but I think
I omitted it here in the mistaken belief that there would be no overhead
if there were no aggregate functions to be evaluated.  This can result in
more than 2X underestimate of the hash table size, if the tuples being
aggregated aren't very wide.  Per report from Tomas Vondra.

This bug is of long standing, but per discussion we'll only back-patch into
9.3.  Changing the estimation behavior in stable branches seems to carry too
much risk of destabilizing plan choices for already-tuned applications.

src/backend/optimizer/plan/planner.c

index bcc0d451f0d48a5203704769ebb90e2be53d3231..99284cb6de1cd3f5a491c14c49c99bf4267c64fd 100644 (file)
@@ -2848,7 +2848,11 @@ choose_hashed_distinct(PlannerInfo *root,
         * Don't do it if it doesn't look like the hashtable will fit into
         * work_mem.
         */
+
+       /* Estimate per-hash-entry space at tuple width... */
        hashentrysize = MAXALIGN(path_width) + MAXALIGN(sizeof(MinimalTupleData));
+       /* plus the per-hash-entry overhead */
+       hashentrysize += hash_agg_entry_size(0);
 
        if (hashentrysize * dNumDistinctRows > work_mem * 1024L)
                return false;