PostgreSQL Source Code git master
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_restrictinfo (RestrictInfo *rinfo, int relid, int ojrelid)
 
static void remove_rel_from_eclass (EquivalenceClass *ec, SpecialJoinInfo *sjinfo, int relid, int subst)
 
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)
 
static void remove_rel_from_query (PlannerInfo *root, RelOptInfo *rel, int subst, SpecialJoinInfo *sjinfo, Relids joinrelids)
 
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 1659 of file analyzejoins.c.

1663{
1664 foreach_node(RestrictInfo, rinfo, rinfo_candidates)
1665 {
1666 bool is_redundant = false;
1667
1668 Assert(!bms_is_member(removed_relid, rinfo->required_relids));
1669
1670 foreach_node(RestrictInfo, src, (*keep_rinfo_list))
1671 {
1672 if (!bms_equal(src->clause_relids, rinfo->clause_relids))
1673 /* Can't compare trivially different clauses */
1674 continue;
1675
1676 if (src == rinfo ||
1677 (rinfo->parent_ec != NULL &&
1678 src->parent_ec == rinfo->parent_ec) ||
1680 {
1681 is_redundant = true;
1682 break;
1683 }
1684 }
1685 if (!is_redundant)
1687 }
1688}
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
Assert(PointerIsAligned(start, uint64))
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:3558
#define foreach_node(type, var, lst)
Definition: pg_list.h:496
tree ctl root
Definition: radixtree.h:1857

References Assert(), bms_equal(), bms_is_member(), distribute_restrictinfo_to_rels(), 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 1270 of file analyzejoins.c.

1271{
1272 ListCell *lc1,
1273 *lc2;
1274
1275 forboth(lc1, colnos, lc2, opids)
1276 {
1277 if (colno == lfirst_int(lc1))
1278 return lfirst_oid(lc2);
1279 }
1280 return InvalidOid;
1281}
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define lfirst_int(lc)
Definition: pg_list.h:173
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define InvalidOid
Definition: postgres_ext.h:37

References 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 1310 of file analyzejoins.c.

1317{
1318 return innerrel_is_unique_ext(root, joinrelids, outerrelids, innerrel,
1319 jointype, restrictlist, force_cache, NULL);
1320}
bool innerrel_is_unique_ext(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache, List **extra_clauses)

References 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 1332 of file analyzejoins.c.

1340{
1341 MemoryContext old_context;
1342 ListCell *lc;
1343 UniqueRelInfo *uniqueRelInfo;
1344 List *outer_exprs = NIL;
1345 bool self_join = (extra_clauses != NULL);
1346
1347 /* Certainly can't prove uniqueness when there are no joinclauses */
1348 if (restrictlist == NIL)
1349 return false;
1350
1351 /*
1352 * Make a quick check to eliminate cases in which we will surely be unable
1353 * to prove uniqueness of the innerrel.
1354 */
1355 if (!rel_supports_distinctness(root, innerrel))
1356 return false;
1357
1358 /*
1359 * Query the cache to see if we've managed to prove that innerrel is
1360 * unique for any subset of this outerrel. For non-self-join search, we
1361 * don't need an exact match, as extra outerrels can't make the innerrel
1362 * any less unique (or more formally, the restrictlist for a join to a
1363 * superset outerrel must be a superset of the conditions we successfully
1364 * used before). For self-join search, we require an exact match of
1365 * outerrels because we need extra clauses to be valid for our case. Also,
1366 * for self-join checking we've filtered the clauses list. Thus, we can
1367 * match only the result cached for a self-join search for another
1368 * self-join check.
1369 */
1370 foreach(lc, innerrel->unique_for_rels)
1371 {
1372 uniqueRelInfo = (UniqueRelInfo *) lfirst(lc);
1373
1374 if ((!self_join && bms_is_subset(uniqueRelInfo->outerrelids, outerrelids)) ||
1375 (self_join && bms_equal(uniqueRelInfo->outerrelids, outerrelids) &&
1376 uniqueRelInfo->self_join))
1377 {
1378 if (extra_clauses)
1379 *extra_clauses = uniqueRelInfo->extra_clauses;
1380 return true; /* Success! */
1381 }
1382 }
1383
1384 /*
1385 * Conversely, we may have already determined that this outerrel, or some
1386 * superset thereof, cannot prove this innerrel to be unique.
1387 */
1388 foreach(lc, innerrel->non_unique_for_rels)
1389 {
1390 Relids unique_for_rels = (Relids) lfirst(lc);
1391
1392 if (bms_is_subset(outerrelids, unique_for_rels))
1393 return false;
1394 }
1395
1396 /* No cached information, so try to make the proof. */
1397 if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1398 jointype, restrictlist,
1399 self_join ? &outer_exprs : NULL))
1400 {
1401 /*
1402 * Cache the positive result for future probes, being sure to keep it
1403 * in the planner_cxt even if we are working in GEQO.
1404 *
1405 * Note: one might consider trying to isolate the minimal subset of
1406 * the outerrels that proved the innerrel unique. But it's not worth
1407 * the trouble, because the planner builds up joinrels incrementally
1408 * and so we'll see the minimally sufficient outerrels before any
1409 * supersets of them anyway.
1410 */
1411 old_context = MemoryContextSwitchTo(root->planner_cxt);
1412 uniqueRelInfo = makeNode(UniqueRelInfo);
1413 uniqueRelInfo->outerrelids = bms_copy(outerrelids);
1414 uniqueRelInfo->self_join = self_join;
1415 uniqueRelInfo->extra_clauses = outer_exprs;
1416 innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1417 uniqueRelInfo);
1418 MemoryContextSwitchTo(old_context);
1419
1420 if (extra_clauses)
1421 *extra_clauses = outer_exprs;
1422 return true; /* Success! */
1423 }
1424 else
1425 {
1426 /*
1427 * None of the join conditions for outerrel proved innerrel unique, so
1428 * we can safely reject this outerrel or any subset of it in future
1429 * checks.
1430 *
1431 * However, in normal planning mode, caching this knowledge is totally
1432 * pointless; it won't be queried again, because we build up joinrels
1433 * from smaller to larger. It's only useful when using GEQO or
1434 * another planner extension that attempts planning multiple times.
1435 *
1436 * Also, allow callers to override that heuristic and force caching;
1437 * that's useful for reduce_unique_semijoins, which calls here before
1438 * the normal join search starts.
1439 */
1440 if (force_cache || root->assumeReplanning)
1441 {
1442 old_context = MemoryContextSwitchTo(root->planner_cxt);
1443 innerrel->non_unique_for_rels =
1444 lappend(innerrel->non_unique_for_rels,
1445 bms_copy(outerrelids));
1446 MemoryContextSwitchTo(old_context);
1447 }
1448
1449 return false;
1450 }
1451}
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)
Definition: analyzejoins.c:921
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:30
#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:1028
List * non_unique_for_rels
Definition: pathnodes.h:1030
Relids outerrelids
Definition: pathnodes.h:3716
List * extra_clauses
Definition: pathnodes.h:3730

References bms_copy(), bms_equal(), bms_is_subset(), UniqueRelInfo::extra_clauses, is_innerrel_unique_for(), lappend(), lfirst, makeNode, MemoryContextSwitchTo(), NIL, RelOptInfo::non_unique_for_rels, UniqueRelInfo::outerrelids, rel_supports_distinctness(), root, UniqueRelInfo::self_join, 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 1459 of file analyzejoins.c.

1466{
1467 List *clause_list = NIL;
1468 ListCell *lc;
1469
1470 /*
1471 * Search for mergejoinable clauses that constrain the inner rel against
1472 * the outer rel. If an operator is mergejoinable then it behaves like
1473 * equality for some btree opclass, so it's what we want. The
1474 * mergejoinability test also eliminates clauses containing volatile
1475 * functions, which we couldn't depend on.
1476 */
1477 foreach(lc, restrictlist)
1478 {
1479 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1480
1481 /*
1482 * As noted above, if it's a pushed-down clause and we're at an outer
1483 * join, we can't use it.
1484 */
1485 if (IS_OUTER_JOIN(jointype) &&
1486 RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
1487 continue;
1488
1489 /* Ignore if it's not a mergejoinable clause */
1490 if (!restrictinfo->can_join ||
1491 restrictinfo->mergeopfamilies == NIL)
1492 continue; /* not mergejoinable */
1493
1494 /*
1495 * Check if the clause has the form "outer op inner" or "inner op
1496 * outer", and if so mark which side is inner.
1497 */
1498 if (!clause_sides_match_join(restrictinfo, outerrelids,
1499 innerrel->relids))
1500 continue; /* no good for these input relations */
1501
1502 /* OK, add to the list */
1503 clause_list = lappend(clause_list, restrictinfo);
1504 }
1505
1506 /* Let rel_is_distinct_for() do the hard work */
1507 return rel_is_distinct_for(root, innerrel, clause_list, extra_clauses);
1508}
static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list, List **extra_clauses)
Definition: analyzejoins.c:981
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:348
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2949
static bool clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids, Relids innerrelids)
Definition: restrictinfo.h:73
Relids relids
Definition: pathnodes.h:927

