PostgreSQL Source Code git master
Loading...
Searching...
No Matches
analyzejoins.c File Reference
Include dependency graph for analyzejoins.c:

Go to the source code of this file.

Data Structures

struct  SelfJoinCandidate
 

Functions

static bool join_is_removable (PlannerInfo *root, SpecialJoinInfo *sjinfo)
 
static void remove_leftjoinrel_from_query (PlannerInfo *root, int relid, SpecialJoinInfo *sjinfo)
 
static void remove_rel_from_query (PlannerInfo *root, int relid, int subst, SpecialJoinInfo *sjinfo, Relids joinrelids)
 
static void remove_rel_from_restrictinfo (RestrictInfo *rinfo, int relid, int ojrelid)
 
static void remove_rel_from_eclass (EquivalenceClass *ec, int relid, int ojrelid)
 
static Listremove_rel_from_joinlist (List *joinlist, int relid, int *nremoved)
 
static bool rel_supports_distinctness (PlannerInfo *root, RelOptInfo *rel)
 
static bool rel_is_distinct_for (PlannerInfo *root, RelOptInfo *rel, List *clause_list, List **extra_clauses)
 
static Oid distinct_col_search (int colno, List *colnos, List *opids)
 
static bool is_innerrel_unique_for (PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, List **extra_clauses)
 
static int self_join_candidates_cmp (const void *a, const void *b)
 
static bool replace_relid_callback (Node *node, ChangeVarNodes_context *context)
 
Listremove_useless_joins (PlannerInfo *root, List *joinlist)
 
void reduce_unique_semijoins (PlannerInfo *root)
 
bool query_supports_distinctness (Query *query)
 
bool query_is_distinct_for (Query *query, List *colnos, List *opids)
 
bool innerrel_is_unique (PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
 
bool innerrel_is_unique_ext (PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache, List **extra_clauses)
 
static void update_eclasses (EquivalenceClass *ec, int from, int to)
 
static bool restrict_infos_logically_equal (RestrictInfo *a, RestrictInfo *b)
 
static void add_non_redundant_clauses (PlannerInfo *root, List *rinfo_candidates, List **keep_rinfo_list, Index removed_relid)
 
static void remove_self_join_rel (PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark, RelOptInfo *toKeep, RelOptInfo *toRemove, List *restrictlist)
 
static void split_selfjoin_quals (PlannerInfo *root, List *joinquals, List **selfjoinquals, List **otherjoinquals, int from, int to)
 
static bool match_unique_clauses (PlannerInfo *root, RelOptInfo *outer, List *uclauses, Index relid)
 
static Relids remove_self_joins_one_group (PlannerInfo *root, Relids relids)
 
static Relids remove_self_joins_recurse (PlannerInfo *root, List *joinlist, Relids toRemove)
 
Listremove_useless_self_joins (PlannerInfo *root, List *joinlist)
 

Variables

bool enable_self_join_elimination
 

Function Documentation

◆ add_non_redundant_clauses()

static void add_non_redundant_clauses ( PlannerInfo root,
List rinfo_candidates,
List **  keep_rinfo_list,
Index  removed_relid 
)
static

Definition at line 1687 of file analyzejoins.c.

1691{
1693 {
1694 bool is_redundant = false;
1695
1696 Assert(!bms_is_member(removed_relid, rinfo->required_relids));
1697
1699 {
1700 if (!bms_equal(src->clause_relids, rinfo->clause_relids))
1701 /* Can't compare trivially different clauses */
1702 continue;
1703
1704 if (src == rinfo ||
1705 (rinfo->parent_ec != NULL &&
1706 src->parent_ec == rinfo->parent_ec) ||
1708 {
1709 is_redundant = true;
1710 break;
1711 }
1712 }
1713 if (!is_redundant)
1715 }
1716}
static bool restrict_infos_logically_equal(RestrictInfo *a, RestrictInfo *b)
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:142
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
#define Assert(condition)
Definition c.h:943
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition initsplan.c:3578
#define foreach_node(type, var, lst)
Definition pg_list.h:528
static int fb(int x)
tree ctl root
Definition radixtree.h:1857

References Assert, bms_equal(), bms_is_member(), distribute_restrictinfo_to_rels(), fb(), foreach_node, restrict_infos_logically_equal(), and root.

Referenced by remove_self_join_rel().

◆ distinct_col_search()

static Oid distinct_col_search ( int  colno,
List colnos,
List opids 
)
static

Definition at line 1298 of file analyzejoins.c.

1299{
1300 ListCell *lc1,
1301 *lc2;
1302
1304 {
1305 if (colno == lfirst_int(lc1))
1306 return lfirst_oid(lc2);
1307 }
1308 return InvalidOid;
1309}
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
#define lfirst_int(lc)
Definition pg_list.h:173
#define lfirst_oid(lc)
Definition pg_list.h:174
#define InvalidOid

References fb(), forboth, InvalidOid, lfirst_int, and lfirst_oid.

Referenced by query_is_distinct_for().

◆ innerrel_is_unique()

bool innerrel_is_unique ( PlannerInfo root,
Relids  joinrelids,
Relids  outerrelids,
RelOptInfo innerrel,
JoinType  jointype,
List restrictlist,
bool  force_cache 
)

Definition at line 1338 of file analyzejoins.c.

1345{
1346 return innerrel_is_unique_ext(root, joinrelids, outerrelids, innerrel,
1347 jointype, restrictlist, force_cache, NULL);
1348}
bool innerrel_is_unique_ext(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache, List **extra_clauses)

References fb(), innerrel_is_unique_ext(), and root.

Referenced by add_paths_to_joinrel(), and reduce_unique_semijoins().

◆ innerrel_is_unique_ext()

bool innerrel_is_unique_ext ( PlannerInfo root,
Relids  joinrelids,
Relids  outerrelids,
RelOptInfo innerrel,
JoinType  jointype,
List restrictlist,
bool  force_cache,
List **  extra_clauses 
)

Definition at line 1360 of file analyzejoins.c.

1368{
1370 ListCell *lc;
1372 List *outer_exprs = NIL;
1373 bool self_join = (extra_clauses != NULL);
1374
1375 /* Certainly can't prove uniqueness when there are no joinclauses */
1376 if (restrictlist == NIL)
1377 return false;
1378
1379 /*
1380 * Make a quick check to eliminate cases in which we will surely be unable
1381 * to prove uniqueness of the innerrel.
1382 */
1383 if (!rel_supports_distinctness(root, innerrel))
1384 return false;
1385
1386 /*
1387 * Query the cache to see if we've managed to prove that innerrel is
1388 * unique for any subset of this outerrel. For non-self-join search, we
1389 * don't need an exact match, as extra outerrels can't make the innerrel
1390 * any less unique (or more formally, the restrictlist for a join to a
1391 * superset outerrel must be a superset of the conditions we successfully
1392 * used before). For self-join search, we require an exact match of
1393 * outerrels because we need extra clauses to be valid for our case. Also,
1394 * for self-join checking we've filtered the clauses list. Thus, we can
1395 * match only the result cached for a self-join search for another
1396 * self-join check.
1397 */
1398 foreach(lc, innerrel->unique_for_rels)
1399 {
1401
1402 if ((!self_join && bms_is_subset(uniqueRelInfo->outerrelids, outerrelids)) ||
1403 (self_join && bms_equal(uniqueRelInfo->outerrelids, outerrelids) &&
1404 uniqueRelInfo->self_join))
1405 {
1406 if (extra_clauses)
1407 *extra_clauses = uniqueRelInfo->extra_clauses;
1408 return true; /* Success! */
1409 }
1410 }
1411
1412 /*
1413 * Conversely, we may have already determined that this outerrel, or some
1414 * superset thereof, cannot prove this innerrel to be unique.
1415 */
1416 foreach(lc, innerrel->non_unique_for_rels)
1417 {
1418 Relids unique_for_rels = (Relids) lfirst(lc);
1419
1420 if (bms_is_subset(outerrelids, unique_for_rels))
1421 return false;
1422 }
1423
1424 /* No cached information, so try to make the proof. */
1425 if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1426 jointype, restrictlist,
1427 self_join ? &outer_exprs : NULL))
1428 {
1429 /*
1430 * Cache the positive result for future probes, being sure to keep it
1431 * in the planner_cxt even if we are working in GEQO.
1432 *
1433 * Note: one might consider trying to isolate the minimal subset of
1434 * the outerrels that proved the innerrel unique. But it's not worth
1435 * the trouble, because the planner builds up joinrels incrementally
1436 * and so we'll see the minimally sufficient outerrels before any
1437 * supersets of them anyway.
1438 */
1439 old_context = MemoryContextSwitchTo(root->planner_cxt);
1441 uniqueRelInfo->outerrelids = bms_copy(outerrelids);
1442 uniqueRelInfo->self_join = self_join;
1443 uniqueRelInfo->extra_clauses = outer_exprs;
1444 innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1447
1448 if (extra_clauses)
1449 *extra_clauses = outer_exprs;
1450 return true; /* Success! */
1451 }
1452 else
1453 {
1454 /*
1455 * None of the join conditions for outerrel proved innerrel unique, so
1456 * we can safely reject this outerrel or any subset of it in future
1457 * checks.
1458 *
1459 * However, in normal planning mode, caching this knowledge is totally
1460 * pointless; it won't be queried again, because we build up joinrels
1461 * from smaller to larger. It's only useful when using GEQO or
1462 * another planner extension that attempts planning multiple times.
1463 *
1464 * Also, allow callers to override that heuristic and force caching;
1465 * that's useful for reduce_unique_semijoins, which calls here before
1466 * the normal join search starts.
1467 */
1468 if (force_cache || root->assumeReplanning)
1469 {
1470 old_context = MemoryContextSwitchTo(root->planner_cxt);
1471 innerrel->non_unique_for_rels =
1472 lappend(innerrel->non_unique_for_rels,
1473 bms_copy(outerrelids));
1475 }
1476
1477 return false;
1478 }
1479}
static bool is_innerrel_unique_for(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, List **extra_clauses)
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:412
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
List * lappend(List *list, void *datum)
Definition list.c:339
#define makeNode(_type_)
Definition nodes.h:161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
Bitmapset * Relids
Definition pathnodes.h:103
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
Definition pg_list.h:54
List * unique_for_rels
Definition pathnodes.h:1124
List * non_unique_for_rels
Definition pathnodes.h:1126

References bms_copy(), bms_equal(), bms_is_subset(), fb(), is_innerrel_unique_for(), lappend(), lfirst, makeNode, MemoryContextSwitchTo(), NIL, RelOptInfo::non_unique_for_rels, rel_supports_distinctness(), root, and RelOptInfo::unique_for_rels.

Referenced by innerrel_is_unique(), and remove_self_joins_one_group().

◆ is_innerrel_unique_for()

static bool is_innerrel_unique_for ( PlannerInfo root,
Relids  joinrelids,
Relids  outerrelids,
RelOptInfo innerrel,
JoinType  jointype,
List restrictlist,
List **  extra_clauses 
)
static

Definition at line 1487 of file analyzejoins.c.

