diff options
author | Richard Guo <rguo@postgresql.org> | 2025-05-08 18:21:32 +0900 |
---|---|---|
committer | Richard Guo <rguo@postgresql.org> | 2025-05-08 18:21:32 +0900 |
commit | c06e909c26f070dee78f73c35565d6f4a4ffdcda (patch) | |
tree | 9f69470d8ba03027f37796542b8c0e7bafb2f44e /src/backend/optimizer/path/joinpath.c | |
parent | 773db22269d474fab46d25e9e15b1e55252cf92c (diff) | |
download | postgresql-c06e909c26f070dee78f73c35565d6f4a4ffdcda.tar.gz postgresql-c06e909c26f070dee78f73c35565d6f4a4ffdcda.zip |
Track the number of presorted outer pathkeys in MergePath
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
Diffstat (limited to 'src/backend/optimizer/path/joinpath.c')
-rw-r--r-- | src/backend/optimizer/path/joinpath.c | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c index 14ed9daeca8..26f0336f1e4 100644 --- a/src/backend/optimizer/path/joinpath.c +++ b/src/backend/optimizer/path/joinpath.c @@ -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)); } /* |