1 /*-------------------------------------------------------------------------
4 * Routines for managing EquivalenceClasses
6 * See src/backend/optimizer/README for discussion of EquivalenceClasses.
9 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
15 *-------------------------------------------------------------------------
19 #include "access/skey.h"
20 #include "nodes/nodeFuncs.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/paths.h"
24 #include "optimizer/planmain.h"
25 #include "optimizer/prep.h"
26 #include "optimizer/var.h"
27 #include "utils/lsyscache.h"
30 static EquivalenceMember
*add_eq_member(EquivalenceClass
*ec
,
31 Expr
*expr
, Relids relids
,
32 bool is_child
, Oid datatype
);
33 static void generate_base_implied_equalities_const(PlannerInfo
*root
,
34 EquivalenceClass
*ec
);
35 static void generate_base_implied_equalities_no_const(PlannerInfo
*root
,
36 EquivalenceClass
*ec
);
37 static void generate_base_implied_equalities_broken(PlannerInfo
*root
,
38 EquivalenceClass
*ec
);
39 static List
*generate_join_implied_equalities_normal(PlannerInfo
*root
,
42 RelOptInfo
*outer_rel
,
43 RelOptInfo
*inner_rel
);
44 static List
*generate_join_implied_equalities_broken(PlannerInfo
*root
,
47 RelOptInfo
*outer_rel
,
48 RelOptInfo
*inner_rel
);
49 static Oid
select_equality_operator(EquivalenceClass
*ec
,
50 Oid lefttype
, Oid righttype
);
51 static RestrictInfo
*create_join_clause(PlannerInfo
*root
,
52 EquivalenceClass
*ec
, Oid opno
,
53 EquivalenceMember
*leftem
,
54 EquivalenceMember
*rightem
,
55 EquivalenceClass
*parent_ec
);
56 static bool reconsider_outer_join_clause(PlannerInfo
*root
,
59 static bool reconsider_full_join_clause(PlannerInfo
*root
,
65 * The given clause has a mergejoinable operator and can be applied without
66 * any delay by an outer join, so its two sides can be considered equal
67 * anywhere they are both computable; moreover that equality can be
68 * extended transitively. Record this knowledge in the EquivalenceClass
69 * data structure. Returns TRUE if successful, FALSE if not (in which
70 * case caller should treat the clause as ordinary, not an equivalence).
72 * If below_outer_join is true, then the clause was found below the nullable
73 * side of an outer join, so its sides might validly be both NULL rather than
74 * strictly equal. We can still deduce equalities in such cases, but we take
75 * care to mark an EquivalenceClass if it came from any such clauses. Also,
76 * we have to check that both sides are either pseudo-constants or strict
77 * functions of Vars, else they might not both go to NULL above the outer
78 * join. (This is the reason why we need a failure return. It's more
79 * convenient to check this case here than at the call sites...)
81 * Note: constructing merged EquivalenceClasses is a standard UNION-FIND
82 * problem, for which there exist better data structures than simple lists.
83 * If this code ever proves to be a bottleneck then it could be sped up ---
84 * but for now, simple is beautiful.
86 * Note: this is only called during planner startup, not during GEQO
87 * exploration, so we need not worry about whether we're in the right
91 process_equivalence(PlannerInfo
*root
, RestrictInfo
*restrictinfo
,
92 bool below_outer_join
)
94 Expr
*clause
= restrictinfo
->clause
;
103 EquivalenceClass
*ec1
,
105 EquivalenceMember
*em1
,
109 /* Extract info from given clause */
110 Assert(is_opclause(clause
));
111 opno
= ((OpExpr
*) clause
)->opno
;
112 item1
= (Expr
*) get_leftop(clause
);
113 item2
= (Expr
*) get_rightop(clause
);
114 item1_relids
= restrictinfo
->left_relids
;
115 item2_relids
= restrictinfo
->right_relids
;
118 * If below outer join, check for strictness, else reject.
120 if (below_outer_join
)
122 if (!bms_is_empty(item1_relids
) &&
123 contain_nonstrict_functions((Node
*) item1
))
124 return false; /* LHS is non-strict but not constant */
125 if (!bms_is_empty(item2_relids
) &&
126 contain_nonstrict_functions((Node
*) item2
))
127 return false; /* RHS is non-strict but not constant */
131 * We use the declared input types of the operator, not exprType() of the
132 * inputs, as the nominal datatypes for opfamily lookup. This presumes
133 * that btree operators are always registered with amoplefttype and
134 * amoprighttype equal to their declared input types. We will need this
135 * info anyway to build EquivalenceMember nodes, and by extracting it now
136 * we can use type comparisons to short-circuit some equal() tests.
138 op_input_types(opno
, &item1_type
, &item2_type
);
140 opfamilies
= restrictinfo
->mergeopfamilies
;
143 * Sweep through the existing EquivalenceClasses looking for matches to
144 * item1 and item2. These are the possible outcomes:
146 * 1. We find both in the same EC. The equivalence is already known, so
147 * there's nothing to do.
149 * 2. We find both in different ECs. Merge the two ECs together.
151 * 3. We find just one. Add the other to its EC.
153 * 4. We find neither. Make a new, two-entry EC.
155 * Note: since all ECs are built through this process, it's impossible
156 * that we'd match an item in more than one existing EC. It is possible
157 * to match more than once within an EC, if someone fed us something silly
158 * like "WHERE X=X". (However, we can't simply discard such clauses,
159 * since they should fail when X is null; so we will build a 2-member EC
160 * to ensure the correct restriction clause gets generated. Hence there
161 * is no shortcut here for item1 and item2 equal.)
165 foreach(lc1
, root
->eq_classes
)
167 EquivalenceClass
*cur_ec
= (EquivalenceClass
*) lfirst(lc1
);
170 /* Never match to a volatile EC */
171 if (cur_ec
->ec_has_volatile
)
175 * A "match" requires matching sets of btree opfamilies. Use of
176 * equal() for this test has implications discussed in the comments
177 * for get_mergejoin_opfamilies().
179 if (!equal(opfamilies
, cur_ec
->ec_opfamilies
))
182 foreach(lc2
, cur_ec
->ec_members
)
184 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
186 Assert(!cur_em
->em_is_child
); /* no children yet */
189 * If below an outer join, don't match constants: they're not as
190 * constant as they look.
192 if ((below_outer_join
|| cur_ec
->ec_below_outer_join
) &&
197 item1_type
== cur_em
->em_datatype
&&
198 equal(item1
, cur_em
->em_expr
))
207 item2_type
== cur_em
->em_datatype
&&
208 equal(item2
, cur_em
->em_expr
))
221 /* Sweep finished, what did we find? */
225 /* If case 1, nothing to do, except add to sources */
228 ec1
->ec_sources
= lappend(ec1
->ec_sources
, restrictinfo
);
229 ec1
->ec_below_outer_join
|= below_outer_join
;
230 /* mark the RI as usable with this pair of EMs */
231 /* NB: can't set left_ec/right_ec until merging is finished */
232 restrictinfo
->left_em
= em1
;
233 restrictinfo
->right_em
= em2
;
238 * Case 2: need to merge ec1 and ec2. We add ec2's items to ec1, then
239 * set ec2's ec_merged link to point to ec1 and remove ec2 from the
240 * eq_classes list. We cannot simply delete ec2 because that could
241 * leave dangling pointers in existing PathKeys. We leave it behind
242 * with a link so that the merged EC can be found.
244 ec1
->ec_members
= list_concat(ec1
->ec_members
, ec2
->ec_members
);
245 ec1
->ec_sources
= list_concat(ec1
->ec_sources
, ec2
->ec_sources
);
246 ec1
->ec_derives
= list_concat(ec1
->ec_derives
, ec2
->ec_derives
);
247 ec1
->ec_relids
= bms_join(ec1
->ec_relids
, ec2
->ec_relids
);
248 ec1
->ec_has_const
|= ec2
->ec_has_const
;
249 /* can't need to set has_volatile */
250 ec1
->ec_below_outer_join
|= ec2
->ec_below_outer_join
;
251 ec2
->ec_merged
= ec1
;
252 root
->eq_classes
= list_delete_ptr(root
->eq_classes
, ec2
);
253 /* just to avoid debugging confusion w/ dangling pointers: */
254 ec2
->ec_members
= NIL
;
255 ec2
->ec_sources
= NIL
;
256 ec2
->ec_derives
= NIL
;
257 ec2
->ec_relids
= NULL
;
258 ec1
->ec_sources
= lappend(ec1
->ec_sources
, restrictinfo
);
259 ec1
->ec_below_outer_join
|= below_outer_join
;
260 /* mark the RI as usable with this pair of EMs */
261 restrictinfo
->left_em
= em1
;
262 restrictinfo
->right_em
= em2
;
266 /* Case 3: add item2 to ec1 */
267 em2
= add_eq_member(ec1
, item2
, item2_relids
, false, item2_type
);
268 ec1
->ec_sources
= lappend(ec1
->ec_sources
, restrictinfo
);
269 ec1
->ec_below_outer_join
|= below_outer_join
;
270 /* mark the RI as usable with this pair of EMs */
271 restrictinfo
->left_em
= em1
;
272 restrictinfo
->right_em
= em2
;
276 /* Case 3: add item1 to ec2 */
277 em1
= add_eq_member(ec2
, item1
, item1_relids
, false, item1_type
);
278 ec2
->ec_sources
= lappend(ec2
->ec_sources
, restrictinfo
);
279 ec2
->ec_below_outer_join
|= below_outer_join
;
280 /* mark the RI as usable with this pair of EMs */
281 restrictinfo
->left_em
= em1
;
282 restrictinfo
->right_em
= em2
;
286 /* Case 4: make a new, two-entry EC */
287 EquivalenceClass
*ec
= makeNode(EquivalenceClass
);
289 ec
->ec_opfamilies
= opfamilies
;
290 ec
->ec_members
= NIL
;
291 ec
->ec_sources
= list_make1(restrictinfo
);
292 ec
->ec_derives
= NIL
;
293 ec
->ec_relids
= NULL
;
294 ec
->ec_has_const
= false;
295 ec
->ec_has_volatile
= false;
296 ec
->ec_below_outer_join
= below_outer_join
;
297 ec
->ec_broken
= false;
299 ec
->ec_merged
= NULL
;
300 em1
= add_eq_member(ec
, item1
, item1_relids
, false, item1_type
);
301 em2
= add_eq_member(ec
, item2
, item2_relids
, false, item2_type
);
303 root
->eq_classes
= lappend(root
->eq_classes
, ec
);
305 /* mark the RI as usable with this pair of EMs */
306 restrictinfo
->left_em
= em1
;
307 restrictinfo
->right_em
= em2
;
314 * add_eq_member - build a new EquivalenceMember and add it to an EC
316 static EquivalenceMember
*
317 add_eq_member(EquivalenceClass
*ec
, Expr
*expr
, Relids relids
,
318 bool is_child
, Oid datatype
)
320 EquivalenceMember
*em
= makeNode(EquivalenceMember
);
323 em
->em_relids
= relids
;
324 em
->em_is_const
= false;
325 em
->em_is_child
= is_child
;
326 em
->em_datatype
= datatype
;
328 if (bms_is_empty(relids
))
331 * No Vars, assume it's a pseudoconstant. This is correct for entries
332 * generated from process_equivalence(), because a WHERE clause can't
333 * contain aggregates or SRFs, and non-volatility was checked before
334 * process_equivalence() ever got called. But
335 * get_eclass_for_sort_expr() has to work harder. We put the tests
336 * there not here to save cycles in the equivalence case.
339 em
->em_is_const
= true;
340 ec
->ec_has_const
= true;
341 /* it can't affect ec_relids */
343 else if (!is_child
) /* child members don't add to ec_relids */
345 ec
->ec_relids
= bms_add_members(ec
->ec_relids
, relids
);
347 ec
->ec_members
= lappend(ec
->ec_members
, em
);
354 * get_eclass_for_sort_expr
355 * Given an expression and opfamily info, find an existing equivalence
356 * class it is a member of; if none, build a new single-member
357 * EquivalenceClass for it.
359 * sortref is the SortGroupRef of the originating SortGroupClause, if any,
362 * This can be used safely both before and after EquivalenceClass merging;
363 * since it never causes merging it does not invalidate any existing ECs
366 * Note: opfamilies must be chosen consistently with the way
367 * process_equivalence() would do; that is, generated from a mergejoinable
368 * equality operator. Else we might fail to detect valid equivalences,
369 * generating poor (but not incorrect) plans.
372 get_eclass_for_sort_expr(PlannerInfo
*root
,
378 EquivalenceClass
*newec
;
379 EquivalenceMember
*newem
;
381 MemoryContext oldcontext
;
384 * Scan through the existing EquivalenceClasses for a match
386 foreach(lc1
, root
->eq_classes
)
388 EquivalenceClass
*cur_ec
= (EquivalenceClass
*) lfirst(lc1
);
391 /* Never match to a volatile EC */
392 if (cur_ec
->ec_has_volatile
)
395 if (!equal(opfamilies
, cur_ec
->ec_opfamilies
))
398 foreach(lc2
, cur_ec
->ec_members
)
400 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
403 * If below an outer join, don't match constants: they're not as
404 * constant as they look.
406 if (cur_ec
->ec_below_outer_join
&&
410 if (expr_datatype
== cur_em
->em_datatype
&&
411 equal(expr
, cur_em
->em_expr
))
412 return cur_ec
; /* Match! */
417 * No match, so build a new single-member EC
419 * Here, we must be sure that we construct the EC in the right context. We
420 * can assume, however, that the passed expr is long-lived.
422 oldcontext
= MemoryContextSwitchTo(root
->planner_cxt
);
424 newec
= makeNode(EquivalenceClass
);
425 newec
->ec_opfamilies
= list_copy(opfamilies
);
426 newec
->ec_members
= NIL
;
427 newec
->ec_sources
= NIL
;
428 newec
->ec_derives
= NIL
;
429 newec
->ec_relids
= NULL
;
430 newec
->ec_has_const
= false;
431 newec
->ec_has_volatile
= contain_volatile_functions((Node
*) expr
);
432 newec
->ec_below_outer_join
= false;
433 newec
->ec_broken
= false;
434 newec
->ec_sortref
= sortref
;
435 newec
->ec_merged
= NULL
;
436 newem
= add_eq_member(newec
, expr
, pull_varnos((Node
*) expr
),
437 false, expr_datatype
);
440 * add_eq_member doesn't check for volatile functions, set-returning
441 * functions, aggregates, or window functions, but such could appear in
442 * sort expressions; so we have to check whether its const-marking was
445 if (newec
->ec_has_const
)
447 if (newec
->ec_has_volatile
||
448 expression_returns_set((Node
*) expr
) ||
449 contain_agg_clause((Node
*) expr
) ||
450 contain_window_function((Node
*) expr
))
452 newec
->ec_has_const
= false;
453 newem
->em_is_const
= false;
457 root
->eq_classes
= lappend(root
->eq_classes
, newec
);
459 MemoryContextSwitchTo(oldcontext
);
466 * generate_base_implied_equalities
467 * Generate any restriction clauses that we can deduce from equivalence
470 * When an EC contains pseudoconstants, our strategy is to generate
471 * "member = const1" clauses where const1 is the first constant member, for
472 * every other member (including other constants). If we are able to do this
473 * then we don't need any "var = var" comparisons because we've successfully
474 * constrained all the vars at their points of creation. If we fail to
475 * generate any of these clauses due to lack of cross-type operators, we fall
476 * back to the "ec_broken" strategy described below. (XXX if there are
477 * multiple constants of different types, it's possible that we might succeed
478 * in forming all the required clauses if we started from a different const
479 * member; but this seems a sufficiently hokey corner case to not be worth
480 * spending lots of cycles on.)
482 * For ECs that contain no pseudoconstants, we generate derived clauses
483 * "member1 = member2" for each pair of members belonging to the same base
484 * relation (actually, if there are more than two for the same base relation,
485 * we only need enough clauses to link each to each other). This provides
486 * the base case for the recursion: each row emitted by a base relation scan
487 * will constrain all computable members of the EC to be equal. As each
488 * join path is formed, we'll add additional derived clauses on-the-fly
489 * to maintain this invariant (see generate_join_implied_equalities).
491 * If the opfamilies used by the EC do not provide complete sets of cross-type
492 * equality operators, it is possible that we will fail to generate a clause
493 * that must be generated to maintain the invariant. (An example: given
494 * "WHERE a.x = b.y AND b.y = a.z", the scheme breaks down if we cannot
495 * generate "a.x = a.z" as a restriction clause for A.) In this case we mark
496 * the EC "ec_broken" and fall back to regurgitating its original source
497 * RestrictInfos at appropriate times. We do not try to retract any derived
498 * clauses already generated from the broken EC, so the resulting plan could
499 * be poor due to bad selectivity estimates caused by redundant clauses. But
500 * the correct solution to that is to fix the opfamilies ...
502 * Equality clauses derived by this function are passed off to
503 * process_implied_equality (in plan/initsplan.c) to be inserted into the
504 * restrictinfo datastructures. Note that this must be called after initial
505 * scanning of the quals and before Path construction begins.
507 * We make no attempt to avoid generating duplicate RestrictInfos here: we
508 * don't search ec_sources for matches, nor put the created RestrictInfos
509 * into ec_derives. Doing so would require some slightly ugly changes in
510 * initsplan.c's API, and there's no real advantage, because the clauses
511 * generated here can't duplicate anything we will generate for joins anyway.
514 generate_base_implied_equalities(PlannerInfo
*root
)
519 foreach(lc
, root
->eq_classes
)
521 EquivalenceClass
*ec
= (EquivalenceClass
*) lfirst(lc
);
523 Assert(ec
->ec_merged
== NULL
); /* else shouldn't be in list */
524 Assert(!ec
->ec_broken
); /* not yet anyway... */
526 /* Single-member ECs won't generate any deductions */
527 if (list_length(ec
->ec_members
) <= 1)
530 if (ec
->ec_has_const
)
531 generate_base_implied_equalities_const(root
, ec
);
533 generate_base_implied_equalities_no_const(root
, ec
);
535 /* Recover if we failed to generate required derived clauses */
537 generate_base_implied_equalities_broken(root
, ec
);
541 * This is also a handy place to mark base rels (which should all exist by
542 * now) with flags showing whether they have pending eclass joins.
544 for (rti
= 1; rti
< root
->simple_rel_array_size
; rti
++)
546 RelOptInfo
*brel
= root
->simple_rel_array
[rti
];
551 brel
->has_eclass_joins
= has_relevant_eclass_joinclause(root
, brel
);
556 * generate_base_implied_equalities when EC contains pseudoconstant(s)
559 generate_base_implied_equalities_const(PlannerInfo
*root
,
560 EquivalenceClass
*ec
)
562 EquivalenceMember
*const_em
= NULL
;
566 * In the trivial case where we just had one "var = const" clause, push
567 * the original clause back into the main planner machinery. There is
568 * nothing to be gained by doing it differently, and we save the effort to
569 * re-build and re-analyze an equality clause that will be exactly
570 * equivalent to the old one.
572 if (list_length(ec
->ec_members
) == 2 &&
573 list_length(ec
->ec_sources
) == 1)
575 RestrictInfo
*restrictinfo
= (RestrictInfo
*) linitial(ec
->ec_sources
);
577 if (bms_membership(restrictinfo
->required_relids
) != BMS_MULTIPLE
)
579 distribute_restrictinfo_to_rels(root
, restrictinfo
);
584 /* Find the constant member to use */
585 foreach(lc
, ec
->ec_members
)
587 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc
);
589 if (cur_em
->em_is_const
)
595 Assert(const_em
!= NULL
);
597 /* Generate a derived equality against each other member */
598 foreach(lc
, ec
->ec_members
)
600 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc
);
603 Assert(!cur_em
->em_is_child
); /* no children yet */
604 if (cur_em
== const_em
)
606 eq_op
= select_equality_operator(ec
,
608 const_em
->em_datatype
);
609 if (!OidIsValid(eq_op
))
612 ec
->ec_broken
= true;
615 process_implied_equality(root
, eq_op
,
616 cur_em
->em_expr
, const_em
->em_expr
,
618 ec
->ec_below_outer_join
,
619 cur_em
->em_is_const
);
624 * generate_base_implied_equalities when EC contains no pseudoconstants
627 generate_base_implied_equalities_no_const(PlannerInfo
*root
,
628 EquivalenceClass
*ec
)
630 EquivalenceMember
**prev_ems
;
634 * We scan the EC members once and track the last-seen member for each
635 * base relation. When we see another member of the same base relation,
636 * we generate "prev_mem = cur_mem". This results in the minimum number
637 * of derived clauses, but it's possible that it will fail when a
638 * different ordering would succeed. XXX FIXME: use a UNION-FIND
639 * algorithm similar to the way we build merged ECs. (Use a list-of-lists
642 prev_ems
= (EquivalenceMember
**)
643 palloc0(root
->simple_rel_array_size
* sizeof(EquivalenceMember
*));
645 foreach(lc
, ec
->ec_members
)
647 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc
);
650 Assert(!cur_em
->em_is_child
); /* no children yet */
651 if (bms_membership(cur_em
->em_relids
) != BMS_SINGLETON
)
653 relid
= bms_singleton_member(cur_em
->em_relids
);
654 Assert(relid
< root
->simple_rel_array_size
);
656 if (prev_ems
[relid
] != NULL
)
658 EquivalenceMember
*prev_em
= prev_ems
[relid
];
661 eq_op
= select_equality_operator(ec
,
662 prev_em
->em_datatype
,
663 cur_em
->em_datatype
);
664 if (!OidIsValid(eq_op
))
667 ec
->ec_broken
= true;
670 process_implied_equality(root
, eq_op
,
671 prev_em
->em_expr
, cur_em
->em_expr
,
673 ec
->ec_below_outer_join
,
676 prev_ems
[relid
] = cur_em
;
682 * We also have to make sure that all the Vars used in the member clauses
683 * will be available at any join node we might try to reference them at.
684 * For the moment we force all the Vars to be available at all join nodes
685 * for this eclass. Perhaps this could be improved by doing some
686 * pre-analysis of which members we prefer to join, but it's no worse than
687 * what happened in the pre-8.3 code.
689 foreach(lc
, ec
->ec_members
)
691 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc
);
692 List
*vars
= pull_var_clause((Node
*) cur_em
->em_expr
,
693 PVC_INCLUDE_PLACEHOLDERS
);
695 add_vars_to_targetlist(root
, vars
, ec
->ec_relids
);
701 * generate_base_implied_equalities cleanup after failure
703 * What we must do here is push any zero- or one-relation source RestrictInfos
704 * of the EC back into the main restrictinfo datastructures. Multi-relation
705 * clauses will be regurgitated later by generate_join_implied_equalities().
706 * (We do it this way to maintain continuity with the case that ec_broken
707 * becomes set only after we've gone up a join level or two.)
710 generate_base_implied_equalities_broken(PlannerInfo
*root
,
711 EquivalenceClass
*ec
)
715 foreach(lc
, ec
->ec_sources
)
717 RestrictInfo
*restrictinfo
= (RestrictInfo
*) lfirst(lc
);
719 if (bms_membership(restrictinfo
->required_relids
) != BMS_MULTIPLE
)
720 distribute_restrictinfo_to_rels(root
, restrictinfo
);
726 * generate_join_implied_equalities
727 * Generate any join clauses that we can deduce from equivalence classes.
729 * At a join node, we must enforce restriction clauses sufficient to ensure
730 * that all equivalence-class members computable at that node are equal.
731 * Since the set of clauses to enforce can vary depending on which subset
732 * relations are the inputs, we have to compute this afresh for each join
733 * path pair. Hence a fresh List of RestrictInfo nodes is built and passed
736 * The results are sufficient for use in merge, hash, and plain nestloop join
737 * methods. We do not worry here about selecting clauses that are optimal
738 * for use in a nestloop-with-inner-indexscan join, however. indxpath.c makes
739 * its own selections of clauses to use, and if the ones we pick here are
740 * redundant with those, the extras will be eliminated in createplan.c.
742 * Because the same join clauses are likely to be needed multiple times as
743 * we consider different join paths, we avoid generating multiple copies:
744 * whenever we select a particular pair of EquivalenceMembers to join,
745 * we check to see if the pair matches any original clause (in ec_sources)
746 * or previously-built clause (in ec_derives). This saves memory and allows
747 * re-use of information cached in RestrictInfos.
750 generate_join_implied_equalities(PlannerInfo
*root
,
752 RelOptInfo
*outer_rel
,
753 RelOptInfo
*inner_rel
)
758 foreach(lc
, root
->eq_classes
)
760 EquivalenceClass
*ec
= (EquivalenceClass
*) lfirst(lc
);
763 /* ECs containing consts do not need any further enforcement */
764 if (ec
->ec_has_const
)
767 /* Single-member ECs won't generate any deductions */
768 if (list_length(ec
->ec_members
) <= 1)
771 /* We can quickly ignore any that don't overlap the join, too */
772 if (!bms_overlap(ec
->ec_relids
, joinrel
->relids
))
776 sublist
= generate_join_implied_equalities_normal(root
,
782 /* Recover if we failed to generate required derived clauses */
784 sublist
= generate_join_implied_equalities_broken(root
,
790 result
= list_concat(result
, sublist
);
797 * generate_join_implied_equalities for a still-valid EC
800 generate_join_implied_equalities_normal(PlannerInfo
*root
,
801 EquivalenceClass
*ec
,
803 RelOptInfo
*outer_rel
,
804 RelOptInfo
*inner_rel
)
807 List
*new_members
= NIL
;
808 List
*outer_members
= NIL
;
809 List
*inner_members
= NIL
;
813 * First, scan the EC to identify member values that are computable at the
814 * outer rel, at the inner rel, or at this relation but not in either
815 * input rel. The outer-rel members should already be enforced equal,
816 * likewise for the inner-rel members. We'll need to create clauses to
817 * enforce that any newly computable members are all equal to each other
818 * as well as to at least one input member, plus enforce at least one
819 * outer-rel member equal to at least one inner-rel member.
821 foreach(lc1
, ec
->ec_members
)
823 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc1
);
825 if (cur_em
->em_is_child
)
826 continue; /* ignore children here */
827 if (!bms_is_subset(cur_em
->em_relids
, joinrel
->relids
))
828 continue; /* ignore --- not computable yet */
830 if (bms_is_subset(cur_em
->em_relids
, outer_rel
->relids
))
831 outer_members
= lappend(outer_members
, cur_em
);
832 else if (bms_is_subset(cur_em
->em_relids
, inner_rel
->relids
))
833 inner_members
= lappend(inner_members
, cur_em
);
835 new_members
= lappend(new_members
, cur_em
);
839 * First, select the joinclause if needed. We can equate any one outer
840 * member to any one inner member, but we have to find a datatype
841 * combination for which an opfamily member operator exists. If we have
842 * choices, we prefer simple Var members (possibly with RelabelType) since
843 * these are (a) cheapest to compute at runtime and (b) most likely to
844 * have useful statistics. Also, if enable_hashjoin is on, we prefer
845 * operators that are also hashjoinable.
847 if (outer_members
&& inner_members
)
849 EquivalenceMember
*best_outer_em
= NULL
;
850 EquivalenceMember
*best_inner_em
= NULL
;
851 Oid best_eq_op
= InvalidOid
;
855 foreach(lc1
, outer_members
)
857 EquivalenceMember
*outer_em
= (EquivalenceMember
*) lfirst(lc1
);
860 foreach(lc2
, inner_members
)
862 EquivalenceMember
*inner_em
= (EquivalenceMember
*) lfirst(lc2
);
866 eq_op
= select_equality_operator(ec
,
867 outer_em
->em_datatype
,
868 inner_em
->em_datatype
);
869 if (!OidIsValid(eq_op
))
872 if (IsA(outer_em
->em_expr
, Var
) ||
873 (IsA(outer_em
->em_expr
, RelabelType
) &&
874 IsA(((RelabelType
*) outer_em
->em_expr
)->arg
, Var
)))
876 if (IsA(inner_em
->em_expr
, Var
) ||
877 (IsA(inner_em
->em_expr
, RelabelType
) &&
878 IsA(((RelabelType
*) inner_em
->em_expr
)->arg
, Var
)))
880 if (!enable_hashjoin
|| op_hashjoinable(eq_op
))
882 if (score
> best_score
)
884 best_outer_em
= outer_em
;
885 best_inner_em
= inner_em
;
889 break; /* no need to look further */
893 break; /* no need to look further */
898 ec
->ec_broken
= true;
903 * Create clause, setting parent_ec to mark it as redundant with other
906 rinfo
= create_join_clause(root
, ec
, best_eq_op
,
907 best_outer_em
, best_inner_em
,
910 result
= lappend(result
, rinfo
);
914 * Now deal with building restrictions for any expressions that involve
915 * Vars from both sides of the join. We have to equate all of these to
916 * each other as well as to at least one old member (if any).
918 * XXX as in generate_base_implied_equalities_no_const, we could be a lot
919 * smarter here to avoid unnecessary failures in cross-type situations.
920 * For now, use the same left-to-right method used there.
924 List
*old_members
= list_concat(outer_members
, inner_members
);
925 EquivalenceMember
*prev_em
= NULL
;
928 /* For now, arbitrarily take the first old_member as the one to use */
930 new_members
= lappend(new_members
, linitial(old_members
));
932 foreach(lc1
, new_members
)
934 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc1
);
940 eq_op
= select_equality_operator(ec
,
941 prev_em
->em_datatype
,
942 cur_em
->em_datatype
);
943 if (!OidIsValid(eq_op
))
946 ec
->ec_broken
= true;
949 /* do NOT set parent_ec, this qual is not redundant! */
950 rinfo
= create_join_clause(root
, ec
, eq_op
,
954 result
= lappend(result
, rinfo
);
964 * generate_join_implied_equalities cleanup after failure
966 * Return any original RestrictInfos that are enforceable at this join.
969 generate_join_implied_equalities_broken(PlannerInfo
*root
,
970 EquivalenceClass
*ec
,
972 RelOptInfo
*outer_rel
,
973 RelOptInfo
*inner_rel
)
978 foreach(lc
, ec
->ec_sources
)
980 RestrictInfo
*restrictinfo
= (RestrictInfo
*) lfirst(lc
);
982 if (bms_is_subset(restrictinfo
->required_relids
, joinrel
->relids
) &&
983 !bms_is_subset(restrictinfo
->required_relids
, outer_rel
->relids
) &&
984 !bms_is_subset(restrictinfo
->required_relids
, inner_rel
->relids
))
985 result
= lappend(result
, restrictinfo
);
993 * select_equality_operator
994 * Select a suitable equality operator for comparing two EC members
996 * Returns InvalidOid if no operator can be found for this datatype combination
999 select_equality_operator(EquivalenceClass
*ec
, Oid lefttype
, Oid righttype
)
1003 foreach(lc
, ec
->ec_opfamilies
)
1005 Oid opfamily
= lfirst_oid(lc
);
1008 opno
= get_opfamily_member(opfamily
, lefttype
, righttype
,
1009 BTEqualStrategyNumber
);
1010 if (OidIsValid(opno
))
1018 * create_join_clause
1019 * Find or make a RestrictInfo comparing the two given EC members
1020 * with the given operator.
1022 * parent_ec is either equal to ec (if the clause is a potentially-redundant
1023 * join clause) or NULL (if not). We have to treat this as part of the
1024 * match requirements --- it's possible that a clause comparing the same two
1025 * EMs is a join clause in one join path and a restriction clause in another.
1027 static RestrictInfo
*
1028 create_join_clause(PlannerInfo
*root
,
1029 EquivalenceClass
*ec
, Oid opno
,
1030 EquivalenceMember
*leftem
,
1031 EquivalenceMember
*rightem
,
1032 EquivalenceClass
*parent_ec
)
1034 RestrictInfo
*rinfo
;
1036 MemoryContext oldcontext
;
1039 * Search to see if we already built a RestrictInfo for this pair of
1040 * EquivalenceMembers. We can use either original source clauses or
1041 * previously-derived clauses. The check on opno is probably redundant,
1044 foreach(lc
, ec
->ec_sources
)
1046 rinfo
= (RestrictInfo
*) lfirst(lc
);
1047 if (rinfo
->left_em
== leftem
&&
1048 rinfo
->right_em
== rightem
&&
1049 rinfo
->parent_ec
== parent_ec
&&
1050 opno
== ((OpExpr
*) rinfo
->clause
)->opno
)
1054 foreach(lc
, ec
->ec_derives
)
1056 rinfo
= (RestrictInfo
*) lfirst(lc
);
1057 if (rinfo
->left_em
== leftem
&&
1058 rinfo
->right_em
== rightem
&&
1059 rinfo
->parent_ec
== parent_ec
&&
1060 opno
== ((OpExpr
*) rinfo
->clause
)->opno
)
1065 * Not there, so build it, in planner context so we can re-use it. (Not
1066 * important in normal planning, but definitely so in GEQO.)
1068 oldcontext
= MemoryContextSwitchTo(root
->planner_cxt
);
1070 rinfo
= build_implied_join_equality(opno
,
1073 bms_union(leftem
->em_relids
,
1074 rightem
->em_relids
));
1076 /* Mark the clause as redundant, or not */
1077 rinfo
->parent_ec
= parent_ec
;
1080 * We can set these now, rather than letting them be looked up later,
1081 * since this is only used after EC merging is complete.
1083 rinfo
->left_ec
= ec
;
1084 rinfo
->right_ec
= ec
;
1086 /* Mark it as usable with these EMs */
1087 rinfo
->left_em
= leftem
;
1088 rinfo
->right_em
= rightem
;
1089 /* and save it for possible re-use */
1090 ec
->ec_derives
= lappend(ec
->ec_derives
, rinfo
);
1092 MemoryContextSwitchTo(oldcontext
);
1099 * reconsider_outer_join_clauses
1100 * Re-examine any outer-join clauses that were set aside by
1101 * distribute_qual_to_rels(), and see if we can derive any
1102 * EquivalenceClasses from them. Then, if they were not made
1103 * redundant, push them out into the regular join-clause lists.
1105 * When we have mergejoinable clauses A = B that are outer-join clauses,
1106 * we can't blindly combine them with other clauses A = C to deduce B = C,
1107 * since in fact the "equality" A = B won't necessarily hold above the
1108 * outer join (one of the variables might be NULL instead). Nonetheless
1109 * there are cases where we can add qual clauses using transitivity.
1111 * One case that we look for here is an outer-join clause OUTERVAR = INNERVAR
1112 * for which there is also an equivalence clause OUTERVAR = CONSTANT.
1113 * It is safe and useful to push a clause INNERVAR = CONSTANT into the
1114 * evaluation of the inner (nullable) relation, because any inner rows not
1115 * meeting this condition will not contribute to the outer-join result anyway.
1116 * (Any outer rows they could join to will be eliminated by the pushed-down
1117 * equivalence clause.)
1119 * Note that the above rule does not work for full outer joins; nor is it
1120 * very interesting to consider cases where the generated equivalence clause
1121 * would involve relations outside the outer join, since such clauses couldn't
1122 * be pushed into the inner side's scan anyway. So the restriction to
1123 * outervar = pseudoconstant is not really giving up anything.
1125 * For full-join cases, we can only do something useful if it's a FULL JOIN
1126 * USING and a merged column has an equivalence MERGEDVAR = CONSTANT.
1127 * By the time it gets here, the merged column will look like
1128 * COALESCE(LEFTVAR, RIGHTVAR)
1129 * and we will have a full-join clause LEFTVAR = RIGHTVAR that we can match
1130 * the COALESCE expression to. In this situation we can push LEFTVAR = CONSTANT
1131 * and RIGHTVAR = CONSTANT into the input relations, since any rows not
1132 * meeting these conditions cannot contribute to the join result.
1134 * Again, there isn't any traction to be gained by trying to deal with
1135 * clauses comparing a mergedvar to a non-pseudoconstant. So we can make
1136 * use of the EquivalenceClasses to search for matching variables that were
1137 * equivalenced to constants. The interesting outer-join clauses were
1138 * accumulated for us by distribute_qual_to_rels.
1140 * When we find one of these cases, we implement the changes we want by
1141 * generating a new equivalence clause INNERVAR = CONSTANT (or LEFTVAR, etc)
1142 * and pushing it into the EquivalenceClass structures. This is because we
1143 * may already know that INNERVAR is equivalenced to some other var(s), and
1144 * we'd like the constant to propagate to them too. Note that it would be
1145 * unsafe to merge any existing EC for INNERVAR with the OUTERVAR's EC ---
1146 * that could result in propagating constant restrictions from
1147 * INNERVAR to OUTERVAR, which would be very wrong.
1149 * It's possible that the INNERVAR is also an OUTERVAR for some other
1150 * outer-join clause, in which case the process can be repeated. So we repeat
1151 * looping over the lists of clauses until no further deductions can be made.
1152 * Whenever we do make a deduction, we remove the generating clause from the
1153 * lists, since we don't want to make the same deduction twice.
1155 * If we don't find any match for a set-aside outer join clause, we must
1156 * throw it back into the regular joinclause processing by passing it to
1157 * distribute_restrictinfo_to_rels(). If we do generate a derived clause,
1158 * however, the outer-join clause is redundant. We still throw it back,
1159 * because otherwise the join will be seen as a clauseless join and avoided
1160 * during join order searching; but we mark it as redundant to keep from
1161 * messing up the joinrel's size estimate. (This behavior means that the
1162 * API for this routine is uselessly complex: we could have just put all
1163 * the clauses into the regular processing initially. We keep it because
1164 * someday we might want to do something else, such as inserting "dummy"
1165 * joinclauses instead of real ones.)
1167 * Outer join clauses that are marked outerjoin_delayed are special: this
1168 * condition means that one or both VARs might go to null due to a lower
1169 * outer join. We can still push a constant through the clause, but only
1170 * if its operator is strict; and we *have to* throw the clause back into
1171 * regular joinclause processing. By keeping the strict join clause,
1172 * we ensure that any null-extended rows that are mistakenly generated due
1173 * to suppressing rows not matching the constant will be rejected at the
1174 * upper outer join. (This doesn't work for full-join clauses.)
1177 reconsider_outer_join_clauses(PlannerInfo
*root
)
1184 /* Outer loop repeats until we find no more deductions */
1189 /* Process the LEFT JOIN clauses */
1191 for (cell
= list_head(root
->left_join_clauses
); cell
; cell
= next
)
1193 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(cell
);
1196 if (reconsider_outer_join_clause(root
, rinfo
, true))
1199 /* remove it from the list */
1200 root
->left_join_clauses
=
1201 list_delete_cell(root
->left_join_clauses
, cell
, prev
);
1202 /* we throw it back anyway (see notes above) */
1203 /* but the thrown-back clause has no extra selectivity */
1204 rinfo
->norm_selec
= 2.0;
1205 rinfo
->outer_selec
= 1.0;
1206 distribute_restrictinfo_to_rels(root
, rinfo
);
1212 /* Process the RIGHT JOIN clauses */
1214 for (cell
= list_head(root
->right_join_clauses
); cell
; cell
= next
)
1216 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(cell
);
1219 if (reconsider_outer_join_clause(root
, rinfo
, false))
1222 /* remove it from the list */
1223 root
->right_join_clauses
=
1224 list_delete_cell(root
->right_join_clauses
, cell
, prev
);
1225 /* we throw it back anyway (see notes above) */
1226 /* but the thrown-back clause has no extra selectivity */
1227 rinfo
->norm_selec
= 2.0;
1228 rinfo
->outer_selec
= 1.0;
1229 distribute_restrictinfo_to_rels(root
, rinfo
);
1235 /* Process the FULL JOIN clauses */
1237 for (cell
= list_head(root
->full_join_clauses
); cell
; cell
= next
)
1239 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(cell
);
1242 if (reconsider_full_join_clause(root
, rinfo
))
1245 /* remove it from the list */
1246 root
->full_join_clauses
=
1247 list_delete_cell(root
->full_join_clauses
, cell
, prev
);
1248 /* we throw it back anyway (see notes above) */
1249 /* but the thrown-back clause has no extra selectivity */
1250 rinfo
->norm_selec
= 2.0;
1251 rinfo
->outer_selec
= 1.0;
1252 distribute_restrictinfo_to_rels(root
, rinfo
);
1259 /* Now, any remaining clauses have to be thrown back */
1260 foreach(cell
, root
->left_join_clauses
)
1262 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(cell
);
1264 distribute_restrictinfo_to_rels(root
, rinfo
);
1266 foreach(cell
, root
->right_join_clauses
)
1268 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(cell
);
1270 distribute_restrictinfo_to_rels(root
, rinfo
);
1272 foreach(cell
, root
->full_join_clauses
)
1274 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(cell
);
1276 distribute_restrictinfo_to_rels(root
, rinfo
);
1281 * reconsider_outer_join_clauses for a single LEFT/RIGHT JOIN clause
1283 * Returns TRUE if we were able to propagate a constant through the clause.
1286 reconsider_outer_join_clause(PlannerInfo
*root
, RestrictInfo
*rinfo
,
1295 Relids inner_relids
;
1298 Assert(is_opclause(rinfo
->clause
));
1299 opno
= ((OpExpr
*) rinfo
->clause
)->opno
;
1301 /* If clause is outerjoin_delayed, operator must be strict */
1302 if (rinfo
->outerjoin_delayed
&& !op_strict(opno
))
1305 /* Extract needed info from the clause */
1306 op_input_types(opno
, &left_type
, &right_type
);
1309 outervar
= (Expr
*) get_leftop(rinfo
->clause
);
1310 innervar
= (Expr
*) get_rightop(rinfo
->clause
);
1311 inner_datatype
= right_type
;
1312 inner_relids
= rinfo
->right_relids
;
1316 outervar
= (Expr
*) get_rightop(rinfo
->clause
);
1317 innervar
= (Expr
*) get_leftop(rinfo
->clause
);
1318 inner_datatype
= left_type
;
1319 inner_relids
= rinfo
->left_relids
;
1322 /* Scan EquivalenceClasses for a match to outervar */
1323 foreach(lc1
, root
->eq_classes
)
1325 EquivalenceClass
*cur_ec
= (EquivalenceClass
*) lfirst(lc1
);
1329 /* Ignore EC unless it contains pseudoconstants */
1330 if (!cur_ec
->ec_has_const
)
1332 /* Never match to a volatile EC */
1333 if (cur_ec
->ec_has_volatile
)
1335 /* It has to match the outer-join clause as to opfamilies, too */
1336 if (!equal(rinfo
->mergeopfamilies
, cur_ec
->ec_opfamilies
))
1338 /* Does it contain a match to outervar? */
1340 foreach(lc2
, cur_ec
->ec_members
)
1342 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
1344 if (equal(outervar
, cur_em
->em_expr
))
1351 continue; /* no match, so ignore this EC */
1354 * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
1355 * CONSTANT in the EC. Note that we must succeed with at least one
1356 * constant before we can decide to throw away the outer-join clause.
1359 foreach(lc2
, cur_ec
->ec_members
)
1361 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
1363 RestrictInfo
*newrinfo
;
1365 if (!cur_em
->em_is_const
)
1366 continue; /* ignore non-const members */
1367 eq_op
= select_equality_operator(cur_ec
,
1369 cur_em
->em_datatype
);
1370 if (!OidIsValid(eq_op
))
1371 continue; /* can't generate equality */
1372 newrinfo
= build_implied_join_equality(eq_op
,
1376 if (process_equivalence(root
, newrinfo
, true))
1381 * If we were able to equate INNERVAR to any constant, report success.
1382 * Otherwise, fall out of the search loop, since we know the OUTERVAR
1383 * appears in at most one EC.
1391 return false; /* failed to make any deduction */
1395 * reconsider_outer_join_clauses for a single FULL JOIN clause
1397 * Returns TRUE if we were able to propagate a constant through the clause.
1400 reconsider_full_join_clause(PlannerInfo
*root
, RestrictInfo
*rinfo
)
1411 /* Can't use an outerjoin_delayed clause here */
1412 if (rinfo
->outerjoin_delayed
)
1415 /* Extract needed info from the clause */
1416 Assert(is_opclause(rinfo
->clause
));
1417 opno
= ((OpExpr
*) rinfo
->clause
)->opno
;
1418 op_input_types(opno
, &left_type
, &right_type
);
1419 leftvar
= (Expr
*) get_leftop(rinfo
->clause
);
1420 rightvar
= (Expr
*) get_rightop(rinfo
->clause
);
1421 left_relids
= rinfo
->left_relids
;
1422 right_relids
= rinfo
->right_relids
;
1424 foreach(lc1
, root
->eq_classes
)
1426 EquivalenceClass
*cur_ec
= (EquivalenceClass
*) lfirst(lc1
);
1427 EquivalenceMember
*coal_em
= NULL
;
1433 /* Ignore EC unless it contains pseudoconstants */
1434 if (!cur_ec
->ec_has_const
)
1436 /* Never match to a volatile EC */
1437 if (cur_ec
->ec_has_volatile
)
1439 /* It has to match the outer-join clause as to opfamilies, too */
1440 if (!equal(rinfo
->mergeopfamilies
, cur_ec
->ec_opfamilies
))
1444 * Does it contain a COALESCE(leftvar, rightvar) construct?
1446 * We can assume the COALESCE() inputs are in the same order as the
1447 * join clause, since both were automatically generated in the cases
1450 * XXX currently this may fail to match in cross-type cases because
1451 * the COALESCE will contain typecast operations while the join clause
1452 * may not (if there is a cross-type mergejoin operator available for
1453 * the two column types). Is it OK to strip implicit coercions from
1454 * the COALESCE arguments?
1457 foreach(lc2
, cur_ec
->ec_members
)
1459 coal_em
= (EquivalenceMember
*) lfirst(lc2
);
1460 if (IsA(coal_em
->em_expr
, CoalesceExpr
))
1462 CoalesceExpr
*cexpr
= (CoalesceExpr
*) coal_em
->em_expr
;
1466 if (list_length(cexpr
->args
) != 2)
1468 cfirst
= (Node
*) linitial(cexpr
->args
);
1469 csecond
= (Node
*) lsecond(cexpr
->args
);
1471 if (equal(leftvar
, cfirst
) && equal(rightvar
, csecond
))
1479 continue; /* no match, so ignore this EC */
1482 * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
1483 * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
1484 * succeed with at least one constant for each var before we can
1485 * decide to throw away the outer-join clause.
1487 matchleft
= matchright
= false;
1488 foreach(lc2
, cur_ec
->ec_members
)
1490 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
1492 RestrictInfo
*newrinfo
;
1494 if (!cur_em
->em_is_const
)
1495 continue; /* ignore non-const members */
1496 eq_op
= select_equality_operator(cur_ec
,
1498 cur_em
->em_datatype
);
1499 if (OidIsValid(eq_op
))
1501 newrinfo
= build_implied_join_equality(eq_op
,
1505 if (process_equivalence(root
, newrinfo
, true))
1508 eq_op
= select_equality_operator(cur_ec
,
1510 cur_em
->em_datatype
);
1511 if (OidIsValid(eq_op
))
1513 newrinfo
= build_implied_join_equality(eq_op
,
1517 if (process_equivalence(root
, newrinfo
, true))
1523 * If we were able to equate both vars to constants, we're done, and
1524 * we can throw away the full-join clause as redundant. Moreover, we
1525 * can remove the COALESCE entry from the EC, since the added
1526 * restrictions ensure it will always have the expected value. (We
1527 * don't bother trying to update ec_relids or ec_sources.)
1529 if (matchleft
&& matchright
)
1531 cur_ec
->ec_members
= list_delete_ptr(cur_ec
->ec_members
, coal_em
);
1536 * Otherwise, fall out of the search loop, since we know the COALESCE
1537 * appears in at most one EC (XXX might stop being true if we allow
1538 * stripping of coercions above?)
1543 return false; /* failed to make any deduction */
1549 * Detect whether two expressions are known equal due to equivalence
1552 * Actually, this only shows that the expressions are equal according
1553 * to some opfamily's notion of equality --- but we only use it for
1554 * selectivity estimation, so a fuzzy idea of equality is OK.
1556 * Note: does not bother to check for "equal(item1, item2)"; caller must
1557 * check that case if it's possible to pass identical items.
1560 exprs_known_equal(PlannerInfo
*root
, Node
*item1
, Node
*item2
)
1564 foreach(lc1
, root
->eq_classes
)
1566 EquivalenceClass
*ec
= (EquivalenceClass
*) lfirst(lc1
);
1567 bool item1member
= false;
1568 bool item2member
= false;
1571 /* Never match to a volatile EC */
1572 if (ec
->ec_has_volatile
)
1575 foreach(lc2
, ec
->ec_members
)
1577 EquivalenceMember
*em
= (EquivalenceMember
*) lfirst(lc2
);
1579 if (equal(item1
, em
->em_expr
))
1581 else if (equal(item2
, em
->em_expr
))
1583 /* Exit as soon as equality is proven */
1584 if (item1member
&& item2member
)
1593 * add_child_rel_equivalences
1594 * Search for EC members that reference (only) the parent_rel, and
1595 * add transformed members referencing the child_rel.
1597 * We only need to do this for ECs that could generate join conditions,
1598 * since the child members are only used for creating inner-indexscan paths.
1600 * parent_rel and child_rel could be derived from appinfo, but since the
1601 * caller has already computed them, we might as well just pass them in.
1604 add_child_rel_equivalences(PlannerInfo
*root
,
1605 AppendRelInfo
*appinfo
,
1606 RelOptInfo
*parent_rel
,
1607 RelOptInfo
*child_rel
)
1611 foreach(lc1
, root
->eq_classes
)
1613 EquivalenceClass
*cur_ec
= (EquivalenceClass
*) lfirst(lc1
);
1617 * Won't generate joinclauses if const or single-member (the latter
1618 * test covers the volatile case too)
1620 if (cur_ec
->ec_has_const
|| list_length(cur_ec
->ec_members
) <= 1)
1623 /* No point in searching if parent rel not mentioned in eclass */
1624 if (!bms_is_subset(parent_rel
->relids
, cur_ec
->ec_relids
))
1627 foreach(lc2
, cur_ec
->ec_members
)
1629 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
1631 /* Does it reference (only) parent_rel? */
1632 if (bms_equal(cur_em
->em_relids
, parent_rel
->relids
))
1634 /* Yes, generate transformed child version */
1637 child_expr
= (Expr
*)
1638 adjust_appendrel_attrs((Node
*) cur_em
->em_expr
,
1640 (void) add_eq_member(cur_ec
, child_expr
, child_rel
->relids
,
1641 true, cur_em
->em_datatype
);
1649 * mutate_eclass_expressions
1650 * Apply an expression tree mutator to all expressions stored in
1651 * equivalence classes.
1653 * This is a bit of a hack ... it's currently needed only by planagg.c,
1654 * which needs to do a global search-and-replace of MIN/MAX Aggrefs
1655 * after eclasses are already set up. Without changing the eclasses too,
1656 * subsequent matching of ORDER BY clauses would fail.
1658 * Note that we assume the mutation won't affect relation membership or any
1659 * other properties we keep track of (which is a bit bogus, but by the time
1660 * planagg.c runs, it no longer matters). Also we must be called in the
1661 * main planner memory context.
1664 mutate_eclass_expressions(PlannerInfo
*root
,
1665 Node
*(*mutator
) (),
1670 foreach(lc1
, root
->eq_classes
)
1672 EquivalenceClass
*cur_ec
= (EquivalenceClass
*) lfirst(lc1
);
1675 foreach(lc2
, cur_ec
->ec_members
)
1677 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
1679 cur_em
->em_expr
= (Expr
*)
1680 mutator((Node
*) cur_em
->em_expr
, context
);
1687 * find_eclass_clauses_for_index_join
1688 * Create joinclauses usable for a nestloop-with-inner-indexscan
1689 * scanning the given inner rel with the specified set of outer rels.
1692 find_eclass_clauses_for_index_join(PlannerInfo
*root
, RelOptInfo
*rel
,
1693 Relids outer_relids
)
1696 bool is_child_rel
= (rel
->reloptkind
== RELOPT_OTHER_MEMBER_REL
);
1699 foreach(lc1
, root
->eq_classes
)
1701 EquivalenceClass
*cur_ec
= (EquivalenceClass
*) lfirst(lc1
);
1705 * Won't generate joinclauses if const or single-member (the latter
1706 * test covers the volatile case too)
1708 if (cur_ec
->ec_has_const
|| list_length(cur_ec
->ec_members
) <= 1)
1712 * No point in searching if rel not mentioned in eclass (but we can't
1713 * tell that for a child rel).
1715 if (!is_child_rel
&&
1716 !bms_is_subset(rel
->relids
, cur_ec
->ec_relids
))
1718 /* ... nor if no overlap with outer_relids */
1719 if (!bms_overlap(outer_relids
, cur_ec
->ec_relids
))
1722 /* Scan members, looking for indexable columns */
1723 foreach(lc2
, cur_ec
->ec_members
)
1725 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
1726 EquivalenceMember
*best_outer_em
= NULL
;
1727 Oid best_eq_op
= InvalidOid
;
1730 if (!bms_equal(cur_em
->em_relids
, rel
->relids
) ||
1731 !eclass_matches_any_index(cur_ec
, cur_em
, rel
))
1735 * Found one, so try to generate a join clause. This is like
1736 * generate_join_implied_equalities_normal, except simpler since
1737 * our only preference item is to pick a Var on the outer side. We
1738 * only need one join clause per index col.
1740 foreach(lc3
, cur_ec
->ec_members
)
1742 EquivalenceMember
*outer_em
= (EquivalenceMember
*) lfirst(lc3
);
1745 if (!bms_is_subset(outer_em
->em_relids
, outer_relids
))
1747 eq_op
= select_equality_operator(cur_ec
,
1748 cur_em
->em_datatype
,
1749 outer_em
->em_datatype
);
1750 if (!OidIsValid(eq_op
))
1752 best_outer_em
= outer_em
;
1754 if (IsA(outer_em
->em_expr
, Var
) ||
1755 (IsA(outer_em
->em_expr
, RelabelType
) &&
1756 IsA(((RelabelType
*) outer_em
->em_expr
)->arg
, Var
)))
1757 break; /* no need to look further */
1762 /* Found a suitable joinclause */
1763 RestrictInfo
*rinfo
;
1765 /* set parent_ec to mark as redundant with other joinclauses */
1766 rinfo
= create_join_clause(root
, cur_ec
, best_eq_op
,
1767 cur_em
, best_outer_em
,
1770 result
= lappend(result
, rinfo
);
1773 * Note: we keep scanning here because we want to provide a
1774 * clause for every possible indexcol.
1785 * have_relevant_eclass_joinclause
1786 * Detect whether there is an EquivalenceClass that could produce
1787 * a joinclause between the two given relations.
1789 * This is essentially a very cut-down version of
1790 * generate_join_implied_equalities(). Note it's OK to occasionally say "yes"
1791 * incorrectly. Hence we don't bother with details like whether the lack of a
1792 * cross-type operator might prevent the clause from actually being generated.
1795 have_relevant_eclass_joinclause(PlannerInfo
*root
,
1796 RelOptInfo
*rel1
, RelOptInfo
*rel2
)
1800 foreach(lc1
, root
->eq_classes
)
1802 EquivalenceClass
*ec
= (EquivalenceClass
*) lfirst(lc1
);
1808 * Won't generate joinclauses if single-member (this test covers the
1809 * volatile case too)
1811 if (list_length(ec
->ec_members
) <= 1)
1815 * Note we don't test ec_broken; if we did, we'd need a separate code
1816 * path to look through ec_sources. Checking the members anyway is OK
1817 * as a possibly-overoptimistic heuristic.
1819 * We don't test ec_has_const either, even though a const eclass won't
1820 * generate real join clauses. This is because if we had "WHERE a.x =
1821 * b.y and a.x = 42", it is worth considering a join between a and b,
1822 * since the join result is likely to be small even though it'll end
1823 * up being an unqualified nestloop.
1826 /* Needn't scan if it couldn't contain members from each rel */
1827 if (!bms_overlap(rel1
->relids
, ec
->ec_relids
) ||
1828 !bms_overlap(rel2
->relids
, ec
->ec_relids
))
1831 /* Scan the EC to see if it has member(s) in each rel */
1832 has_rel1
= has_rel2
= false;
1833 foreach(lc2
, ec
->ec_members
)
1835 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
1837 if (cur_em
->em_is_const
|| cur_em
->em_is_child
)
1838 continue; /* ignore consts and children here */
1839 if (bms_is_subset(cur_em
->em_relids
, rel1
->relids
))
1845 if (bms_is_subset(cur_em
->em_relids
, rel2
->relids
))
1853 if (has_rel1
&& has_rel2
)
1862 * has_relevant_eclass_joinclause
1863 * Detect whether there is an EquivalenceClass that could produce
1864 * a joinclause between the given relation and anything else.
1866 * This is the same as have_relevant_eclass_joinclause with the other rel
1867 * implicitly defined as "everything else in the query".
1870 has_relevant_eclass_joinclause(PlannerInfo
*root
, RelOptInfo
*rel1
)
1874 foreach(lc1
, root
->eq_classes
)
1876 EquivalenceClass
*ec
= (EquivalenceClass
*) lfirst(lc1
);
1882 * Won't generate joinclauses if single-member (this test covers the
1883 * volatile case too)
1885 if (list_length(ec
->ec_members
) <= 1)
1889 * Note we don't test ec_broken; if we did, we'd need a separate code
1890 * path to look through ec_sources. Checking the members anyway is OK
1891 * as a possibly-overoptimistic heuristic.
1893 * We don't test ec_has_const either, even though a const eclass won't
1894 * generate real join clauses. This is because if we had "WHERE a.x =
1895 * b.y and a.x = 42", it is worth considering a join between a and b,
1896 * since the join result is likely to be small even though it'll end
1897 * up being an unqualified nestloop.
1900 /* Needn't scan if it couldn't contain members from each rel */
1901 if (!bms_overlap(rel1
->relids
, ec
->ec_relids
) ||
1902 bms_is_subset(ec
->ec_relids
, rel1
->relids
))
1905 /* Scan the EC to see if it has member(s) in each rel */
1906 has_rel1
= has_rel2
= false;
1907 foreach(lc2
, ec
->ec_members
)
1909 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc2
);
1911 if (cur_em
->em_is_const
|| cur_em
->em_is_child
)
1912 continue; /* ignore consts and children here */
1913 if (bms_is_subset(cur_em
->em_relids
, rel1
->relids
))
1919 if (!bms_overlap(cur_em
->em_relids
, rel1
->relids
))
1927 if (has_rel1
&& has_rel2
)
1936 * eclass_useful_for_merging
1937 * Detect whether the EC could produce any mergejoinable join clauses
1938 * against the specified relation.
1940 * This is just a heuristic test and doesn't have to be exact; it's better
1941 * to say "yes" incorrectly than "no". Hence we don't bother with details
1942 * like whether the lack of a cross-type operator might prevent the clause
1943 * from actually being generated.
1946 eclass_useful_for_merging(EquivalenceClass
*eclass
,
1951 Assert(!eclass
->ec_merged
);
1954 * Won't generate joinclauses if const or single-member (the latter test
1955 * covers the volatile case too)
1957 if (eclass
->ec_has_const
|| list_length(eclass
->ec_members
) <= 1)
1961 * Note we don't test ec_broken; if we did, we'd need a separate code path
1962 * to look through ec_sources. Checking the members anyway is OK as a
1963 * possibly-overoptimistic heuristic.
1966 /* If rel already includes all members of eclass, no point in searching */
1967 if (bms_is_subset(eclass
->ec_relids
, rel
->relids
))
1970 /* To join, we need a member not in the given rel */
1971 foreach(lc
, eclass
->ec_members
)
1973 EquivalenceMember
*cur_em
= (EquivalenceMember
*) lfirst(lc
);
1975 if (!cur_em
->em_is_child
&&
1976 !bms_overlap(cur_em
->em_relids
, rel
->relids
))