1494{
1495 List *clause_list = NIL;
1496 ListCell *lc;
1497
1498 /*
1499 * Search for mergejoinable clauses that constrain the inner rel against
1500 * the outer rel. If an operator is mergejoinable then it behaves like
1501 * equality for some btree opclass, so it's what we want. The
1502 * mergejoinability test also eliminates clauses containing volatile
1503 * functions, which we couldn't depend on.
1504 */
1505 foreach(lc, restrictlist)
1506 {
1508
1509 /*
1510 * As noted above, if it's a pushed-down clause and we're at an outer
1511 * join, we can't use it.
1512 */
1513 if (IS_OUTER_JOIN(jointype) &&
1515 continue;
1516
1517 /* Ignore if it's not a mergejoinable clause */
1518 if (!restrictinfo->can_join ||
1519 restrictinfo->mergeopfamilies == NIL)
1520 continue; /* not mergejoinable */
1521
1522 /*
1523 * Check if the clause has the form "outer op inner" or "inner op
1524 * outer", and if so mark which side is inner.
1525 */
1526 if (!clause_sides_match_join(restrictinfo, outerrelids,
1527 innerrel->relids))
1528 continue; /* no good for these input relations */
1529
1530 /* OK, add to the list */
1532 }
1533
1534 /* Let rel_is_distinct_for() do the hard work */
1535 return rel_is_distinct_for(root, innerrel, clause_list, extra_clauses);
1536}
static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list, List **extra_clauses)
#define IS_OUTER_JOIN(jointype)
Definition nodes.h:348
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition pathnodes.h:3058
static bool clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids, Relids innerrelids)
Relids relids
Definition pathnodes.h:1021

References clause_sides_match_join(), fb(), IS_OUTER_JOIN, lappend(), lfirst, NIL, rel_is_distinct_for(), RelOptInfo::relids, RINFO_IS_PUSHED_DOWN, and root.

Referenced by innerrel_is_unique_ext().

◆ join_is_removable()

static bool join_is_removable ( PlannerInfo root,
SpecialJoinInfo sjinfo 
)
static

Definition at line 158 of file analyzejoins.c.

159{
160 int innerrelid;
161 RelOptInfo *innerrel;
165 ListCell *l;
166 int attroff;
167
168 /*
169 * Must be a left join to a single baserel, else we aren't going to be
170 * able to do anything with it.
171 */
172 if (sjinfo->jointype != JOIN_LEFT)
173 return false;
174
176 return false;
177
178 /*
179 * Never try to eliminate a left join to the query result rel. Although
180 * the case is syntactically impossible in standard SQL, MERGE will build
181 * a join tree that looks exactly like that.
182 */
183 if (innerrelid == root->parse->resultRelation)
184 return false;
185
186 innerrel = find_base_rel(root, innerrelid);
187
188 /*
189 * Before we go to the effort of checking whether any innerrel variables
190 * are needed above the join, make a quick check to eliminate cases in
191 * which we will surely be unable to prove uniqueness of the innerrel.
192 */
193 if (!rel_supports_distinctness(root, innerrel))
194 return false;
195
196 /* Compute the relid set for the join we are considering */
198 Assert(sjinfo->ojrelid != 0);
201
202 /*
203 * We can't remove the join if any inner-rel attributes are used above the
204 * join. Here, "above" the join includes pushed-down conditions, so we
205 * should reject if attr_needed includes the OJ's own relid; therefore,
206 * compare to inputrelids not joinrelids.
207 *
208 * As a micro-optimization, it seems better to start with max_attr and
209 * count down rather than starting with min_attr and counting up, on the
210 * theory that the system attributes are somewhat less likely to be wanted
211 * and should be tested last.
212 */
213 for (attroff = innerrel->max_attr - innerrel->min_attr;
214 attroff >= 0;
215 attroff--)
216 {
217 if (!bms_is_subset(innerrel->attr_needed[attroff], inputrelids))
218 return false;
219 }
220
221 /*
222 * Similarly check that the inner rel isn't needed by any PlaceHolderVars
223 * that will be used above the join. The PHV case is a little bit more
224 * complicated, because PHVs may have been assigned a ph_eval_at location
225 * that includes the innerrel, yet their contained expression might not
226 * actually reference the innerrel (it could be just a constant, for
227 * instance). If such a PHV is due to be evaluated above the join then it
228 * needn't prevent join removal.
229 */
230 foreach(l, root->placeholder_list)
231 {
233
234 if (bms_overlap(phinfo->ph_lateral, innerrel->relids))
235 return false; /* it references innerrel laterally */
236 if (!bms_overlap(phinfo->ph_eval_at, innerrel->relids))
237 continue; /* it definitely doesn't reference innerrel */
238 if (bms_is_subset(phinfo->ph_needed, inputrelids))
239 continue; /* PHV is not used above the join */
240 if (!bms_is_member(sjinfo->ojrelid, phinfo->ph_eval_at))
241 return false; /* it has to be evaluated below the join */
242
243 /*
244 * We need to be sure there will still be a place to evaluate the PHV
245 * if we remove the join, ie that ph_eval_at wouldn't become empty.
246 */
247 if (!bms_overlap(sjinfo->min_lefthand, phinfo->ph_eval_at))
248 return false; /* there isn't any other place to eval PHV */
249 /* Check contained expression last, since this is a bit expensive */
250 if (bms_overlap(pull_varnos(root, (Node *) phinfo->ph_var->phexpr),
251 innerrel->relids))
252 return false; /* contained expression references innerrel */
253 }
254
255 /*
256 * Search for mergejoinable clauses that constrain the inner rel against
257 * either the outer rel or a pseudoconstant. If an operator is
258 * mergejoinable then it behaves like equality for some btree opclass, so
259 * it's what we want. The mergejoinability test also eliminates clauses
260 * containing volatile functions, which we couldn't depend on.
261 */
262 foreach(l, innerrel->joininfo)
263 {
265
266 /*
267 * If the current join commutes with some other outer join(s) via
268 * outer join identity 3, there will be multiple clones of its join
269 * clauses in the joininfo list. We want to consider only the
270 * has_clone form of such clauses. Processing more than one form
271 * would be wasteful, and also some of the others would confuse the
272 * RINFO_IS_PUSHED_DOWN test below.
273 */
274 if (restrictinfo->is_clone)
275 continue; /* ignore it */
276
277 /*
278 * If it's not a join clause for this outer join, we can't use it.
279 * Note that if the clause is pushed-down, then it is logically from
280 * above the outer join, even if it references no other rels (it might
281 * be from WHERE, for example).
282 */
284 continue; /* ignore; not useful here */
285
286 /* Ignore if it's not a mergejoinable clause */
287 if (!restrictinfo->can_join ||
288 restrictinfo->mergeopfamilies == NIL)
289 continue; /* not mergejoinable */
290
291 /*
292 * Check if the clause has the form "outer op inner" or "inner op
293 * outer", and if so mark which side is inner.
294 */
296 innerrel->relids))
297 continue; /* no good for these input relations */
298
299 /* OK, add to list */
301 }
302
303 /*
304 * Now that we have the relevant equality join clauses, try to prove the
305 * innerrel distinct.
306 */
307 if (rel_is_distinct_for(root, innerrel, clause_list, NULL))
308 return true;
309
310 /*
311 * Some day it would be nice to check for other methods of establishing
312 * distinctness.
313 */
314 return false;
315}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:575
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition bitmapset.c:708
@ JOIN_LEFT
Definition nodes.h:304
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition relnode.c:544
Definition nodes.h:135
List * joininfo
Definition pathnodes.h:1148
AttrNumber max_attr
Definition pathnodes.h:1077
AttrNumber min_attr
Definition pathnodes.h:1075
Relids min_righthand
Definition pathnodes.h:3227
JoinType jointype
Definition pathnodes.h:3230
Relids min_lefthand
Definition pathnodes.h:3226
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition var.c:114

References Assert, bms_add_member(), bms_copy(), bms_get_singleton_member(), bms_is_member(), bms_is_subset(), bms_overlap(), bms_union(), clause_sides_match_join(), fb(), find_base_rel(), JOIN_LEFT, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lappend(), lfirst, RelOptInfo::max_attr, RelOptInfo::min_attr, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, SpecialJoinInfo::ojrelid, pull_varnos(), rel_is_distinct_for(), rel_supports_distinctness(), RelOptInfo::relids, RINFO_IS_PUSHED_DOWN, and root.

Referenced by remove_useless_joins().

◆ match_unique_clauses()

static bool match_unique_clauses ( PlannerInfo root,
RelOptInfo outer,
List uclauses,
Index  relid 
)
static

Definition at line 2100 of file analyzejoins.c.

