Error out when Gather Merge input is not sorted
authorTomas Vondra <tomas.vondra@postgresql.org>
Tue, 15 Dec 2020 22:13:38 +0000 (23:13 +0100)
committerTomas Vondra <tomas.vondra@postgresql.org>
Tue, 15 Dec 2020 22:19:41 +0000 (23:19 +0100)
To build Gather Merge path, the input needs to be sufficiently sorted.
Ensuring this is the responsibility of the code constructing the paths,
but create_gather_merge_plan tried to handle unsorted paths by adding
an explicit Sort. In light of the recent issues related to Incremental
Sort, this is rather fragile. Some of the expressions may be volatile
or parallel unsafe, in which case we can't add the Sort here.

We could do more checks and add the Sort in at least some cases, but
it seems cleaner to just error out and make it clear this is a bug in
code constructing those paths.

Author: James Coleman
Reviewed-by: Tomas Vondra
Discussion: https://postgr.es/m/CAAaqYe8cK3g5CfLC4w7bs%3DhC0mSksZC%3DH5M8LSchj5e5OxpTAg%40mail.gmail.com
Discussion: https://postgr.es/m/CAJGNTeNaxpXgBVcRhJX%2B2vSbq%2BF2kJqGBcvompmpvXb7pq%2BoFA%40mail.gmail.com

src/backend/optimizer/plan/createplan.c

index 40abe6f9f623ed2922ccc4e1991e97e90322f47d..5ecf9f40658a47a43c3e546df066ce235be8596a 100644 (file)
@@ -1793,13 +1793,15 @@ create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
                                         &gm_plan->nullsFirst);
 
 
-   /* Now, insert a Sort node if subplan isn't sufficiently ordered */
+   /*
+    * All gather merge paths should have already guaranteed the necessary sort
+    * order either by adding an explicit sort node or by using presorted input.
+    * We can't simply add a sort here on additional pathkeys, because we can't
+    * guarantee the sort would be safe. For example, expressions may be
+    * volatile or otherwise parallel unsafe.
+    */
    if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
-       subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
-                                    gm_plan->sortColIdx,
-                                    gm_plan->sortOperators,
-                                    gm_plan->collations,
-                                    gm_plan->nullsFirst);
+       elog(ERROR, "gather merge input not sufficiently sorted");
 
    /* Now insert the subplan under GatherMerge. */
    gm_plan->plan.lefttree = subplan;