@@ -1873,17 +1873,12 @@ hash_agg_update_metrics(AggState *aggstate, bool from_tape, int npartitions)
1873
1873
aggstate -> hash_disk_used = disk_used ;
1874
1874
}
1875
1875
1876
- /*
1877
- * Update hashentrysize estimate based on contents. Don't include meta_mem
1878
- * in the memory used, because empty buckets would inflate the per-entry
1879
- * cost. An underestimate of the per-entry size is better than an
1880
- * overestimate, because an overestimate could compound with each level of
1881
- * recursion.
1882
- */
1876
+ /* update hashentrysize estimate based on contents */
1883
1877
if (aggstate -> hash_ngroups_current > 0 )
1884
1878
{
1885
1879
aggstate -> hashentrysize =
1886
- hash_mem / (double )aggstate -> hash_ngroups_current ;
1880
+ sizeof (TupleHashEntryData ) +
1881
+ (hash_mem / (double )aggstate -> hash_ngroups_current );
1887
1882
}
1888
1883
}
1889
1884
@@ -1899,10 +1894,10 @@ hash_choose_num_buckets(double hashentrysize, long ngroups, Size memory)
1899
1894
max_nbuckets = memory / hashentrysize ;
1900
1895
1901
1896
/*
1902
- * Leave room for slop to avoid a case where the initial hash table size
1903
- * exceeds the memory limit (though that may still happen in edge cases) .
1897
+ * Underestimating is better than overestimating. Too many buckets crowd
1898
+ * out space for group keys and transition state values .
1904
1899
*/
1905
- max_nbuckets *= 0.75 ;
1900
+ max_nbuckets >>= 1 ;
1906
1901
1907
1902
if (nbuckets > max_nbuckets )
1908
1903
nbuckets = max_nbuckets ;
@@ -3548,7 +3543,7 @@ ExecInitAgg(Agg *node, EState *estate, int eflags)
3548
3543
* reasonable.
3549
3544
*/
3550
3545
for (i = 0 ; i < aggstate -> num_hashes ; i ++ )
3551
- totalGroups = aggstate -> perhash [i ].aggnode -> numGroups ;
3546
+ totalGroups + = aggstate -> perhash [i ].aggnode -> numGroups ;
3552
3547
3553
3548
hash_agg_set_limits (aggstate -> hashentrysize , totalGroups , 0 ,
3554
3549
& aggstate -> hash_mem_limit ,
0 commit comments