From c06e909c26f070dee78f73c35565d6f4a4ffdcda Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Thu, 8 May 2025 18:21:32 +0900 Subject: 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 Author: Richard Guo Reviewed-by: Tender Wang Discussion: https://postgr.es/m/CAApHDvqvBireB_w6x8BN5txdvBEHxVgZBt=rUnpf5ww5P_E_ww@mail.gmail.com --- src/include/nodes/pathnodes.h | 8 ++++++++ src/include/optimizer/cost.h | 1 + src/include/optimizer/pathnode.h | 3 ++- 3 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src/include') diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index 011e5a811c3..1dd2d1560cb 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -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; diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h index c5987440817..d397fe27dc1 100644 --- a/src/include/optimizer/cost.h +++ b/src/include/optimizer/cost.h @@ -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, diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h index 719be3897f6..60dcdb77e41 100644 --- a/src/include/optimizer/pathnode.h +++ b/src/include/optimizer/pathnode.h @@ -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, -- cgit v1.2.3