diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index bcc0d45..99284cb 100644
*** a/src/backend/optimizer/plan/planner.c
--- b/src/backend/optimizer/plan/planner.c
*************** choose_hashed_distinct(PlannerInfo *root
*** 2848,2854 ****
--- 2848,2858 ----
  	 * 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;
