Track the number of presorted outer pathkeys in MergePath
authorRichard Guo <rguo@postgresql.org>
Thu, 8 May 2025 09:21:32 +0000 (18:21 +0900)
committerRichard Guo <rguo@postgresql.org>
Thu, 8 May 2025 09:21:32 +0000 (18:21 +0900)
When creating an explicit Sort node for the outer path of a mergejoin,
we need to determine the number of presorted keys of the outer path to
decide whether explicit incremental sort can be applied.  Currently,
this is done by repeatedly calling pathkeys_count_contained_in.

This patch caches the number of presorted outer pathkeys in MergePath,
allowing us to save several calls to pathkeys_count_contained_in.  It
can be considered a complement to the changes in commit 828e94c9d.

Reported-by: David Rowley <dgrowleyml@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/CAApHDvqvBireB_w6x8BN5txdvBEHxVgZBt=rUnpf5ww5P_E_ww@mail.gmail.com

src/backend/foreign/foreign.c
src/backend/optimizer/path/costsize.c
src/backend/optimizer/path/joinpath.c
src/backend/optimizer/plan/createplan.c
src/backend/optimizer/util/pathnode.c
src/include/nodes/pathnodes.h
src/include/optimizer/cost.h
src/include/optimizer/pathnode.h

index 9c57f7a259c3c42a0f53d4145f3adbe4bf622f01..a57e59f27ea64f37117580e9a79d783fe08e4e79 100644 (file)
@@ -821,8 +821,9 @@ GetExistingLocalJoinPath(RelOptInfo *joinrel)
                     * for the mergejoin, we can skip doing an explicit sort.
                     */
                    if (merge_path->outersortkeys &&
-                       pathkeys_contained_in(merge_path->outersortkeys,
-                                             joinpath->outerjoinpath->pathkeys))
+                       pathkeys_count_contained_in(merge_path->outersortkeys,
+                                                   joinpath->outerjoinpath->pathkeys,
+                                                   &merge_path->outer_presorted_keys))
                        merge_path->outersortkeys = NIL;
                }
            }
