Skip to content

Commit b5956a2

Browse files
committed
Detect case where an outer join can be reduced to a plain inner join
because there are WHERE clauses that will reject the null-extended rows. Per suggestion from Brandon Craig Rhodes, 19-Nov-02.
1 parent 43785a4 commit b5956a2

File tree

3 files changed

+378
-15
lines changed

3 files changed

+378
-15
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.145 2003/02/09 00:30:39 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.146 2003/02/09 23:57:19 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -144,6 +144,7 @@ subquery_planner(Query *parse, double tuple_fraction)
144144
{
145145
List *saved_initplan = PlannerInitPlan;
146146
int saved_planid = PlannerPlanId;
147+
bool hasOuterJoins;
147148
Plan *plan;
148149
List *newHaving;
149150
List *lst;
@@ -172,18 +173,25 @@ subquery_planner(Query *parse, double tuple_fraction)
172173

173174
/*
174175
* Detect whether any rangetable entries are RTE_JOIN kind; if not,
175-
* we can avoid the expense of doing flatten_join_alias_vars().
176+
* we can avoid the expense of doing flatten_join_alias_vars(). Also
177+
* check for outer joins --- if none, we can skip reduce_outer_joins().
176178
* This must be done after we have done pull_up_subqueries, of course.
177179
*/
178180
parse->hasJoinRTEs = false;
181+
hasOuterJoins = false;
179182
foreach(lst, parse->rtable)
180183
{
181184
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lst);
182185

183186
if (rte->rtekind == RTE_JOIN)
184187
{
185188
parse->hasJoinRTEs = true;
186-
break;
189+
if (IS_OUTER_JOIN(rte->jointype))
190+
{
191+
hasOuterJoins = true;
192+
/* Can quit scanning once we find an outer join */
193+
break;
194+
}
187195
}
188196
}
189197

@@ -244,15 +252,23 @@ subquery_planner(Query *parse, double tuple_fraction)
244252
}
245253
parse->havingQual = (Node *) newHaving;
246254

255+
/*
256+
* If we have any outer joins, try to reduce them to plain inner joins.
257+
* This step is most easily done after we've done expression preprocessing.
258+
*/
259+
if (hasOuterJoins)
260+
reduce_outer_joins(parse);
261+
247262
/*
248263
* See if we can simplify the jointree; opportunities for this may come
249264
* from having pulled up subqueries, or from flattening explicit JOIN
250265
* syntax. We must do this after flattening JOIN alias variables, since
251266
* eliminating explicit JOIN nodes from the jointree will cause
252-
* get_relids_for_join() to fail.
267+
* get_relids_for_join() to fail. But it should happen after
268+
* reduce_outer_joins, anyway.
253269
*/
254270
parse->jointree = (FromExpr *)
255-
preprocess_jointree(parse, (Node *) parse->jointree);
271+
simplify_jointree(parse, (Node *) parse->jointree);
256272

257273
/*
258274
* Do the main planning. If we have an inherited target relation,

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