References clause_sides_match_join(), 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 156 of file analyzejoins.c.

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

2077{
2078 foreach_node(RestrictInfo, rinfo, uclauses)
2079 {
2080 Expr *clause;
2081 Node *iclause;
2082 Node *c1;
2083 bool matched = false;
2084
2085 Assert(outer->relid > 0 && relid > 0);
2086
2087 /* Only filters like f(R.x1,...,R.xN) == expr we should consider. */
2088 Assert(bms_is_empty(rinfo->left_relids) ^
2089 bms_is_empty(rinfo->right_relids));
2090
2091 clause = (Expr *) copyObject(rinfo->clause);
2092 ChangeVarNodesExtended((Node *) clause, relid, outer->relid, 0,
2094
2095 iclause = bms_is_empty(rinfo->left_relids) ? get_rightop(clause) :
2096 get_leftop(clause);
2097 c1 = bms_is_empty(rinfo->left_relids) ? get_leftop(clause) :
2098 get_rightop(clause);
2099
2100 /*
2101 * Compare these left and right sides with the corresponding sides of
2102 * the outer's filters. If no one is detected - return immediately.
2103 */
2105 {
2106 Node *oclause;
2107 Node *c2;
2108
2109 if (orinfo->mergeopfamilies == NIL)
2110 /* Don't consider clauses that aren't similar to 'F(X)=G(Y)' */
2111 continue;
2112
2113 Assert(is_opclause(orinfo->clause));
2114
2115 oclause = bms_is_empty(orinfo->left_relids) ?
2116 get_rightop(orinfo->clause) : get_leftop(orinfo->clause);
2117 c2 = (bms_is_empty(orinfo->left_relids) ?
2118 get_leftop(orinfo->clause) : get_rightop(orinfo->clause));
2119
2120 if (equal(iclause, oclause) && equal(c1, c2))
2121 {
2122 matched = true;
2123 break;
2124 }
2125 }
2126
2127 if (!matched)
2128 return false;
2129 }
2130
2131 return true;
2132}
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)
Definition: rewriteManip.c:677
List * baserestrictinfo
Definition: pathnodes.h:1046
Index relid
Definition: pathnodes.h:973

References Assert(), RelOptInfo::baserestrictinfo, bms_is_empty, ChangeVarNodesExtended(), copyObject, equal(), 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 1117 of file analyzejoins.c.

1118{
1119 ListCell *l;
1120 Oid opid;
1121
1122 Assert(list_length(colnos) == list_length(opids));
1123
1124 /*
1125 * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
1126 * columns in the DISTINCT clause appear in colnos and operator semantics
1127 * match. This is true even if there are SRFs in the DISTINCT columns or
1128 * elsewhere in the tlist.
1129 */
1130 if (query->distinctClause)
1131 {
1132 foreach(l, query->distinctClause)
1133 {
1136 query->targetList);
1137
1138 opid = distinct_col_search(tle->resno, colnos, opids);
1139 if (!OidIsValid(opid) ||
1140 !equality_ops_are_compatible(opid, sgc->eqop))
1141 break; /* exit early if no match */
1142 }
1143 if (l == NULL) /* had matches for all? */
1144 return true;
1145 }
1146
1147 /*
1148 * Otherwise, a set-returning function in the query's targetlist can
1149 * result in returning duplicate rows, despite any grouping that might
1150 * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
1151 * columns, it would be safe because they'd be expanded before grouping.
1152 * But it doesn't currently seem worth the effort to check for that.)
1153 */
1154 if (query->hasTargetSRFs)
1155 return false;
1156
1157 /*
1158 * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
1159 * the grouped columns appear in colnos and operator semantics match.
1160 */
1161 if (query->groupClause && !query->groupingSets)
1162 {
1163 foreach(l, query->groupClause)
1164 {
1167 query->targetList);
1168
1169 opid = distinct_col_search(tle->resno, colnos, opids);
1170 if (!OidIsValid(opid) ||
1171 !equality_ops_are_compatible(opid, sgc->eqop))
1172 break; /* exit early if no match */
1173 }
1174 if (l == NULL) /* had matches for all? */
1175 return true;
1176 }
1177 else if (query->groupingSets)
1178 {
1179 List *gsets;
1180
1181 /*
1182 * If we have grouping sets with expressions, we probably don't have
1183 * uniqueness and analysis would be hard. Punt.
1184 */
1185 if (query->groupClause)
1186 return false;
1187
1188 /*
1189 * If we have no groupClause (therefore no grouping expressions), we
1190 * might have one or many empty grouping sets. If there's just one,
1191 * or if the DISTINCT clause is used on the GROUP BY, then we're
1192 * returning only one row and are certainly unique. But otherwise, we
1193 * know we're certainly not unique.
1194 */
1195 if (query->groupDistinct)
1196 return true;
1197
1198 gsets = expand_grouping_sets(query->groupingSets, false, -1);
1199
1200 return (list_length(gsets) == 1);
1201 }
1202 else
1203 {
1204 /*
1205 * If we have no GROUP BY, but do have aggregates or HAVING, then the
1206 * result is at most one row so it's surely unique, for any operators.
1207 */
1208 if (query->hasAggs || query->havingQual)
1209 return true;
1210 }
1211
1212 /*
1213 * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
1214 * except with ALL.
1215 */
1216 if (query->setOperations)
1217 {
1219
1220 Assert(topop->op != SETOP_NONE);
1221
1222 if (!topop->all)
1223 {
1224 ListCell *lg;
1225
1226 /* We're good if all the nonjunk output columns are in colnos */
1227 lg = list_head(topop->groupClauses);
1228 foreach(l, query->targetList)
1229 {
1230 TargetEntry *tle = (TargetEntry *) lfirst(l);
1231 SortGroupClause *sgc;
1232
1233 if (tle->resjunk)
1234 continue; /* ignore resjunk columns */
1235
1236 /* non-resjunk columns should have grouping clauses */
1237 Assert(lg != NULL);
1238 sgc = (SortGroupClause *) lfirst(lg);
1239 lg = lnext(topop->groupClauses, lg);
1240
1241 opid = distinct_col_search(tle->resno, colnos, opids);
1242 if (!OidIsValid(opid) ||
1243 !equality_ops_are_compatible(opid, sgc->eqop))
1244 break; /* exit early if no match */
1245 }
1246 if (l == NULL) /* had matches for all? */
1247 return true;
1248 }
1249 }
1250
1251 /*
1252 * XXX Are there any other cases in which we can easily see the result
1253 * must be distinct?
1254 *
1255 * If you do add more smarts to this function, be sure to update
1256 * query_supports_distinctness() to match.
1257 */
1258
1259 return false;
1260}
static Oid distinct_col_search(int colno, List *colnos, List *opids)
#define OidIsValid(objectId)
Definition: c.h:788
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition: lsyscache.c:778
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
List * expand_grouping_sets(List *groupingSets, bool groupDistinct, int limit)
Definition: parse_agg.c:1947
@ SETOP_NONE
Definition: parsenodes.h:2176
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:343
unsigned int Oid
Definition: postgres_ext.h:32
bool groupDistinct
Definition: parsenodes.h:217
Node * setOperations
Definition: parsenodes.h:236
List * groupClause
Definition: parsenodes.h:216
Node * havingQual
Definition: parsenodes.h:222
List * targetList
Definition: parsenodes.h:198
List * groupingSets
Definition: parsenodes.h:220
List * distinctClause
Definition: parsenodes.h:226
SetOperation op
Definition: parsenodes.h:2255
AttrNumber resno
Definition: primnodes.h:2241
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367

References SetOperationStmt::all, Assert(), castNode, distinct_col_search(), Query::distinctClause, SortGroupClause::eqop, equality_ops_are_compatible(), expand_grouping_sets(), get_sortgroupclause_tle(), Query::groupClause, Query::groupDistinct, Query::groupingSets, Query::havingQual, lfirst, list_head(), list_length(), lnext(), OidIsValid, SetOperationStmt::op, TargetEntry::resno, 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 1079 of file analyzejoins.c.

