@@ -4850,11 +4850,10 @@ create_distinct_paths(PlannerInfo *root,
48504850 * Consider hash-based implementations of DISTINCT, if possible.
48514851 *
48524852 * If we were not able to make any other types of path, we *must* hash or
4853- * die trying. If we do have other choices, there are several things that
4853+ * die trying. If we do have other choices, there are two things that
48544854 * should prevent selection of hashing: if the query uses DISTINCT ON
48554855 * (because it won't really have the expected behavior if we hash), or if
4856- * enable_hashagg is off, or if it looks like the hashtable will exceed
4857- * work_mem.
4856+ * enable_hashagg is off.
48584857 *
48594858 * Note: grouping_is_hashable() is much more expensive to check than the
48604859 * other gating conditions, so we want to do it last.
@@ -4864,12 +4863,7 @@ create_distinct_paths(PlannerInfo *root,
48644863 else if (parse -> hasDistinctOn || !enable_hashagg )
48654864 allow_hash = false; /* policy-based decision not to hash */
48664865 else
4867- {
4868- Size hashentrysize = hash_agg_entry_size (0 , cheapest_input_path -> pathtarget -> width , 0 );
4869-
4870- allow_hash = !hashagg_avoid_disk_plan ||
4871- (hashentrysize * numDistinctRows <= work_mem * 1024L );
4872- }
4866+ allow_hash = true; /* default */
48734867
48744868 if (allow_hash && grouping_is_hashable (parse -> distinctClause ))
48754869 {
@@ -6749,8 +6743,6 @@ add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
67496743
67506744 if (can_hash )
67516745 {
6752- double hashaggtablesize ;
6753-
67546746 if (parse -> groupingSets )
67556747 {
67566748 /*
@@ -6762,63 +6754,41 @@ add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
67626754 }
67636755 else
67646756 {
6765- hashaggtablesize = estimate_hashagg_tablesize (cheapest_path ,
6766- agg_costs ,
6767- dNumGroups );
6768-
67696757 /*
6770- * Provided that the estimated size of the hashtable does not
6771- * exceed work_mem, we'll generate a HashAgg Path, although if we
6772- * were unable to sort above, then we'd better generate a Path, so
6773- * that we at least have one.
6758+ * Generate a HashAgg Path. We just need an Agg over the
6759+ * cheapest-total input path, since input order won't matter.
67746760 */
6775- if (!hashagg_avoid_disk_plan ||
6776- hashaggtablesize < work_mem * 1024L ||
6777- grouped_rel -> pathlist == NIL )
6778- {
6779- /*
6780- * We just need an Agg over the cheapest-total input path,
6781- * since input order won't matter.
6782- */
6783- add_path (grouped_rel , (Path * )
6784- create_agg_path (root , grouped_rel ,
6785- cheapest_path ,
6786- grouped_rel -> reltarget ,
6787- AGG_HASHED ,
6788- AGGSPLIT_SIMPLE ,
6789- parse -> groupClause ,
6790- havingQual ,
6791- agg_costs ,
6792- dNumGroups ));
6793- }
6761+ add_path (grouped_rel , (Path * )
6762+ create_agg_path (root , grouped_rel ,
6763+ cheapest_path ,
6764+ grouped_rel -> reltarget ,
6765+ AGG_HASHED ,
6766+ AGGSPLIT_SIMPLE ,
6767+ parse -> groupClause ,
6768+ havingQual ,
6769+ agg_costs ,
6770+ dNumGroups ));
67946771 }
67956772
67966773 /*
67976774 * Generate a Finalize HashAgg Path atop of the cheapest partially
6798- * grouped path, assuming there is one. Once again, we'll only do this
6799- * if it looks as though the hash table won't exceed work_mem.
6775+ * grouped path, assuming there is one
68006776 */
68016777 if (partially_grouped_rel && partially_grouped_rel -> pathlist )
68026778 {
68036779 Path * path = partially_grouped_rel -> cheapest_total_path ;
68046780
6805- hashaggtablesize = estimate_hashagg_tablesize (path ,
6806- agg_final_costs ,
6807- dNumGroups );
6808-
6809- if (!hashagg_avoid_disk_plan ||
6810- hashaggtablesize < work_mem * 1024L )
6811- add_path (grouped_rel , (Path * )
6812- create_agg_path (root ,
6813- grouped_rel ,
6814- path ,
6815- grouped_rel -> reltarget ,
6816- AGG_HASHED ,
6817- AGGSPLIT_FINAL_DESERIAL ,
6818- parse -> groupClause ,
6819- havingQual ,
6820- agg_final_costs ,
6821- dNumGroups ));
6781+ add_path (grouped_rel , (Path * )
6782+ create_agg_path (root ,
6783+ grouped_rel ,
6784+ path ,
6785+ grouped_rel -> reltarget ,
6786+ AGG_HASHED ,
6787+ AGGSPLIT_FINAL_DESERIAL ,
6788+ parse -> groupClause ,
6789+ havingQual ,
6790+ agg_final_costs ,
6791+ dNumGroups ));
68226792 }
68236793 }
68246794
@@ -7171,65 +7141,43 @@ create_partial_grouping_paths(PlannerInfo *root,
71717141 }
71727142 }
71737143
7144+ /*
7145+ * Add a partially-grouped HashAgg Path where possible
7146+ */
71747147 if (can_hash && cheapest_total_path != NULL )
71757148 {
7176- double hashaggtablesize ;
7177-
71787149 /* Checked above */
71797150 Assert (parse -> hasAggs || parse -> groupClause );
71807151
7181- hashaggtablesize =
7182- estimate_hashagg_tablesize (cheapest_total_path ,
7183- agg_partial_costs ,
7184- dNumPartialGroups );
7185-
7186- /*
7187- * Tentatively produce a partial HashAgg Path, depending on if it
7188- * looks as if the hash table will fit in work_mem.
7189- */
7190- if ((!hashagg_avoid_disk_plan || hashaggtablesize < work_mem * 1024L ) &&
7191- cheapest_total_path != NULL )
7192- {
7193- add_path (partially_grouped_rel , (Path * )
7194- create_agg_path (root ,
7195- partially_grouped_rel ,
7196- cheapest_total_path ,
7197- partially_grouped_rel -> reltarget ,
7198- AGG_HASHED ,
7199- AGGSPLIT_INITIAL_SERIAL ,
7200- parse -> groupClause ,
7201- NIL ,
7202- agg_partial_costs ,
7203- dNumPartialGroups ));
7204- }
7152+ add_path (partially_grouped_rel , (Path * )
7153+ create_agg_path (root ,
7154+ partially_grouped_rel ,
7155+ cheapest_total_path ,
7156+ partially_grouped_rel -> reltarget ,
7157+ AGG_HASHED ,
7158+ AGGSPLIT_INITIAL_SERIAL ,
7159+ parse -> groupClause ,
7160+ NIL ,
7161+ agg_partial_costs ,
7162+ dNumPartialGroups ));
72057163 }
72067164
7165+ /*
7166+ * Now add a partially-grouped HashAgg partial Path where possible
7167+ */
72077168 if (can_hash && cheapest_partial_path != NULL )
72087169 {
7209- double hashaggtablesize ;
7210-
7211- hashaggtablesize =
7212- estimate_hashagg_tablesize (cheapest_partial_path ,
7213- agg_partial_costs ,
7214- dNumPartialPartialGroups );
7215-
7216- /* Do the same for partial paths. */
7217- if ((!hashagg_avoid_disk_plan ||
7218- hashaggtablesize < work_mem * 1024L ) &&
7219- cheapest_partial_path != NULL )
7220- {
7221- add_partial_path (partially_grouped_rel , (Path * )
7222- create_agg_path (root ,
7223- partially_grouped_rel ,
7224- cheapest_partial_path ,
7225- partially_grouped_rel -> reltarget ,
7226- AGG_HASHED ,
7227- AGGSPLIT_INITIAL_SERIAL ,
7228- parse -> groupClause ,
7229- NIL ,
7230- agg_partial_costs ,
7231- dNumPartialPartialGroups ));
7232- }
7170+ add_partial_path (partially_grouped_rel , (Path * )
7171+ create_agg_path (root ,
7172+ partially_grouped_rel ,
7173+ cheapest_partial_path ,
7174+ partially_grouped_rel -> reltarget ,
7175+ AGG_HASHED ,
7176+ AGGSPLIT_INITIAL_SERIAL ,
7177+ parse -> groupClause ,
7178+ NIL ,
7179+ agg_partial_costs ,
7180+ dNumPartialPartialGroups ));
72337181 }
72347182
72357183 /*
0 commit comments