index 60b0fcfb6be542552903f00643de82ac1e91cb80..3d44815ed5adf577ecfae8eaa064a140ca333368 100644 (file)
@@ -3542,6 +3542,7 @@ final_cost_nestloop(PlannerInfo *root, NestPath *path,
  * 'inner_path' is the inner input to the join
  * 'outersortkeys' is the list of sort keys for the outer path
  * 'innersortkeys' is the list of sort keys for the inner path
+ * 'outer_presorted_keys' is the number of presorted keys of the outer path
  * 'extra' contains miscellaneous information about the join
  *
  * Note: outersortkeys and innersortkeys should be NIL if no explicit
@@ -3553,6 +3554,7 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
                       List *mergeclauses,
                       Path *outer_path, Path *inner_path,
                       List *outersortkeys, List *innersortkeys,
+                      int outer_presorted_keys,
                       JoinPathExtraData *extra)
 {
    int         disabled_nodes;
@@ -3683,27 +3685,33 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
 
    if (outersortkeys)          /* do we need to sort outer? */
    {
-       bool        use_incremental_sort = false;
-       int         presorted_keys;
+       /*
+        * We can assert that the outer path is not already ordered
+        * appropriately for the mergejoin; otherwise, outersortkeys would
+        * have been set to NIL.
+        */
+       Assert(!pathkeys_contained_in(outersortkeys, outer_path->pathkeys));
 
        /*
         * We choose to use incremental sort if it is enabled and there are
         * presorted keys; otherwise we use full sort.
         */
-       if (enable_incremental_sort)
+       if (enable_incremental_sort && outer_presorted_keys > 0)
        {
-           bool        is_sorted PG_USED_FOR_ASSERTS_ONLY;
-
-           is_sorted = pathkeys_count_contained_in(outersortkeys,
-                                                   outer_path->pathkeys,
-                                                   &presorted_keys);
-           Assert(!is_sorted);
-
-           if (presorted_keys > 0)
-               use_incremental_sort = true;
+           cost_incremental_sort(&sort_path,
+                                 root,
+                                 outersortkeys,
+                                 outer_presorted_keys,
+                                 outer_path->disabled_nodes,
+                                 outer_path->startup_cost,
+                                 outer_path->total_cost,
+                                 outer_path_rows,
+                                 outer_path->pathtarget->width,
+                                 0.0,
+                                 work_mem,
+                                 -1.0);
        }
-
-       if (!use_incremental_sort)
+       else
        {
            cost_sort(&sort_path,
                      root,
@@ -3716,21 +3724,7 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
                      work_mem,
                      -1.0);
        }
-       else
-       {
-           cost_incremental_sort(&sort_path,
-                                 root,
-                                 outersortkeys,
-                                 presorted_keys,
-                                 outer_path->disabled_nodes,
-                                 outer_path->startup_cost,
-                                 outer_path->total_cost,
-                                 outer_path_rows,
-                                 outer_path->pathtarget->width,
-                                 0.0,
-                                 work_mem,
-                                 -1.0);
-       }
+
        disabled_nodes += sort_path.disabled_nodes;
        startup_cost += sort_path.startup_cost;
        startup_cost += (sort_path.total_cost - sort_path.startup_cost)
@@ -3750,6 +3744,13 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
 
    if (innersortkeys)          /* do we need to sort inner? */
    {
+       /*
+        * We can assert that the inner path is not already ordered
+        * appropriately for the mergejoin; otherwise, innersortkeys would
+        * have been set to NIL.
+        */
+       Assert(!pathkeys_contained_in(innersortkeys, inner_path->pathkeys));
+
        /*
         * We do not consider incremental sort for inner path, because
         * incremental sort does not support mark/restore.
index 14ed9daeca8fb8b4290087a16b6275a07b169cbd..26f0336f1e40972918cd1f980464bf94200af8a8 100644 (file)
@@ -1042,6 +1042,7 @@ try_mergejoin_path(PlannerInfo *root,
                   bool is_partial)
 {
    Relids      required_outer;
+   int         outer_presorted_keys = 0;
    JoinCostWorkspace workspace;
 
    if (is_partial)
@@ -1087,9 +1088,16 @@ try_mergejoin_path(PlannerInfo *root,
    /*
     * If the given paths are already well enough ordered, we can skip doing
     * an explicit sort.
+    *
+    * We need to determine the number of presorted keys of the outer path to
+    * decide whether explicit incremental sort can be applied when
+    * outersortkeys is not NIL.  We do not need to do the same for the inner
+    * path though, as incremental sort currently does not support
+    * mark/restore.
     */
    if (outersortkeys &&
-       pathkeys_contained_in(outersortkeys, outer_path->pathkeys))
+       pathkeys_count_contained_in(outersortkeys, outer_path->pathkeys,
+                                   &outer_presorted_keys))
        outersortkeys = NIL;
    if (innersortkeys &&
        pathkeys_contained_in(innersortkeys, inner_path->pathkeys))
@@ -1101,6 +1109,7 @@ try_mergejoin_path(PlannerInfo *root,
    initial_cost_mergejoin(root, &workspace, jointype, mergeclauses,
                           outer_path, inner_path,
                           outersortkeys, innersortkeys,
+                          outer_presorted_keys,
                           extra);
 
    if (add_path_precheck(joinrel, workspace.disabled_nodes,
@@ -1120,7 +1129,8 @@ try_mergejoin_path(PlannerInfo *root,
                                       required_outer,
                                       mergeclauses,
                                       outersortkeys,
-                                      innersortkeys));
+                                      innersortkeys,
+                                      outer_presorted_keys));
    }
    else
    {
@@ -1146,6 +1156,7 @@ try_partial_mergejoin_path(PlannerInfo *root,
                           JoinType jointype,
                           JoinPathExtraData *extra)
 {
+   int         outer_presorted_keys = 0;
    JoinCostWorkspace workspace;
 
    /*
@@ -1159,9 +1170,16 @@ try_partial_mergejoin_path(PlannerInfo *root,
    /*
     * If the given paths are already well enough ordered, we can skip doing
     * an explicit sort.
+    *
+    * We need to determine the number of presorted keys of the outer path to
+    * decide whether explicit incremental sort can be applied when
+    * outersortkeys is not NIL.  We do not need to do the same for the inner
+    * path though, as incremental sort currently does not support
+    * mark/restore.
     */
    if (outersortkeys &&
-       pathkeys_contained_in(outersortkeys, outer_path->pathkeys))
+       pathkeys_count_contained_in(outersortkeys, outer_path->pathkeys,
+                                   &outer_presorted_keys))
        outersortkeys = NIL;
    if (innersortkeys &&
        pathkeys_contained_in(innersortkeys, inner_path->pathkeys))
@@ -1173,6 +1191,7 @@ try_partial_mergejoin_path(PlannerInfo *root,
    initial_cost_mergejoin(root, &workspace, jointype, mergeclauses,
                           outer_path, inner_path,
                           outersortkeys, innersortkeys,
+                          outer_presorted_keys,
                           extra);
 
    if (!add_partial_path_precheck(joinrel, workspace.disabled_nodes,
@@ -1193,7 +1212,8 @@ try_partial_mergejoin_path(PlannerInfo *root,
                                           NULL,
                                           mergeclauses,
                                           outersortkeys,
-                                          innersortkeys));
+                                          innersortkeys,
+                                          outer_presorted_keys));
 }
 
 /*
index a8f22a8c154afac5e05329f55ac0b284d54280d5..4ad30b7627e6ec6713ce30af32b830fc128288e1 100644 (file)
@@ -4521,48 +4521,41 @@ create_mergejoin_plan(PlannerInfo *root,
    {
        Relids      outer_relids = outer_path->parent->relids;
        Plan       *sort_plan;
-       bool        use_incremental_sort = false;
-       int         presorted_keys;
+
+       /*
+        * We can assert that the outer path is not already ordered
+        * appropriately for the mergejoin; otherwise, outersortkeys would
+        * have been set to NIL.
+        */
+       Assert(!pathkeys_contained_in(best_path->outersortkeys,
+                                     outer_path->pathkeys));
 
        /*
         * We choose to use incremental sort if it is enabled and there are
         * presorted keys; otherwise we use full sort.
         */
-       if (enable_incremental_sort)
-       {
-           bool        is_sorted PG_USED_FOR_ASSERTS_ONLY;
-
-           is_sorted = pathkeys_count_contained_in(best_path->outersortkeys,
-                                                   outer_path->pathkeys,
-                                                   &presorted_keys);
-           Assert(!is_sorted);
-
-           if (presorted_keys > 0)
-               use_incremental_sort = true;
-       }
-
-       if (!use_incremental_sort)
-       {
-           sort_plan = (Plan *)
-               make_sort_from_pathkeys(outer_plan,
-                                       best_path->outersortkeys,
-                                       outer_relids);
-
-           label_sort_with_costsize(root, (Sort *) sort_plan, -1.0);
-       }
-       else
+       if (enable_incremental_sort && best_path->outer_presorted_keys > 0)
        {
            sort_plan = (Plan *)
                make_incrementalsort_from_pathkeys(outer_plan,
                                                   best_path->outersortkeys,
                                                   outer_relids,
-                                                  presorted_keys);
+                                                  best_path->outer_presorted_keys);
 
            label_incrementalsort_with_costsize(root,
                                                (IncrementalSort *) sort_plan,
                                                best_path->outersortkeys,
                                                -1.0);
        }
