diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 2d7e1d84d0..e020c0fe6f 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -100,7 +100,8 @@ static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel, static Path *get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer); -static List *accumulate_append_subpath(List *subpaths, Path *path); +static List *accumulate_append_subpath(List *subpaths, Path *path, + List **partitioned_rels); static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte); static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, @@ -1285,15 +1286,29 @@ add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *all_child_pathkeys = NIL; List *all_child_outers = NIL; ListCell *l; - List *partitioned_rels = NIL; + List *original_partitioned_rels = NIL; + List *nonpartial_partitioned_rels = NIL; + List *partial_partitioned_rels = NIL; RangeTblEntry *rte; rte = planner_rt_fetch(rel->relid, root); if (rte->relkind == RELKIND_PARTITIONED_TABLE) { - partitioned_rels = get_partitioned_child_rels(root, rel->relid); + original_partitioned_rels = + get_partitioned_child_rels(root, rel->relid); /* The root partitioned table is included as a child rel */ - Assert(list_length(partitioned_rels) >= 1); + Assert(list_length(original_partitioned_rels) >= 1); + + /* + * It's sort of annoying that we end up accumulating separate lists of + * partitioned rels for the partial and non-partial cases, but the + * partial list is taken from the partial paths and the non-partial + * case from the non-partial paths, which could in theory be + * different. It's unclear why that would ever actually happen in + * practice, though; maybe there's a better way to handle this. + */ + nonpartial_partitioned_rels = list_copy(original_partitioned_rels); + partial_partitioned_rels = list_copy(original_partitioned_rels); } /* @@ -1313,14 +1328,16 @@ add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, */ if (childrel->cheapest_total_path->param_info == NULL) subpaths = accumulate_append_subpath(subpaths, - childrel->cheapest_total_path); + childrel->cheapest_total_path, + &nonpartial_partitioned_rels); else subpaths_valid = false; /* Same idea, but for a partial plan. */ if (childrel->partial_pathlist != NIL) partial_subpaths = accumulate_append_subpath(partial_subpaths, - linitial(childrel->partial_pathlist)); + linitial(childrel->partial_pathlist), + &partial_partitioned_rels); else partial_subpaths_valid = false; @@ -1396,7 +1413,7 @@ add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, */ if (subpaths_valid) add_path(rel, (Path *) create_append_path(rel, subpaths, NULL, 0, - partitioned_rels)); + nonpartial_partitioned_rels)); /* * Consider an append of partial unordered, unparameterized partial paths. @@ -1423,7 +1440,8 @@ add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, /* Generate a partial append path. */ appendpath = create_append_path(rel, partial_subpaths, NULL, - parallel_workers, partitioned_rels); + parallel_workers, + partial_partitioned_rels); add_partial_path(rel, (Path *) appendpath); } @@ -1434,7 +1452,7 @@ add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, if (subpaths_valid) generate_mergeappend_paths(root, rel, live_childrels, all_child_pathkeys, - partitioned_rels); + nonpartial_partitioned_rels); /* * Build Append paths for each parameterization seen among the child rels. @@ -1453,6 +1471,9 @@ add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, { Relids required_outer = (Relids) lfirst(l); ListCell *lcr; + List *outer_partitioned_rels; + + outer_partitioned_rels = list_copy(original_partitioned_rels); /* Select the child paths for an Append with this parameterization */ subpaths = NIL; @@ -1471,13 +1492,14 @@ add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, subpaths_valid = false; break; } - subpaths = accumulate_append_subpath(subpaths, subpath); + subpaths = accumulate_append_subpath(subpaths, subpath, + &outer_partitioned_rels); } if (subpaths_valid) add_path(rel, (Path *) create_append_path(rel, subpaths, required_outer, 0, - partitioned_rels)); + outer_partitioned_rels)); } } @@ -1511,6 +1533,8 @@ generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel, List *partitioned_rels) { ListCell *lcp; + List *startup_partitioned_rels = list_copy(partitioned_rels); + List *total_partitioned_rels = list_copy(partitioned_rels); foreach(lcp, all_child_pathkeys) { @@ -1562,25 +1586,29 @@ generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel, startup_neq_total = true; startup_subpaths = - accumulate_append_subpath(startup_subpaths, cheapest_startup); + accumulate_append_subpath(startup_subpaths, cheapest_startup, + &startup_partitioned_rels); total_subpaths = - accumulate_append_subpath(total_subpaths, cheapest_total); + accumulate_append_subpath(total_subpaths, cheapest_total, + &total_partitioned_rels); } /* ... and build the MergeAppend paths */ - add_path(rel, (Path *) create_merge_append_path(root, - rel, - startup_subpaths, - pathkeys, - NULL, - partitioned_rels)); + add_path(rel, (Path *) + create_merge_append_path(root, + rel, + startup_subpaths, + pathkeys, + NULL, + startup_partitioned_rels)); if (startup_neq_total) - add_path(rel, (Path *) create_merge_append_path(root, - rel, - total_subpaths, - pathkeys, - NULL, - partitioned_rels)); + add_path(rel, + (Path *) create_merge_append_path(root, + rel, + total_subpaths, + pathkeys, + NULL, + total_partitioned_rels)); } } @@ -1671,15 +1699,21 @@ get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel, * omitting a sort step, which seems fine: if the parent is to be an Append, * its result would be unsorted anyway, while if the parent is to be a * MergeAppend, there's no point in a separate sort on a child. + * + * Also accumulate the subpath's partitioned_rels into the provided list. */ static List * -accumulate_append_subpath(List *subpaths, Path *path) +accumulate_append_subpath(List *subpaths, Path *path, List **partitioned_rels) { if (IsA(path, AppendPath)) { AppendPath *apath = (AppendPath *) path; /* list_copy is important here to avoid sharing list substructure */ + *partitioned_rels = + list_concat(*partitioned_rels, apath->partitioned_rels); + + /* list_copy is important here to avoid sharing list substructure */ return list_concat(subpaths, list_copy(apath->subpaths)); } else if (IsA(path, MergeAppendPath)) @@ -1687,6 +1721,10 @@ accumulate_append_subpath(List *subpaths, Path *path) MergeAppendPath *mpath = (MergeAppendPath *) path; /* list_copy is important here to avoid sharing list substructure */ + *partitioned_rels = + list_concat(*partitioned_rels, mpath->partitioned_rels); + + /* list_copy is important here to avoid sharing list substructure */ return list_concat(subpaths, list_copy(mpath->subpaths)); } else