diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c index aea0677d2b..33b62561ef 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -55,6 +55,7 @@ static RelOptInfo *generate_recursion_path(SetOperationStmt *setOp, static void build_setop_child_paths(PlannerInfo *root, RelOptInfo *rel, bool trivial_tlist, List *child_tlist, List *interesting_pathkeys, + List *groupList, double *pNumGroups); static RelOptInfo *generate_union_paths(SetOperationStmt *op, PlannerInfo *root, List *refnames_tlist, @@ -399,7 +400,7 @@ generate_recursion_path(SetOperationStmt *setOp, PlannerInfo *root, &lpath_trivial_tlist); if (lrel->rtekind == RTE_SUBQUERY) build_setop_child_paths(root, lrel, lpath_trivial_tlist, lpath_tlist, - NIL, NULL); + NIL, NIL, NULL); lpath = lrel->cheapest_total_path; /* The right path will want to look at the left one ... */ root->non_recursive_path = lpath; @@ -411,7 +412,7 @@ generate_recursion_path(SetOperationStmt *setOp, PlannerInfo *root, &rpath_trivial_tlist); if (rrel->rtekind == RTE_SUBQUERY) build_setop_child_paths(root, rrel, rpath_trivial_tlist, rpath_tlist, - NIL, NULL); + NIL, NIL, NULL); rpath = rrel->cheapest_total_path; root->non_recursive_path = NULL; @@ -486,13 +487,18 @@ generate_recursion_path(SetOperationStmt *setOp, PlannerInfo *root, * * interesting_pathkeys: if not NIL, also include paths that suit these * pathkeys, sorting any unsorted paths as required. + * groupList: if not NIL, the SortGroupClauses (tleSortGroupRef = output + * column number) that define a group, e.g. the DISTINCT ON key of a UNION + * DISTINCT ON; the estimate below is then made over those columns only. + * NIL means the whole target list. * *pNumGroups: if not NULL, we estimate the number of distinct groups * in the result, and store it there. */ static void build_setop_child_paths(PlannerInfo *root, RelOptInfo *rel, bool trivial_tlist, List *child_tlist, - List *interesting_pathkeys, double *pNumGroups) + List *interesting_pathkeys, List *groupList, + double *pNumGroups) { RelOptInfo *final_rel; List *setop_pathkeys = rel->subroot->setop_pathkeys; @@ -680,11 +686,26 @@ build_setop_child_paths(PlannerInfo *root, RelOptInfo *rel, subquery->hasAggs) *pNumGroups = rel->cheapest_total_path->rows; else + { + List *groupExprs = get_tlist_exprs(subroot->parse->targetList, + false); + + if (groupList != NIL) + { + List *keyExprs = NIL; + + foreach_node(SortGroupClause, sgc, groupList) + keyExprs = lappend(keyExprs, + list_nth(groupExprs, + sgc->tleSortGroupRef - 1)); + groupExprs = keyExprs; + } *pNumGroups = estimate_num_groups(subroot, - get_tlist_exprs(subroot->parse->targetList, false), + groupExprs, rel->cheapest_total_path->rows, NULL, NULL); + } } } @@ -786,6 +807,7 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root, if (rel->rtekind == RTE_SUBQUERY) build_setop_child_paths(root, rel, trivial_tlist, child_tlist, union_pathkeys, + op->sortClauses ? groupList : NIL, op->all ? NULL : &childGroups); else childGroups = rel->rows; @@ -1167,12 +1189,12 @@ generate_nonunion_paths(SetOperationStmt *op, PlannerInfo *root, */ if (lrel->rtekind == RTE_SUBQUERY) build_setop_child_paths(root, lrel, lpath_trivial_tlist, lpath_tlist, - nonunion_pathkeys, &dLeftGroups); + nonunion_pathkeys, NIL, &dLeftGroups); else dLeftGroups = lrel->rows; if (rrel->rtekind == RTE_SUBQUERY) build_setop_child_paths(root, rrel, rpath_trivial_tlist, rpath_tlist, - nonunion_pathkeys, &dRightGroups); + nonunion_pathkeys, NIL, &dRightGroups); else dRightGroups = rrel->rows;