1080{
1081 /* SRFs break distinctness except with DISTINCT, see below */
1082 if (query->hasTargetSRFs && query->distinctClause == NIL)
1083 return false;
1084
1085 /* check for features we can prove distinctness with */
1086 if (query->distinctClause != NIL ||
1087 query->groupClause != NIL ||
1088 query->groupingSets != NIL ||
1089 query->hasAggs ||
1090 query->havingQual ||
1091 query->setOperations)
1092 return true;
1093
1094 return false;
1095}

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 845 of file analyzejoins.c.

846{
847 ListCell *lc;
848
849 /*
850 * Scan the join_info_list to find semijoins.
851 */
852 foreach(lc, root->join_info_list)
853 {
854 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
855 int innerrelid;
856 RelOptInfo *innerrel;
857 Relids joinrelids;
858 List *restrictlist;
859
860 /*
861 * Must be a semijoin to a single baserel, else we aren't going to be
862 * able to do anything with it.
863 */
864 if (sjinfo->jointype != JOIN_SEMI)
865 continue;
866
867 if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
868 continue;
869
870 innerrel = find_base_rel(root, innerrelid);
871
872 /*
873 * Before we trouble to run generate_join_implied_equalities, make a
874 * quick check to eliminate cases in which we will surely be unable to
875 * prove uniqueness of the innerrel.
876 */
877 if (!rel_supports_distinctness(root, innerrel))
878 continue;
879
880 /* Compute the relid set for the join we are considering */
881 joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
882 Assert(sjinfo->ojrelid == 0); /* SEMI joins don't have RT indexes */
883
884 /*
885 * Since we're only considering a single-rel RHS, any join clauses it
886 * has must be clauses linking it to the semijoin's min_lefthand. We
887 * can also consider EC-derived join clauses.
888 */
889 restrictlist =
891 joinrelids,
892 sjinfo->min_lefthand,
893 innerrel,
894 NULL),
895 innerrel->joininfo);
896
897 /* Test whether the innerrel is unique for those clauses. */
899 joinrelids, sjinfo->min_lefthand, innerrel,
900 JOIN_SEMI, restrictlist, true))
901 continue;
902
903 /* OK, remove the SpecialJoinInfo from the list. */
904 root->join_info_list = foreach_delete_current(root->join_info_list, lc);
905 }
906}
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)
Definition: equivclass.c:1550
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:391

References Assert(), bms_get_singleton_member(), bms_union(), 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 981 of file analyzejoins.c.

983{
984 /*
985 * We could skip a couple of tests here if we assume all callers checked
986 * rel_supports_distinctness first, but it doesn't seem worth taking any
987 * risk for.
988 */
989 if (rel->reloptkind != RELOPT_BASEREL)
990 return false;
991 if (rel->rtekind == RTE_RELATION)
992 {
993 /*
994 * Examine the indexes to see if we have a matching unique index.
995 * relation_has_unique_index_for automatically adds any usable
996 * restriction clauses for the rel, so we needn't do that here.
997 */
998 if (relation_has_unique_index_for(root, rel, clause_list, extra_clauses))
999 return true;
1000 }
1001 else if (rel->rtekind == RTE_SUBQUERY)
1002 {
1003 Index relid = rel->relid;
1004 Query *subquery = root->simple_rte_array[relid]->subquery;
1005 List *colnos = NIL;
1006 List *opids = NIL;
1007 ListCell *l;
1008
1009 /*
1010 * Build the argument lists for query_is_distinct_for: a list of
1011 * output column numbers that the query needs to be distinct over, and
1012 * a list of equality operators that the output columns need to be
1013 * distinct according to.
1014 *
1015 * (XXX we are not considering restriction clauses attached to the
1016 * subquery; is that worth doing?)
1017 */
1018 foreach(l, clause_list)
1019 {
1021 Oid op;
1022 Var *var;
1023
1024 /*
1025 * Get the equality operator we need uniqueness according to.
1026 * (This might be a cross-type operator and thus not exactly the
1027 * same operator the subquery would consider; that's all right
1028 * since query_is_distinct_for can resolve such cases.) The
1029 * caller's mergejoinability test should have selected only
1030 * OpExprs.
1031 */
1032 op = castNode(OpExpr, rinfo->clause)->opno;
1033
1034 /* caller identified the inner side for us */
1035 if (rinfo->outer_is_left)
1036 var = (Var *) get_rightop(rinfo->clause);
1037 else
1038 var = (Var *) get_leftop(rinfo->clause);
1039
1040 /*
1041 * We may ignore any RelabelType node above the operand. (There
1042 * won't be more than one, since eval_const_expressions() has been
1043 * applied already.)
1044 */
1045 if (var && IsA(var, RelabelType))
1046 var = (Var *) ((RelabelType *) var)->arg;
1047
1048 /*
1049 * If inner side isn't a Var referencing a subquery output column,
1050 * this clause doesn't help us.
1051 */
1052 if (!var || !IsA(var, Var) ||
1053 var->varno != relid || var->varlevelsup != 0)
1054 continue;
1055
1056 colnos = lappend_int(colnos, var->varattno);
1057 opids = lappend_oid(opids, op);
1058 }
1059
1060 if (query_is_distinct_for(subquery, colnos, opids))
1061 return true;
1062 }
1063 return false;
1064}
bool query_is_distinct_for(Query *query, List *colnos, List *opids)
unsigned int Index
Definition: c.h:633
bool relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List **extra_clauses)
Definition: indxpath.c:4145
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
Definition: parsenodes.h:1044
@ RTE_RELATION
Definition: parsenodes.h:1043
@ RELOPT_BASEREL
Definition: pathnodes.h:883
#define lfirst_node(type, lc)
Definition: pg_list.h:176
RelOptKind reloptkind
Definition: pathnodes.h:921
RTEKind rtekind
Definition: pathnodes.h:977
Expr * clause
Definition: pathnodes.h:2792
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269
Index varlevelsup
Definition: primnodes.h:294

References castNode, RestrictInfo::clause, 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 921 of file analyzejoins.c.

922{
923 /* We only know about baserels ... */
924 if (rel->reloptkind != RELOPT_BASEREL)
925 return false;
926 if (rel->rtekind == RTE_RELATION)
927 {
928 /*
929 * For a plain relation, we only know how to prove uniqueness by
930 * reference to unique indexes. Make sure there's at least one
931 * suitable unique index. It must be immediately enforced, and not a
932 * partial index. (Keep these conditions in sync with
933 * relation_has_unique_index_for!)
934 */
935 ListCell *lc;
936
937 foreach(lc, rel->indexlist)
938 {
940
941 if (ind->unique && ind->immediate && ind->indpred == NIL)
942 return true;
943 }
944 }
945 else if (rel->rtekind == RTE_SUBQUERY)
946 {
947 Query *subquery = root->simple_rte_array[rel->relid]->subquery;
948
949 /* Check if the subquery has any qualities that support distinctness */
950 if (query_supports_distinctness(subquery))
951 return true;
952 }
953 /* We have no proof rules for any other rtekinds. */
954 return false;
955}
bool query_supports_distinctness(Query *query)
List * indexlist
Definition: pathnodes.h:995

References 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 545 of file analyzejoins.c.

