diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index bcc0d451f0d48a5203704769ebb90e2be53d3231..99284cb6de1cd3f5a491c14c49c99bf4267c64fd 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -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;