+       else
+       {
+           sort_plan = (Plan *)
+               make_sort_from_pathkeys(outer_plan,
+                                       best_path->outersortkeys,
+                                       outer_relids);
+
+           label_sort_with_costsize(root, (Sort *) sort_plan, -1.0);
+       }
 
        outer_plan = sort_plan;
        outerpathkeys = best_path->outersortkeys;
@@ -4578,9 +4571,19 @@ create_mergejoin_plan(PlannerInfo *root,
         */
 
        Relids      inner_relids = inner_path->parent->relids;
-       Sort       *sort = make_sort_from_pathkeys(inner_plan,
-                                                  best_path->innersortkeys,
-                                                  inner_relids);
+       Sort       *sort;
+
+       /*
+        * We can assert that the inner path is not already ordered
+        * appropriately for the mergejoin; otherwise, innersortkeys would
+        * have been set to NIL.
+        */
+       Assert(!pathkeys_contained_in(best_path->innersortkeys,
+                                     inner_path->pathkeys));
+
+       sort = make_sort_from_pathkeys(inner_plan,
+                                      best_path->innersortkeys,
+                                      inner_relids);
 
        label_sort_with_costsize(root, sort, -1.0);
        inner_plan = (Plan *) sort;
index 93e73cb44dbbd3756e1c9a37b75e935fedd19be9..e0192d4a491d25e693027d51c83e1dd068c18844 100644 (file)
@@ -2626,6 +2626,7 @@ create_nestloop_path(PlannerInfo *root,
  *     (this should be a subset of the restrict_clauses list)
  * 'outersortkeys' are the sort varkeys for the outer relation
  * 'innersortkeys' are the sort varkeys for the inner relation
+ * 'outer_presorted_keys' is the number of presorted keys of the outer path
  */
 MergePath *
 create_mergejoin_path(PlannerInfo *root,
@@ -2640,7 +2641,8 @@ create_mergejoin_path(PlannerInfo *root,
                      Relids required_outer,
                      List *mergeclauses,
                      List *outersortkeys,
-                     List *innersortkeys)
+                     List *innersortkeys,
+                     int outer_presorted_keys)
 {
    MergePath  *pathnode = makeNode(MergePath);
 
@@ -2669,6 +2671,7 @@ create_mergejoin_path(PlannerInfo *root,
    pathnode->path_mergeclauses = mergeclauses;
    pathnode->outersortkeys = outersortkeys;
    pathnode->innersortkeys = innersortkeys;
+   pathnode->outer_presorted_keys = outer_presorted_keys;
    /* pathnode->skip_mark_restore will be set by final_cost_mergejoin */
    /* pathnode->materialize_inner will be set by final_cost_mergejoin */
 
index 011e5a811c3f6dbacfb5dfcc919d3c5ad62894c7..1dd2d1560cb3807adaacd27eafcf2210afa86b41 100644 (file)
@@ -2253,6 +2253,12 @@ typedef struct NestPath
  * mergejoin.  If it is not NIL then it is a PathKeys list describing
  * the ordering that must be created by an explicit Sort node.
  *
+ * outer_presorted_keys is the number of presorted keys of the outer
+ * path that match outersortkeys.  It is used to determine whether
+ * explicit incremental sort can be applied when outersortkeys is not
+ * NIL.  We do not track the number of presorted keys of the inner
+ * path, as incremental sort currently does not support mark/restore.
+ *
  * skip_mark_restore is true if the executor need not do mark/restore calls.
  * Mark/restore overhead is usually required, but can be skipped if we know
  * that the executor need find only one match per outer tuple, and that the
@@ -2270,6 +2276,8 @@ typedef struct MergePath
    List       *path_mergeclauses;  /* join clauses to be used for merge */
    List       *outersortkeys;  /* keys for explicit sort, if any */
    List       *innersortkeys;  /* keys for explicit sort, if any */
+   int         outer_presorted_keys;   /* number of presorted keys of the
+                                        * outer path */
    bool        skip_mark_restore;  /* can executor skip mark/restore? */
    bool        materialize_inner;  /* add Materialize to inner? */
 } MergePath;
index c59874408178a6f6fec8c1bcab48d50d282ac4e4..d397fe27dc1e1638e23cf941aea9164023ec93b9 100644 (file)
@@ -160,6 +160,7 @@ extern void initial_cost_mergejoin(PlannerInfo *root,
                                   List *mergeclauses,
                                   Path *outer_path, Path *inner_path,
                                   List *outersortkeys, List *innersortkeys,
+                                  int outer_presorted_keys,
                                   JoinPathExtraData *extra);
 extern void final_cost_mergejoin(PlannerInfo *root, MergePath *path,
                                 JoinCostWorkspace *workspace,
index 719be3897f637c222a6fcc1e6106ea669b4a1361..60dcdb77e41be59a26e6ed979c297f86cde18b5d 100644 (file)
@@ -179,7 +179,8 @@ extern MergePath *create_mergejoin_path(PlannerInfo *root,
                                        Relids required_outer,
                                        List *mergeclauses,
                                        List *outersortkeys,
-                                       List *innersortkeys);
+                                       List *innersortkeys,
+                                       int outer_presorted_keys);
 
 extern HashPath *create_hashjoin_path(PlannerInfo *root,
                                      RelOptInfo *joinrel,