547{
548 RelOptInfo *rel = find_base_rel(root, relid);
549 int ojrelid = sjinfo->ojrelid;
550 Relids joinrelids;
551 Relids join_plus_commute;
552 List *joininfos;
553 ListCell *l;
554
555 /* Compute the relid set for the join we are considering */
556 joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
557 Assert(ojrelid != 0);
558 joinrelids = bms_add_member(joinrelids, ojrelid);
559
560 remove_rel_from_query(root, rel, -1, sjinfo, joinrelids);
561
562 /*
563 * Remove any joinquals referencing the rel from the joininfo lists.
564 *
565 * In some cases, a joinqual has to be put back after deleting its
566 * reference to the target rel. This can occur for pseudoconstant and
567 * outerjoin-delayed quals, which can get marked as requiring the rel in
568 * order to force them to be evaluated at or above the join. We can't
569 * just discard them, though. Only quals that logically belonged to the
570 * outer join being discarded should be removed from the query.
571 *
572 * We might encounter a qual that is a clone of a deletable qual with some
573 * outer-join relids added (see deconstruct_distribute_oj_quals). To
574 * ensure we get rid of such clones as well, add the relids of all OJs
575 * commutable with this one to the set we test against for
576 * pushed-down-ness.
577 */
578 join_plus_commute = bms_union(joinrelids,
579 sjinfo->commute_above_r);
580 join_plus_commute = bms_add_members(join_plus_commute,
581 sjinfo->commute_below_l);
582
583 /*
584 * We must make a copy of the rel's old joininfo list before starting the
585 * loop, because otherwise remove_join_clause_from_rels would destroy the
586 * list while we're scanning it.
587 */
588 joininfos = list_copy(rel->joininfo);
589 foreach(l, joininfos)
590 {
591 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
592
594
595 if (RINFO_IS_PUSHED_DOWN(rinfo, join_plus_commute))
596 {
597 /*
598 * There might be references to relid or ojrelid in the
599 * RestrictInfo's relid sets, as a consequence of PHVs having had
600 * ph_eval_at sets that include those. We already checked above
601 * that any such PHV is safe (and updated its ph_eval_at), so we
602 * can just drop those references.
603 */
604 remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
605
606 /*
607 * Cross-check that the clause itself does not reference the
608 * target rel or join.
609 */
610#ifdef USE_ASSERT_CHECKING
611 {
612 Relids clause_varnos = pull_varnos(root,
613 (Node *) rinfo->clause);
614
615 Assert(!bms_is_member(relid, clause_varnos));
616 Assert(!bms_is_member(ojrelid, clause_varnos));
617 }
618#endif
619 /* Now throw it back into the joininfo lists */
621 }
622 }
623
624 /*
625 * There may be references to the rel in root->fkey_list, but if so,
626 * match_foreign_keys_to_quals() will get rid of them.
627 */
628
629 /*
630 * Now remove the rel from the baserel array to prevent it from being
631 * referenced again. (We can't do this earlier because
632 * remove_join_clause_from_rels will touch it.)
633 */
634 root->simple_rel_array[relid] = NULL;
635 root->simple_rte_array[relid] = NULL;
636
637 /* And nuke the RelOptInfo, just in case there's another access path */
638 pfree(rel);
639
640 /*
641 * Now repeat construction of attr_needed bits coming from all other
642 * sources.
643 */
648}
static void remove_rel_from_query(PlannerInfo *root, RelOptInfo *rel, int subst, SpecialJoinInfo *sjinfo, Relids joinrelids)
Definition: analyzejoins.c:326
static void remove_rel_from_restrictinfo(RestrictInfo *rinfo, int relid, int ojrelid)
Definition: analyzejoins.c:659
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:916
void rebuild_eclass_attr_needed(PlannerInfo *root)
Definition: equivclass.c:2574
void rebuild_lateral_attr_needed(PlannerInfo *root)
Definition: initsplan.c:1174
void rebuild_joinclause_attr_needed(PlannerInfo *root)
Definition: initsplan.c:3890
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:1594
void rebuild_placeholder_attr_needed(PlannerInfo *root)
Definition: placeholder.c:327
Relids required_relids
Definition: pathnodes.h:2823
Relids commute_above_r
Definition: pathnodes.h:3124
Relids commute_below_l
Definition: pathnodes.h:3125

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(), 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,
SpecialJoinInfo sjinfo,
int  relid,
int  subst 
)
static

Definition at line 720 of file analyzejoins.c.

722{
723 ListCell *lc;
724
725 /* Fix up the EC's overall relids */
726 ec->ec_relids = adjust_relid_set(ec->ec_relids, relid, subst);
727 if (sjinfo != NULL)
729 sjinfo->ojrelid, subst);
730
731 /*
732 * We don't expect any EC child members to exist at this point. Ensure
733 * that's the case, otherwise, we might be getting asked to do something
734 * this function hasn't been coded for.
735 */
736 Assert(ec->ec_childmembers == NULL);
737
738 /*
739 * Fix up the member expressions. Any non-const member that ends with
740 * empty em_relids must be a Var or PHV of the removed relation. We don't
741 * need it anymore, so we can drop it.
742 */
743 foreach(lc, ec->ec_members)
744 {
746
747 if (bms_is_member(relid, cur_em->em_relids) ||
748 (sjinfo != NULL && bms_is_member(sjinfo->ojrelid,
749 cur_em->em_relids)))
750 {
751 Assert(!cur_em->em_is_const);
752 cur_em->em_relids = adjust_relid_set(cur_em->em_relids, relid, subst);
753 if (sjinfo != NULL)
754 cur_em->em_relids = adjust_relid_set(cur_em->em_relids,
755 sjinfo->ojrelid, subst);
756 if (bms_is_empty(cur_em->em_relids))
758 }
759 }
760
761 /* Fix up the source clauses, in case we can re-use them later */
762 foreach(lc, ec->ec_sources)
763 {
764 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
765
766 if (sjinfo == NULL)
767 ChangeVarNodesExtended((Node *) rinfo, relid, subst, 0,
769 else
770 remove_rel_from_restrictinfo(rinfo, relid, sjinfo->ojrelid);
771 }
772
773 /*
774 * Rather than expend code on fixing up any already-derived clauses, just
775 * drop them. (At this point, any such clauses would be base restriction
776 * clauses, which we'd not need anymore anyway.)
777 */
779}
void ec_clear_derived_clauses(EquivalenceClass *ec)
Definition: equivclass.c:3831
Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid)
Definition: rewriteManip.c:761
List ** ec_childmembers
Definition: pathnodes.h:1565

References adjust_relid_set(), Assert(), bms_is_empty, bms_is_member(), ChangeVarNodesExtended(), EquivalenceClass::ec_childmembers, ec_clear_derived_clauses(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceClass::ec_sources, EquivalenceMember::em_is_const, EquivalenceMember::em_relids, foreach_delete_current, lfirst, SpecialJoinInfo::ojrelid, remove_rel_from_restrictinfo(), and replace_relid_callback().

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 791 of file analyzejoins.c.

792{
793 List *result = NIL;
794 ListCell *jl;
795
796 foreach(jl, joinlist)
797 {
798 Node *jlnode = (Node *) lfirst(jl);
799
800 if (IsA(jlnode, RangeTblRef))
801 {
802 int varno = ((RangeTblRef *) jlnode)->rtindex;
803
804 if (varno == relid)
805 (*nremoved)++;
806 else
807 result = lappend(result, jlnode);
808 }
809 else if (IsA(jlnode, List))
810 {
811 /* Recurse to handle subproblem */
812 List *sublist;
813
814 sublist = remove_rel_from_joinlist((List *) jlnode,
815 relid, nremoved);
816 /* Avoid including empty sub-lists in the result */
817 if (sublist)
818 result = lappend(result, sublist);
819 }
820 else
821 {
822 elog(ERROR, "unrecognized joinlist node type: %d",
823 (int) nodeTag(jlnode));
824 }
825 }
826
827 return result;
828}
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
Definition: analyzejoins.c:791
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define nodeTag(nodeptr)
Definition: nodes.h:139

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

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,
RelOptInfo rel,
int  subst,
SpecialJoinInfo sjinfo,
Relids  joinrelids 
)
static

Definition at line 326 of file analyzejoins.c.

