PostgreSQL Source Code git master
Loading...
Searching...
No Matches
analyzejoins.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * analyzejoins.c
4 * Routines for simplifying joins after initial query analysis
5 *
6 * While we do a great deal of join simplification in prep/prepjointree.c,
7 * certain optimizations cannot be performed at that stage for lack of
8 * detailed information about the query. The routines here are invoked
9 * after initsplan.c has done its work, and can do additional join removal
10 * and simplification steps based on the information extracted. The penalty
11 * is that we have to work harder to clean up after ourselves when we modify
12 * the query, since the derived data structures have to be updated too.
13 *
14 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
15 * Portions Copyright (c) 1994, Regents of the University of California
16 *
17 *
18 * IDENTIFICATION
19 * src/backend/optimizer/plan/analyzejoins.c
20 *
21 *-------------------------------------------------------------------------
22 */
23#include "postgres.h"
24
25#include "catalog/pg_class.h"
26#include "nodes/nodeFuncs.h"
27#include "optimizer/joininfo.h"
28#include "optimizer/optimizer.h"
29#include "optimizer/pathnode.h"
30#include "optimizer/paths.h"
32#include "optimizer/planmain.h"
34#include "parser/parse_agg.h"
36#include "utils/lsyscache.h"
37
38/*
39 * Utility structure. A sorting procedure is needed to simplify the search
40 * of SJE-candidate baserels referencing the same database relation. Having
41 * collected all baserels from the query jointree, the planner sorts them
42 * according to the reloid value, groups them with the next pass and attempts
43 * to remove self-joins.
44 *
45 * Preliminary sorting prevents quadratic behavior that can be harmful in the
46 * case of numerous joins.
47 */
48typedef struct
49{
50 int relid;
53
55
56/* local functions */
58static void remove_leftjoinrel_from_query(PlannerInfo *root, int relid,
59 SpecialJoinInfo *sjinfo);
60static void remove_rel_from_query(PlannerInfo *root, int relid,
61 int subst, SpecialJoinInfo *sjinfo,
64 int relid, int ojrelid);
66 int relid, int ojrelid);
67static List *remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved);
70 List *clause_list, List **extra_clauses);
71static Oid distinct_col_search(int colno, List *colnos, List *opids);
74 Relids outerrelids,
75 RelOptInfo *innerrel,
76 JoinType jointype,
77 List *restrictlist,
78 List **extra_clauses);
79static int self_join_candidates_cmp(const void *a, const void *b);
80static bool replace_relid_callback(Node *node,
81 ChangeVarNodes_context *context);
82
83
84/*
85 * remove_useless_joins
86 * Check for relations that don't actually need to be joined at all,
87 * and remove them from the query.
88 *
89 * We are passed the current joinlist and return the updated list. Other
90 * data structures that have to be updated are accessible via "root".
91 */
92List *
94{
95 ListCell *lc;
96
97 /*
98 * We are only interested in relations that are left-joined to, so we can
99 * scan the join_info_list to find them easily.
100 */
101restart:
102 foreach(lc, root->join_info_list)
103 {
105 int innerrelid;
106 int nremoved;
107
108 /* Skip if not removable */
109 if (!join_is_removable(root, sjinfo))
110 continue;
111
112 /*
113 * Currently, join_is_removable can only succeed when the sjinfo's
114 * righthand is a single baserel. Remove that rel from the query and
115 * joinlist.
116 */
118
120
121 /* We verify that exactly one reference gets removed from joinlist */
122 nremoved = 0;
124 if (nremoved != 1)
125 elog(ERROR, "failed to find relation %d in joinlist", innerrelid);
126
127 /*
128 * We can delete this SpecialJoinInfo from the list too, since it's no
129 * longer of interest. (Since we'll restart the foreach loop
130 * immediately, we don't bother with foreach_delete_current.)
131 */
132 root->join_info_list = list_delete_cell(root->join_info_list, lc);
133
134 /*
135 * Restart the scan. This is necessary to ensure we find all
136 * removable joins independently of ordering of the join_info_list
137 * (note that removal of attr_needed bits may make a join appear
138 * removable that did not before).
139 */
140 goto restart;
141 }
142
143 return joinlist;
144}
145
146/*
147 * join_is_removable
148 * Check whether we need not perform this special join at all, because
149 * it will just duplicate its left input.
150 *
151 * This is true for a left join for which the join condition cannot match
152 * more than one inner-side row. (There are other possibly interesting
153 * cases, but we don't have the infrastructure to prove them.) We also
154 * have to check that the inner side doesn't generate any variables needed
155 * above the join.
156 */
157static bool
159{
160 int innerrelid;
161 RelOptInfo *innerrel;
165 ListCell *l;
166 int attroff;
167
168 /*
169 * Must be a left join to a single baserel, else we aren't going to be
170 * able to do anything with it.
171 */
172 if (sjinfo->jointype != JOIN_LEFT)
173 return false;
174
176 return false;
177
178 /*
179 * Never try to eliminate a left join to the query result rel. Although
180 * the case is syntactically impossible in standard SQL, MERGE will build
181 * a join tree that looks exactly like that.
182 */
183 if (innerrelid == root->parse->resultRelation)
184 return false;
185
186 innerrel = find_base_rel(root, innerrelid);
187
188 /*
189 * Before we go to the effort of checking whether any innerrel variables
190 * are needed above the join, make a quick check to eliminate cases in
191 * which we will surely be unable to prove uniqueness of the innerrel.
192 */
193 if (!rel_supports_distinctness(root, innerrel))
194 return false;
195
196 /* Compute the relid set for the join we are considering */
198 Assert(sjinfo->ojrelid != 0);
201
202 /*
203 * We can't remove the join if any inner-rel attributes are used above the
204 * join. Here, "above" the join includes pushed-down conditions, so we
205 * should reject if attr_needed includes the OJ's own relid; therefore,
206 * compare to inputrelids not joinrelids.
207 *
208 * As a micro-optimization, it seems better to start with max_attr and
209 * count down rather than starting with min_attr and counting up, on the
210 * theory that the system attributes are somewhat less likely to be wanted
211 * and should be tested last.
212 */
213 for (attroff = innerrel->max_attr - innerrel->min_attr;
214 attroff >= 0;
215 attroff--)
216 {
217 if (!bms_is_subset(innerrel->attr_needed[attroff], inputrelids))
218 return false;
219 }
220
221 /*
222 * Similarly check that the inner rel isn't needed by any PlaceHolderVars
223 * that will be used above the join. The PHV case is a little bit more
224 * complicated, because PHVs may have been assigned a ph_eval_at location
225 * that includes the innerrel, yet their contained expression might not
226 * actually reference the innerrel (it could be just a constant, for
227 * instance). If such a PHV is due to be evaluated above the join then it
228 * needn't prevent join removal.
229 */
230 foreach(l, root->placeholder_list)
231 {
233
234 if (bms_overlap(phinfo->ph_lateral, innerrel->relids))
235 return false; /* it references innerrel laterally */
236 if (!bms_overlap(phinfo->ph_eval_at, innerrel->relids))
237 continue; /* it definitely doesn't reference innerrel */
238 if (bms_is_subset(phinfo->ph_needed, inputrelids))
239 continue; /* PHV is not used above the join */
240 if (!bms_is_member(sjinfo->ojrelid, phinfo->ph_eval_at))
241 return false; /* it has to be evaluated below the join */
242
243 /*
244 * We need to be sure there will still be a place to evaluate the PHV
245 * if we remove the join, ie that ph_eval_at wouldn't become empty.
246 */
247 if (!bms_overlap(sjinfo->min_lefthand, phinfo->ph_eval_at))
248 return false; /* there isn't any other place to eval PHV */
249 /* Check contained expression last, since this is a bit expensive */
250 if (bms_overlap(pull_varnos(root, (Node *) phinfo->ph_var->phexpr),
251 innerrel->relids))
252 return false; /* contained expression references innerrel */
253 }
254
255 /*
256 * Search for mergejoinable clauses that constrain the inner rel against
257 * either the outer rel or a pseudoconstant. If an operator is
258 * mergejoinable then it behaves like equality for some btree opclass, so
259 * it's what we want. The mergejoinability test also eliminates clauses
260 * containing volatile functions, which we couldn't depend on.
261 */
262 foreach(l, innerrel->joininfo)
263 {
265
266 /*
267 * If the current join commutes with some other outer join(s) via
268 * outer join identity 3, there will be multiple clones of its join
269 * clauses in the joininfo list. We want to consider only the
270 * has_clone form of such clauses. Processing more than one form
271 * would be wasteful, and also some of the others would confuse the
272 * RINFO_IS_PUSHED_DOWN test below.
273 */
274 if (restrictinfo->is_clone)
275 continue; /* ignore it */
276
277 /*
278 * If it's not a join clause for this outer join, we can't use it.
279 * Note that if the clause is pushed-down, then it is logically from
280 * above the outer join, even if it references no other rels (it might
281 * be from WHERE, for example).
282 */
284 continue; /* ignore; not useful here */
285
286 /* Ignore if it's not a mergejoinable clause */
287 if (!restrictinfo->can_join ||
288 restrictinfo->mergeopfamilies == NIL)
289 continue; /* not mergejoinable */
290
291 /*
292 * Check if the clause has the form "outer op inner" or "inner op
293 * outer", and if so mark which side is inner.
294 */
296 innerrel->relids))
297 continue; /* no good for these input relations */
298
299 /* OK, add to list */
301 }
302
303 /*
304 * Now that we have the relevant equality join clauses, try to prove the
305 * innerrel distinct.
306 */
307 if (rel_is_distinct_for(root, innerrel, clause_list, NULL))
308 return true;
309
310 /*
311 * Some day it would be nice to check for other methods of establishing
312 * distinctness.
313 */
314 return false;
315}
316
317/*
318 * Remove the target relid and references to the target join from the
319 * planner's data structures, having determined that there is no need
320 * to include them in the query.
321 *
322 * We are not terribly thorough here. We only bother to update parts of
323 * the planner's data structures that will actually be consulted later.
324 */
325static void
327 SpecialJoinInfo *sjinfo)
328{
329 RelOptInfo *rel = find_base_rel(root, relid);
330 int ojrelid = sjinfo->ojrelid;
334 ListCell *l;
335
336 /* Compute the relid set for the join we are considering */
338 Assert(ojrelid != 0);
340
341 remove_rel_from_query(root, relid, -1, sjinfo, joinrelids);
342
343 /*
344 * Remove any joinquals referencing the rel from the joininfo lists.
345 *
346 * In some cases, a joinqual has to be put back after deleting its
347 * reference to the target rel. This can occur for pseudoconstant and
348 * outerjoin-delayed quals, which can get marked as requiring the rel in
349 * order to force them to be evaluated at or above the join. We can't
350 * just discard them, though. Only quals that logically belonged to the
351 * outer join being discarded should be removed from the query.
352 *
353 * We might encounter a qual that is a clone of a deletable qual with some
354 * outer-join relids added (see deconstruct_distribute_oj_quals). To
355 * ensure we get rid of such clones as well, add the relids of all OJs
356 * commutable with this one to the set we test against for
357 * pushed-down-ness.
358 */
360 sjinfo->commute_above_r);
362 sjinfo->commute_below_l);
363
364 /*
365 * We must make a copy of the rel's old joininfo list before starting the
366 * loop, because otherwise remove_join_clause_from_rels would destroy the
367 * list while we're scanning it.
368 */
370 foreach(l, joininfos)
371 {
372 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
373
375
377 {
378 /*
379 * There might be references to relid or ojrelid in the
380 * RestrictInfo's relid sets, as a consequence of PHVs having had
381 * ph_eval_at sets that include those. We already checked above
382 * that any such PHV is safe (and updated its ph_eval_at), so we
383 * can just drop those references.
384 */
385 remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
386
387 /*
388 * Cross-check that the clause itself does not reference the
389 * target rel or join.
390 */
391#ifdef USE_ASSERT_CHECKING
392 {
394 (Node *) rinfo->clause);
395
398 }
399#endif
400 /* Now throw it back into the joininfo lists */
402 }
403 }
404
405 /*
406 * There may be references to the rel in root->fkey_list, but if so,
407 * match_foreign_keys_to_quals() will get rid of them.
408 */
409
410 /*
411 * Now remove the rel from the baserel array to prevent it from being
412 * referenced again. (We can't do this earlier because
413 * remove_join_clause_from_rels will touch it.)
414 */
415 root->simple_rel_array[relid] = NULL;
416 root->simple_rte_array[relid] = NULL;
417
418 /* And nuke the RelOptInfo, just in case there's another access path */
419 pfree(rel);
420
421 /*
422 * Now repeat construction of attr_needed bits coming from all other
423 * sources.
424 */
429}
430
431/*
432 * Remove the target relid and references to the target join from the
433 * planner's data structures, having determined that there is no need
434 * to include them in the query. Optionally replace references to the
435 * removed relid with subst if this is a self-join removal.
436 *
437 * This function serves as the common infrastructure for left-join removal
438 * and self-join elimination. It is intentionally scoped to update only the
439 * shared planner data structures that are universally affected by relation
440 * removal. Each specific caller remains responsible for updating any
441 * remaining data structures required by its unique removal logic.
442 *
443 * The specific type of removal being performed is dictated by the combination
444 * of the sjinfo and subst parameters. A non-NULL sjinfo indicates left-join
445 * removal. When sjinfo is NULL, a positive subst value indicates self-join
446 * elimination (where references are replaced with subst).
447 */
448static void
450 int subst, SpecialJoinInfo *sjinfo,
452{
453 int ojrelid = sjinfo ? sjinfo->ojrelid : 0;
454 Index rti;
455 ListCell *l;
456 bool is_outer_join = (sjinfo != NULL);
457 bool is_self_join = (!is_outer_join && subst > 0);
458
460 Assert(!is_outer_join || ojrelid > 0);
462
463 /*
464 * Update all_baserels and related relid sets.
465 */
466 root->all_baserels = adjust_relid_set(root->all_baserels, relid, subst);
467 root->all_query_rels = adjust_relid_set(root->all_query_rels, relid, subst);
468
469 if (is_outer_join)
470 {
471 root->outer_join_rels = bms_del_member(root->outer_join_rels, ojrelid);
472 root->all_query_rels = bms_del_member(root->all_query_rels, ojrelid);
473 }
474
475 /*
476 * Likewise remove references from SpecialJoinInfo data structures.
477 *
478 * This is relevant in case the relation we're deleting is part of the
479 * relid sets of special joins: those sets have to be adjusted. If we are
480 * removing an outer join, the RHS of the target outer join will be made
481 * empty here, but that's OK since the caller will delete that
482 * SpecialJoinInfo entirely.
483 */
484 foreach(l, root->join_info_list)
485 {
487
488 /*
489 * initsplan.c is fairly cavalier about allowing SpecialJoinInfos'
490 * lefthand/righthand relid sets to be shared with other data
491 * structures. Ensure that we don't modify the original relid sets.
492 * (The commute_xxx sets are always per-SpecialJoinInfo though.)
493 */
494 sjinf->min_lefthand = bms_copy(sjinf->min_lefthand);
495 sjinf->min_righthand = bms_copy(sjinf->min_righthand);
496 sjinf->syn_lefthand = bms_copy(sjinf->syn_lefthand);
497 sjinf->syn_righthand = bms_copy(sjinf->syn_righthand);
498
499 /* Now adjust relid bit in the sets: */
500 sjinf->min_lefthand = adjust_relid_set(sjinf->min_lefthand, relid, subst);
501 sjinf->min_righthand = adjust_relid_set(sjinf->min_righthand, relid, subst);
502 sjinf->syn_lefthand = adjust_relid_set(sjinf->syn_lefthand, relid, subst);
503 sjinf->syn_righthand = adjust_relid_set(sjinf->syn_righthand, relid, subst);
504
505 if (is_outer_join)
506 {
507 /* Remove ojrelid bit from the sets: */
508 sjinf->min_lefthand = bms_del_member(sjinf->min_lefthand, ojrelid);
509 sjinf->min_righthand = bms_del_member(sjinf->min_righthand, ojrelid);
510 sjinf->syn_lefthand = bms_del_member(sjinf->syn_lefthand, ojrelid);
511 sjinf->syn_righthand = bms_del_member(sjinf->syn_righthand, ojrelid);
512 /* relid cannot appear in these fields, but ojrelid can: */
513 sjinf->commute_above_l = bms_del_member(sjinf->commute_above_l, ojrelid);
514 sjinf->commute_above_r = bms_del_member(sjinf->commute_above_r, ojrelid);
515 sjinf->commute_below_l = bms_del_member(sjinf->commute_below_l, ojrelid);
516 sjinf->commute_below_r = bms_del_member(sjinf->commute_below_r, ojrelid);
517 }
518 else
519 {
520 /*
521 * For self-join removal, replace relid references in
522 * semi_rhs_exprs.
523 */
524 ChangeVarNodesExtended((Node *) sjinf->semi_rhs_exprs, relid, subst,
526 }
527 }
528
529 /*
530 * Likewise remove references from PlaceHolderVar data structures,
531 * removing any no-longer-needed placeholders entirely. We only remove
532 * PHVs for left-join removal. With self-join elimination, PHVs already
533 * get moved to the remaining relation, where they might still be needed.
534 * It might also happen that we skip the removal of some PHVs that could
535 * be removed. However, the overhead of extra PHVs is small compared to
536 * the complexity of analysis needed to remove them.
537 *
538 * Removal is a bit trickier than it might seem: we can remove PHVs that
539 * are used at the target rel and/or in the join qual, but not those that
540 * are used at join partner rels or above the join. It's not that easy to
541 * distinguish PHVs used at partner rels from those used in the join qual,
542 * since they will both have ph_needed sets that are subsets of
543 * joinrelids. However, a PHV used at a partner rel could not have the
544 * target rel in ph_eval_at, so we check that while deciding whether to
545 * remove or just update the PHV. There is no corresponding test in
546 * join_is_removable because it doesn't need to distinguish those cases.
547 */
548 foreach(l, root->placeholder_list)
549 {
551
552 Assert(!is_outer_join || !bms_is_member(relid, phinfo->ph_lateral));
553
554 if (is_outer_join &&
555 bms_is_subset(phinfo->ph_needed, joinrelids) &&
556 bms_is_member(relid, phinfo->ph_eval_at) &&
557 !bms_is_member(ojrelid, phinfo->ph_eval_at))
558 {
559 root->placeholder_list = foreach_delete_current(root->placeholder_list,
560 l);
561 root->placeholder_array[phinfo->phid] = NULL;
562 }
563 else
564 {
565 PlaceHolderVar *phv = phinfo->ph_var;
566
567 phinfo->ph_eval_at = adjust_relid_set(phinfo->ph_eval_at, relid, subst);
568 if (is_outer_join)
569 phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, ojrelid);
570 Assert(!bms_is_empty(phinfo->ph_eval_at)); /* checked previously */
571
572 /* Reduce ph_needed to contain only "relation 0"; see below */
573 if (bms_is_member(0, phinfo->ph_needed))
574 phinfo->ph_needed = bms_make_singleton(0);
575 else
576 phinfo->ph_needed = NULL;
577
578 phv->phrels = adjust_relid_set(phv->phrels, relid, subst);
579 if (is_outer_join)
580 phv->phrels = bms_del_member(phv->phrels, ojrelid);
581 Assert(!bms_is_empty(phv->phrels));
582
583 /*
584 * For self-join removal, update Var nodes within the PHV's
585 * expression to reference the replacement relid, and adjust
586 * ph_lateral for the relid substitution. (For left-join removal,
587 * we're removing rather than replacing, and any surviving PHV
588 * shouldn't reference the removed rel in its expression. Also,
589 * relid can't appear in ph_lateral for outer joins.)
590 */
591 if (is_self_join)
592 {
593 ChangeVarNodesExtended((Node *) phv->phexpr, relid, subst, 0,
595 phinfo->ph_lateral = adjust_relid_set(phinfo->ph_lateral, relid, subst);
596
597 /*
598 * ph_lateral might contain rels mentioned in ph_eval_at after
599 * the replacement, remove them.
600 */
601 phinfo->ph_lateral = bms_difference(phinfo->ph_lateral, phinfo->ph_eval_at);
602 /* ph_lateral might or might not be empty */
603 }
604
605 Assert(phv->phnullingrels == NULL); /* no need to adjust */
606 }
607 }
608
609 /*
610 * Likewise remove references from EquivalenceClasses.
611 *
612 * For self-join removal, the caller has already updated the
613 * EquivalenceClasses, so we can skip this step.
614 */
615 if (is_outer_join)
616 {
617 foreach(l, root->eq_classes)
618 {
620
621 if (bms_is_member(relid, ec->ec_relids) ||
622 bms_is_member(ojrelid, ec->ec_relids))
623 remove_rel_from_eclass(ec, relid, ojrelid);
624 }
625 }
626
627 /*
628 * Finally, we must prepare for the caller to recompute per-Var
629 * attr_needed and per-PlaceHolderVar ph_needed relid sets. These have to
630 * be known accurately, else we may fail to remove other now-removable
631 * joins. Because the caller removes the join clause(s) associated with
632 * the removed join, Vars that were formerly needed may no longer be.
633 *
634 * The actual reconstruction of these relid sets is performed by the
635 * specific caller. Here, we simply clear out the existing attr_needed
636 * sets (we already did this above for ph_needed) to ensure they are
637 * rebuilt from scratch. We can cheat to one small extent: we can avoid
638 * re-examining the targetlist and HAVING qual by preserving "relation 0"
639 * bits from the existing relid sets. This is safe because we'd never
640 * remove such references.
641 *
642 * Additionally, if we are performing self-join elimination, we must
643 * replace references to the removed relid with subst within the
644 * lateral_vars lists.
645 */
646 for (rti = 1; rti < root->simple_rel_array_size; rti++)
647 {
648 RelOptInfo *otherrel = root->simple_rel_array[rti];
649 int attroff;
650
651 /* there may be empty slots corresponding to non-baserel RTEs */
652 if (otherrel == NULL)
653 continue;
654
655 Assert(otherrel->relid == rti); /* sanity check on array */
656
657 for (attroff = otherrel->max_attr - otherrel->min_attr;
658 attroff >= 0;
659 attroff--)
660 {
661 if (bms_is_member(0, otherrel->attr_needed[attroff]))
662 otherrel->attr_needed[attroff] = bms_make_singleton(0);
663 else
664 otherrel->attr_needed[attroff] = NULL;
665 }
666
667 if (is_self_join)
668 ChangeVarNodesExtended((Node *) otherrel->lateral_vars, relid,
669 subst, 0, replace_relid_callback);
670 }
671}
672
673/*
674 * Remove any references to relid or ojrelid from the RestrictInfo.
675 *
676 * We only bother to clean out bits in the RestrictInfo's various relid sets,
677 * not nullingrel bits in contained Vars and PHVs. (This might have to be
678 * improved sometime.) However, if the RestrictInfo contains an OR clause
679 * we have to also clean up the sub-clauses.
680 */
681static void
682remove_rel_from_restrictinfo(RestrictInfo *rinfo, int relid, int ojrelid)
683{
684 /*
685 * initsplan.c is fairly cavalier about allowing RestrictInfos to share
686 * relid sets with other RestrictInfos, and SpecialJoinInfos too. Make
687 * sure this RestrictInfo has its own relid sets before we modify them.
688 * (In present usage, clause_relids is probably not shared, but
689 * required_relids could be; let's not assume anything.)
690 */
691 rinfo->clause_relids = bms_copy(rinfo->clause_relids);
692 rinfo->clause_relids = bms_del_member(rinfo->clause_relids, relid);
693 rinfo->clause_relids = bms_del_member(rinfo->clause_relids, ojrelid);
694 /* Likewise for required_relids */
696 rinfo->required_relids = bms_del_member(rinfo->required_relids, relid);
697 rinfo->required_relids = bms_del_member(rinfo->required_relids, ojrelid);
698 /* Likewise for incompatible_relids */
702 /* Likewise for outer_relids */
703 rinfo->outer_relids = bms_copy(rinfo->outer_relids);
704 rinfo->outer_relids = bms_del_member(rinfo->outer_relids, relid);
705 rinfo->outer_relids = bms_del_member(rinfo->outer_relids, ojrelid);
706 /* Likewise for left_relids */
707 rinfo->left_relids = bms_copy(rinfo->left_relids);
708 rinfo->left_relids = bms_del_member(rinfo->left_relids, relid);
709 rinfo->left_relids = bms_del_member(rinfo->left_relids, ojrelid);
710 /* Likewise for right_relids */
711 rinfo->right_relids = bms_copy(rinfo->right_relids);
712 rinfo->right_relids = bms_del_member(rinfo->right_relids, relid);
713 rinfo->right_relids = bms_del_member(rinfo->right_relids, ojrelid);
714
715 /* If it's an OR, recurse to clean up sub-clauses */
716 if (restriction_is_or_clause(rinfo))
717 {
718 ListCell *lc;
719
720 Assert(is_orclause(rinfo->orclause));
721 foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
722 {
723 Node *orarg = (Node *) lfirst(lc);
724
725 /* OR arguments should be ANDs or sub-RestrictInfos */
726 if (is_andclause(orarg))
727 {
728 List *andargs = ((BoolExpr *) orarg)->args;
729 ListCell *lc2;
730
731 foreach(lc2, andargs)
732 {
734
735 remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
736 }
737 }
738 else
739 {
741
742 remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
743 }
744 }
745 }
746}
747
748/*
749 * Remove any references to relid or ojrelid from the EquivalenceClass.
750 *
751 * Like remove_rel_from_restrictinfo, we don't worry about cleaning out
752 * any nullingrel bits in contained Vars and PHVs. (This might have to be
753 * improved sometime.) We do need to fix the EC and EM relid sets to ensure
754 * that implied join equalities will be generated at the appropriate join
755 * level(s).
756 */
757static void
758remove_rel_from_eclass(EquivalenceClass *ec, int relid, int ojrelid)
759{
760 ListCell *lc;
761
762 /* Fix up the EC's overall relids */
763 ec->ec_relids = bms_del_member(ec->ec_relids, relid);
764 ec->ec_relids = bms_del_member(ec->ec_relids, ojrelid);
765
766 /*
767 * We don't expect any EC child members to exist at this point. Ensure
768 * that's the case, otherwise, we might be getting asked to do something
769 * this function hasn't been coded for.
770 */
772
773 /*
774 * Fix up the member expressions. Any non-const member that ends with
775 * empty em_relids must be a Var or PHV of the removed relation. We don't
776 * need it anymore, so we can drop it.
777 */
778 foreach(lc, ec->ec_members)
779 {
781
782 if (bms_is_member(relid, cur_em->em_relids) ||
783 bms_is_member(ojrelid, cur_em->em_relids))
784 {
785 Assert(!cur_em->em_is_const);
786 /* em_relids is likely to be shared with some RestrictInfo */
787 cur_em->em_relids = bms_copy(cur_em->em_relids);
788 cur_em->em_relids = bms_del_member(cur_em->em_relids, relid);
789 cur_em->em_relids = bms_del_member(cur_em->em_relids, ojrelid);
790 if (bms_is_empty(cur_em->em_relids))
792 }
793 }
794
795 /* Fix up the source clauses, in case we can re-use them later */
796 foreach(lc, ec->ec_sources)
797 {
798 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
799
800 remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
801 }
802
803 /*
804 * Rather than expend code on fixing up any already-derived clauses, just
805 * drop them. (At this point, any such clauses would be base restriction
806 * clauses, which we'd not need anymore anyway.)
807 */
809}
810
811/*
812 * Remove any occurrences of the target relid from a joinlist structure.
813 *
814 * It's easiest to build a whole new list structure, so we handle it that
815 * way. Efficiency is not a big deal here.
816 *
817 * *nremoved is incremented by the number of occurrences removed (there
818 * should be exactly one, but the caller checks that).
819 */
820static List *
822{
823 List *result = NIL;
824 ListCell *jl;
825
826 foreach(jl, joinlist)
827 {
828 Node *jlnode = (Node *) lfirst(jl);
829
830 if (IsA(jlnode, RangeTblRef))
831 {
832 int varno = ((RangeTblRef *) jlnode)->rtindex;
833
834 if (varno == relid)
835 (*nremoved)++;
836 else
838 }
839 else if (IsA(jlnode, List))
840 {
841 /* Recurse to handle subproblem */
842 List *sublist;
843
845 relid, nremoved);
846 /* Avoid including empty sub-lists in the result */
847 if (sublist)
849 }
850 else
851 {
852 elog(ERROR, "unrecognized joinlist node type: %d",
853 (int) nodeTag(jlnode));
854 }
855 }
856
857 return result;
858}
859
860
861/*
862 * reduce_unique_semijoins
863 * Check for semijoins that can be simplified to plain inner joins
864 * because the inner relation is provably unique for the join clauses.
865 *
866 * Ideally this would happen during reduce_outer_joins, but we don't have
867 * enough information at that point.
868 *
869 * To perform the strength reduction when applicable, we need only delete
870 * the semijoin's SpecialJoinInfo from root->join_info_list. (We don't
871 * bother fixing the join type attributed to it in the query jointree,
872 * since that won't be consulted again.)
873 */
874void
876{
877 ListCell *lc;
878
879 /*
880 * Scan the join_info_list to find semijoins.
881 */
882 foreach(lc, root->join_info_list)
883 {
885 int innerrelid;
886 RelOptInfo *innerrel;
888 List *restrictlist;
889
890 /*
891 * Must be a semijoin to a single baserel, else we aren't going to be
892 * able to do anything with it.
893 */
894 if (sjinfo->jointype != JOIN_SEMI)
895 continue;
896
898 continue;
899
900 innerrel = find_base_rel(root, innerrelid);
901
902 /*
903 * Before we trouble to run generate_join_implied_equalities, make a
904 * quick check to eliminate cases in which we will surely be unable to
905 * prove uniqueness of the innerrel.
906 */
907 if (!rel_supports_distinctness(root, innerrel))
908 continue;
909
910 /* Compute the relid set for the join we are considering */
912 Assert(sjinfo->ojrelid == 0); /* SEMI joins don't have RT indexes */
913
914 /*
915 * Since we're only considering a single-rel RHS, any join clauses it
916 * has must be clauses linking it to the semijoin's min_lefthand. We
917 * can also consider EC-derived join clauses.
918 */
919 restrictlist =
922 sjinfo->min_lefthand,
923 innerrel,
924 NULL),
925 innerrel->joininfo);
926
927 /* Test whether the innerrel is unique for those clauses. */
929 joinrelids, sjinfo->min_lefthand, innerrel,
930 JOIN_SEMI, restrictlist, true))
931 continue;
932
933 /* OK, remove the SpecialJoinInfo from the list. */
934 root->join_info_list = foreach_delete_current(root->join_info_list, lc);
935 }
936}
937
938
939/*
940 * rel_supports_distinctness
941 * Could the relation possibly be proven distinct on some set of columns?
942 *
943 * This is effectively a pre-checking function for rel_is_distinct_for().
944 * It must return true if rel_is_distinct_for() could possibly return true
945 * with this rel, but it should not expend a lot of cycles. The idea is
946 * that callers can avoid doing possibly-expensive processing to compute
947 * rel_is_distinct_for()'s argument lists if the call could not possibly
948 * succeed.
949 */
950static bool
952{
953 /* We only know about baserels ... */
954 if (rel->reloptkind != RELOPT_BASEREL)
955 return false;
956 if (rel->rtekind == RTE_RELATION)
957 {
958 /*
959 * For a plain relation, we only know how to prove uniqueness by
960 * reference to unique indexes. Make sure there's at least one
961 * suitable unique index. It must be immediately enforced, and not a
962 * partial index. (Keep these conditions in sync with
963 * relation_has_unique_index_for!)
964 */
965 ListCell *lc;
966
967 foreach(lc, rel->indexlist)
968 {
970
971 if (ind->unique && ind->immediate && ind->indpred == NIL)
972 return true;
973 }
974 }
975 else if (rel->rtekind == RTE_SUBQUERY)
976 {
977 Query *subquery = root->simple_rte_array[rel->relid]->subquery;
978
979 /* Check if the subquery has any qualities that support distinctness */
980 if (query_supports_distinctness(subquery))
981 return true;
982 }
983 /* We have no proof rules for any other rtekinds. */
984 return false;
985}
986
987/*
988 * rel_is_distinct_for
989 * Does the relation return only distinct rows according to clause_list?
990 *
991 * clause_list is a list of join restriction clauses involving this rel and
992 * some other one. Return true if no two rows emitted by this rel could
993 * possibly join to the same row of the other rel.
994 *
995 * The caller must have already determined that each condition is a
996 * mergejoinable equality with an expression in this relation on one side, and
997 * an expression not involving this relation on the other. The transient
998 * outer_is_left flag is used to identify which side references this relation:
999 * left side if outer_is_left is false, right side if it is true.
1000 *
1001 * Note that the passed-in clause_list may be destructively modified! This
1002 * is OK for current uses, because the clause_list is built by the caller for
1003 * the sole purpose of passing to this function.
1004 *
1005 * (*extra_clauses) to be set to the right sides of baserestrictinfo clauses,
1006 * looking like "x = const" if distinctness is derived from such clauses, not
1007 * joininfo clauses. Pass NULL to the extra_clauses if this value is not
1008 * needed.
1009 */
1010static bool
1012 List **extra_clauses)
1013{
1014 /*
1015 * We could skip a couple of tests here if we assume all callers checked
1016 * rel_supports_distinctness first, but it doesn't seem worth taking any
1017 * risk for.
1018 */
1019 if (rel->reloptkind != RELOPT_BASEREL)
1020 return false;
1021 if (rel->rtekind == RTE_RELATION)
1022 {
1023 /*
1024 * Examine the indexes to see if we have a matching unique index.
1025 * relation_has_unique_index_for automatically adds any usable
1026 * restriction clauses for the rel, so we needn't do that here.
1027 */
1028 if (relation_has_unique_index_for(root, rel, clause_list, extra_clauses))
1029 return true;
1030 }
1031 else if (rel->rtekind == RTE_SUBQUERY)
1032 {
1033 Index relid = rel->relid;
1034 Query *subquery = root->simple_rte_array[relid]->subquery;
1035 List *colnos = NIL;
1036 List *opids = NIL;
1037 ListCell *l;
1038
1039 /*
1040 * Build the argument lists for query_is_distinct_for: a list of
1041 * output column numbers that the query needs to be distinct over, and
1042 * a list of equality operators that the output columns need to be
1043 * distinct according to.
1044 *
1045 * (XXX we are not considering restriction clauses attached to the
1046 * subquery; is that worth doing?)
1047 */
1048 foreach(l, clause_list)
1049 {
1051 Oid op;
1052 Var *var;
1053
1054 /*
1055 * Get the equality operator we need uniqueness according to.
1056 * (This might be a cross-type operator and thus not exactly the
1057 * same operator the subquery would consider; that's all right
1058 * since query_is_distinct_for can resolve such cases.) The
1059 * caller's mergejoinability test should have selected only
1060 * OpExprs.
1061 */
1062 op = castNode(OpExpr, rinfo->clause)->opno;
1063
1064 /* caller identified the inner side for us */
1065 if (rinfo->outer_is_left)
1066 var = (Var *) get_rightop(rinfo->clause);
1067 else
1068 var = (Var *) get_leftop(rinfo->clause);
1069
1070 /*
1071 * We may ignore any RelabelType node above the operand. (There
1072 * won't be more than one, since eval_const_expressions() has been
1073 * applied already.)
1074 */
1075 if (var && IsA(var, RelabelType))
1076 var = (Var *) ((RelabelType *) var)->arg;
1077
1078 /*
1079 * If inner side isn't a Var referencing a subquery output column,
1080 * this clause doesn't help us.
1081 */
1082 if (!var || !IsA(var, Var) ||
1083 var->varno != relid || var->varlevelsup != 0)
1084 continue;
1085
1087 opids = lappend_oid(opids, op);
1088 }
1089
1090 if (query_is_distinct_for(subquery, colnos, opids))
1091 return true;
1092 }
1093 return false;
1094}
1095
1096
1097/*
1098 * query_supports_distinctness - could the query possibly be proven distinct
1099 * on some set of output columns?
1100 *
1101 * This is effectively a pre-checking function for query_is_distinct_for().
1102 * It must return true if query_is_distinct_for() could possibly return true
1103 * with this query, but it should not expend a lot of cycles. The idea is
1104 * that callers can avoid doing possibly-expensive processing to compute
1105 * query_is_distinct_for()'s argument lists if the call could not possibly
1106 * succeed.
1107 */
1108bool
1110{
1111 /* SRFs break distinctness except with DISTINCT, see below */
1112 if (query->hasTargetSRFs && query->distinctClause == NIL)
1113 return false;
1114
1115 /* check for features we can prove distinctness with */
1116 if (query->distinctClause != NIL ||
1117 query->groupClause != NIL ||
1118 query->groupingSets != NIL ||
1119 query->hasAggs ||
1120 query->havingQual ||
1121 query->setOperations)
1122 return true;
1123
1124 return false;
1125}
1126
1127/*
1128 * query_is_distinct_for - does query never return duplicates of the
1129 * specified columns?
1130 *
1131 * query is a not-yet-planned subquery (in current usage, it's always from
1132 * a subquery RTE, which the planner avoids scribbling on).
1133 *
1134 * colnos is an integer list of output column numbers (resno's). We are
1135 * interested in whether rows consisting of just these columns are certain
1136 * to be distinct. "Distinctness" is defined according to whether the
1137 * corresponding upper-level equality operators listed in opids would think
1138 * the values are distinct. (Note: the opids entries could be cross-type
1139 * operators, and thus not exactly the equality operators that the subquery
1140 * would use itself. We use equality_ops_are_compatible() to check
1141 * compatibility. That looks at opfamily membership for index AMs that have
1142 * declared that they support consistent equality semantics within an
1143 * opfamily, and so should give trustworthy answers for all operators that we
1144 * might need to deal with here.)
1145 */
1146bool
1148{
1149 ListCell *l;
1150 Oid opid;
1151
1153
1154 /*
1155 * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
1156 * columns in the DISTINCT clause appear in colnos and operator semantics
1157 * match. This is true even if there are SRFs in the DISTINCT columns or
1158 * elsewhere in the tlist.
1159 */
1160 if (query->distinctClause)
1161 {
1162 foreach(l, query->distinctClause)
1163 {
1166 query->targetList);
1167
1169 if (!OidIsValid(opid) ||
1171 break; /* exit early if no match */
1172 }
1173 if (l == NULL) /* had matches for all? */
1174 return true;
1175 }
1176
1177 /*
1178 * Otherwise, a set-returning function in the query's targetlist can
1179 * result in returning duplicate rows, despite any grouping that might
1180 * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
1181 * columns, it would be safe because they'd be expanded before grouping.
1182 * But it doesn't currently seem worth the effort to check for that.)
1183 */
1184 if (query->hasTargetSRFs)
1185 return false;
1186
1187 /*
1188 * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
1189 * the grouped columns appear in colnos and operator semantics match.
1190 */
1191 if (query->groupClause && !query->groupingSets)
1192 {
1193 foreach(l, query->groupClause)
1194 {
1197 query->targetList);
1198
1200 if (!OidIsValid(opid) ||
1202 break; /* exit early if no match */
1203 }
1204 if (l == NULL) /* had matches for all? */
1205 return true;
1206 }
1207 else if (query->groupingSets)
1208 {
1209 List *gsets;
1210
1211 /*
1212 * If we have grouping sets with expressions, we probably don't have
1213 * uniqueness and analysis would be hard. Punt.
1214 */
1215 if (query->groupClause)
1216 return false;
1217
1218 /*
1219 * If we have no groupClause (therefore no grouping expressions), we
1220 * might have one or many empty grouping sets. If there's just one,
1221 * or if the DISTINCT clause is used on the GROUP BY, then we're
1222 * returning only one row and are certainly unique. But otherwise, we
1223 * know we're certainly not unique.
1224 */
1225 if (query->groupDistinct)
1226 return true;
1227
1228 gsets = expand_grouping_sets(query->groupingSets, false, -1);
1229
1230 return (list_length(gsets) == 1);
1231 }
1232 else
1233 {
1234 /*
1235 * If we have no GROUP BY, but do have aggregates or HAVING, then the
1236 * result is at most one row so it's surely unique, for any operators.
1237 */
1238 if (query->hasAggs || query->havingQual)
1239 return true;
1240 }
1241
1242 /*
1243 * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
1244 * except with ALL.
1245 */
1246 if (query->setOperations)
1247 {
1249
1250 Assert(topop->op != SETOP_NONE);
1251
1252 if (!topop->all)
1253 {
1254 ListCell *lg;
1255
1256 /* We're good if all the nonjunk output columns are in colnos */
1257 lg = list_head(topop->groupClauses);
1258 foreach(l, query->targetList)
1259 {
1262
1263 if (tle->resjunk)
1264 continue; /* ignore resjunk columns */
1265
1266 /* non-resjunk columns should have grouping clauses */
1267 Assert(lg != NULL);
1269 lg = lnext(topop->groupClauses, lg);
1270
1272 if (!OidIsValid(opid) ||
1274 break; /* exit early if no match */
1275 }
1276 if (l == NULL) /* had matches for all? */
1277 return true;
1278 }
1279 }
1280
1281 /*
1282 * XXX Are there any other cases in which we can easily see the result
1283 * must be distinct?
1284 *
1285 * If you do add more smarts to this function, be sure to update
1286 * query_supports_distinctness() to match.
1287 */
1288
1289 return false;
1290}
1291
1292/*
1293 * distinct_col_search - subroutine for query_is_distinct_for
1294 *
1295 * If colno is in colnos, return the corresponding element of opids,
1296 * else return InvalidOid. (Ordinarily colnos would not contain duplicates,
1297 * but if it does, we arbitrarily select the first match.)
1298 */
1299static Oid
1301{
1302 ListCell *lc1,
1303 *lc2;
1304
1306 {
1307 if (colno == lfirst_int(lc1))
1308 return lfirst_oid(lc2);
1309 }
1310 return InvalidOid;
1311}
1312
1313
1314/*
1315 * innerrel_is_unique
1316 * Check if the innerrel provably contains at most one tuple matching any
1317 * tuple from the outerrel, based on join clauses in the 'restrictlist'.
1318 *
1319 * We need an actual RelOptInfo for the innerrel, but it's sufficient to
1320 * identify the outerrel by its Relids. This asymmetry supports use of this
1321 * function before joinrels have been built. (The caller is expected to
1322 * also supply the joinrelids, just to save recalculating that.)
1323 *
1324 * The proof must be made based only on clauses that will be "joinquals"
1325 * rather than "otherquals" at execution. For an inner join there's no
1326 * difference; but if the join is outer, we must ignore pushed-down quals,
1327 * as those will become "otherquals". Note that this means the answer might
1328 * vary depending on whether IS_OUTER_JOIN(jointype); since we cache the
1329 * answer without regard to that, callers must take care not to call this
1330 * with jointypes that would be classified differently by IS_OUTER_JOIN().
1331 *
1332 * The actual proof is undertaken by is_innerrel_unique_for(); this function
1333 * is a frontend that is mainly concerned with caching the answers.
1334 * In particular, the force_cache argument allows overriding the internal
1335 * heuristic about whether to cache negative answers; it should be "true"
1336 * if making an inquiry that is not part of the normal bottom-up join search
1337 * sequence.
1338 */
1339bool
1342 Relids outerrelids,
1343 RelOptInfo *innerrel,
1344 JoinType jointype,
1345 List *restrictlist,
1346 bool force_cache)
1347{
1348 return innerrel_is_unique_ext(root, joinrelids, outerrelids, innerrel,
1349 jointype, restrictlist, force_cache, NULL);
1350}
1351
1352/*
1353 * innerrel_is_unique_ext
1354 * Do the same as innerrel_is_unique(), but also set to (*extra_clauses)
1355 * additional clauses from a baserestrictinfo list used to prove the
1356 * uniqueness.
1357 *
1358 * A non-NULL extra_clauses indicates that we're checking for self-join and
1359 * correspondingly dealing with filtered clauses.
1360 */
1361bool
1364 Relids outerrelids,
1365 RelOptInfo *innerrel,
1366 JoinType jointype,
1367 List *restrictlist,
1368 bool force_cache,
1369 List **extra_clauses)
1370{
1372 ListCell *lc;
1374 List *outer_exprs = NIL;
1375 bool self_join = (extra_clauses != NULL);
1376
1377 /* Certainly can't prove uniqueness when there are no joinclauses */
1378 if (restrictlist == NIL)
1379 return false;
1380
1381 /*
1382 * Make a quick check to eliminate cases in which we will surely be unable
1383 * to prove uniqueness of the innerrel.
1384 */
1385 if (!rel_supports_distinctness(root, innerrel))
1386 return false;
1387
1388 /*
1389 * Query the cache to see if we've managed to prove that innerrel is
1390 * unique for any subset of this outerrel. For non-self-join search, we
1391 * don't need an exact match, as extra outerrels can't make the innerrel
1392 * any less unique (or more formally, the restrictlist for a join to a
1393 * superset outerrel must be a superset of the conditions we successfully
1394 * used before). For self-join search, we require an exact match of
1395 * outerrels because we need extra clauses to be valid for our case. Also,
1396 * for self-join checking we've filtered the clauses list. Thus, we can
1397 * match only the result cached for a self-join search for another
1398 * self-join check.
1399 */
1400 foreach(lc, innerrel->unique_for_rels)
1401 {
1403
1404 if ((!self_join && bms_is_subset(uniqueRelInfo->outerrelids, outerrelids)) ||
1405 (self_join && bms_equal(uniqueRelInfo->outerrelids, outerrelids) &&
1406 uniqueRelInfo->self_join))
1407 {
1408 if (extra_clauses)
1409 *extra_clauses = uniqueRelInfo->extra_clauses;
1410 return true; /* Success! */
1411 }
1412 }
1413
1414 /*
1415 * Conversely, we may have already determined that this outerrel, or some
1416 * superset thereof, cannot prove this innerrel to be unique.
1417 */
1418 foreach(lc, innerrel->non_unique_for_rels)
1419 {
1420 Relids unique_for_rels = (Relids) lfirst(lc);
1421
1422 if (bms_is_subset(outerrelids, unique_for_rels))
1423 return false;
1424 }
1425
1426 /* No cached information, so try to make the proof. */
1427 if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1428 jointype, restrictlist,
1429 self_join ? &outer_exprs : NULL))
1430 {
1431 /*
1432 * Cache the positive result for future probes, being sure to keep it
1433 * in the planner_cxt even if we are working in GEQO.
1434 *
1435 * Note: one might consider trying to isolate the minimal subset of
1436 * the outerrels that proved the innerrel unique. But it's not worth
1437 * the trouble, because the planner builds up joinrels incrementally
1438 * and so we'll see the minimally sufficient outerrels before any
1439 * supersets of them anyway.
1440 */
1441 old_context = MemoryContextSwitchTo(root->planner_cxt);
1443 uniqueRelInfo->outerrelids = bms_copy(outerrelids);
1444 uniqueRelInfo->self_join = self_join;
1445 uniqueRelInfo->extra_clauses = outer_exprs;
1446 innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1449
1450 if (extra_clauses)
1451 *extra_clauses = outer_exprs;
1452 return true; /* Success! */
1453 }
1454 else
1455 {
1456 /*
1457 * None of the join conditions for outerrel proved innerrel unique, so
1458 * we can safely reject this outerrel or any subset of it in future
1459 * checks.
1460 *
1461 * However, in normal planning mode, caching this knowledge is totally
1462 * pointless; it won't be queried again, because we build up joinrels
1463 * from smaller to larger. It's only useful when using GEQO or
1464 * another planner extension that attempts planning multiple times.
1465 *
1466 * Also, allow callers to override that heuristic and force caching;
1467 * that's useful for reduce_unique_semijoins, which calls here before
1468 * the normal join search starts.
1469 */
1470 if (force_cache || root->assumeReplanning)
1471 {
1472 old_context = MemoryContextSwitchTo(root->planner_cxt);
1473 innerrel->non_unique_for_rels =
1474 lappend(innerrel->non_unique_for_rels,
1475 bms_copy(outerrelids));
1477 }
1478
1479 return false;
1480 }
1481}
1482
1483/*
1484 * is_innerrel_unique_for
1485 * Check if the innerrel provably contains at most one tuple matching any
1486 * tuple from the outerrel, based on join clauses in the 'restrictlist'.
1487 */
1488static bool
1491 Relids outerrelids,
1492 RelOptInfo *innerrel,
1493 JoinType jointype,
1494 List *restrictlist,
1495 List **extra_clauses)
1496{
1497 List *clause_list = NIL;
1498 ListCell *lc;
1499
1500 /*
1501 * Search for mergejoinable clauses that constrain the inner rel against
1502 * the outer rel. If an operator is mergejoinable then it behaves like
1503 * equality for some btree opclass, so it's what we want. The
1504 * mergejoinability test also eliminates clauses containing volatile
1505 * functions, which we couldn't depend on.
1506 */
1507 foreach(lc, restrictlist)
1508 {
1510
1511 /*
1512 * As noted above, if it's a pushed-down clause and we're at an outer
1513 * join, we can't use it.
1514 */
1515 if (IS_OUTER_JOIN(jointype) &&
1517 continue;
1518
1519 /* Ignore if it's not a mergejoinable clause */
1520 if (!restrictinfo->can_join ||
1521 restrictinfo->mergeopfamilies == NIL)
1522 continue; /* not mergejoinable */
1523
1524 /*
1525 * Check if the clause has the form "outer op inner" or "inner op
1526 * outer", and if so mark which side is inner.
1527 */
1528 if (!clause_sides_match_join(restrictinfo, outerrelids,
1529 innerrel->relids))
1530 continue; /* no good for these input relations */
1531
1532 /* OK, add to the list */
1534 }
1535
1536 /* Let rel_is_distinct_for() do the hard work */
1537 return rel_is_distinct_for(root, innerrel, clause_list, extra_clauses);
1538}
1539
1540/*
1541 * Update EC members to point to the remaining relation instead of the removed
1542 * one, removing duplicates.
1543 *
1544 * Restriction clauses for base relations are already distributed to
1545 * the respective baserestrictinfo lists (see
1546 * generate_implied_equalities_for_column). The above code has already processed
1547 * this list and updated these clauses to reference the remaining
1548 * relation, so that we can skip them here based on their relids.
1549 *
1550 * Likewise, we have already processed the join clauses that join the
1551 * removed relation to the remaining one.
1552 *
1553 * Finally, there might be join clauses tying the removed relation to
1554 * some third relation. We can't just delete the source clauses and
1555 * regenerate them from the EC because the corresponding equality
1556 * operators might be missing (see the handling of ec_broken).
1557 * Therefore, we will update the references in the source clauses.
1558 *
1559 * Derived clauses can be generated again, so it is simpler just to
1560 * delete them.
1561 */
1562static void
1563update_eclasses(EquivalenceClass *ec, int from, int to)
1564{
1565 List *new_members = NIL;
1566 List *new_sources = NIL;
1567
1568 /*
1569 * We don't expect any EC child members to exist at this point. Ensure
1570 * that's the case, otherwise, we might be getting asked to do something
1571 * this function hasn't been coded for.
1572 */
1573 Assert(ec->ec_childmembers == NULL);
1574
1576 {
1577 bool is_redundant = false;
1578
1579 if (!bms_is_member(from, em->em_relids))
1580 {
1582 continue;
1583 }
1584
1585 em->em_relids = adjust_relid_set(em->em_relids, from, to);
1586 em->em_jdomain->jd_relids = adjust_relid_set(em->em_jdomain->jd_relids, from, to);
1587
1588 /* We only process inner joins */
1589 ChangeVarNodesExtended((Node *) em->em_expr, from, to, 0,
1591
1593 {
1594 if (!equal(em->em_relids, other->em_relids))
1595 continue;
1596
1597 if (equal(em->em_expr, other->em_expr))
1598 {
1599 is_redundant = true;
1600 break;
1601 }
1602 }
1603
1604 if (!is_redundant)
1606 }
1607
1608 list_free(ec->ec_members);
1609 ec->ec_members = new_members;
1610
1612
1613 /* Update EC source expressions */
1615 {
1616 bool is_redundant = false;
1617
1618 if (!bms_is_member(from, rinfo->required_relids))
1619 {
1621 continue;
1622 }
1623
1624 ChangeVarNodesExtended((Node *) rinfo, from, to, 0,
1626
1627 /*
1628 * After switching the clause to the remaining relation, check it for
1629 * redundancy with existing ones. We don't have to check for
1630 * redundancy with derived clauses, because we've just deleted them.
1631 */
1633 {
1634 if (!equal(rinfo->clause_relids, other->clause_relids))
1635 continue;
1636
1637 if (equal(rinfo->clause, other->clause))
1638 {
1639 is_redundant = true;
1640 break;
1641 }
1642 }
1643
1644 if (!is_redundant)
1646 }
1647
1648 list_free(ec->ec_sources);
1649 ec->ec_sources = new_sources;
1650 ec->ec_relids = adjust_relid_set(ec->ec_relids, from, to);
1651}
1652
1653/*
1654 * "Logically" compares two RestrictInfo's ignoring the 'rinfo_serial' field,
1655 * which makes almost every RestrictInfo unique. This type of comparison is
1656 * useful when removing duplicates while moving RestrictInfo's from removed
1657 * relation to remaining relation during self-join elimination.
1658 *
1659 * XXX: In the future, we might remove the 'rinfo_serial' field completely and
1660 * get rid of this function.
1661 */
1662static bool
1664{
1665 int saved_rinfo_serial = a->rinfo_serial;
1666 bool result;
1667
1668 a->rinfo_serial = b->rinfo_serial;
1669 result = equal(a, b);
1670 a->rinfo_serial = saved_rinfo_serial;
1671
1672 return result;
1673}
1674
1675/*
1676 * This function adds all non-redundant clauses to the keeping relation
1677 * during self-join elimination. That is a contradictory operation. On the
1678 * one hand, we reduce the length of the `restrict` lists, which can
1679 * impact planning or executing time. Additionally, we improve the
1680 * accuracy of cardinality estimation. On the other hand, it is one more
1681 * place that can make planning time much longer in specific cases. It
1682 * would have been better to avoid calling the equal() function here, but
1683 * it's the only way to detect duplicated inequality expressions.
1684 *
1685 * (*keep_rinfo_list) is given by pointer because it might be altered by
1686 * distribute_restrictinfo_to_rels().
1687 */
1688static void
1693{
1695 {
1696 bool is_redundant = false;
1697
1698 Assert(!bms_is_member(removed_relid, rinfo->required_relids));
1699
1701 {
1702 if (!bms_equal(src->clause_relids, rinfo->clause_relids))
1703 /* Can't compare trivially different clauses */
1704 continue;
1705
1706 if (src == rinfo ||
1707 (rinfo->parent_ec != NULL &&
1708 src->parent_ec == rinfo->parent_ec) ||
1710 {
1711 is_redundant = true;
1712 break;
1713 }
1714 }
1715 if (!is_redundant)
1717 }
1718}
1719
1720/*
1721 * A custom callback for ChangeVarNodesExtended() providing Self-join
1722 * elimination (SJE) related functionality
1723 *
1724 * SJE needs to skip the RangeTblRef node type. During SJE's last
1725 * step, remove_rel_from_joinlist() removes remaining RangeTblRefs
1726 * with target relid. If ChangeVarNodes() replaces the target relid
1727 * before, remove_rel_from_joinlist() would fail to identify the nodes
1728 * to delete.
1729 *
1730 * SJE also needs to change the relids within RestrictInfo's.
1731 */
1732static bool
1734{
1735 if (IsA(node, RangeTblRef))
1736 {
1737 return true;
1738 }
1739 else if (IsA(node, RestrictInfo))
1740 {
1741 RestrictInfo *rinfo = (RestrictInfo *) node;
1742 int relid = -1;
1743 bool is_req_equal =
1744 (rinfo->required_relids == rinfo->clause_relids);
1746 (bms_membership(rinfo->clause_relids) == BMS_MULTIPLE);
1747
1748 /*
1749 * Recurse down into clauses if the target relation is present in
1750 * clause_relids or required_relids. We must check required_relids
1751 * because the relation not present in clause_relids might still be
1752 * present somewhere in orclause.
1753 */
1754 if (bms_is_member(context->rt_index, rinfo->clause_relids) ||
1755 bms_is_member(context->rt_index, rinfo->required_relids))
1756 {
1758
1759 ChangeVarNodesWalkExpression((Node *) rinfo->clause, context);
1760 ChangeVarNodesWalkExpression((Node *) rinfo->orclause, context);
1761
1762 new_clause_relids = adjust_relid_set(rinfo->clause_relids,
1763 context->rt_index,
1764 context->new_index);
1765
1766 /*
1767 * Incrementally adjust num_base_rels based on the change of
1768 * clause_relids, which could contain both base relids and
1769 * outer-join relids. This operation is legal until we remove
1770 * only baserels.
1771 */
1772 rinfo->num_base_rels -= bms_num_members(rinfo->clause_relids) -
1774
1775 rinfo->clause_relids = new_clause_relids;
1776 rinfo->left_relids =
1777 adjust_relid_set(rinfo->left_relids, context->rt_index, context->new_index);
1778 rinfo->right_relids =
1779 adjust_relid_set(rinfo->right_relids, context->rt_index, context->new_index);
1780 }
1781
1782 if (is_req_equal)
1783 rinfo->required_relids = rinfo->clause_relids;
1784 else
1785 rinfo->required_relids =
1786 adjust_relid_set(rinfo->required_relids, context->rt_index, context->new_index);
1787
1788 rinfo->outer_relids =
1789 adjust_relid_set(rinfo->outer_relids, context->rt_index, context->new_index);
1790 rinfo->incompatible_relids =
1791 adjust_relid_set(rinfo->incompatible_relids, context->rt_index, context->new_index);
1792
1793 if (rinfo->mergeopfamilies &&
1794 bms_get_singleton_member(rinfo->clause_relids, &relid) &&
1796 relid == context->new_index && IsA(rinfo->clause, OpExpr))
1797 {
1798 Expr *leftOp;
1799 Expr *rightOp;
1800
1801 leftOp = (Expr *) get_leftop(rinfo->clause);
1802 rightOp = (Expr *) get_rightop(rinfo->clause);
1803
1804 /*
1805 * For self-join elimination, changing varnos could transform
1806 * "t1.a = t2.a" into "t1.a = t1.a". That is always true as long
1807 * as "t1.a" is not null. We use equal() to check for such a
1808 * case, and then we replace the qual with a check for not null
1809 * (NullTest).
1810 */
1811 if (leftOp != NULL && equal(leftOp, rightOp))
1812 {
1814
1815 ntest->arg = leftOp;
1816 ntest->nulltesttype = IS_NOT_NULL;
1817 ntest->argisrow = false;
1818 ntest->location = -1;
1819 rinfo->clause = (Expr *) ntest;
1820 rinfo->mergeopfamilies = NIL;
1821 rinfo->left_em = NULL;
1822 rinfo->right_em = NULL;
1823 }
1824 Assert(rinfo->orclause == NULL);
1825 }
1826 return true;
1827 }
1828
1829 return false;
1830}
1831
1832/*
1833 * Remove a relation after we have proven that it participates only in an
1834 * unneeded unique self-join.
1835 *
1836 * Replace any links in planner info structures.
1837 *
1838 * Transfer join and restriction clauses from the removed relation to the
1839 * remaining one. We change the Vars of the clause to point to the
1840 * remaining relation instead of the removed one. The clauses that require
1841 * a subset of joinrelids become restriction clauses of the remaining
1842 * relation, and others remain join clauses. We append them to
1843 * baserestrictinfo and joininfo, respectively, trying not to introduce
1844 * duplicates.
1845 *
1846 * We also have to process the 'joinclauses' list here, because it
1847 * contains EC-derived join clauses which must become filter clauses. It
1848 * is not enough to just correct the ECs because the EC-derived
1849 * restrictions are generated before join removal (see
1850 * generate_base_implied_equalities).
1851 *
1852 * NOTE: Remember to keep the code in sync with PlannerInfo to be sure all
1853 * cached relids and relid bitmapsets can be correctly cleaned during the
1854 * self-join elimination procedure.
1855 */
1856static void
1859 List *restrictlist)
1860{
1861 List *joininfos;
1862 ListCell *lc;
1863 int i;
1866
1867 Assert(toKeep->relid > 0);
1868 Assert(toRemove->relid > 0);
1869
1870 /*
1871 * Replace the index of the removing table with the keeping one. The
1872 * technique of removing/distributing restrictinfo is used here to attach
1873 * just appeared (for keeping relation) join clauses and avoid adding
1874 * duplicates of those that already exist in the joininfo list.
1875 */
1876 joininfos = list_copy(toRemove->joininfo);
1878 {
1879 remove_join_clause_from_rels(root, rinfo, rinfo->required_relids);
1880 ChangeVarNodesExtended((Node *) rinfo, toRemove->relid, toKeep->relid,
1882
1883 if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
1885 else
1887 }
1888
1889 /*
1890 * Concatenate restrictlist to the list of base restrictions of the
1891 * removing table just to simplify the replacement procedure: all of them
1892 * weren't connected to any keeping relations and need to be added to some
1893 * rels.
1894 */
1895 toRemove->baserestrictinfo = list_concat(toRemove->baserestrictinfo,
1896 restrictlist);
1897 foreach_node(RestrictInfo, rinfo, toRemove->baserestrictinfo)
1898 {
1899 ChangeVarNodesExtended((Node *) rinfo, toRemove->relid, toKeep->relid,
1901
1902 if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
1904 else
1906 }
1907
1908 /*
1909 * Now, add all non-redundant clauses to the keeping relation.
1910 */
1912 &toKeep->baserestrictinfo, toRemove->relid);
1914 &toKeep->joininfo, toRemove->relid);
1915
1918
1919 /*
1920 * Arrange equivalence classes, mentioned removing a table, with the
1921 * keeping one: varno of removing table should be replaced in members and
1922 * sources lists. Also, remove duplicated elements if this replacement
1923 * procedure created them.
1924 */
1925 i = -1;
1926 while ((i = bms_next_member(toRemove->eclass_indexes, i)) >= 0)
1927 {
1928 EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
1929
1930 update_eclasses(ec, toRemove->relid, toKeep->relid);
1931 toKeep->eclass_indexes = bms_add_member(toKeep->eclass_indexes, i);
1932 }
1933
1934 /*
1935 * Transfer the targetlist and attr_needed flags.
1936 */
1937
1938 foreach(lc, toRemove->reltarget->exprs)
1939 {
1940 Node *node = lfirst(lc);
1941
1942 ChangeVarNodesExtended(node, toRemove->relid, toKeep->relid, 0,
1944 if (!list_member(toKeep->reltarget->exprs, node))
1945 toKeep->reltarget->exprs = lappend(toKeep->reltarget->exprs, node);
1946 }
1947
1948 for (i = toKeep->min_attr; i <= toKeep->max_attr; i++)
1949 {
1950 int attno = i - toKeep->min_attr;
1951
1952 toRemove->attr_needed[attno] = adjust_relid_set(toRemove->attr_needed[attno],
1953 toRemove->relid, toKeep->relid);
1954 toKeep->attr_needed[attno] = bms_add_members(toKeep->attr_needed[attno],
1955 toRemove->attr_needed[attno]);
1956 }
1957
1958 /*
1959 * If the removed relation has a row mark, transfer it to the remaining
1960 * one.
1961 *
1962 * If both rels have row marks, just keep the one corresponding to the
1963 * remaining relation because we verified earlier that they have the same
1964 * strength.
1965 */
1966 if (rmark)
1967 {
1968 if (kmark)
1969 {
1970 Assert(kmark->markType == rmark->markType);
1971
1972 root->rowMarks = list_delete_ptr(root->rowMarks, rmark);
1973 }
1974 else
1975 {
1976 /* Shouldn't have inheritance children here. */
1977 Assert(rmark->rti == rmark->prti);
1978
1979 rmark->rti = rmark->prti = toKeep->relid;
1980 }
1981 }
1982
1983 /*
1984 * Replace varno in all the query structures, except nodes RangeTblRef
1985 * otherwise later remove_rel_from_joinlist will yield errors.
1986 */
1987 ChangeVarNodesExtended((Node *) root->parse, toRemove->relid, toKeep->relid,
1989
1990 /* Replace links in the planner info */
1992
1993 /* Replace varno in the fully-processed targetlist */
1994 ChangeVarNodesExtended((Node *) root->processed_tlist, toRemove->relid,
1995 toKeep->relid, 0, replace_relid_callback);
1996
1997 adjust_relid_set(root->all_result_relids, toRemove->relid, toKeep->relid);
1998 adjust_relid_set(root->leaf_result_relids, toRemove->relid, toKeep->relid);
1999
2000 /*
2001 * There may be references to the rel in root->fkey_list, but if so,
2002 * match_foreign_keys_to_quals() will get rid of them.
2003 */
2004
2005 /*
2006 * Finally, remove the rel from the baserel array to prevent it from being
2007 * referenced again. (We can't do this earlier because
2008 * remove_join_clause_from_rels will touch it.)
2009 */
2010 root->simple_rel_array[toRemove->relid] = NULL;
2011 root->simple_rte_array[toRemove->relid] = NULL;
2012
2013 /* And nuke the RelOptInfo, just in case there's another access path. */
2014 pfree(toRemove);
2015
2016
2017 /*
2018 * Now repeat construction of attr_needed bits coming from all other
2019 * sources.
2020 */
2025}
2026
2027/*
2028 * split_selfjoin_quals
2029 * Processes 'joinquals' by building two lists: one containing the quals
2030 * where the columns/exprs are on either side of the join match and
2031 * another one containing the remaining quals.
2032 *
2033 * 'joinquals' must only contain quals for a RTE_RELATION being joined to
2034 * itself.
2035 */
2036static void
2038 List **otherjoinquals, int from, int to)
2039{
2040 List *sjoinquals = NIL;
2041 List *ojoinquals = NIL;
2042
2044 {
2045 OpExpr *expr;
2046 Node *leftexpr;
2047 Node *rightexpr;
2048
2049 /* In general, clause looks like F(arg1) = G(arg2) */
2050 if (!rinfo->mergeopfamilies ||
2051 bms_num_members(rinfo->clause_relids) != 2 ||
2052 bms_membership(rinfo->left_relids) != BMS_SINGLETON ||
2053 bms_membership(rinfo->right_relids) != BMS_SINGLETON)
2054 {
2055 ojoinquals = lappend(ojoinquals, rinfo);
2056 continue;
2057 }
2058
2059 expr = (OpExpr *) rinfo->clause;
2060
2061 if (!IsA(expr, OpExpr) || list_length(expr->args) != 2)
2062 {
2063 ojoinquals = lappend(ojoinquals, rinfo);
2064 continue;
2065 }
2066
2067 leftexpr = get_leftop(rinfo->clause);
2068 rightexpr = copyObject(get_rightop(rinfo->clause));
2069
2071 leftexpr = (Node *) ((RelabelType *) leftexpr)->arg;
2073 rightexpr = (Node *) ((RelabelType *) rightexpr)->arg;
2074
2075 /*
2076 * Quite an expensive operation, narrowing the use case. For example,
2077 * when we have cast of the same var to different (but compatible)
2078 * types.
2079 */
2081 bms_singleton_member(rinfo->right_relids),
2082 bms_singleton_member(rinfo->left_relids), 0,
2084
2085 if (equal(leftexpr, rightexpr))
2086 sjoinquals = lappend(sjoinquals, rinfo);
2087 else
2088 ojoinquals = lappend(ojoinquals, rinfo);
2089 }
2090
2093}
2094
2095/*
2096 * Check for a case when uniqueness is at least partly derived from a
2097 * baserestrictinfo clause. In this case, we have a chance to return only
2098 * one row (if such clauses on both sides of SJ are equal) or nothing (if they
2099 * are different).
2100 */
2101static bool
2103 Index relid)
2104{
2106 {
2107 Expr *clause;
2108 Node *iclause;
2109 Node *c1;
2110 bool matched = false;
2111
2112 Assert(outer->relid > 0 && relid > 0);
2113
2114 /* Only filters like f(R.x1,...,R.xN) == expr we should consider. */
2115 Assert(bms_is_empty(rinfo->left_relids) ^
2116 bms_is_empty(rinfo->right_relids));
2117
2118 clause = (Expr *) copyObject(rinfo->clause);
2119 ChangeVarNodesExtended((Node *) clause, relid, outer->relid, 0,
2121
2122 iclause = bms_is_empty(rinfo->left_relids) ? get_rightop(clause) :
2123 get_leftop(clause);
2124 c1 = bms_is_empty(rinfo->left_relids) ? get_leftop(clause) :
2125 get_rightop(clause);
2126
2127 /*
2128 * Compare these left and right sides with the corresponding sides of
2129 * the outer's filters. If no one is detected - return immediately.
2130 */
2132 {
2133 Node *oclause;
2134 Node *c2;
2135
2136 if (orinfo->mergeopfamilies == NIL)
2137 /* Don't consider clauses that aren't similar to 'F(X)=G(Y)' */
2138 continue;
2139
2140 Assert(is_opclause(orinfo->clause));
2141
2142 oclause = bms_is_empty(orinfo->left_relids) ?
2143 get_rightop(orinfo->clause) : get_leftop(orinfo->clause);
2144 c2 = (bms_is_empty(orinfo->left_relids) ?
2145 get_leftop(orinfo->clause) : get_rightop(orinfo->clause));
2146
2147 if (equal(iclause, oclause) && equal(c1, c2))
2148 {
2149 matched = true;
2150 break;
2151 }
2152 }
2153
2154 if (!matched)
2155 return false;
2156 }
2157
2158 return true;
2159}
2160
2161/*
2162 * Find and remove unique self-joins in a group of base relations that have
2163 * the same Oid.
2164 *
2165 * Returns a set of relids that were removed.
2166 */
2167static Relids
2169{
2170 Relids result = NULL;
2171 int k; /* Index of kept relation */
2172 int r = -1; /* Index of removed relation */
2173
2174 while ((r = bms_next_member(relids, r)) > 0)
2175 {
2176 RelOptInfo *rrel = root->simple_rel_array[r];
2177
2178 k = r;
2179
2180 while ((k = bms_next_member(relids, k)) > 0)
2181 {
2183 RelOptInfo *krel = root->simple_rel_array[k];
2184 List *restrictlist;
2187 ListCell *lc;
2188 bool jinfo_check = true;
2191 List *uclauses = NIL;
2192
2193 /* A sanity check: the relations have the same Oid. */
2194 Assert(root->simple_rte_array[k]->relid ==
2195 root->simple_rte_array[r]->relid);
2196
2197 /*
2198 * It is impossible to eliminate the join of two relations if they
2199 * belong to different rules of order. Otherwise, the planner
2200 * can't find any variants of the correct query plan.
2201 */
2202 foreach(lc, root->join_info_list)
2203 {
2205
2206 if ((bms_is_member(k, info->syn_lefthand) ^
2207 bms_is_member(r, info->syn_lefthand)) ||
2208 (bms_is_member(k, info->syn_righthand) ^
2209 bms_is_member(r, info->syn_righthand)))
2210 {
2211 jinfo_check = false;
2212 break;
2213 }
2214 }
2215 if (!jinfo_check)
2216 continue;
2217
2218 /*
2219 * Check Row Marks equivalence. We can't remove the join if the
2220 * relations have row marks of different strength (e.g., one is
2221 * locked FOR UPDATE, and another just has ROW_MARK_REFERENCE for
2222 * EvalPlanQual rechecking).
2223 */
2224 foreach(lc, root->rowMarks)
2225 {
2227
2228 if (rowMark->rti == r)
2229 {
2230 Assert(rmark == NULL);
2231 rmark = rowMark;
2232 }
2233 else if (rowMark->rti == k)
2234 {
2235 Assert(kmark == NULL);
2236 kmark = rowMark;
2237 }
2238
2239 if (kmark && rmark)
2240 break;
2241 }
2242 if (kmark && rmark && kmark->markType != rmark->markType)
2243 continue;
2244
2245 /*
2246 * We only deal with base rels here, so their relids bitset
2247 * contains only one member -- their relid.
2248 */
2251
2252 /*
2253 * PHVs should not impose any constraints on removing self-joins.
2254 */
2255
2256 /*
2257 * At this stage, joininfo lists of inner and outer can contain
2258 * only clauses required for a superior outer join that can't
2259 * influence this optimization. So, we can avoid to call the
2260 * build_joinrel_restrictlist() routine.
2261 */
2263 rrel->relids,
2264 krel, NULL);
2265 if (restrictlist == NIL)
2266 continue;
2267
2268 /*
2269 * Process restrictlist to separate the self-join quals from the
2270 * other quals. e.g., "x = x" goes to selfjoinquals and "a = b" to
2271 * otherjoinquals.
2272 */
2273 split_selfjoin_quals(root, restrictlist, &selfjoinquals,
2274 &otherjoinquals, rrel->relid, krel->relid);
2275
2276 Assert(list_length(restrictlist) ==
2278
2279 /*
2280 * To enable SJE for the only degenerate case without any self
2281 * join clauses at all, add baserestrictinfo to this list. The
2282 * degenerate case works only if both sides have the same clause.
2283 * So doesn't matter which side to add.
2284 */
2285 selfjoinquals = list_concat(selfjoinquals, krel->baserestrictinfo);
2286
2287 /*
2288 * Determine if the rrel can duplicate outer rows. We must bypass
2289 * the unique rel cache here since we're possibly using a subset
2290 * of join quals. We can use 'force_cache' == true when all join
2291 * quals are self-join quals. Otherwise, we could end up putting
2292 * false negatives in the cache.
2293 */
2297 &uclauses))
2298 continue;
2299
2300 /*
2301 * 'uclauses' is the copy of outer->baserestrictinfo that are
2302 * associated with an index. We proved by matching selfjoinquals
2303 * to a unique index that the outer relation has at most one
2304 * matching row for each inner row. Sometimes that is not enough.
2305 * e.g. "WHERE s1.b = s2.b AND s1.a = 1 AND s2.a = 2" when the
2306 * unique index is (a,b). Having non-empty uclauses, we must
2307 * validate that the inner baserestrictinfo contains the same
2308 * expressions, or we won't match the same row on each side of the
2309 * join.
2310 */
2311 if (!match_unique_clauses(root, rrel, uclauses, krel->relid))
2312 continue;
2313
2314 /*
2315 * Remove rrel RelOptInfo from the planner structures and the
2316 * corresponding row mark.
2317 */
2318 remove_self_join_rel(root, kmark, rmark, krel, rrel, restrictlist);
2319
2321
2322 /* We have removed the outer relation, try the next one. */
2323 break;
2324 }
2325 }
2326
2327 return result;
2328}
2329
2330/*
2331 * Gather indexes of base relations from the joinlist and try to eliminate self
2332 * joins.
2333 */
2334static Relids
2336{
2337 ListCell *jl;
2338 Relids relids = NULL;
2340 int i;
2341 int j;
2342 int numRels;
2343
2344 /* Collect indexes of base relations of the join tree */
2345 foreach(jl, joinlist)
2346 {
2347 Node *jlnode = (Node *) lfirst(jl);
2348
2349 if (IsA(jlnode, RangeTblRef))
2350 {
2351 int varno = ((RangeTblRef *) jlnode)->rtindex;
2352 RangeTblEntry *rte = root->simple_rte_array[varno];
2353
2354 /*
2355 * We only consider ordinary relations as candidates to be
2356 * removed, and these relations should not have TABLESAMPLE
2357 * clauses specified. Removing a relation with TABLESAMPLE clause
2358 * could potentially change the syntax of the query. Because of
2359 * UPDATE/DELETE EPQ mechanism, currently Query->resultRelation or
2360 * Query->mergeTargetRelation associated rel cannot be eliminated.
2361 */
2362 if (rte->rtekind == RTE_RELATION &&
2363 rte->relkind == RELKIND_RELATION &&
2364 rte->tablesample == NULL &&
2365 varno != root->parse->resultRelation &&
2366 varno != root->parse->mergeTargetRelation)
2367 {
2368 Assert(!bms_is_member(varno, relids));
2369 relids = bms_add_member(relids, varno);
2370 }
2371 }
2372 else if (IsA(jlnode, List))
2373 {
2374 /* Recursively go inside the sub-joinlist */
2376 toRemove);
2377 }
2378 else
2379 elog(ERROR, "unrecognized joinlist node type: %d",
2380 (int) nodeTag(jlnode));
2381 }
2382
2383 numRels = bms_num_members(relids);
2384
2385 /* Need at least two relations for the join */
2386 if (numRels < 2)
2387 return toRemove;
2388
2389 /*
2390 * In order to find relations with the same oid we first build an array of
2391 * candidates and then sort it by oid.
2392 */
2394 i = -1;
2395 j = 0;
2396 while ((i = bms_next_member(relids, i)) >= 0)
2397 {
2398 candidates[j].relid = i;
2399 candidates[j].reloid = root->simple_rte_array[i]->relid;
2400 j++;
2401 }
2402
2405
2406 /*
2407 * Iteratively form a group of relation indexes with the same oid and
2408 * launch the routine that detects self-joins in this group and removes
2409 * excessive range table entries.
2410 *
2411 * At the end of the iteration, exclude the group from the overall relids
2412 * list. So each next iteration of the cycle will involve less and less
2413 * value of relids.
2414 */
2415 i = 0;
2416 for (j = 1; j < numRels + 1; j++)
2417 {
2418 if (j == numRels || candidates[j].reloid != candidates[i].reloid)
2419 {
2420 if (j - i >= 2)
2421 {
2422 /* Create a group of relation indexes with the same oid */
2423 Relids group = NULL;
2425
2426 while (i < j)
2427 {
2428 group = bms_add_member(group, candidates[i].relid);
2429 i++;
2430 }
2431 relids = bms_del_members(relids, group);
2432
2433 /*
2434 * Try to remove self-joins from a group of identical entries.
2435 * Make the next attempt iteratively - if something is deleted
2436 * from a group, changes in clauses and equivalence classes
2437 * can give us a chance to find more candidates.
2438 */
2439 do
2440 {
2441 Assert(!bms_overlap(group, toRemove));
2444 group = bms_del_members(group, removed);
2445 } while (!bms_is_empty(removed) &&
2446 bms_membership(group) == BMS_MULTIPLE);
2448 bms_free(group);
2449 }
2450 else
2451 {
2452 /* Single relation, just remove it from the set */
2453 relids = bms_del_member(relids, candidates[i].relid);
2454 i = j;
2455 }
2456 }
2457 }
2458
2459 Assert(bms_is_empty(relids));
2460
2461 return toRemove;
2462}
2463
2464/*
2465 * Compare self-join candidates by their oids.
2466 */
2467static int
2468self_join_candidates_cmp(const void *a, const void *b)
2469{
2470 const SelfJoinCandidate *ca = (const SelfJoinCandidate *) a;
2471 const SelfJoinCandidate *cb = (const SelfJoinCandidate *) b;
2472
2473 if (ca->reloid != cb->reloid)
2474 return (ca->reloid < cb->reloid ? -1 : 1);
2475 else
2476 return 0;
2477}
2478
2479/*
2480 * Find and remove useless self joins.
2481 *
2482 * Search for joins where a relation is joined to itself. If the join clause
2483 * for each tuple from one side of the join is proven to match the same
2484 * physical row (or nothing) on the other side, that self-join can be
2485 * eliminated from the query. Suitable join clauses are assumed to be in the
2486 * form of X = X, and can be replaced with NOT NULL clauses.
2487 *
2488 * For the sake of simplicity, we don't apply this optimization to special
2489 * joins. Here is a list of what we could do in some particular cases:
2490 * 'a a1 semi join a a2': is reduced to inner by reduce_unique_semijoins,
2491 * and then removed normally.
2492 * 'a a1 anti join a a2': could simplify to a scan with 'outer quals AND
2493 * (IS NULL on join columns OR NOT inner quals)'.
2494 * 'a a1 left join a a2': could simplify to a scan like inner but without
2495 * NOT NULL conditions on join columns.
2496 * 'a a1 left join (a a2 join b)': can't simplify this, because join to b
2497 * can both remove rows and introduce duplicates.
2498 *
2499 * To search for removable joins, we order all the relations on their Oid,
2500 * go over each set with the same Oid, and consider each pair of relations
2501 * in this set.
2502 *
2503 * To remove the join, we mark one of the participating relations as dead
2504 * and rewrite all references to it to point to the remaining relation.
2505 * This includes modifying RestrictInfos, EquivalenceClasses, and
2506 * EquivalenceMembers. We also have to modify the row marks. The join clauses
2507 * of the removed relation become either restriction or join clauses, based on
2508 * whether they reference any relations not participating in the removed join.
2509 *
2510 * 'joinlist' is the top-level joinlist of the query. If it has any
2511 * references to the removed relations, we update them to point to the
2512 * remaining ones.
2513 */
2514List *
2516{
2518 int relid = -1;
2519
2522 return joinlist;
2523
2524 /*
2525 * Merge pairs of relations participated in self-join. Remove unnecessary
2526 * range table entries.
2527 */
2529
2530 if (unlikely(toRemove != NULL))
2531 {
2532 /* At the end, remove orphaned relation links */
2533 while ((relid = bms_next_member(toRemove, relid)) >= 0)
2534 {
2535 int nremoved = 0;
2536
2538 if (nremoved != 1)
2539 elog(ERROR, "failed to find relation %d in joinlist", relid);
2540 }
2541 }
2542
2543 return joinlist;
2544}
static int self_join_candidates_cmp(const void *a, const void *b)
static bool match_unique_clauses(PlannerInfo *root, RelOptInfo *outer, List *uclauses, Index relid)
static bool replace_relid_callback(Node *node, ChangeVarNodes_context *context)
static void split_selfjoin_quals(PlannerInfo *root, List *joinquals, List **selfjoinquals, List **otherjoinquals, int from, int to)
static void add_non_redundant_clauses(PlannerInfo *root, List *rinfo_candidates, List **keep_rinfo_list, Index removed_relid)
bool innerrel_is_unique(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
static void remove_leftjoinrel_from_query(PlannerInfo *root, int relid, SpecialJoinInfo *sjinfo)
bool query_is_distinct_for(Query *query, List *colnos, List *opids)
static Relids remove_self_joins_one_group(PlannerInfo *root, Relids relids)
List * remove_useless_joins(PlannerInfo *root, List *joinlist)
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 bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list, List **extra_clauses)
bool innerrel_is_unique_ext(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache, List **extra_clauses)
List * remove_useless_self_joins(PlannerInfo *root, List *joinlist)
bool query_supports_distinctness(Query *query)
static void update_eclasses(EquivalenceClass *ec, int from, int to)
static bool restrict_infos_logically_equal(RestrictInfo *a, RestrictInfo *b)
static Relids remove_self_joins_recurse(PlannerInfo *root, List *joinlist, Relids toRemove)
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
static void remove_rel_from_query(PlannerInfo *root, int relid, int subst, SpecialJoinInfo *sjinfo, Relids joinrelids)
void reduce_unique_semijoins(PlannerInfo *root)
bool enable_self_join_elimination
static void remove_rel_from_restrictinfo(RestrictInfo *rinfo, int relid, int ojrelid)
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
static void remove_self_join_rel(PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark, RelOptInfo *toKeep, RelOptInfo *toRemove, List *restrictlist)
static void remove_rel_from_eclass(EquivalenceClass *ec, int relid, int ojrelid)
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:346
Bitmapset * bms_make_singleton(int x)
Definition bitmapset.c:216
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:142
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1145
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:412
int bms_singleton_member(const Bitmapset *a)
Definition bitmapset.c:665
void bms_free(Bitmapset *a)
Definition bitmapset.c:239
int bms_num_members(const Bitmapset *a)
Definition bitmapset.c:744
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:901
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
BMS_Membership bms_membership(const Bitmapset *a)
Definition bitmapset.c:765
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:575
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition bitmapset.c:708
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
#define bms_is_empty(a)
Definition bitmapset.h:118
@ BMS_SINGLETON
Definition bitmapset.h:72
@ BMS_MULTIPLE
Definition bitmapset.h:73
#define Assert(condition)
Definition c.h:943
#define unlikely(x)
Definition c.h:438
unsigned int Index
Definition c.h:698
#define OidIsValid(objectId)
Definition c.h:858
uint32 result
Datum arg
Definition elog.c:1323
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
void rebuild_eclass_attr_needed(PlannerInfo *root)
void ec_clear_derived_clauses(EquivalenceClass *ec)
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
#define palloc_array(type, count)
Definition fe_memutils.h:76
bool relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List **extra_clauses)
Definition indxpath.c:4144
void rebuild_lateral_attr_needed(PlannerInfo *root)
Definition initsplan.c:1194
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition initsplan.c:3578
void rebuild_joinclause_attr_needed(PlannerInfo *root)
Definition initsplan.c:3910
int b
Definition isn.c:74
int a
Definition isn.c:73
int j
Definition isn.c:78
int i
Definition isn.c:77
void remove_join_clause_from_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition joininfo.c:161
List * list_delete_ptr(List *list, void *datum)
Definition list.c:872
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_concat(List *list1, const List *list2)
Definition list.c:561
List * list_delete_cell(List *list, ListCell *cell)
Definition list.c:841
List * list_copy(const List *oldlist)
Definition list.c:1573
List * lappend_int(List *list, int datum)
Definition list.c:357
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
void list_free(List *list)
Definition list.c:1546
bool list_member(const List *list, const void *datum)
Definition list.c:661
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition lsyscache.c:773
void pfree(void *pointer)
Definition mcxt.c:1616
static bool is_andclause(const void *clause)
Definition nodeFuncs.h:107
static bool is_orclause(const void *clause)
Definition nodeFuncs.h:116
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 IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
#define nodeTag(nodeptr)
Definition nodes.h:139
#define IS_OUTER_JOIN(jointype)
Definition nodes.h:348
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
JoinType
Definition nodes.h:298
@ JOIN_SEMI
Definition nodes.h:317
@ JOIN_INNER
Definition nodes.h:303
@ JOIN_LEFT
Definition nodes.h:304
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
List * expand_grouping_sets(List *groupingSets, bool groupDistinct, int limit)
Definition parse_agg.c:2019
@ SETOP_NONE
@ RTE_SUBQUERY
@ RTE_RELATION
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition pathnodes.h:3058
Bitmapset * Relids
Definition pathnodes.h:103
@ RELOPT_BASEREL
Definition pathnodes.h:977
#define lfirst(lc)
Definition pg_list.h:172
#define lfirst_node(type, lc)
Definition pg_list.h:176
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
#define lfirst_int(lc)
Definition pg_list.h:173
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:423
static void * list_nth(const List *list, int n)
Definition pg_list.h:331
#define linitial(l)
Definition pg_list.h:178
#define foreach_node(type, var, lst)
Definition pg_list.h:528
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:375
#define lfirst_oid(lc)
Definition pg_list.h:174
void rebuild_placeholder_attr_needed(PlannerInfo *root)
#define qsort(a, b, c, d)
Definition port.h:495
#define InvalidOid
unsigned int Oid
static int fb(int x)
@ IS_NOT_NULL
Definition primnodes.h:1980
tree ctl root
Definition radixtree.h:1857
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition relnode.c:544
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
static bool clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids, Relids innerrelids)
bool ChangeVarNodesWalkExpression(Node *node, ChangeVarNodes_context *context)
Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid)
void ChangeVarNodesExtended(Node *node, int rt_index, int new_index, int sublevels_up, ChangeVarNodes_callback callback)
List ** ec_childmembers
Definition pathnodes.h:1663
Definition pg_list.h:54
Definition nodes.h:135
List * args
Definition primnodes.h:869
bool groupDistinct
Definition parsenodes.h:220
Node * setOperations
Definition parsenodes.h:239
List * groupClause
Definition parsenodes.h:219
Node * havingQual
Definition parsenodes.h:225
List * targetList
Definition parsenodes.h:201
List * groupingSets
Definition parsenodes.h:223
List * distinctClause
Definition parsenodes.h:229
List * baserestrictinfo
Definition pathnodes.h:1142
List * joininfo
Definition pathnodes.h:1148
Relids relids
Definition pathnodes.h:1021
Index relid
Definition pathnodes.h:1069
List * unique_for_rels
Definition pathnodes.h:1124
RelOptKind reloptkind
Definition pathnodes.h:1015
List * indexlist
Definition pathnodes.h:1091
List * non_unique_for_rels
Definition pathnodes.h:1126
AttrNumber max_attr
Definition pathnodes.h:1077
AttrNumber min_attr
Definition pathnodes.h:1075
RTEKind rtekind
Definition pathnodes.h:1073
Relids required_relids
Definition pathnodes.h:2932
Relids outer_relids
Definition pathnodes.h:2938
Relids incompatible_relids
Definition pathnodes.h:2935
Expr * clause
Definition pathnodes.h:2901
Relids commute_above_r
Definition pathnodes.h:3233
Relids syn_lefthand
Definition pathnodes.h:3228
Relids min_righthand
Definition pathnodes.h:3227
JoinType jointype
Definition pathnodes.h:3230
Relids commute_below_l
Definition pathnodes.h:3234
Relids min_lefthand
Definition pathnodes.h:3226
Relids syn_righthand
Definition pathnodes.h:3229
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
Index varlevelsup
Definition primnodes.h:295
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition tlist.c:376
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition var.c:114