2102{
2104 {
2105 Expr *clause;
2106 Node *iclause;
2107 Node *c1;
2108 bool matched = false;
2109
2110 Assert(outer->relid > 0 && relid > 0);
2111
2112 /* Only filters like f(R.x1,...,R.xN) == expr we should consider. */
2113 Assert(bms_is_empty(rinfo->left_relids) ^
2114 bms_is_empty(rinfo->right_relids));
2115
2116 clause = (Expr *) copyObject(rinfo->clause);
2117 ChangeVarNodesExtended((Node *) clause, relid, outer->relid, 0,
2119
2120 iclause = bms_is_empty(rinfo->left_relids) ? get_rightop(clause) :
2121 get_leftop(clause);
2122 c1 = bms_is_empty(rinfo->left_relids) ? get_leftop(clause) :
2123 get_rightop(clause);
2124
2125 /*
2126 * Compare these left and right sides with the corresponding sides of
2127 * the outer's filters. If no one is detected - return immediately.
2128 */
2130 {
2131 Node *oclause;
2132 Node *c2;
2133
2134 if (orinfo->mergeopfamilies == NIL)
2135 /* Don't consider clauses that aren't similar to 'F(X)=G(Y)' */
2136 continue;
2137
2138 Assert(is_opclause(orinfo->clause));
2139
2140 oclause = bms_is_empty(orinfo->left_relids) ?
2141 get_rightop(orinfo->clause) : get_leftop(orinfo->clause);
2142 c2 = (bms_is_empty(orinfo->left_relids) ?
2143 get_leftop(orinfo->clause) : get_rightop(orinfo->clause));
2144
2145 if (equal(iclause, oclause) && equal(c1, c2))
2146 {
2147 matched = true;
2148 break;
2149 }
2150 }
2151
2152 if (!matched)
2153 return false;
2154 }
2155
2156 return true;
2157}
static bool replace_relid_callback(Node *node, ChangeVarNodes_context *context)
#define bms_is_empty(a)
Definition bitmapset.h:118
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
static Node * get_rightop(const void *clause)
Definition nodeFuncs.h:95
static bool is_opclause(const void *clause)
Definition nodeFuncs.h:76
static Node * get_leftop(const void *clause)
Definition nodeFuncs.h:83
#define copyObject(obj)
Definition nodes.h:232
void ChangeVarNodesExtended(Node *node, int rt_index, int new_index, int sublevels_up, ChangeVarNodes_callback callback)
List * baserestrictinfo
Definition pathnodes.h:1142
Index relid
Definition pathnodes.h:1069

References Assert, RelOptInfo::baserestrictinfo, bms_is_empty, ChangeVarNodesExtended(), copyObject, equal(), fb(), foreach_node, get_leftop(), get_rightop(), is_opclause(), NIL, RelOptInfo::relid, and replace_relid_callback().

Referenced by remove_self_joins_one_group().

◆ query_is_distinct_for()

bool query_is_distinct_for ( Query query,
List colnos,
List opids 
)

Definition at line 1145 of file analyzejoins.c.

1146{
1147 ListCell *l;
1148 Oid opid;
1149
1151
1152 /*
1153 * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
1154 * columns in the DISTINCT clause appear in colnos and operator semantics
1155 * match. This is true even if there are SRFs in the DISTINCT columns or
1156 * elsewhere in the tlist.
1157 */
1158 if (query->distinctClause)
1159 {
1160 foreach(l, query->distinctClause)
1161 {
1164 query->targetList);
1165
1167 if (!OidIsValid(opid) ||
1169 break; /* exit early if no match */
1170 }
1171 if (l == NULL) /* had matches for all? */
1172 return true;
1173 }
1174
1175 /*
1176 * Otherwise, a set-returning function in the query's targetlist can
1177 * result in returning duplicate rows, despite any grouping that might
1178 * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
1179 * columns, it would be safe because they'd be expanded before grouping.
1180 * But it doesn't currently seem worth the effort to check for that.)
1181 */
1182 if (query->hasTargetSRFs)
1183 return false;
1184
1185 /*
1186 * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
1187 * the grouped columns appear in colnos and operator semantics match.
1188 */
1189 if (query->groupClause && !query->groupingSets)
1190 {
1191 foreach(l, query->groupClause)
1192 {
1195 query->targetList);
1196
1198 if (!OidIsValid(opid) ||
1200 break; /* exit early if no match */
1201 }
1202 if (l == NULL) /* had matches for all? */
1203 return true;
1204 }
1205 else if (query->groupingSets)
1206 {
1207 List *gsets;
1208
1209 /*
1210 * If we have grouping sets with expressions, we probably don't have
1211 * uniqueness and analysis would be hard. Punt.
1212 */
1213 if (query->groupClause)
1214 return false;
1215
1216 /*
1217 * If we have no groupClause (therefore no grouping expressions), we
1218 * might have one or many empty grouping sets. If there's just one,
1219 * or if the DISTINCT clause is used on the GROUP BY, then we're
1220 * returning only one row and are certainly unique. But otherwise, we
1221 * know we're certainly not unique.
1222 */
1223 if (query->groupDistinct)
1224 return true;
1225
1226 gsets = expand_grouping_sets(query->groupingSets, false, -1);
1227
1228 return (list_length(gsets) == 1);
1229 }
1230 else
1231 {
1232 /*
1233 * If we have no GROUP BY, but do have aggregates or HAVING, then the
1234 * result is at most one row so it's surely unique, for any operators.
1235 */
1236 if (query->hasAggs || query->havingQual)
1237 return true;
1238 }
1239
1240 /*
1241 * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
1242 * except with ALL.
1243 */
1244 if (query->setOperations)
1245 {
1247
1248 Assert(topop->op != SETOP_NONE);
1249
1250 if (!topop->all)
1251 {
1252 ListCell *lg;
1253
1254 /* We're good if all the nonjunk output columns are in colnos */
1255 lg = list_head(topop->groupClauses);
1256 foreach(l, query->targetList)
1257 {
1260
1261 if (tle->resjunk)
1262 continue; /* ignore resjunk columns */
1263
1264 /* non-resjunk columns should have grouping clauses */
1265 Assert(lg != NULL);
1267 lg = lnext(topop->groupClauses, lg);
1268
1270 if (!OidIsValid(opid) ||
1272 break; /* exit early if no match */
1273 }
1274 if (l == NULL) /* had matches for all? */
1275 return true;
1276 }
1277 }
1278
1279 /*
1280 * XXX Are there any other cases in which we can easily see the result
1281 * must be distinct?
1282 *
1283 * If you do add more smarts to this function, be sure to update
1284 * query_supports_distinctness() to match.
1285 */
1286
1287 return false;
1288}
static Oid distinct_col_search(int colno, List *colnos, List *opids)
#define OidIsValid(objectId)
Definition c.h:858
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition lsyscache.c:773
#define castNode(_type_, nodeptr)
Definition nodes.h:182
List * expand_grouping_sets(List *groupingSets, bool groupDistinct, int limit)
Definition parse_agg.c:2019
@ SETOP_NONE
static int list_length(const List *l)
Definition pg_list.h:152
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:375
unsigned int Oid
bool groupDistinct
Definition parsenodes.h:220
Node * setOperations
Definition parsenodes.h:239
List * groupClause
Definition parsenodes.h:219
Node * havingQual
Definition parsenodes.h:225
List * targetList
Definition parsenodes.h:201
List * groupingSets
Definition parsenodes.h:223
List * distinctClause
Definition parsenodes.h:229
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition tlist.c:376

References Assert, castNode, distinct_col_search(), Query::distinctClause, equality_ops_are_compatible(), expand_grouping_sets(), fb(), get_sortgroupclause_tle(), Query::groupClause, Query::groupDistinct, Query::groupingSets, Query::havingQual, lfirst, list_head(), list_length(), lnext(), OidIsValid, SETOP_NONE, Query::setOperations, and Query::targetList.

Referenced by rel_is_distinct_for().

◆ query_supports_distinctness()

bool query_supports_distinctness ( Query query)

Definition at line 1107 of file analyzejoins.c.

1108{
1109 /* SRFs break distinctness except with DISTINCT, see below */
1110 if (query->hasTargetSRFs && query->distinctClause == NIL)
1111 return false;
1112
1113 /* check for features we can prove distinctness with */
1114 if (query->distinctClause != NIL ||
1115 query->groupClause != NIL ||
1116 query->groupingSets != NIL ||
1117 query->hasAggs ||
1118 query->havingQual ||
1119 query->setOperations)
1120 return true;
1121
1122 return false;
1123}

References Query::distinctClause, Query::groupClause, Query::groupingSets, Query::havingQual, NIL, and Query::setOperations.

Referenced by rel_supports_distinctness().

◆ reduce_unique_semijoins()

void reduce_unique_semijoins ( PlannerInfo root)

Definition at line 873 of file analyzejoins.c.

874{
875 ListCell *lc;
876
877 /*
878 * Scan the join_info_list to find semijoins.
879 */
880 foreach(lc, root->join_info_list)
881 {
883 int innerrelid;
884 RelOptInfo *innerrel;
886 List *restrictlist;
887
888 /*
889 * Must be a semijoin to a single baserel, else we aren't going to be
890 * able to do anything with it.
891 */
892 if (sjinfo->jointype != JOIN_SEMI)
893 continue;
894
896 continue;
897
898 innerrel = find_base_rel(root, innerrelid);
899
900 /*
901 * Before we trouble to run generate_join_implied_equalities, make a
902 * quick check to eliminate cases in which we will surely be unable to
903 * prove uniqueness of the innerrel.
904 */
905 if (!rel_supports_distinctness(root, innerrel))
906 continue;
907
908 /* Compute the relid set for the join we are considering */
910 Assert(sjinfo->ojrelid == 0); /* SEMI joins don't have RT indexes */
911
912 /*
913 * Since we're only considering a single-rel RHS, any join clauses it
914 * has must be clauses linking it to the semijoin's min_lefthand. We
915 * can also consider EC-derived join clauses.
916 */
917 restrictlist =
920 sjinfo->min_lefthand,
921 innerrel,
922 NULL),
923 innerrel->joininfo);
924
925 /* Test whether the innerrel is unique for those clauses. */
927 joinrelids, sjinfo->min_lefthand, innerrel,
928 JOIN_SEMI, restrictlist, true))
929 continue;
930
931 /* OK, remove the SpecialJoinInfo from the list. */
932 root->join_info_list = foreach_delete_current(root->join_info_list, lc);
933 }
934}
bool innerrel_is_unique(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
List * list_concat(List *list1, const List *list2)
Definition list.c:561
@ JOIN_SEMI
Definition nodes.h:317
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:423

References Assert, bms_get_singleton_member(), bms_union(), fb(), find_base_rel(), foreach_delete_current, generate_join_implied_equalities(), innerrel_is_unique(), JOIN_SEMI, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lfirst, list_concat(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, rel_supports_distinctness(), and root.

Referenced by query_planner().

◆ rel_is_distinct_for()

static bool rel_is_distinct_for ( PlannerInfo root,
RelOptInfo rel,
List clause_list,
List **  extra_clauses 
)
static

Definition at line 1009 of file analyzejoins.c.

1011{
1012 /*
1013 * We could skip a couple of tests here if we assume all callers checked
1014 * rel_supports_distinctness first, but it doesn't seem worth taking any
1015 * risk for.
1016 */
1017 if (rel->reloptkind != RELOPT_BASEREL)
1018 return false;
1019 if (rel->rtekind == RTE_RELATION)
1020 {
1021 /*
1022 * Examine the indexes to see if we have a matching unique index.
1023 * relation_has_unique_index_for automatically adds any usable
1024 * restriction clauses for the rel, so we needn't do that here.
1025 */
1026 if (relation_has_unique_index_for(root, rel, clause_list, extra_clauses))
1027 return true;
1028 }
1029 else if (rel->rtekind == RTE_SUBQUERY)
1030 {
1031 Index relid = rel->relid;
1032 Query *subquery = root->simple_rte_array[relid]->subquery;
1033 List *colnos = NIL;
1034 List *opids = NIL;
1035 ListCell *l;
1036
1037 /*
1038 * Build the argument lists for query_is_distinct_for: a list of
1039 * output column numbers that the query needs to be distinct over, and
1040 * a list of equality operators that the output columns need to be
1041 * distinct according to.
1042 *
1043 * (XXX we are not considering restriction clauses attached to the
1044 * subquery; is that worth doing?)
1045 */
1046 foreach(l, clause_list)
1047 {
1049 Oid op;
1050 Var *var;
1051
1052 /*
1053 * Get the equality operator we need uniqueness according to.
1054 * (This might be a cross-type operator and thus not exactly the
1055 * same operator the subquery would consider; that's all right
1056 * since query_is_distinct_for can resolve such cases.) The
1057 * caller's mergejoinability test should have selected only
1058 * OpExprs.
1059 */
1060 op = castNode(OpExpr, rinfo->clause)->opno;
1061
1062 /* caller identified the inner side for us */
1063 if (rinfo->outer_is_left)
1064 var = (Var *) get_rightop(rinfo->clause);
1065 else
1066 var = (Var *) get_leftop(rinfo->clause);
1067
1068 /*
1069 * We may ignore any RelabelType node above the operand. (There
1070 * won't be more than one, since eval_const_expressions() has been
1071 * applied already.)
1072 */
1073 if (var && IsA(var, RelabelType))
1074 var = (Var *) ((RelabelType *) var)->arg;
1075
1076 /*
1077 * If inner side isn't a Var referencing a subquery output column,
1078 * this clause doesn't help us.
1079 */
1080 if (!var || !IsA(var, Var) ||
1081 var->varno != relid || var->varlevelsup != 0)
1082 continue;
1083
1085 opids = lappend_oid(opids, op);
1086 }
1087
1088 if (query_is_distinct_for(subquery, colnos, opids))
1089 return true;
1090 }
1091 return false;
1092}
bool query_is_distinct_for(Query *query, List *colnos, List *opids)
unsigned int Index
Definition c.h:698
bool relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List **extra_clauses)
Definition indxpath.c:4144
List * lappend_int(List *list, int datum)
Definition list.c:357
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
#define IsA(nodeptr, _type_)
Definition nodes.h:164
@ RTE_SUBQUERY
@ RTE_RELATION
@ RELOPT_BASEREL
Definition pathnodes.h:977
#define lfirst_node(type, lc)
Definition pg_list.h:176
RelOptKind reloptkind
Definition pathnodes.h:1015
RTEKind rtekind
Definition pathnodes.h:1073
Expr * clause
Definition pathnodes.h:2901
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
Index varlevelsup
Definition primnodes.h:295

References castNode, RestrictInfo::clause, fb(), get_leftop(), get_rightop(), IsA, lappend_int(), lappend_oid(), lfirst_node, NIL, query_is_distinct_for(), relation_has_unique_index_for(), RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, root, RTE_RELATION, RTE_SUBQUERY, RelOptInfo::rtekind, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by is_innerrel_unique_for(), and join_is_removable().

◆ rel_supports_distinctness()

static bool rel_supports_distinctness ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 949 of file analyzejoins.c.

950{
951 /* We only know about baserels ... */
952 if (rel->reloptkind != RELOPT_BASEREL)
953 return false;
954 if (rel->rtekind == RTE_RELATION)
955 {
956 /*
957 * For a plain relation, we only know how to prove uniqueness by
958 * reference to unique indexes. Make sure there's at least one
959 * suitable unique index. It must be immediately enforced, and not a
960 * partial index. (Keep these conditions in sync with
961 * relation_has_unique_index_for!)
962 */
963 ListCell *lc;
964
965 foreach(lc, rel->indexlist)
966 {
968
969 if (ind->unique && ind->immediate && ind->indpred == NIL)
970 return true;
971 }
972 }
973 else if (rel->rtekind == RTE_SUBQUERY)
974 {
975 Query *subquery = root->simple_rte_array[rel->relid]->subquery;
976
977 /* Check if the subquery has any qualities that support distinctness */
978 if (query_supports_distinctness(subquery))
979 return true;
980 }
981 /* We have no proof rules for any other rtekinds. */
982 return false;
983}
bool query_supports_distinctness(Query *query)
List * indexlist
Definition pathnodes.h:1091

References fb(), RelOptInfo::indexlist, lfirst, NIL, query_supports_distinctness(), RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, root, RTE_RELATION, RTE_SUBQUERY, and RelOptInfo::rtekind.

Referenced by innerrel_is_unique_ext(), join_is_removable(), and reduce_unique_semijoins().

◆ remove_leftjoinrel_from_query()

static void remove_leftjoinrel_from_query ( PlannerInfo root,
int  relid,
SpecialJoinInfo sjinfo 
)
static

Definition at line 326 of file analyzejoins.c.

328{
329 RelOptInfo *rel = find_base_rel(root, relid);
330 int ojrelid = sjinfo->ojrelid;
334 ListCell *l;
335
336 /* Compute the relid set for the join we are considering */
338 Assert(ojrelid != 0);
340
341 remove_rel_from_query(root, relid, -1, sjinfo, joinrelids);
342
343 /*
344 * Remove any joinquals referencing the rel from the joininfo lists.
345 *
346 * In some cases, a joinqual has to be put back after deleting its
347 * reference to the target rel. This can occur for pseudoconstant and
348 * outerjoin-delayed quals, which can get marked as requiring the rel in
349 * order to force them to be evaluated at or above the join. We can't
350 * just discard them, though. Only quals that logically belonged to the
351 * outer join being discarded should be removed from the query.
352 *
353 * We might encounter a qual that is a clone of a deletable qual with some
354 * outer-join relids added (see deconstruct_distribute_oj_quals). To
355 * ensure we get rid of such clones as well, add the relids of all OJs
356 * commutable with this one to the set we test against for
357 * pushed-down-ness.
358 */
360 sjinfo->commute_above_r);
362 sjinfo->commute_below_l);
363
364 /*
365 * We must make a copy of the rel's old joininfo list before starting the
366 * loop, because otherwise remove_join_clause_from_rels would destroy the
367 * list while we're scanning it.
368 */
370 foreach(l, joininfos)
371 {
372 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
373
375
377 {
378 /*
379 * There might be references to relid or ojrelid in the
380 * RestrictInfo's relid sets, as a consequence of PHVs having had
381 * ph_eval_at sets that include those. We already checked above
382 * that any such PHV is safe (and updated its ph_eval_at), so we
383 * can just drop those references.
384 */
385 remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
386
387 /*
388 * Cross-check that the clause itself does not reference the
389 * target rel or join.
390 */
391#ifdef USE_ASSERT_CHECKING
392 {
394 (Node *) rinfo->clause);
395
398 }
399#endif
400 /* Now throw it back into the joininfo lists */
402 }
403 }
404
405 /*
406 * There may be references to the rel in root->fkey_list, but if so,
407 * match_foreign_keys_to_quals() will get rid of them.
408 */
409
410 /*
411 * Now remove the rel from the baserel array to prevent it from being
412 * referenced again. (We can't do this earlier because
413 * remove_join_clause_from_rels will touch it.)
414 */
415 root->simple_rel_array[relid] = NULL;
416 root->simple_rte_array[relid] = NULL;
417
418 /* And nuke the RelOptInfo, just in case there's another access path */
419 pfree(rel);
420
421 /*
422 * Now repeat construction of attr_needed bits coming from all other
423 * sources.
424 */
429}
static void remove_rel_from_query(PlannerInfo *root, int relid, int subst, SpecialJoinInfo *sjinfo, Relids joinrelids)
static void remove_rel_from_restrictinfo(RestrictInfo *rinfo, int relid, int ojrelid)
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:901
void rebuild_eclass_attr_needed(PlannerInfo *root)
void rebuild_lateral_attr_needed(PlannerInfo *root)
Definition initsplan.c:1194
void rebuild_joinclause_attr_needed(PlannerInfo *root)
Definition initsplan.c:3910
void remove_join_clause_from_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition joininfo.c:161
List * list_copy(const List *oldlist)
Definition list.c:1573
void pfree(void *pointer)
Definition mcxt.c:1616
void rebuild_placeholder_attr_needed(PlannerInfo *root)
Relids required_relids
Definition pathnodes.h:2932
Relids commute_above_r
Definition pathnodes.h:3233
Relids commute_below_l
Definition pathnodes.h:3234