329{
330 int relid = rel->relid;
331 Index rti;
332 ListCell *l;
333
334 /*
335 * Update all_baserels and related relid sets.
336 */
337 root->all_baserels = adjust_relid_set(root->all_baserels, relid, subst);
338 root->all_query_rels = adjust_relid_set(root->all_query_rels, relid, subst);
339
340 if (sjinfo != NULL)
341 {
342 root->outer_join_rels = bms_del_member(root->outer_join_rels,
343 sjinfo->ojrelid);
344 root->all_query_rels = bms_del_member(root->all_query_rels,
345 sjinfo->ojrelid);
346 }
347
348 /*
349 * Likewise remove references from SpecialJoinInfo data structures.
350 *
351 * This is relevant in case the outer join we're deleting is nested inside
352 * other outer joins: the upper joins' relid sets have to be adjusted. The
353 * RHS of the target outer join will be made empty here, but that's OK
354 * since caller will delete that SpecialJoinInfo entirely.
355 */
356 foreach(l, root->join_info_list)
357 {
358 SpecialJoinInfo *sjinf = (SpecialJoinInfo *) lfirst(l);
359
360 /*
361 * initsplan.c is fairly cavalier about allowing SpecialJoinInfos'
362 * lefthand/righthand relid sets to be shared with other data
363 * structures. Ensure that we don't modify the original relid sets.
364 * (The commute_xxx sets are always per-SpecialJoinInfo though.)
365 */
366 sjinf->min_lefthand = bms_copy(sjinf->min_lefthand);
367 sjinf->min_righthand = bms_copy(sjinf->min_righthand);
368 sjinf->syn_lefthand = bms_copy(sjinf->syn_lefthand);
369 sjinf->syn_righthand = bms_copy(sjinf->syn_righthand);
370 /* Now remove relid from the sets: */
371 sjinf->min_lefthand = adjust_relid_set(sjinf->min_lefthand, relid, subst);
372 sjinf->min_righthand = adjust_relid_set(sjinf->min_righthand, relid, subst);
373 sjinf->syn_lefthand = adjust_relid_set(sjinf->syn_lefthand, relid, subst);
374 sjinf->syn_righthand = adjust_relid_set(sjinf->syn_righthand, relid, subst);
375
376 if (sjinfo != NULL)
377 {
378 Assert(subst <= 0);
379
380 /* Remove sjinfo->ojrelid bits from the sets: */
382 sjinfo->ojrelid);
384 sjinfo->ojrelid);
386 sjinfo->ojrelid);
388 sjinfo->ojrelid);
389 /* relid cannot appear in these fields, but ojrelid can: */
391 sjinfo->ojrelid);
393 sjinfo->ojrelid);
395 sjinfo->ojrelid);
397 sjinfo->ojrelid);
398 }
399 else
400 {
401 Assert(subst > 0);
402
403 ChangeVarNodesExtended((Node *) sjinf->semi_rhs_exprs, relid, subst,
405 }
406 }
407
408 /*
409 * Likewise remove references from PlaceHolderVar data structures,
410 * removing any no-longer-needed placeholders entirely. We remove PHV
411 * only for left-join removal. With self-join elimination, PHVs already
412 * get moved to the remaining relation, where they might still be needed.
413 * It might also happen that we skip the removal of some PHVs that could
414 * be removed. However, the overhead of extra PHVs is small compared to
415 * the complexity of analysis needed to remove them.
416 *
417 * Removal is a bit trickier than it might seem: we can remove PHVs that
418 * are used at the target rel and/or in the join qual, but not those that
419 * are used at join partner rels or above the join. It's not that easy to
420 * distinguish PHVs used at partner rels from those used in the join qual,
421 * since they will both have ph_needed sets that are subsets of
422 * joinrelids. However, a PHV used at a partner rel could not have the
423 * target rel in ph_eval_at, so we check that while deciding whether to
424 * remove or just update the PHV. There is no corresponding test in
425 * join_is_removable because it doesn't need to distinguish those cases.
426 */
427 foreach(l, root->placeholder_list)
428 {
429 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
430
431 Assert(sjinfo == NULL || !bms_is_member(relid, phinfo->ph_lateral));
432 if (sjinfo != NULL &&
433 bms_is_subset(phinfo->ph_needed, joinrelids) &&
434 bms_is_member(relid, phinfo->ph_eval_at) &&
435 !bms_is_member(sjinfo->ojrelid, phinfo->ph_eval_at))
436 {
437 /*
438 * This code shouldn't be executed if one relation is substituted
439 * with another: in this case, the placeholder may be employed in
440 * a filter inside the scan node the SJE removes.
441 */
442 root->placeholder_list = foreach_delete_current(root->placeholder_list,
443 l);
444 root->placeholder_array[phinfo->phid] = NULL;
445 }
446 else
447 {
448 PlaceHolderVar *phv = phinfo->ph_var;
449
450 phinfo->ph_eval_at = adjust_relid_set(phinfo->ph_eval_at, relid, subst);
451 if (sjinfo != NULL)
452 phinfo->ph_eval_at = adjust_relid_set(phinfo->ph_eval_at,
453 sjinfo->ojrelid, subst);
454 Assert(!bms_is_empty(phinfo->ph_eval_at)); /* checked previously */
455 /* Reduce ph_needed to contain only "relation 0"; see below */
456 if (bms_is_member(0, phinfo->ph_needed))
457 phinfo->ph_needed = bms_make_singleton(0);
458 else
459 phinfo->ph_needed = NULL;
460
461 phinfo->ph_lateral = adjust_relid_set(phinfo->ph_lateral, relid, subst);
462
463 /*
464 * ph_lateral might contain rels mentioned in ph_eval_at after the
465 * replacement, remove them.
466 */
467 phinfo->ph_lateral = bms_difference(phinfo->ph_lateral, phinfo->ph_eval_at);
468 /* ph_lateral might or might not be empty */
469
470 phv->phrels = adjust_relid_set(phv->phrels, relid, subst);
471 if (sjinfo != NULL)
472 phv->phrels = adjust_relid_set(phv->phrels,
473 sjinfo->ojrelid, subst);
474 Assert(!bms_is_empty(phv->phrels));
475
476 ChangeVarNodesExtended((Node *) phv->phexpr, relid, subst, 0,
478
479 Assert(phv->phnullingrels == NULL); /* no need to adjust */
480 }
481 }
482
483 /*
484 * Likewise remove references from EquivalenceClasses.
485 */
486 foreach(l, root->eq_classes)
487 {
489
490 if (bms_is_member(relid, ec->ec_relids) ||
491 (sjinfo == NULL || bms_is_member(sjinfo->ojrelid, ec->ec_relids)))
492 remove_rel_from_eclass(ec, sjinfo, relid, subst);
493 }
494
495 /*
496 * Finally, we must recompute per-Var attr_needed and per-PlaceHolderVar
497 * ph_needed relid sets. These have to be known accurately, else we may
498 * fail to remove other now-removable outer joins. And our removal of the
499 * join clause(s) for this outer join may mean that Vars that were
500 * formerly needed no longer are. So we have to do this honestly by
501 * repeating the construction of those relid sets. We can cheat to one
502 * small extent: we can avoid re-examining the targetlist and HAVING qual
503 * by preserving "relation 0" bits from the existing relid sets. This is
504 * safe because we'd never remove such references.
505 *
506 * So, start by removing all other bits from attr_needed sets and
507 * lateral_vars lists. (We already did this above for ph_needed.)
508 */
509 for (rti = 1; rti < root->simple_rel_array_size; rti++)
510 {
511 RelOptInfo *otherrel = root->simple_rel_array[rti];
512 int attroff;
513
514 /* there may be empty slots corresponding to non-baserel RTEs */
515 if (otherrel == NULL)
516 continue;
517
518 Assert(otherrel->relid == rti); /* sanity check on array */
519
520 for (attroff = otherrel->max_attr - otherrel->min_attr;
521 attroff >= 0;
522 attroff--)
523 {
524 if (bms_is_member(0, otherrel->attr_needed[attroff]))
525 otherrel->attr_needed[attroff] = bms_make_singleton(0);
526 else
527 otherrel->attr_needed[attroff] = NULL;
528 }
529
530 if (subst > 0)
531 ChangeVarNodesExtended((Node *) otherrel->lateral_vars, relid,
532 subst, 0, replace_relid_callback);
533 }
534}
static void remove_rel_from_eclass(EquivalenceClass *ec, SpecialJoinInfo *sjinfo, int relid, int subst)
Definition: analyzejoins.c:720
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:867
Relids phnullingrels
Definition: pathnodes.h:3019
List * lateral_vars
Definition: pathnodes.h:991
Relids syn_lefthand
Definition: pathnodes.h:3119
List * semi_rhs_exprs
Definition: pathnodes.h:3132
Relids commute_above_l
Definition: pathnodes.h:3123
Relids syn_righthand
Definition: pathnodes.h:3120
Relids commute_below_r
Definition: pathnodes.h:3126

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(), SpecialJoinInfo::commute_above_l, SpecialJoinInfo::commute_above_r, SpecialJoinInfo::commute_below_l, SpecialJoinInfo::commute_below_r, EquivalenceClass::ec_relids, foreach_delete_current, RelOptInfo::lateral_vars, lfirst, RelOptInfo::max_attr, RelOptInfo::min_attr, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_var, PlaceHolderInfo::phid, PlaceHolderVar::phnullingrels, RelOptInfo::relid, remove_rel_from_eclass(), replace_relid_callback(), root, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

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 659 of file analyzejoins.c.

660{
661 /*
662 * initsplan.c is fairly cavalier about allowing RestrictInfos to share
663 * relid sets with other RestrictInfos, and SpecialJoinInfos too. Make
664 * sure this RestrictInfo has its own relid sets before we modify them.
665 * (In present usage, clause_relids is probably not shared, but
666 * required_relids could be; let's not assume anything.)
667 */
668 rinfo->clause_relids = bms_copy(rinfo->clause_relids);
669 rinfo->clause_relids = bms_del_member(rinfo->clause_relids, relid);
670 rinfo->clause_relids = bms_del_member(rinfo->clause_relids, ojrelid);
671 /* Likewise for required_relids */
673 rinfo->required_relids = bms_del_member(rinfo->required_relids, relid);
674 rinfo->required_relids = bms_del_member(rinfo->required_relids, ojrelid);
675
676 /* If it's an OR, recurse to clean up sub-clauses */
677 if (restriction_is_or_clause(rinfo))
678 {
679 ListCell *lc;
680
681 Assert(is_orclause(rinfo->orclause));
682 foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
683 {
684 Node *orarg = (Node *) lfirst(lc);
685
686 /* OR arguments should be ANDs or sub-RestrictInfos */
687 if (is_andclause(orarg))
688 {
689 List *andargs = ((BoolExpr *) orarg)->args;
690 ListCell *lc2;
691
692 foreach(lc2, andargs)
693 {
694 RestrictInfo *rinfo2 = lfirst_node(RestrictInfo, lc2);
695
696 remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
697 }
698 }
699 else
700 {
701 RestrictInfo *rinfo2 = castNode(RestrictInfo, orarg);
702
703 remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
704 }
705 }
706 }
707}
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)
Definition: restrictinfo.c:407

