8000 Remove redundant code in create_gather_merge_path · postgrespro/postgres@8b2e9fd · GitHub
[go: up one dir, main page]

Skip to content
  • Commit 8b2e9fd

    Browse files
    author
    Richard Guo
    committed
    Remove redundant code in create_gather_merge_path
    In create_gather_merge_path, we should always guarantee that the subpath is adequately ordered, and we do not add a Sort node in createplan.c for a Gather Merge node. Therefore, the 'else' branch in create_gather_merge_path, which computes the cost for a Sort node, is redundant. This patch removes the redundant code and emits an error if the subpath is not sufficiently ordered. Meanwhile, this patch changes the check for the subpath's pathkeys in create_gather_merge_plan to an Assert. Author: Richard Guo Discussion: https://postgr.es/m/CAMbWs48u=0bWf3epVtULjJ-=M9Hbkz+ieZQAOS=BfbXZFqbDCg@mail.gmail.com
    1 parent 581df21 commit 8b2e9fd

    File tree

    2 files changed

    +14
    -30
    lines changed

    2 files changed

    +14
    -30
    lines changed

    src/backend/optimizer/plan/createplan.c

    Lines changed: 2 additions & 7 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1987,16 +1987,11 @@ create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
    19871987
    &gm_plan->collations,
    19881988
    &gm_plan->nullsFirst);
    19891989

    1990-
    19911990
    /*
    19921991
    * All gather merge paths should have already guaranteed the necessary
    1993-
    * sort order either by adding an explicit sort node or by using presorted
    1994-
    * input. We can't simply add a sort here on additional pathkeys, because
    1995-
    * we can't guarantee the sort would be safe. Fo 8000 r example, expressions may
    1996-
    * be volatile or otherwise parallel unsafe.
    1992+
    * sort order. See create_gather_merge_path.
    19971993
    */
    1998-
    if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
    1999-
    elog(ERROR, "gather merge input not sufficiently sorted");
    1994+
    Assert(pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys));
    20001995

    20011996
    /* Now insert the subplan under GatherMerge. */
    20021997
    gm_plan->plan.lefttree = subplan;

    src/backend/optimizer/util/pathnode.c

    Lines changed: 12 additions & 23 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1889,6 +1889,16 @@ create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
    18891889
    Assert(subpath->parallel_safe);
    18901890
    Assert(pathkeys);
    18911891

    1892+
    /*
    1893+
    * The subpath should guarantee that it is adequately ordered either by
    1894+
    * adding an explicit sort node or by using presorted input. We cannot
    1895+
    * add an explicit Sort node for the subpath in createplan.c on additional
    1896+
    * pathkeys, because we can't guarantee the sort would be safe. For
    1897+
    * example, expressions may be volatile or otherwise parallel unsafe.
    1898+
    */
    1899+
    if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
    1900+
    elog(ERROR, "gather merge input not sufficiently sorted");
    1901+
    18921902
    pathnode->path.pathtype = T_GatherMerge;
    18931903
    pathnode->path.parent = rel;
    18941904
    pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
    @@ -1900,29 +1910,8 @@ create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
    19001910
    pathnode->path.pathkeys = pathkeys;
    19011911
    pathnode->path.pathtarget = target ? target : rel->reltarget;
    19021912

    1903-
    if (pathkeys_contained_in(pathkeys, subpath->pathkeys))
    1904-
    {
    1905-
    /* Subpath is adequately ordered, we won't need to sort it */
    1906-
    input_startup_cost += subpath->startup_cost;
    1907-
    input_total_cost += subpath->total_cost;
    1908-
    }
    1909-
    else
    1910-
    {
    1911-
    /* We'll need to insert a Sort node, so include cost for that */
    1912-
    Path sort_path; /* dummy for result of cost_sort */
    1913-
    1914-
    cost_sort(&sort_path,
    1915-
    root,
    1916-
    pathkeys,
    1917-
    subpath->total_cost,
    1918-
    subpath->rows,
    1919-
    subpath->pathtarget->width,
    1920-
    0.0,
    1921-
    work_mem,
    1922-
    -1);
    1923-
    input_startup_cost += sort_path.startup_cost;
    1924-
    input_total_cost += sort_path.total_cost;
    1925-
    }
    1913+
    input_startup_cost += subpath->startup_cost;
    1914+
    input_total_cost += subpath->total_cost;
    19261915

    19271916
    cost_gather_merge(pathnode, root, rel, pathnode->path.param_info,
    19281917
    input_startup_cost, input_total_cost, rows);

    0 commit comments

    Comments
     (0)
    0