References Assert, bms_add_member(), bms_add_members(), bms_is_member(), bms_union(), RestrictInfo::clause, SpecialJoinInfo::commute_above_r, SpecialJoinInfo::commute_below_l, distribute_restrictinfo_to_rels(), fb(), find_base_rel(), RelOptInfo::joininfo, lfirst, list_copy(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, pfree(), pull_varnos(), rebuild_eclass_attr_needed(), rebuild_joinclause_attr_needed(), rebuild_lateral_attr_needed(), rebuild_placeholder_attr_needed(), remove_join_clause_from_rels(), remove_rel_from_query(), remove_rel_from_restrictinfo(), RestrictInfo::required_relids, RINFO_IS_PUSHED_DOWN, and root.

Referenced by remove_useless_joins().

◆ remove_rel_from_eclass()

static void remove_rel_from_eclass ( EquivalenceClass ec,
int  relid,
int  ojrelid 
)
static

Definition at line 758 of file analyzejoins.c.

759{
760 ListCell *lc;
761
762 /* Fix up the EC's overall relids */
763 ec->ec_relids = bms_del_member(ec->ec_relids, relid);
764 ec->ec_relids = bms_del_member(ec->ec_relids, ojrelid);
765
766 /*
767 * We don't expect any EC child members to exist at this point. Ensure
768 * that's the case, otherwise, we might be getting asked to do something
769 * this function hasn't been coded for.
770 */
772
773 /*
774 * Fix up the member expressions. Any non-const member that ends with
775 * empty em_relids must be a Var or PHV of the removed relation. We don't
776 * need it anymore, so we can drop it.
777 */
778 foreach(lc, ec->ec_members)
779 {
781
782 if (bms_is_member(relid, cur_em->em_relids) ||
783 bms_is_member(ojrelid, cur_em->em_relids))
784 {
785 Assert(!cur_em->em_is_const);
786 cur_em->em_relids = bms_del_member(cur_em->em_relids, relid);
787 cur_em->em_relids = bms_del_member(cur_em->em_relids, ojrelid);
788 if (bms_is_empty(cur_em->em_relids))
790 }
791 }
792
793 /* Fix up the source clauses, in case we can re-use them later */
794 foreach(lc, ec->ec_sources)
795 {
796 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
797
798 remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
799 }
800
801 /*
802 * Rather than expend code on fixing up any already-derived clauses, just
803 * drop them. (At this point, any such clauses would be base restriction
804 * clauses, which we'd not need anymore anyway.)
805 */
807}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
void ec_clear_derived_clauses(EquivalenceClass *ec)
List ** ec_childmembers
Definition pathnodes.h:1663

References Assert, bms_del_member(), bms_is_empty, bms_is_member(), EquivalenceClass::ec_childmembers, ec_clear_derived_clauses(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceClass::ec_sources, fb(), foreach_delete_current, lfirst, and remove_rel_from_restrictinfo().

Referenced by remove_rel_from_query().

◆ remove_rel_from_joinlist()

static List * remove_rel_from_joinlist ( List joinlist,
int  relid,
int nremoved 
)
static

Definition at line 819 of file analyzejoins.c.

820{
821 List *result = NIL;
822 ListCell *jl;
823
824 foreach(jl, joinlist)
825 {
826 Node *jlnode = (Node *) lfirst(jl);
827
828 if (IsA(jlnode, RangeTblRef))
829 {
830 int varno = ((RangeTblRef *) jlnode)->rtindex;
831
832 if (varno == relid)
833 (*nremoved)++;
834 else
836 }
837 else if (IsA(jlnode, List))
838 {
839 /* Recurse to handle subproblem */
840 List *sublist;
841
843 relid, nremoved);
844 /* Avoid including empty sub-lists in the result */
845 if (sublist)
847 }
848 else
849 {
850 elog(ERROR, "unrecognized joinlist node type: %d",
851 (int) nodeTag(jlnode));
852 }
853 }
854
855 return result;
856}
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
uint32 result
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define nodeTag(nodeptr)
Definition nodes.h:139

References elog, ERROR, fb(), IsA, lappend(), lfirst, NIL, nodeTag, remove_rel_from_joinlist(), and result.

Referenced by remove_rel_from_joinlist(), remove_useless_joins(), and remove_useless_self_joins().

◆ remove_rel_from_query()

static void remove_rel_from_query ( PlannerInfo root,
int  relid,
int  subst,
SpecialJoinInfo sjinfo,
Relids  joinrelids 
)
static

Definition at line 449 of file analyzejoins.c.

452{
453 int ojrelid = sjinfo ? sjinfo->ojrelid : 0;
454 Index rti;
455 ListCell *l;
456 bool is_outer_join = (sjinfo != NULL);
457 bool is_self_join = (!is_outer_join && subst > 0);
458
460 Assert(!is_outer_join || ojrelid > 0);
462
463 /*
464 * Update all_baserels and related relid sets.
465 */
466 root->all_baserels = adjust_relid_set(root->all_baserels, relid, subst);
467 root->all_query_rels = adjust_relid_set(root->all_query_rels, relid, subst);
468
469 if (is_outer_join)
470 {
471 root->outer_join_rels = bms_del_member(root->outer_join_rels, ojrelid);
472 root->all_query_rels = bms_del_member(root->all_query_rels, ojrelid);
473 }
474
475 /*
476 * Likewise remove references from SpecialJoinInfo data structures.
477 *
478 * This is relevant in case the relation we're deleting is part of the
479 * relid sets of special joins: those sets have to be adjusted. If we are
480 * removing an outer join, the RHS of the target outer join will be made
481 * empty here, but that's OK since the caller will delete that
482 * SpecialJoinInfo entirely.
483 */
484 foreach(l, root->join_info_list)
485 {
487
488 /*
489 * initsplan.c is fairly cavalier about allowing SpecialJoinInfos'
490 * lefthand/righthand relid sets to be shared with other data
491 * structures. Ensure that we don't modify the original relid sets.
492 * (The commute_xxx sets are always per-SpecialJoinInfo though.)
493 */
494 sjinf->min_lefthand = bms_copy(sjinf->min_lefthand);
495 sjinf->min_righthand = bms_copy(sjinf->min_righthand);
496 sjinf->syn_lefthand = bms_copy(sjinf->syn_lefthand);
497 sjinf->syn_righthand = bms_copy(sjinf->syn_righthand);
498
499 /* Now adjust relid bit in the sets: */
500 sjinf->min_lefthand = adjust_relid_set(sjinf->min_lefthand, relid, subst);
501 sjinf->min_righthand = adjust_relid_set(sjinf->min_righthand, relid, subst);
502 sjinf->syn_lefthand = adjust_relid_set(sjinf->syn_lefthand, relid, subst);
503 sjinf->syn_righthand = adjust_relid_set(sjinf->syn_righthand, relid, subst);
504
505 if (is_outer_join)
506 {
507 /* Remove ojrelid bit from the sets: */
508 sjinf->min_lefthand = bms_del_member(sjinf->min_lefthand, ojrelid);
509 sjinf->min_righthand = bms_del_member(sjinf->min_righthand, ojrelid);
510 sjinf->syn_lefthand = bms_del_member(sjinf->syn_lefthand, ojrelid);
511 sjinf->syn_righthand = bms_del_member(sjinf->syn_righthand, ojrelid);
512 /* relid cannot appear in these fields, but ojrelid can: */
513 sjinf->commute_above_l = bms_del_member(sjinf->commute_above_l, ojrelid);
514 sjinf->commute_above_r = bms_del_member(sjinf->commute_above_r, ojrelid);
515 sjinf->commute_below_l = bms_del_member(sjinf->commute_below_l, ojrelid);
516 sjinf->commute_below_r = bms_del_member(sjinf->commute_below_r, ojrelid);
517 }
518 else
519 {
520 /*
521 * For self-join removal, replace relid references in
522 * semi_rhs_exprs.
523 */
524 ChangeVarNodesExtended((Node *) sjinf->semi_rhs_exprs, relid, subst,
526 }
527 }
528
529 /*
530 * Likewise remove references from PlaceHolderVar data structures,
531 * removing any no-longer-needed placeholders entirely. We only remove
532 * PHVs for left-join removal. With self-join elimination, PHVs already
533 * get moved to the remaining relation, where they might still be needed.
534 * It might also happen that we skip the removal of some PHVs that could
535 * be removed. However, the overhead of extra PHVs is small compared to
536 * the complexity of analysis needed to remove them.
537 *
538 * Removal is a bit trickier than it might seem: we can remove PHVs that
539 * are used at the target rel and/or in the join qual, but not those that
540 * are used at join partner rels or above the join. It's not that easy to
541 * distinguish PHVs used at partner rels from those used in the join qual,
542 * since they will both have ph_needed sets that are subsets of
543 * joinrelids. However, a PHV used at a partner rel could not have the
544 * target rel in ph_eval_at, so we check that while deciding whether to
545 * remove or just update the PHV. There is no corresponding test in
546 * join_is_removable because it doesn't need to distinguish those cases.
547 */
548 foreach(l, root->placeholder_list)
549 {
551
552 Assert(!is_outer_join || !bms_is_member(relid, phinfo->ph_lateral));
553
554 if (is_outer_join &&
555 bms_is_subset(phinfo->ph_needed, joinrelids) &&
556 bms_is_member(relid, phinfo->ph_eval_at) &&
557 !bms_is_member(ojrelid, phinfo->ph_eval_at))
558 {
559 root->placeholder_list = foreach_delete_current(root->placeholder_list,
560 l);
561 root->placeholder_array[phinfo->phid] = NULL;
562 }
563 else
564 {
565 PlaceHolderVar *phv = phinfo->ph_var;
566
567 phinfo->ph_eval_at = adjust_relid_set(phinfo->ph_eval_at, relid, subst);
568 if (is_outer_join)
569 phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, ojrelid);
570 Assert(!bms_is_empty(phinfo->ph_eval_at)); /* checked previously */
571
572 /* Reduce ph_needed to contain only "relation 0"; see below */
573 if (bms_is_member(0, phinfo->ph_needed))
574 phinfo->ph_needed = bms_make_singleton(0);
575 else
576 phinfo->ph_needed = NULL;
577
578 phv->phrels = adjust_relid_set(phv->phrels, relid, subst);
579 if (is_outer_join)
580 phv->phrels = bms_del_member(phv->phrels, ojrelid);
581 Assert(!bms_is_empty(phv->phrels));
582
583 /*
584 * For self-join removal, update Var nodes within the PHV's
585 * expression to reference the replacement relid, and adjust
586 * ph_lateral for the relid substitution. (For left-join removal,
587 * we're removing rather than replacing, and any surviving PHV
588 * shouldn't reference the removed rel in its expression. Also,
589 * relid can't appear in ph_lateral for outer joins.)
590 */
591 if (is_self_join)
592 {
593 ChangeVarNodesExtended((Node *) phv->phexpr, relid, subst, 0,
595 phinfo->ph_lateral = adjust_relid_set(phinfo->ph_lateral, relid, subst);
596
597 /*
598 * ph_lateral might contain rels mentioned in ph_eval_at after
599 * the replacement, remove them.
600 */
601 phinfo->ph_lateral = bms_difference(phinfo->ph_lateral, phinfo->ph_eval_at);
602 /* ph_lateral might or might not be empty */
603 }
604
605 Assert(phv->phnullingrels == NULL); /* no need to adjust */
606 }
607 }
608
609 /*
610 * Likewise remove references from EquivalenceClasses.
611 *
612 * For self-join removal, the caller has already updated the
613 * EquivalenceClasses, so we can skip this step.
614 */
615 if (is_outer_join)
616 {
617 foreach(l, root->eq_classes)
618 {
620
621 if (bms_is_member(relid, ec->ec_relids) ||
622 bms_is_member(ojrelid, ec->ec_relids))
623 remove_rel_from_eclass(ec, relid, ojrelid);
624 }
625 }
626
627 /*
628 * Finally, we must prepare for the caller to recompute per-Var
629 * attr_needed and per-PlaceHolderVar ph_needed relid sets. These have to
630 * be known accurately, else we may fail to remove other now-removable
631 * joins. Because the caller removes the join clause(s) associated with
632 * the removed join, Vars that were formerly needed may no longer be.
633 *
634 * The actual reconstruction of these relid sets is performed by the
635 * specific caller. Here, we simply clear out the existing attr_needed
636 * sets (we already did this above for ph_needed) to ensure they are
637 * rebuilt from scratch. We can cheat to one small extent: we can avoid
638 * re-examining the targetlist and HAVING qual by preserving "relation 0"
639 * bits from the existing relid sets. This is safe because we'd never
640 * remove such references.
641 *
642 * Additionally, if we are performing self-join elimination, we must
643 * replace references to the removed relid with subst within the
644 * lateral_vars lists.
645 */
646 for (rti = 1; rti < root->simple_rel_array_size; rti++)
647 {
648 RelOptInfo *otherrel = root->simple_rel_array[rti];
649 int attroff;
650
651 /* there may be empty slots corresponding to non-baserel RTEs */
652 if (otherrel == NULL)
653 continue;
654
655 Assert(otherrel->relid == rti); /* sanity check on array */
656
657 for (attroff = otherrel->max_attr - otherrel->min_attr;
658 attroff >= 0;
659 attroff--)
660 {
661 if (bms_is_member(0, otherrel->attr_needed[attroff]))
662 otherrel->attr_needed[attroff] = bms_make_singleton(0);
663 else
664 otherrel->attr_needed[attroff] = NULL;
665 }
666
667 if (is_self_join)
668 ChangeVarNodesExtended((Node *) otherrel->lateral_vars, relid,
669 subst, 0, replace_relid_callback);
670 }
671}
static void remove_rel_from_eclass(EquivalenceClass *ec, int relid, int ojrelid)
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:346
Bitmapset * bms_make_singleton(int x)
Definition bitmapset.c:216
Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid)

References adjust_relid_set(), Assert, bms_copy(), bms_del_member(), bms_difference(), bms_is_empty, bms_is_member(), bms_is_subset(), bms_make_singleton(), ChangeVarNodesExtended(), EquivalenceClass::ec_relids, fb(), foreach_delete_current, lfirst, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::ojrelid, remove_rel_from_eclass(), replace_relid_callback(), and root.

Referenced by remove_leftjoinrel_from_query(), and remove_self_join_rel().

◆ remove_rel_from_restrictinfo()

static void remove_rel_from_restrictinfo ( RestrictInfo rinfo,
int  relid,
int  ojrelid 
)
static

Definition at line 682 of file analyzejoins.c.

683{
684 /*
685 * initsplan.c is fairly cavalier about allowing RestrictInfos to share
686 * relid sets with other RestrictInfos, and SpecialJoinInfos too. Make
687 * sure this RestrictInfo has its own relid sets before we modify them.
688 * (In present usage, clause_relids is probably not shared, but
689 * required_relids could be; let's not assume anything.)
690 */
691 rinfo->clause_relids = bms_copy(rinfo->clause_relids);
692 rinfo->clause_relids = bms_del_member(rinfo->clause_relids, relid);
693 rinfo->clause_relids = bms_del_member(rinfo->clause_relids, ojrelid);
694 /* Likewise for required_relids */
696 rinfo->required_relids = bms_del_member(rinfo->required_relids, relid);
697 rinfo->required_relids = bms_del_member(rinfo->required_relids, ojrelid);
698 /* Likewise for incompatible_relids */
702 /* Likewise for outer_relids */
703 rinfo->outer_relids = bms_copy(rinfo->outer_relids);
704 rinfo->outer_relids = bms_del_member(rinfo->outer_relids, relid);
705 rinfo->outer_relids = bms_del_member(rinfo->outer_relids, ojrelid);
706 /* Likewise for left_relids */
707 rinfo->left_relids = bms_copy(rinfo->left_relids);
708 rinfo->left_relids = bms_del_member(rinfo->left_relids, relid);
709 rinfo->left_relids = bms_del_member(rinfo->left_relids, ojrelid);
710 /* Likewise for right_relids */
711 rinfo->right_relids = bms_copy(rinfo->right_relids);
712 rinfo->right_relids = bms_del_member(rinfo->right_relids, relid);
713 rinfo->right_relids = bms_del_member(rinfo->right_relids, ojrelid);
714
715 /* If it's an OR, recurse to clean up sub-clauses */
716 if (restriction_is_or_clause(rinfo))
717 {
718 ListCell *lc;
719
720 Assert(is_orclause(rinfo->orclause));
721 foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
722 {
723 Node *orarg = (Node *) lfirst(lc);
724
725 /* OR arguments should be ANDs or sub-RestrictInfos */
726 if (is_andclause(orarg))
727 {
728 List *andargs = ((BoolExpr *) orarg)->args;
729 ListCell *lc2;
730
731 foreach(lc2, andargs)
732 {
734
735 remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
736 }
737 }
738 else
739 {
741
742 remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
743 }
744 }
745 }
746}
static bool is_andclause(const void *clause)
Definition nodeFuncs.h:107
static bool is_orclause(const void *clause)
Definition nodeFuncs.h:116
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Relids outer_relids
Definition pathnodes.h:2938
Relids incompatible_relids
Definition pathnodes.h:2935

References Assert, bms_copy(), bms_del_member(), castNode, fb(), RestrictInfo::incompatible_relids, is_andclause(), is_orclause(), lfirst, lfirst_node, RestrictInfo::outer_relids, remove_rel_from_restrictinfo(), RestrictInfo::required_relids, and restriction_is_or_clause().

Referenced by remove_leftjoinrel_from_query(), remove_rel_from_eclass(), and remove_rel_from_restrictinfo().

◆ remove_self_join_rel()

static void remove_self_join_rel ( PlannerInfo root,
PlanRowMark kmark,
PlanRowMark rmark,
RelOptInfo toKeep,
RelOptInfo toRemove,
List restrictlist 
)
static

Definition at line 1855 of file analyzejoins.c.

1858{
1859 List *joininfos;
1860 ListCell *lc;
1861 int i;
1864
1865 Assert(toKeep->relid > 0);
1866 Assert(toRemove->relid > 0);
1867
1868 /*
1869 * Replace the index of the removing table with the keeping one. The
1870 * technique of removing/distributing restrictinfo is used here to attach
1871 * just appeared (for keeping relation) join clauses and avoid adding
1872 * duplicates of those that already exist in the joininfo list.
1873 */
1874 joininfos = list_copy(toRemove->joininfo);
1876 {
1877 remove_join_clause_from_rels(root, rinfo, rinfo->required_relids);
1878 ChangeVarNodesExtended((Node *) rinfo, toRemove->relid, toKeep->relid,
1880
1881 if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
1883 else
1885 }
1886
1887 /*
1888 * Concatenate restrictlist to the list of base restrictions of the
1889 * removing table just to simplify the replacement procedure: all of them
1890 * weren't connected to any keeping relations and need to be added to some
1891 * rels.
1892 */
1893 toRemove->baserestrictinfo = list_concat(toRemove->baserestrictinfo,
1894 restrictlist);
1895 foreach_node(RestrictInfo, rinfo, toRemove->baserestrictinfo)
1896 {
1897 ChangeVarNodesExtended((Node *) rinfo, toRemove->relid, toKeep->relid,
1899
1900 if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
1902 else
1904 }
1905
1906 /*
1907 * Now, add all non-redundant clauses to the keeping relation.
1908 */
1910 &toKeep->baserestrictinfo, toRemove->relid);
1912 &toKeep->joininfo, toRemove->relid);
1913
1916
1917 /*
1918 * Arrange equivalence classes, mentioned removing a table, with the
1919 * keeping one: varno of removing table should be replaced in members and
1920 * sources lists. Also, remove duplicated elements if this replacement
1921 * procedure created them.
1922 */
1923 i = -1;
1924 while ((i = bms_next_member(toRemove->eclass_indexes, i)) >= 0)
1925 {
1926 EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
1927
1928 update_eclasses(ec, toRemove->relid, toKeep->relid);
1929 toKeep->eclass_indexes = bms_add_member(toKeep->eclass_indexes, i);
1930 }
1931
1932 /*
1933 * Transfer the targetlist and attr_needed flags.
1934 */
1935
1936 foreach(lc, toRemove->reltarget->exprs)
1937 {
1938 Node *node = lfirst(lc);
1939
1940 ChangeVarNodesExtended(node, toRemove->relid, toKeep->relid, 0,
1942 if (!list_member(toKeep->reltarget->exprs, node))
1943 toKeep->reltarget->exprs = lappend(toKeep->reltarget->exprs, node);
1944 }
1945
1946 for (i = toKeep->min_attr; i <= toKeep->max_attr; i++)
1947 {
1948 int attno = i - toKeep->min_attr;
1949
1950 toRemove->attr_needed[attno] = adjust_relid_set(toRemove->attr_needed[attno],
1951 toRemove->relid, toKeep->relid);
1952 toKeep->attr_needed[attno] = bms_add_members(toKeep->attr_needed[attno],
1953 toRemove->attr_needed[attno]);
1954 }
1955
1956 /*
1957 * If the removed relation has a row mark, transfer it to the remaining
1958 * one.
1959 *
1960 * If both rels have row marks, just keep the one corresponding to the
1961 * remaining relation because we verified earlier that they have the same
1962 * strength.
1963 */
1964 if (rmark)
1965 {
1966 if (kmark)
1967 {
1968 Assert(kmark->markType == rmark->markType);
1969
1970 root->rowMarks = list_delete_ptr(root->rowMarks, rmark);
1971 }
1972 else
1973 {
1974 /* Shouldn't have inheritance children here. */
1975 Assert(rmark->rti == rmark->prti);
1976
1977 rmark->rti = rmark->prti = toKeep->relid;
1978 }
1979 }
1980
1981 /*
1982 * Replace varno in all the query structures, except nodes RangeTblRef
1983 * otherwise later remove_rel_from_joinlist will yield errors.
1984 */
1985 ChangeVarNodesExtended((Node *) root->parse, toRemove->relid, toKeep->relid,
1987
1988 /* Replace links in the planner info */
1990
1991 /* Replace varno in the fully-processed targetlist */
1992 ChangeVarNodesExtended((Node *) root->processed_tlist, toRemove->relid,
1993 toKeep->relid, 0, replace_relid_callback);
1994
1995 adjust_relid_set(root->all_result_relids, toRemove->relid, toKeep->relid);
1996 adjust_relid_set(root->leaf_result_relids, toRemove->relid, toKeep->relid);
1997
1998 /*
1999 * There may be references to the rel in root->fkey_list, but if so,
2000 * match_foreign_keys_to_quals() will get rid of them.
2001 */
2002
2003 /*
2004 * Finally, remove the rel from the baserel array to prevent it from being
2005 * referenced again. (We can't do this earlier because
2006 * remove_join_clause_from_rels will touch it.)
2007 */
2008 root->simple_rel_array[toRemove->relid] = NULL;
2009 root->simple_rte_array[toRemove->relid] = NULL;
2010
2011 /* And nuke the RelOptInfo, just in case there's another access path. */
2012 pfree(toRemove);
2013
2014
2015 /*
2016 * Now repeat construction of attr_needed bits coming from all other
2017 * sources.
2018 */
2023}
static void add_non_redundant_clauses(PlannerInfo *root, List *rinfo_candidates, List **keep_rinfo_list, Index removed_relid)
static void update_eclasses(EquivalenceClass *ec, int from, int to)
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
BMS_Membership bms_membership(const Bitmapset *a)
Definition bitmapset.c:765
@ BMS_MULTIPLE
Definition bitmapset.h:73
int i
Definition isn.c:77
List * list_delete_ptr(List *list, void *datum)
Definition list.c:872
void list_free(List *list)
Definition list.c:1546
bool list_member(const List *list, const void *datum)
Definition list.c:661
static void * list_nth(const List *list, int n)
Definition pg_list.h:331

References add_non_redundant_clauses(), adjust_relid_set(), Assert, bms_add_member(), bms_add_members(), bms_membership(), BMS_MULTIPLE, bms_next_member(), ChangeVarNodesExtended(), fb(), foreach_node, i, lappend(), lfirst, list_concat(), list_copy(), list_delete_ptr(), list_free(), list_member(), list_nth(), NIL, pfree(), rebuild_eclass_attr_needed(), rebuild_joinclause_attr_needed(), rebuild_lateral_attr_needed(), rebuild_placeholder_attr_needed(), remove_join_clause_from_rels(), remove_rel_from_query(), replace_relid_callback(), root, and update_eclasses().

Referenced by remove_self_joins_one_group().

◆ remove_self_joins_one_group()

static Relids remove_self_joins_one_group ( PlannerInfo root,
Relids  relids 
)
static

Definition at line 2166 of file analyzejoins.c.

2167{
2168 Relids result = NULL;
2169 int k; /* Index of kept relation */
2170 int r = -1; /* Index of removed relation */
2171
2172 while ((r = bms_next_member(relids, r)) > 0)
2173 {
2174 RelOptInfo *rrel = root->simple_rel_array[r];
2175
2176 k = r;
2177
2178 while ((k = bms_next_member(relids, k)) > 0)
2179 {
2181 RelOptInfo *krel = root->simple_rel_array[k];
2182 List *restrictlist;
2185 ListCell *lc;
2186 bool jinfo_check = true;
2189 List *uclauses = NIL;
2190
2191 /* A sanity check: the relations have the same Oid. */
2192 Assert(root->simple_rte_array[k]->relid ==
2193 root->simple_rte_array[r]->relid);
2194
2195 /*
2196 * It is impossible to eliminate the join of two relations if they
2197 * belong to different rules of order. Otherwise, the planner
2198 * can't find any variants of the correct query plan.
2199 */
2200 foreach(lc, root->join_info_list)
2201 {
2203
2204 if ((bms_is_member(k, info->syn_lefthand) ^
2205 bms_is_member(r, info->syn_lefthand)) ||
2206 (bms_is_member(k, info->syn_righthand) ^
2207 bms_is_member(r, info->syn_righthand)))
2208 {
2209 jinfo_check = false;
2210 break;
2211 }
2212 }
2213 if (!jinfo_check)
2214 continue;
2215
2216 /*
2217 * Check Row Marks equivalence. We can't remove the join if the
2218 * relations have row marks of different strength (e.g., one is
2219 * locked FOR UPDATE, and another just has ROW_MARK_REFERENCE for
2220 * EvalPlanQual rechecking).
2221 */
2222 foreach(lc, root->rowMarks)
2223 {
2225
2226 if (rowMark->rti == r)
2227 {
2228 Assert(rmark == NULL);
2229 rmark = rowMark;
2230 }
2231 else if (rowMark->rti == k)
2232 {
2233 Assert(kmark == NULL);
2234 kmark = rowMark;
2235 }
2236
2237 if (kmark && rmark)
2238 break;
2239 }
2240 if (kmark && rmark && kmark->markType != rmark->markType)
2241 continue;
2242
2243 /*
2244 * We only deal with base rels here, so their relids bitset
2245 * contains only one member -- their relid.
2246 */
2249
2250 /*
2251 * PHVs should not impose any constraints on removing self-joins.
2252 */
2253
2254 /*
2255 * At this stage, joininfo lists of inner and outer can contain
2256 * only clauses required for a superior outer join that can't
2257 * influence this optimization. So, we can avoid to call the
2258 * build_joinrel_restrictlist() routine.
2259 */
2261 rrel->relids,
2262 krel, NULL);
2263 if (restrictlist == NIL)
2264 continue;
2265
2266 /*
2267 * Process restrictlist to separate the self-join quals from the
2268 * other quals. e.g., "x = x" goes to selfjoinquals and "a = b" to
2269 * otherjoinquals.
2270 */
2271 split_selfjoin_quals(root, restrictlist, &selfjoinquals,
2272 &otherjoinquals, rrel->relid, krel->relid);
2273
2274 Assert(list_length(restrictlist) ==
2276
2277 /*
2278 * To enable SJE for the only degenerate case without any self
2279 * join clauses at all, add baserestrictinfo to this list. The
2280 * degenerate case works only if both sides have the same clause.
2281 * So doesn't matter which side to add.
2282 */
2283 selfjoinquals = list_concat(selfjoinquals, krel->baserestrictinfo);
2284
2285 /*
2286 * Determine if the rrel can duplicate outer rows. We must bypass
2287 * the unique rel cache here since we're possibly using a subset
2288 * of join quals. We can use 'force_cache' == true when all join
2289 * quals are self-join quals. Otherwise, we could end up putting
2290 * false negatives in the cache.
2291 */
2295 &uclauses))
2296 continue;
2297
2298 /*
2299 * 'uclauses' is the copy of outer->baserestrictinfo that are
2300 * associated with an index. We proved by matching selfjoinquals
2301 * to a unique index that the outer relation has at most one
2302 * matching row for each inner row. Sometimes that is not enough.
2303 * e.g. "WHERE s1.b = s2.b AND s1.a = 1 AND s2.a = 2" when the
2304 * unique index is (a,b). Having non-empty uclauses, we must
2305 * validate that the inner baserestrictinfo contains the same
2306 * expressions, or we won't match the same row on each side of the
2307 * join.
2308 */
2309 if (!match_unique_clauses(root, rrel, uclauses, krel->relid))
2310 continue;
2311
2312 /*
2313 * Remove rrel RelOptInfo from the planner structures and the
2314 * corresponding row mark.
2315 */
2316 remove_self_join_rel(root, kmark, rmark, krel, rrel, restrictlist);
2317
2319
2320 /* We have removed the outer relation, try the next one. */
2321 break;
2322 }
2323 }
2324
2325 return result;
2326}
static bool match_unique_clauses(PlannerInfo *root, RelOptInfo *outer, List *uclauses, Index relid)
static void split_selfjoin_quals(PlannerInfo *root, List *joinquals, List **selfjoinquals, List **otherjoinquals, int from, int to)
static void remove_self_join_rel(PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark, RelOptInfo *toKeep, RelOptInfo *toRemove, List *restrictlist)
@ JOIN_INNER
Definition nodes.h:303
Relids syn_lefthand
Definition pathnodes.h:3228
Relids syn_righthand
Definition pathnodes.h:3229

References Assert, bms_add_member(), bms_is_member(), bms_next_member(), fb(), generate_join_implied_equalities(), innerrel_is_unique_ext(), JOIN_INNER, lfirst, list_concat(), list_length(), match_unique_clauses(), NIL, remove_self_join_rel(), result, root, split_selfjoin_quals(), SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by remove_self_joins_recurse().

◆ remove_self_joins_recurse()

static Relids remove_self_joins_recurse ( PlannerInfo root,
List joinlist,
Relids  toRemove 
)
static

Definition at line 2333 of file analyzejoins.c.

2334{
2335 ListCell *jl;
2336 Relids relids = NULL;
2338 int i;
2339 int j;
2340 int numRels;
2341
2342 /* Collect indexes of base relations of the join tree */
2343 foreach(jl, joinlist)
2344 {
2345 Node *jlnode = (Node *) lfirst(jl);
2346
2347 if (IsA(jlnode, RangeTblRef))
2348 {
2349 int varno = ((RangeTblRef *) jlnode)->rtindex;
2350 RangeTblEntry *rte = root->simple_rte_array[varno];
2351
2352 /*
2353 * We only consider ordinary relations as candidates to be
2354 * removed, and these relations should not have TABLESAMPLE
2355 * clauses specified. Removing a relation with TABLESAMPLE clause
2356 * could potentially change the syntax of the query. Because of
2357 * UPDATE/DELETE EPQ mechanism, currently Query->resultRelation or
2358 * Query->mergeTargetRelation associated rel cannot be eliminated.
2359 */
2360 if (rte->rtekind == RTE_RELATION &&
2361 rte->relkind == RELKIND_RELATION &&
2362 rte->tablesample == NULL &&
2363 varno != root->parse->resultRelation &&
2364 varno != root->parse->mergeTargetRelation)
2365 {
2366 Assert(!bms_is_member(varno, relids));
2367 relids = bms_add_member(relids, varno);
2368 }
2369 }
2370 else if (IsA(jlnode, List))
2371 {
2372 /* Recursively go inside the sub-joinlist */
2374 toRemove);
2375 }
2376 else
2377 elog(ERROR, "unrecognized joinlist node type: %d",
2378 (int) nodeTag(jlnode));
2379 }
2380
2381 numRels = bms_num_members(relids);
2382
2383 /* Need at least two relations for the join */
2384 if (numRels < 2)
2385 return toRemove;
2386
2387 /*
2388 * In order to find relations with the same oid we first build an array of
2389 * candidates and then sort it by oid.
2390 */
2392 i = -1;
2393 j = 0;
2394 while ((i = bms_next_member(relids, i)) >= 0)
2395 {
2396 candidates[j].relid = i;
2397 candidates[j].reloid = root->simple_rte_array[i]->relid;
2398 j++;
2399 }
2400
2403
2404 /*
2405 * Iteratively form a group of relation indexes with the same oid and
2406 * launch the routine that detects self-joins in this group and removes
2407 * excessive range table entries.
2408 *
2409 * At the end of the iteration, exclude the group from the overall relids
2410 * list. So each next iteration of the cycle will involve less and less
2411 * value of relids.
2412 */
2413 i = 0;
2414 for (j = 1; j < numRels + 1; j++)
2415 {
2416 if (j == numRels || candidates[j].reloid != candidates[i].reloid)
2417 {
2418 if (j - i >= 2)
2419 {
2420 /* Create a group of relation indexes with the same oid */
2421 Relids group = NULL;
2423
2424 while (i < j)
2425 {
2426 group = bms_add_member(group, candidates[i].relid);
2427 i++;
2428 }
2429 relids = bms_del_members(relids, group);
2430
2431 /*
2432 * Try to remove self-joins from a group of identical entries.
2433 * Make the next attempt iteratively - if something is deleted
2434 * from a group, changes in clauses and equivalence classes
2435 * can give us a chance to find more candidates.
2436 */
2437 do
2438 {
2439 Assert(!bms_overlap(group, toRemove));
2442 group = bms_del_members(group, removed);
2443 } while (!bms_is_empty(removed) &&
2444 bms_membership(group) == BMS_MULTIPLE);
2446 bms_free(group);
2447 }
2448 else
2449 {
2450 /* Single relation, just remove it from the set */
2451 relids = bms_del_member(relids, candidates[i].relid);
2452 i = j;
2453 }
2454 }
2455 }
2456
2457 Assert(bms_is_empty(relids));
2458
2459 return toRemove;
2460}
static int self_join_candidates_cmp(const void *a, const void *b)
static Relids remove_self_joins_one_group(PlannerInfo *root, Relids relids)
static Relids remove_self_joins_recurse(PlannerInfo *root, List *joinlist, Relids toRemove)
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1145
void bms_free(Bitmapset *a)
Definition bitmapset.c:239
int bms_num_members(const Bitmapset *a)
Definition bitmapset.c:744
#define palloc_array(type, count)
Definition fe_memutils.h:76
int j
Definition isn.c:78
#define qsort(a, b, c, d)
Definition port.h:495

References Assert, bms_add_member(), bms_add_members(), bms_del_member(), bms_del_members(), bms_free(), bms_is_empty, bms_is_member(), bms_membership(), BMS_MULTIPLE, bms_next_member(), bms_num_members(), bms_overlap(), elog, ERROR, fb(), i, IsA, j, lfirst, nodeTag, palloc_array, qsort, remove_self_joins_one_group(), remove_self_joins_recurse(), root, RTE_RELATION, and self_join_candidates_cmp().

Referenced by remove_self_joins_recurse(), and remove_useless_self_joins().

◆ remove_useless_joins()

List * remove_useless_joins ( PlannerInfo root,
List joinlist 
)

Definition at line 93 of file analyzejoins.c.

94{
95 ListCell *lc;
96
97 /*
98 * We are only interested in relations that are left-joined to, so we can
99 * scan the join_info_list to find them easily.
100 */
101restart:
102 foreach(lc, root->join_info_list)
103 {
105 int innerrelid;
106 int nremoved;
107
108 /* Skip if not removable */
109 if (!join_is_removable(root, sjinfo))
110 continue;
111
112 /*
113 * Currently, join_is_removable can only succeed when the sjinfo's
114 * righthand is a single baserel. Remove that rel from the query and
115 * joinlist.
116 */
118
120
121 /* We verify that exactly one reference gets removed from joinlist */
122 nremoved = 0;
124 if (nremoved != 1)
125 elog(ERROR, "failed to find relation %d in joinlist", innerrelid);
126
127 /*
128 * We can delete this SpecialJoinInfo from the list too, since it's no
129 * longer of interest. (Since we'll restart the foreach loop
130 * immediately, we don't bother with foreach_delete_current.)
131 */
132 root->join_info_list = list_delete_cell(root->join_info_list, lc);
133
134 /*
135 * Restart the scan. This is necessary to ensure we find all
136 * removable joins independently of ordering of the join_info_list
137 * (note that removal of attr_needed bits may make a join appear
138 * removable that did not before).
139 */
140 goto restart;
141 }
142
143 return joinlist;
144}
static void remove_leftjoinrel_from_query(PlannerInfo *root, int relid, SpecialJoinInfo *sjinfo)
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
int bms_singleton_member(const Bitmapset *a)
Definition bitmapset.c:665
List * list_delete_cell(List *list, ListCell *cell)
Definition list.c:841

