Skip to content

Commit ec38dcd

Browse files
committed
Tweak a couple of planner APIs to save recalculating join relids.
Discussion: https://postgr.es/m/f8128b11-c5bf-3539-48cd-234178b2314d@proxel.se
1 parent c792c7d commit ec38dcd

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

src/backend/optimizer/path/costsize.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4023,6 +4023,7 @@ get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel,
40234023
* them to all the join cost estimation functions.
40244024
*
40254025
* Input parameters:
4026+
* joinrel: join relation under consideration
40264027
* outerrel: outer relation under consideration
40274028
* innerrel: inner relation under consideration
40284029
* jointype: if not JOIN_SEMI or JOIN_ANTI, we assume it's inner_unique
@@ -4033,6 +4034,7 @@ get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel,
40334034
*/
40344035
void
40354036
compute_semi_anti_join_factors(PlannerInfo *root,
4037+
RelOptInfo *joinrel,
40364038
RelOptInfo *outerrel,
40374039
RelOptInfo *innerrel,
40384040
JoinType jointype,
@@ -4056,14 +4058,12 @@ compute_semi_anti_join_factors(PlannerInfo *root,
40564058
*/
40574059
if (IS_OUTER_JOIN(jointype))
40584060
{
4059-
Relids joinrelids = bms_union(outerrel->relids, innerrel->relids);
4060-
40614061
joinquals = NIL;
40624062
foreach(l, restrictlist)
40634063
{
40644064
RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
40654065

4066-
if (!RINFO_IS_PUSHED_DOWN(rinfo, joinrelids))
4066+
if (!RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
40674067
joinquals = lappend(joinquals, rinfo);
40684068
}
40694069
}

src/backend/optimizer/path/joinpath.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ add_paths_to_joinrel(PlannerInfo *root,
171171
break;
172172
case JOIN_UNIQUE_OUTER:
173173
extra.inner_unique = innerrel_is_unique(root,
174+
joinrel->relids,
174175
outerrel->relids,
175176
innerrel,
176177
JOIN_INNER,
@@ -179,6 +180,7 @@ add_paths_to_joinrel(PlannerInfo *root,
179180
break;
180181
default:
181182
extra.inner_unique = innerrel_is_unique(root,
183+
joinrel->relids,
182184
outerrel->relids,
183185
innerrel,
184186
jointype,
@@ -207,7 +209,7 @@ add_paths_to_joinrel(PlannerInfo *root,
207209
* for cost estimation. These will be the same for all paths.
208210
*/
209211
if (jointype == JOIN_SEMI || jointype == JOIN_ANTI || extra.inner_unique)
210-
compute_semi_anti_join_factors(root, outerrel, innerrel,
212+
compute_semi_anti_join_factors(root, joinrel, outerrel, innerrel,
211213
jointype, sjinfo, restrictlist,
212214
&extra.semifactors);
213215

src/backend/optimizer/plan/analyzejoins.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel,
4242
List *clause_list);
4343
static Oid distinct_col_search(int colno, List *colnos, List *opids);
4444
static bool is_innerrel_unique_for(PlannerInfo *root,
45+
Relids joinrelids,
4546
Relids outerrelids,
4647
RelOptInfo *innerrel,
4748
JoinType jointype,
@@ -565,7 +566,8 @@ reduce_unique_semijoins(PlannerInfo *root)
565566
innerrel->joininfo);
566567

567568
/* Test whether the innerrel is unique for those clauses. */
568-
if (!innerrel_is_unique(root, sjinfo->min_lefthand, innerrel,
569+
if (!innerrel_is_unique(root,
570+
joinrelids, sjinfo->min_lefthand, innerrel,
569571
JOIN_SEMI, restrictlist, true))
570572
continue;
571573

@@ -947,7 +949,8 @@ distinct_col_search(int colno, List *colnos, List *opids)
947949
*
948950
* We need an actual RelOptInfo for the innerrel, but it's sufficient to
949951
* identify the outerrel by its Relids. This asymmetry supports use of this
950-
* function before joinrels have been built.
952+
* function before joinrels have been built. (The caller is expected to
953+
* also supply the joinrelids, just to save recalculating that.)
951954
*
952955
* The proof must be made based only on clauses that will be "joinquals"
953956
* rather than "otherquals" at execution. For an inner join there's no
@@ -966,6 +969,7 @@ distinct_col_search(int colno, List *colnos, List *opids)
966969
*/
967970
bool
968971
innerrel_is_unique(PlannerInfo *root,
972+
Relids joinrelids,
969973
Relids outerrelids,
970974
RelOptInfo *innerrel,
971975
JoinType jointype,
@@ -1014,7 +1018,7 @@ innerrel_is_unique(PlannerInfo *root,
10141018
}
10151019

10161020
/* No cached information, so try to make the proof. */
1017-
if (is_innerrel_unique_for(root, outerrelids, innerrel,
1021+
if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
10181022
jointype, restrictlist))
10191023
{
10201024
/*
@@ -1073,12 +1077,12 @@ innerrel_is_unique(PlannerInfo *root,
10731077
*/
10741078
static bool
10751079
is_innerrel_unique_for(PlannerInfo *root,
1080+
Relids joinrelids,
10761081
Relids outerrelids,
10771082
RelOptInfo *innerrel,
10781083
JoinType jointype,
10791084
List *restrictlist)
10801085
{
1081-
Relids joinrelids = bms_union(outerrelids, innerrel->relids);
10821086
List *clause_list = NIL;
10831087
ListCell *lc;
10841088

src/include/optimizer/cost.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ extern void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan);
166166
extern void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root);
167167
extern void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root);
168168
extern void compute_semi_anti_join_factors(PlannerInfo *root,
169+
RelOptInfo *joinrel,
169170
RelOptInfo *outerrel,
170171
RelOptInfo *innerrel,
171172
JoinType jointype,

src/include/optimizer/planmain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extern void reduce_unique_semijoins(PlannerInfo *root);
108108
extern bool query_supports_distinctness(Query *query);
109109
extern bool query_is_distinct_for(Query *query, List *colnos, List *opids);
110110
extern bool innerrel_is_unique(PlannerInfo *root,
111-
Relids outerrelids, RelOptInfo *innerrel,
111+
Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel,
112112
JoinType jointype, List *restrictlist, bool force_cache);
113113

114114
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy