From bce352fb46196340749cf907f3168b5f63969329 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Tue, 14 Mar 2017 07:28:35 -0400 Subject: [PATCH] Remove some bogus logic from create_gather_merge_plan. This logic was adapated from create_merge_append_plan, but the two cases aren't really analogous, because create_merge_append_plan is not projection-capable and must therefore have a tlist identical to that of the underlying paths. Overwriting the tlist of Gather Merge with whatever the underlying plan happens to produce is no good at all. Patch by me, reviewed by Rushabh Lathia, who also reported the issue and made an initial attempt at a fix. Discussion: http://postgr.es/m/CA+Tgmob_-oHEOBfT9S25bjqokdqv8e8xEmh9zOY+3MPr_LmuhA@mail.gmail.com --- src/backend/optimizer/plan/createplan.c | 48 ++++++------------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index d002e6d567..64f0ee55ec 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -1469,17 +1469,12 @@ create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path) GatherMerge *gm_plan; Plan *subplan; List *pathkeys = best_path->path.pathkeys; - int numsortkeys; - AttrNumber *sortColIdx; - Oid *sortOperators; - Oid *collations; - bool *nullsFirst; List *tlist = build_path_tlist(root, &best_path->path); /* As with Gather, it's best to project away columns in the workers. */ subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST); - /* See create_merge_append_plan for why there's no make_xxx function */ + /* Create a shell for a GatherMerge plan. */ gm_plan = makeNode(GatherMerge); gm_plan->plan.targetlist = tlist; gm_plan->num_workers = best_path->num_workers; @@ -1488,46 +1483,25 @@ create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path) /* Gather Merge is pointless with no pathkeys; use Gather instead. */ Assert(pathkeys != NIL); - /* Compute sort column info, and adjust GatherMerge tlist as needed */ - (void) prepare_sort_from_pathkeys(&gm_plan->plan, pathkeys, - best_path->path.parent->relids, - NULL, - true, - &gm_plan->numCols, - &gm_plan->sortColIdx, - &gm_plan->sortOperators, - &gm_plan->collations, - &gm_plan->nullsFirst); - - /* Compute sort column info, and adjust subplan's tlist as needed */ subplan = prepare_sort_from_pathkeys(subplan, pathkeys, best_path->subpath->parent->relids, gm_plan->sortColIdx, false, - &numsortkeys, - &sortColIdx, - &sortOperators, - &collations, - &nullsFirst); + &gm_plan->numCols, + &gm_plan->sortColIdx, + &gm_plan->sortOperators, + &gm_plan->collations, + &gm_plan->nullsFirst); - /* As for MergeAppend, check that we got the same sort key information. */ - Assert(numsortkeys == gm_plan->numCols); - if (memcmp(sortColIdx, gm_plan->sortColIdx, - numsortkeys * sizeof(AttrNumber)) != 0) - elog(ERROR, "GatherMerge child's targetlist doesn't match GatherMerge"); - Assert(memcmp(sortOperators, gm_plan->sortOperators, - numsortkeys * sizeof(Oid)) == 0); - Assert(memcmp(collations, gm_plan->collations, - numsortkeys * sizeof(Oid)) == 0); - Assert(memcmp(nullsFirst, gm_plan->nullsFirst, - numsortkeys * sizeof(bool)) == 0); /* Now, insert a Sort node if subplan isn't sufficiently ordered */ if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys)) - subplan = (Plan *) make_sort(subplan, numsortkeys, - sortColIdx, sortOperators, - collations, nullsFirst); + subplan = (Plan *) make_sort(subplan, gm_plan->numCols, + gm_plan->sortColIdx, + gm_plan->sortOperators, + gm_plan->collations, + gm_plan->nullsFirst); /* Now insert the subplan under GatherMerge. */ gm_plan->plan.lefttree = subplan; -- 2.40.0