References bms_singleton_member(), elog, ERROR, fb(), join_is_removable(), lfirst, list_delete_cell(), SpecialJoinInfo::min_righthand, remove_leftjoinrel_from_query(), remove_rel_from_joinlist(), and root.

Referenced by query_planner().

◆ remove_useless_self_joins()

List * remove_useless_self_joins ( PlannerInfo root,
List joinlist 
)

Definition at line 2513 of file analyzejoins.c.

2514{
2516 int relid = -1;
2517
2520 return joinlist;
2521
2522 /*
2523 * Merge pairs of relations participated in self-join. Remove unnecessary
2524 * range table entries.
2525 */
2527
2528 if (unlikely(toRemove != NULL))
2529 {
2530 /* At the end, remove orphaned relation links */
2531 while ((relid = bms_next_member(toRemove, relid)) >= 0)
2532 {
2533 int nremoved = 0;
2534
2536 if (nremoved != 1)
2537 elog(ERROR, "failed to find relation %d in joinlist", relid);
2538 }
2539 }
2540
2541 return joinlist;
2542}
bool enable_self_join_elimination
#define unlikely(x)
Definition c.h:438
#define linitial(l)
Definition pg_list.h:178

References bms_next_member(), elog, enable_self_join_elimination, ERROR, fb(), IsA, linitial, list_length(), NIL, remove_rel_from_joinlist(), remove_self_joins_recurse(), root, and unlikely.

