diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 9455710..6b792da 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1211,10 +1211,9 @@ inheritance_planner(PlannerInfo *root) List *orig_append_rel_list = list_copy(root->append_rel_list); List *orig_row_marks = list_copy(root->rowMarks); List *orig_rtable = list_copy(root->parse->rtable); - List *rtable_with_target; List *source_appinfos = NIL; List *source_child_rowmarks = NIL; - List *source_child_rtes = NIL; + bool is_first_child = true; Assert(parse->commandType != CMD_INSERT); @@ -1238,13 +1237,6 @@ inheritance_planner(PlannerInfo *root) return; } - - /* - * This one also contains the child target relations, but no other - * child relations. - */ - rtable_with_target = list_copy(root->parse->rtable); - /* * We generate a modified instance of the original Query for each target * relation, plan that, and put all the plans into a list that will be @@ -1370,19 +1362,13 @@ inheritance_planner(PlannerInfo *root) subroot->rowMarks = copyObject(orig_row_marks); /* - * No need to copy of the RTEs themselves, but do copy the List - * structure. - */ - subroot->parse->rtable = list_copy(rtable_with_target); - - /* * If this isn't the first child query, then we can use the child * objects for source child relations created during the planning of * 1st child query. IOW, this planning run doesn't need to create the * child objects again, indicated by setting contains_inherit_children * sub-PlannerInfo. */ - if (source_appinfos) + if (!is_first_child) { subroot->append_rel_list = list_concat(subroot->append_rel_list, source_appinfos); @@ -1391,9 +1377,6 @@ inheritance_planner(PlannerInfo *root) */ subroot->rowMarks = list_concat(subroot->rowMarks, source_child_rowmarks); - Assert(source_child_rtes != NIL); - subroot->parse->rtable = list_concat(subroot->parse->rtable, - source_child_rtes); /* * We have the children, so no need to add them again during this @@ -1561,21 +1544,23 @@ inheritance_planner(PlannerInfo *root) * If we finished planning our first child query, copy the source * child objects that were added during its planning. */ - if (source_appinfos == NIL && subroot->append_rel_list) + if (is_first_child) { int num_skip_appinfos = list_length(orig_append_rel_list); int num_skip_rowmarks = list_length(orig_row_marks); - int num_skip_rtes = list_length(rtable_with_target); + int num_skip_rtes = list_length(orig_rtable); ListCell *lc2; + List *source_child_rtes = NIL; source_appinfos = list_copy_tail(subroot->append_rel_list, num_skip_appinfos); Assert(source_child_rowmarks == NIL); source_child_rowmarks = list_copy_tail(subroot->rowMarks, num_skip_rowmarks); - Assert(source_child_rtes == NIL); source_child_rtes = list_copy_tail(subroot->parse->rtable, num_skip_rtes); + parent_root->parse->rtable = list_concat(orig_rtable, + source_child_rtes); /* * Original parent PlanRowMark is modified when adding the @@ -1603,6 +1588,7 @@ inheritance_planner(PlannerInfo *root) break; } } + is_first_child = false; } /*