References Assert(), bms_copy(), bms_del_member(), castNode, is_andclause(), is_orclause(), lfirst, lfirst_node, 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 1827 of file analyzejoins.c.

1830{
1831 List *joininfos;
1832 ListCell *lc;
1833 int i;
1834 List *jinfo_candidates = NIL;
1835 List *binfo_candidates = NIL;
1836
1837 Assert(toKeep->relid > 0);
1838 Assert(toRemove->relid > 0);
1839
1840 /*
1841 * Replace the index of the removing table with the keeping one. The
1842 * technique of removing/distributing restrictinfo is used here to attach
1843 * just appeared (for keeping relation) join clauses and avoid adding
1844 * duplicates of those that already exist in the joininfo list.
1845 */
1846 joininfos = list_copy(toRemove->joininfo);
1847 foreach_node(RestrictInfo, rinfo, joininfos)
1848 {
1849 remove_join_clause_from_rels(root, rinfo, rinfo->required_relids);
1850 ChangeVarNodesExtended((Node *) rinfo, toRemove->relid, toKeep->relid,
1852
1853 if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
1854 jinfo_candidates = lappend(jinfo_candidates, rinfo);
1855 else
1856 binfo_candidates = lappend(binfo_candidates, rinfo);
1857 }
1858
1859 /*
1860 * Concatenate restrictlist to the list of base restrictions of the
1861 * removing table just to simplify the replacement procedure: all of them
1862 * weren't connected to any keeping relations and need to be added to some
1863 * rels.
1864 */
1865 toRemove->baserestrictinfo = list_concat(toRemove->baserestrictinfo,
1866 restrictlist);
1867 foreach_node(RestrictInfo, rinfo, toRemove->baserestrictinfo)
1868 {
1869 ChangeVarNodesExtended((Node *) rinfo, toRemove->relid, toKeep->relid,
1871
1872 if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
1873 jinfo_candidates = lappend(jinfo_candidates, rinfo);
1874 else
1875 binfo_candidates = lappend(binfo_candidates, rinfo);
1876 }
1877
1878 /*
1879 * Now, add all non-redundant clauses to the keeping relation.
1880 */
1881 add_non_redundant_clauses(root, binfo_candidates,
1882 &toKeep->baserestrictinfo, toRemove->relid);
1883 add_non_redundant_clauses(root, jinfo_candidates,
1884 &toKeep->joininfo, toRemove->relid);
1885
1886 list_free(binfo_candidates);
1887 list_free(jinfo_candidates);
1888
1889 /*
1890 * Arrange equivalence classes, mentioned removing a table, with the
1891 * keeping one: varno of removing table should be replaced in members and
1892 * sources lists. Also, remove duplicated elements if this replacement
1893 * procedure created them.
1894 */
1895 i = -1;
1896 while ((i = bms_next_member(toRemove->eclass_indexes, i)) >= 0)
1897 {
1898 EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
1899
1900 update_eclasses(ec, toRemove->relid, toKeep->relid);
1901 toKeep->eclass_indexes = bms_add_member(toKeep->eclass_indexes, i);
1902 }
1903
1904 /*
1905 * Transfer the targetlist and attr_needed flags.
1906 */
1907
1908 foreach(lc, toRemove->reltarget->exprs)
1909 {
1910 Node *node = lfirst(lc);
1911
1912 ChangeVarNodesExtended(node, toRemove->relid, toKeep->relid, 0,
1914 if (!list_member(toKeep->reltarget->exprs, node))
1915 toKeep->reltarget->exprs = lappend(toKeep->reltarget->exprs, node);
1916 }
1917
1918 for (i = toKeep->min_attr; i <= toKeep->max_attr; i++)
1919 {
1920 int attno = i - toKeep->min_attr;
1921
1922 toRemove->attr_needed[attno] = adjust_relid_set(toRemove->attr_needed[attno],
1923 toRemove->relid, toKeep->relid);
1924 toKeep->attr_needed[attno] = bms_add_members(toKeep->attr_needed[attno],
1925 toRemove->attr_needed[attno]);
1926 }
1927
1928 /*
1929 * If the removed relation has a row mark, transfer it to the remaining
1930 * one.
1931 *
1932 * If both rels have row marks, just keep the one corresponding to the
1933 * remaining relation because we verified earlier that they have the same
1934 * strength.
1935 */
1936 if (rmark)
1937 {
1938 if (kmark)
1939 {
1940 Assert(kmark->markType == rmark->markType);
1941
1942 root->rowMarks = list_delete_ptr(root->rowMarks, rmark);
1943 }
1944 else
1945 {
1946 /* Shouldn't have inheritance children here. */
1947 Assert(rmark->rti == rmark->prti);
1948
1949 rmark->rti = rmark->prti = toKeep->relid;
1950 }
1951 }
1952
1953 /*
1954 * Replace varno in all the query structures, except nodes RangeTblRef
1955 * otherwise later remove_rel_from_joinlist will yield errors.
1956 */
1957 ChangeVarNodesExtended((Node *) root->parse, toRemove->relid, toKeep->relid,
1959
1960 /* Replace links in the planner info */
1961 remove_rel_from_query(root, toRemove, toKeep->relid, NULL, NULL);
1962
1963 /* At last, replace varno in root targetlist and HAVING clause */
1964 ChangeVarNodesExtended((Node *) root->processed_tlist, toRemove->relid,
1965 toKeep->relid, 0, replace_relid_callback);
1966 ChangeVarNodesExtended((Node *) root->processed_groupClause,
1967 toRemove->relid, toKeep->relid, 0,
1969
1970 adjust_relid_set(root->all_result_relids, toRemove->relid, toKeep->relid);
1971 adjust_relid_set(root->leaf_result_relids, toRemove->relid, toKeep->relid);
1972
1973 /*
1974 * There may be references to the rel in root->fkey_list, but if so,
1975 * match_foreign_keys_to_quals() will get rid of them.
1976 */
1977
1978 /*
1979 * Finally, remove the rel from the baserel array to prevent it from being
1980 * referenced again. (We can't do this earlier because
1981 * remove_join_clause_from_rels will touch it.)
1982 */
1983 root->simple_rel_array[toRemove->relid] = NULL;
1984 root->simple_rte_array[toRemove->relid] = NULL;
1985
1986 /* And nuke the RelOptInfo, just in case there's another access path. */
1987 pfree(toRemove);
1988
1989
1990 /*
1991 * Now repeat construction of attr_needed bits coming from all other
1992 * sources.
1993 */
1998}
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:1305
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:780
@ 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:299
List * exprs
Definition: pathnodes.h:1780
Index prti
Definition: plannodes.h:1592
RowMarkType markType
Definition: plannodes.h:1596
struct PathTarget * reltarget
Definition: pathnodes.h:949
Bitmapset * eclass_indexes
Definition: pathnodes.h:1003

References add_non_redundant_clauses(), adjust_relid_set(), Assert(), RelOptInfo::baserestrictinfo, bms_add_member(), bms_add_members(), bms_membership(), BMS_MULTIPLE, bms_next_member(), ChangeVarNodesExtended(), RelOptInfo::eclass_indexes, PathTarget::exprs, foreach_node, i, RelOptInfo::joininfo, lappend(), lfirst, list_concat(), list_copy(), list_delete_ptr(), list_free(), list_member(), list_nth(), PlanRowMark::markType, RelOptInfo::min_attr, NIL, pfree(), PlanRowMark::prti, rebuild_eclass_attr_needed(), rebuild_joinclause_attr_needed(), rebuild_lateral_attr_needed(), rebuild_placeholder_attr_needed(), RelOptInfo::relid, RelOptInfo::reltarget, remove_join_clause_from_rels(), remove_rel_from_query(), replace_relid_callback(), root, PlanRowMark::rti, 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 2141 of file analyzejoins.c.

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

References Assert(), RelOptInfo::baserestrictinfo, bms_add_member(), bms_is_member(), bms_next_member(), generate_join_implied_equalities(), innerrel_is_unique_ext(), JOIN_INNER, lfirst, list_concat(), list_length(), PlanRowMark::markType, match_unique_clauses(), NIL, RelOptInfo::relid, RelOptInfo::relids, remove_self_join_rel(), root, PlanRowMark::rti, 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 2308 of file analyzejoins.c.

2309{
2310 ListCell *jl;
2311 Relids relids = NULL;
2312 SelfJoinCandidate *candidates = NULL;
2313 int i;
2314 int j;
2315 int numRels;
2316
2317 /* Collect indexes of base relations of the join tree */
2318 foreach(jl, joinlist)
2319 {
2320 Node *jlnode = (Node *) lfirst(jl);
2321
2322 if (IsA(jlnode, RangeTblRef))
2323 {
2324 int varno = ((RangeTblRef *) jlnode)->rtindex;
2325 RangeTblEntry *rte = root->simple_rte_array[varno];
2326
2327 /*
2328 * We only consider ordinary relations as candidates to be
2329 * removed, and these relations should not have TABLESAMPLE
2330 * clauses specified. Removing a relation with TABLESAMPLE clause
2331 * could potentially change the syntax of the query. Because of
2332 * UPDATE/DELETE EPQ mechanism, currently Query->resultRelation or
2333 * Query->mergeTargetRelation associated rel cannot be eliminated.
2334 */
2335 if (rte->rtekind == RTE_RELATION &&
2336 rte->relkind == RELKIND_RELATION &&
2337 rte->tablesample == NULL &&
2338 varno != root->parse->resultRelation &&
2339 varno != root->parse->mergeTargetRelation)
2340 {
2341 Assert(!bms_is_member(varno, relids));
2342 relids = bms_add_member(relids, varno);
2343 }
2344 }
2345 else if (IsA(jlnode, List))
2346 {
2347 /* Recursively go inside the sub-joinlist */
2348 toRemove = remove_self_joins_recurse(root, (List *) jlnode,
2349 toRemove);
2350 }
2351 else
2352 elog(ERROR, "unrecognized joinlist node type: %d",
2353 (int) nodeTag(jlnode));
2354 }
2355
2356 numRels = bms_num_members(relids);
2357
2358 /* Need at least two relations for the join */
2359 if (numRels < 2)
2360 return toRemove;
2361
2362 /*
2363 * In order to find relations with the same oid we first build an array of
2364 * candidates and then sort it by oid.
2365 */
2366 candidates = palloc_array(SelfJoinCandidate, numRels);
2367 i = -1;
2368 j = 0;
2369 while ((i = bms_next_member(relids, i)) >= 0)
2370 {
2371 candidates[j].relid = i;
2372 candidates[j].reloid = root->simple_rte_array[i]->relid;
2373 j++;
2374 }
2375
2376 qsort(candidates, numRels, sizeof(SelfJoinCandidate),
2378
2379 /*
2380 * Iteratively form a group of relation indexes with the same oid and
2381 * launch the routine that detects self-joins in this group and removes
2382 * excessive range table entries.
2383 *
2384 * At the end of the iteration, exclude the group from the overall relids
2385 * list. So each next iteration of the cycle will involve less and less
2386 * value of relids.
2387 */
2388 i = 0;
2389 for (j = 1; j < numRels + 1; j++)
2390 {
2391 if (j == numRels || candidates[j].reloid != candidates[i].reloid)
2392 {
2393 if (j - i >= 2)
2394 {
2395 /* Create a group of relation indexes with the same oid */
2396 Relids group = NULL;
2397 Relids removed;
2398
2399 while (i < j)
2400 {
2401 group = bms_add_member(group, candidates[i].relid);
2402 i++;
2403 }
2404 relids = bms_del_members(relids, group);
2405
2406 /*
2407 * Try to remove self-joins from a group of identical entries.
2408 * Make the next attempt iteratively - if something is deleted
2409 * from a group, changes in clauses and equivalence classes
2410 * can give us a chance to find more candidates.
2411 */
2412 do
2413 {
2414 Assert(!bms_overlap(group, toRemove));
2415 removed = remove_self_joins_one_group(root, group);
2416 toRemove = bms_add_members(toRemove, removed);
2417 group = bms_del_members(group, removed);
2418 } while (!bms_is_empty(removed) &&
2419 bms_membership(group) == BMS_MULTIPLE);
2420 bms_free(removed);
2421 bms_free(group);
2422 }
2423 else
2424 {
2425 /* Single relation, just remove it from the set */
2426 relids = bms_del_member(relids, candidates[i].relid);
2427 i = j;
2428 }
2429 }
2430 }
2431
2432 Assert(bms_is_empty(relids));
2433
2434 return toRemove;
2435}
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:1160
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:750
#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:499
struct TableSampleClause * tablesample
Definition: parsenodes.h:1129
RTEKind rtekind
Definition: parsenodes.h:1078

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, i, IsA, j, lfirst, nodeTag, palloc_array, qsort, SelfJoinCandidate::relid, SelfJoinCandidate::reloid, remove_self_joins_one_group(), remove_self_joins_recurse(), root, RTE_RELATION, RangeTblEntry::rtekind, self_join_candidates_cmp(), and RangeTblEntry::tablesample.

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 91 of file analyzejoins.c.

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

References bms_singleton_member(), elog, ERROR, 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 2488 of file analyzejoins.c.

2489{
2490 Relids toRemove = NULL;
2491 int relid = -1;
2492
2493 if (!enable_self_join_elimination || joinlist == NIL ||
2494 (list_length(joinlist) == 1 && !IsA(linitial(joinlist), List)))
2495 return joinlist;
2496
2497 /*
2498 * Merge pairs of relations participated in self-join. Remove unnecessary
2499 * range table entries.
2500 */
2501 toRemove = remove_self_joins_recurse(root, joinlist, toRemove);
2502
2503 if (unlikely(toRemove != NULL))
2504 {
2505 /* At the end, remove orphaned relation links */
2506 while ((relid = bms_next_member(toRemove, relid)) >= 0)
2507 {
2508 int nremoved = 0;
2509
2510 joinlist = remove_rel_from_joinlist(joinlist, relid, &nremoved);
2511 if (nremoved != 1)
2512 elog(ERROR, "failed to find relation %d in joinlist", relid);
2513 }
2514 }
2515
2516 return joinlist;
2517}
bool enable_self_join_elimination
Definition: analyzejoins.c:54
#define unlikely(x)
Definition: c.h:418
#define linitial(l)
Definition: pg_list.h:178

References bms_next_member(), elog, enable_self_join_elimination, ERROR, 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 1703 of file analyzejoins.c.

1704{
1705 if (IsA(node, RangeTblRef))
1706 {
1707 return true;
1708 }
1709 else if (IsA(node, RestrictInfo))
1710 {
1711 RestrictInfo *rinfo = (RestrictInfo *) node;
1712 int relid = -1;
1713 bool is_req_equal =
1714 (rinfo->required_relids == rinfo->clause_relids);
1715 bool clause_relids_is_multiple =
1716 (bms_membership(rinfo->clause_relids) == BMS_MULTIPLE);
1717
1718 /*
1719 * Recurse down into clauses if the target relation is present in
1720 * clause_relids or required_relids. We must check required_relids
1721 * because the relation not present in clause_relids might still be
1722 * present somewhere in orclause.
1723 */
1724 if (bms_is_member(context->rt_index, rinfo->clause_relids) ||
1725 bms_is_member(context->rt_index, rinfo->required_relids))
1726 {
1727 Relids new_clause_relids;
1728
1729 ChangeVarNodesWalkExpression((Node *) rinfo->clause, context);
1730 ChangeVarNodesWalkExpression((Node *) rinfo->orclause, context);
1731
1732 new_clause_relids = adjust_relid_set(rinfo->clause_relids,
1733 context->rt_index,
1734 context->new_index);
1735
1736 /*
1737 * Incrementally adjust num_base_rels based on the change of
1738 * clause_relids, which could contain both base relids and
1739 * outer-join relids. This operation is legal until we remove
1740 * only baserels.
1741 */
1742 rinfo->num_base_rels -= bms_num_members(rinfo->clause_relids) -
1743 bms_num_members(new_clause_relids);
1744
1745 rinfo->clause_relids = new_clause_relids;
1746 rinfo->left_relids =
1747 adjust_relid_set(rinfo->left_relids, context->rt_index, context->new_index);
1748 rinfo->right_relids =
1749 adjust_relid_set(rinfo->right_relids, context->rt_index, context->new_index);
1750 }
1751
1752 if (is_req_equal)
1753 rinfo->required_relids = rinfo->clause_relids;
1754 else
1755 rinfo->required_relids =
1756 adjust_relid_set(rinfo->required_relids, context->rt_index, context->new_index);
1757
1758 rinfo->outer_relids =
1759 adjust_relid_set(rinfo->outer_relids, context->rt_index, context->new_index);
1760 rinfo->incompatible_relids =
1761 adjust_relid_set(rinfo->incompatible_relids, context->rt_index, context->new_index);
1762
1763 if (rinfo->mergeopfamilies &&
1764 bms_get_singleton_member(rinfo->clause_relids, &relid) &&
1765 clause_relids_is_multiple &&
1766 relid == context->new_index && IsA(rinfo->clause, OpExpr))
1767 {
1768 Expr *leftOp;
1769 Expr *rightOp;
1770
1771 leftOp = (Expr *) get_leftop(rinfo->clause);
1772 rightOp = (Expr *) get_rightop(rinfo->clause);
1773
1774 /*
1775 * For self-join elimination, changing varnos could transform
1776 * "t1.a = t2.a" into "t1.a = t1.a". That is always true as long
1777 * as "t1.a" is not null. We use equal() to check for such a
1778 * case, and then we replace the qual with a check for not null
1779 * (NullTest).
1780 */
1781 if (leftOp != NULL && equal(leftOp, rightOp))
1782 {
1783 NullTest *ntest = makeNode(NullTest);
1784
1785 ntest->arg = leftOp;
1786 ntest->nulltesttype = IS_NOT_NULL;
1787 ntest->argisrow = false;
1788 ntest->location = -1;
1789 rinfo->clause = (Expr *) ntest;
1790 rinfo->mergeopfamilies = NIL;
1791 rinfo->left_em = NULL;
1792 rinfo->right_em = NULL;
1793 }
1794 Assert(rinfo->orclause == NULL);
1795 }
1796 return true;
1797 }
1798
1799 return false;
1800}
@ IS_NOT_NULL
Definition: primnodes.h:1977
bool ChangeVarNodesWalkExpression(Node *node, ChangeVarNodes_context *context)
Definition: rewriteManip.c:744
NullTestType nulltesttype
Definition: primnodes.h:1984
ParseLoc location
Definition: primnodes.h:1987
Expr * arg
Definition: primnodes.h:1983
Relids outer_relids
Definition: pathnodes.h:2829
Relids incompatible_relids
Definition: pathnodes.h:2826

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

Referenced by match_unique_clauses(), remove_rel_from_eclass(), 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 1633 of file analyzejoins.c.

1634{
1635 int saved_rinfo_serial = a->rinfo_serial;
1636 bool result;
1637
1638 a->rinfo_serial = b->rinfo_serial;
1639 result = equal(a, b);
1640 a->rinfo_serial = saved_rinfo_serial;
1641
1642 return result;
1643}
int b
Definition: isn.c:74
int a
Definition: isn.c:73

References a, b, and equal().

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 2441 of file analyzejoins.c.

2442{
2443 const SelfJoinCandidate *ca = (const SelfJoinCandidate *) a;
2444 const SelfJoinCandidate *cb = (const SelfJoinCandidate *) b;
2445
2446 if (ca->reloid != cb->reloid)
2447 return (ca->reloid < cb->reloid ? -1 : 1);
2448 else
2449 return 0;
2450}

References a, b, 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 2010 of file analyzejoins.c.

2012{
2013 List *sjoinquals = NIL;
2014 List *ojoinquals = NIL;
2015
2016 foreach_node(RestrictInfo, rinfo, joinquals)
2017 {
2018 OpExpr *expr;
2019 Node *leftexpr;
2020 Node *rightexpr;
2021
2022 /* In general, clause looks like F(arg1) = G(arg2) */
2023 if (!rinfo->mergeopfamilies ||
2024 bms_num_members(rinfo->clause_relids) != 2 ||
2025 bms_membership(rinfo->left_relids) != BMS_SINGLETON ||
2026 bms_membership(rinfo->right_relids) != BMS_SINGLETON)
2027 {
2028 ojoinquals = lappend(ojoinquals, rinfo);
2029 continue;
2030 }
2031
2032 expr = (OpExpr *) rinfo->clause;
2033
2034 if (!IsA(expr, OpExpr) || list_length(expr->args) != 2)
2035 {
2036 ojoinquals = lappend(ojoinquals, rinfo);
2037 continue;
2038 }
2039
2040 leftexpr = get_leftop(rinfo->clause);
2041 rightexpr = copyObject(get_rightop(rinfo->clause));
2042
2043 if (leftexpr && IsA(leftexpr, RelabelType))
2044 leftexpr = (Node *) ((RelabelType *) leftexpr)->arg;
2045 if (rightexpr && IsA(rightexpr, RelabelType))
2046 rightexpr = (Node *) ((RelabelType *) rightexpr)->arg;
2047
2048 /*
2049 * Quite an expensive operation, narrowing the use case. For example,
2050 * when we have cast of the same var to different (but compatible)
2051 * types.
2052 */
2053 ChangeVarNodesExtended(rightexpr,
2054 bms_singleton_member(rinfo->right_relids),
2055 bms_singleton_member(rinfo->left_relids), 0,
2057
2058 if (equal(leftexpr, rightexpr))
2059 sjoinquals = lappend(sjoinquals, rinfo);
2060 else
2061 ojoinquals = lappend(ojoinquals, rinfo);
2062 }
2063
2064 *selfjoinquals = sjoinquals;
2065 *otherjoinquals = ojoinquals;
2066}
@ BMS_SINGLETON
Definition: bitmapset.h:72
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void * arg
List * args
Definition: primnodes.h:868

References arg, OpExpr::args, bms_membership(), bms_num_members(), BMS_SINGLETON, bms_singleton_member(), ChangeVarNodesExtended(), copyObject, equal(), foreach_node, get_leftop(), get_rightop(), if(), 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 1533 of file analyzejoins.c.

1534{
1535 List *new_members = NIL;
1536 List *new_sources = NIL;
1537
1538 /*
1539 * We don't expect any EC child members to exist at this point. Ensure
1540 * that's the case, otherwise, we might be getting asked to do something
1541 * this function hasn't been coded for.
1542 */
1543 Assert(ec->ec_childmembers == NULL);
1544
1546 {
1547 bool is_redundant = false;
1548
1549 if (!bms_is_member(from, em->em_relids))
1550 {
1551 new_members = lappend(new_members, em);
1552 continue;
1553 }
1554
1555 em->em_relids = adjust_relid_set(em->em_relids, from, to);
1556 em->em_jdomain->jd_relids = adjust_relid_set(em->em_jdomain->jd_relids, from, to);
1557
1558 /* We only process inner joins */
1559 ChangeVarNodesExtended((Node *) em->em_expr, from, to, 0,
1561
1562 foreach_node(EquivalenceMember, other, new_members)
1563 {
1564 if (!equal(em->em_relids, other->em_relids))
1565 continue;
1566
1567 if (equal(em->em_expr, other->em_expr))
1568 {
1569 is_redundant = true;
1570 break;
1571 }
1572 }
1573
1574 if (!is_redundant)
1575 new_members = lappend(new_members, em);
1576 }
1577
1578 list_free(ec->ec_members);
1579 ec->ec_members = new_members;
1580
1582
1583 /* Update EC source expressions */
1585 {
1586 bool is_redundant = false;
1587
1588 if (!bms_is_member(from, rinfo->required_relids))
1589 {
1590 new_sources = lappend(new_sources, rinfo);
1591 continue;
1592 }
1593
1594 ChangeVarNodesExtended((Node *) rinfo, from, to, 0,
1596
1597 /*
1598 * After switching the clause to the remaining relation, check it for
1599 * redundancy with existing ones. We don't have to check for
1600 * redundancy with derived clauses, because we've just deleted them.
1601 */
1602 foreach_node(RestrictInfo, other, new_sources)
1603 {
1604 if (!equal(rinfo->clause_relids, other->clause_relids))
1605 continue;
1606
1607 if (equal(rinfo->clause, other->clause))
1608 {
1609 is_redundant = true;
1610 break;
1611 }
1612 }
1613
1614 if (!is_redundant)
1615 new_sources = lappend(new_sources, rinfo);
1616 }
1617
1618 list_free(ec->ec_sources);
1619 ec->ec_sources = new_sources;
1620 ec->ec_relids = adjust_relid_set(ec->ec_relids, from, to);
1621}

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(), 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().