Referenced by query_planner().

◆ replace_relid_callback()

static bool replace_relid_callback ( Node node,
ChangeVarNodes_context context 
)
static

Definition at line 1731 of file analyzejoins.c.

1732{
1733 if (IsA(node, RangeTblRef))
1734 {
1735 return true;
1736 }
1737 else if (IsA(node, RestrictInfo))
1738 {
1739 RestrictInfo *rinfo = (RestrictInfo *) node;
1740 int relid = -1;
1741 bool is_req_equal =
1742 (rinfo->required_relids == rinfo->clause_relids);
1744 (bms_membership(rinfo->clause_relids) == BMS_MULTIPLE);
1745
1746 /*
1747 * Recurse down into clauses if the target relation is present in
1748 * clause_relids or required_relids. We must check required_relids
1749 * because the relation not present in clause_relids might still be
1750 * present somewhere in orclause.
1751 */
1752 if (bms_is_member(context->rt_index, rinfo->clause_relids) ||
1753 bms_is_member(context->rt_index, rinfo->required_relids))
1754 {
1756
1757 ChangeVarNodesWalkExpression((Node *) rinfo->clause, context);
1758 ChangeVarNodesWalkExpression((Node *) rinfo->orclause, context);
1759
1760 new_clause_relids = adjust_relid_set(rinfo->clause_relids,
1761 context->rt_index,
1762 context->new_index);
1763
1764 /*
1765 * Incrementally adjust num_base_rels based on the change of
1766 * clause_relids, which could contain both base relids and
1767 * outer-join relids. This operation is legal until we remove
1768 * only baserels.
1769 */
1770 rinfo->num_base_rels -= bms_num_members(rinfo->clause_relids) -
1772
1773 rinfo->clause_relids = new_clause_relids;
1774 rinfo->left_relids =
1775 adjust_relid_set(rinfo->left_relids, context->rt_index, context->new_index);
1776 rinfo->right_relids =
1777 adjust_relid_set(rinfo->right_relids, context->rt_index, context->new_index);
1778 }
1779
1780 if (is_req_equal)
1781 rinfo->required_relids = rinfo->clause_relids;
1782 else
1783 rinfo->required_relids =
1784 adjust_relid_set(rinfo->required_relids, context->rt_index, context->new_index);
1785
1786 rinfo->outer_relids =
1787 adjust_relid_set(rinfo->outer_relids, context->rt_index, context->new_index);
1788 rinfo->incompatible_relids =
1789 adjust_relid_set(rinfo->incompatible_relids, context->rt_index, context->new_index);
1790
1791 if (rinfo->mergeopfamilies &&
1792 bms_get_singleton_member(rinfo->clause_relids, &relid) &&
1794 relid == context->new_index && IsA(rinfo->clause, OpExpr))
1795 {
1796 Expr *leftOp;
1797 Expr *rightOp;
1798
1799 leftOp = (Expr *) get_leftop(rinfo->clause);
1800 rightOp = (Expr *) get_rightop(rinfo->clause);
1801
1802 /*
1803 * For self-join elimination, changing varnos could transform
1804 * "t1.a = t2.a" into "t1.a = t1.a". That is always true as long
1805 * as "t1.a" is not null. We use equal() to check for such a
1806 * case, and then we replace the qual with a check for not null
1807 * (NullTest).
1808 */
1809 if (leftOp != NULL && equal(leftOp, rightOp))
1810 {
1812
1813 ntest->arg = leftOp;
1814 ntest->nulltesttype = IS_NOT_NULL;
1815 ntest->argisrow = false;
1816 ntest->location = -1;
1817 rinfo->clause = (Expr *) ntest;
1818 rinfo->mergeopfamilies = NIL;
1819 rinfo->left_em = NULL;
1820 rinfo->right_em = NULL;
1821 }
1822 Assert(rinfo->orclause == NULL);
1823 }
1824 return true;
1825 }
1826
1827 return false;
1828}
@ IS_NOT_NULL
Definition primnodes.h:1980
bool ChangeVarNodesWalkExpression(Node *node, ChangeVarNodes_context *context)

References adjust_relid_set(), Assert, bms_get_singleton_member(), bms_is_member(), bms_membership(), BMS_MULTIPLE, bms_num_members(), ChangeVarNodesWalkExpression(), RestrictInfo::clause, equal(), fb(), get_leftop(), get_rightop(), RestrictInfo::incompatible_relids, IS_NOT_NULL, IsA, makeNode, ChangeVarNodes_context::new_index, NIL, RestrictInfo::outer_relids, RestrictInfo::required_relids, and ChangeVarNodes_context::rt_index.

Referenced by match_unique_clauses(), remove_rel_from_query(), remove_self_join_rel(), split_selfjoin_quals(), and update_eclasses().

◆ restrict_infos_logically_equal()

static bool restrict_infos_logically_equal ( RestrictInfo a,
RestrictInfo b 
)
static

Definition at line 1661 of file analyzejoins.c.

1662{
1663 int saved_rinfo_serial = a->rinfo_serial;
1664 bool result;
1665
1666 a->rinfo_serial = b->rinfo_serial;
1667 result = equal(a, b);
1668 a->rinfo_serial = saved_rinfo_serial;
1669
1670 return result;
1671}
int b
Definition isn.c:74
int a
Definition isn.c:73

References a, b, equal(), fb(), and result.

Referenced by add_non_redundant_clauses().

◆ self_join_candidates_cmp()

static int self_join_candidates_cmp ( const void a,
const void b 
)
static

Definition at line 2466 of file analyzejoins.c.

2467{
2468 const SelfJoinCandidate *ca = (const SelfJoinCandidate *) a;
2469 const SelfJoinCandidate *cb = (const SelfJoinCandidate *) b;
2470
2471 if (ca->reloid != cb->reloid)
2472 return (ca->reloid < cb->reloid ? -1 : 1);
2473 else
2474 return 0;
2475}

References a, b, fb(), and SelfJoinCandidate::reloid.

Referenced by remove_self_joins_recurse().

◆ split_selfjoin_quals()

static void split_selfjoin_quals ( PlannerInfo root,
List joinquals,
List **  selfjoinquals,
List **  otherjoinquals,
int  from,
int  to 
)
static

Definition at line 2035 of file analyzejoins.c.

2037{
2038 List *sjoinquals = NIL;
2039 List *ojoinquals = NIL;
2040
2042 {
2043 OpExpr *expr;
2044 Node *leftexpr;
2045 Node *rightexpr;
2046
2047 /* In general, clause looks like F(arg1) = G(arg2) */
2048 if (!rinfo->mergeopfamilies ||
2049 bms_num_members(rinfo->clause_relids) != 2 ||
2050 bms_membership(rinfo->left_relids) != BMS_SINGLETON ||
2051 bms_membership(rinfo->right_relids) != BMS_SINGLETON)
2052 {
2053 ojoinquals = lappend(ojoinquals, rinfo);
2054 continue;
2055 }
2056
2057 expr = (OpExpr *) rinfo->clause;
2058
2059 if (!IsA(expr, OpExpr) || list_length(expr->args) != 2)
2060 {
2061 ojoinquals = lappend(ojoinquals, rinfo);
2062 continue;
2063 }
2064
2065 leftexpr = get_leftop(rinfo->clause);
2066 rightexpr = copyObject(get_rightop(rinfo->clause));
2067
2069 leftexpr = (Node *) ((RelabelType *) leftexpr)->arg;
2071 rightexpr = (Node *) ((RelabelType *) rightexpr)->arg;
2072
2073 /*
2074 * Quite an expensive operation, narrowing the use case. For example,
2075 * when we have cast of the same var to different (but compatible)
2076 * types.
2077 */
2079 bms_singleton_member(rinfo->right_relids),
2080 bms_singleton_member(rinfo->left_relids), 0,
2082
2083 if (equal(leftexpr, rightexpr))
2084 sjoinquals = lappend(sjoinquals, rinfo);
2085 else
2086 ojoinquals = lappend(ojoinquals, rinfo);
2087 }
2088
2091}
@ BMS_SINGLETON
Definition bitmapset.h:72
Datum arg
Definition elog.c:1323
List * args
Definition primnodes.h:869

References arg, OpExpr::args, bms_membership(), bms_num_members(), BMS_SINGLETON, bms_singleton_member(), ChangeVarNodesExtended(), copyObject, equal(), fb(), foreach_node, get_leftop(), get_rightop(), IsA, lappend(), list_length(), NIL, and replace_relid_callback().

Referenced by remove_self_joins_one_group().

◆ update_eclasses()

static void update_eclasses ( EquivalenceClass ec,
int  from,
int  to 
)
static

Definition at line 1561 of file analyzejoins.c.

1562{
1563 List *new_members = NIL;
1564 List *new_sources = NIL;
1565
1566 /*
1567 * We don't expect any EC child members to exist at this point. Ensure
1568 * that's the case, otherwise, we might be getting asked to do something
1569 * this function hasn't been coded for.
1570 */
1571 Assert(ec->ec_childmembers == NULL);
1572
1574 {
1575 bool is_redundant = false;
1576
1577 if (!bms_is_member(from, em->em_relids))
1578 {
1580 continue;
1581 }
1582
1583 em->em_relids = adjust_relid_set(em->em_relids, from, to);
1584 em->em_jdomain->jd_relids = adjust_relid_set(em->em_jdomain->jd_relids, from, to);
1585
1586 /* We only process inner joins */
1587 ChangeVarNodesExtended((Node *) em->em_expr, from, to, 0,
1589
1591 {
1592 if (!equal(em->em_relids, other->em_relids))
1593 continue;
1594
1595 if (equal(em->em_expr, other->em_expr))
1596 {
1597 is_redundant = true;
1598 break;
1599 }
1600 }
1601
1602 if (!is_redundant)
1604 }
1605
1606 list_free(ec->ec_members);
1607 ec->ec_members = new_members;
1608
1610
1611 /* Update EC source expressions */
1613 {
1614 bool is_redundant = false;
1615
1616 if (!bms_is_member(from, rinfo->required_relids))
1617 {
1619 continue;
1620 }
1621
1622 ChangeVarNodesExtended((Node *) rinfo, from, to, 0,
1624
1625 /*
1626 * After switching the clause to the remaining relation, check it for
1627 * redundancy with existing ones. We don't have to check for
1628 * redundancy with derived clauses, because we've just deleted them.
1629 */
1631 {
1632 if (!equal(rinfo->clause_relids, other->clause_relids))
1633 continue;
1634
1635 if (equal(rinfo->clause, other->clause))
1636 {
1637 is_redundant = true;
1638 break;
1639 }
1640 }
1641
1642 if (!is_redundant)
1644 }
1645
1646 list_free(ec->ec_sources);
1647 ec->ec_sources = new_sources;
1648 ec->ec_relids = adjust_relid_set(ec->ec_relids, from, to);
1649}

References adjust_relid_set(), Assert, bms_is_member(), ChangeVarNodesExtended(), EquivalenceClass::ec_childmembers, ec_clear_derived_clauses(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceClass::ec_sources, equal(), fb(), foreach_node, lappend(), list_free(), NIL, and replace_relid_callback().

Referenced by remove_self_join_rel().

Variable Documentation

◆ enable_self_join_elimination

bool enable_self_join_elimination

Definition at line 54 of file analyzejoins.c.

Referenced by remove_useless_self_joins().