Update copyright for 2022
[pgsql.git] / src / backend / optimizer / path / equivclass.c
blob8c6770de972dad59638773e51a173d04d37c952c
1 /*-------------------------------------------------------------------------
3 * equivclass.c
4 * Routines for managing EquivalenceClasses
6 * See src/backend/optimizer/README for discussion of EquivalenceClasses.
9 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
12 * IDENTIFICATION
13 * src/backend/optimizer/path/equivclass.c
15 *-------------------------------------------------------------------------
17 #include "postgres.h"
19 #include <limits.h>
21 #include "access/stratnum.h"
22 #include "catalog/pg_type.h"
23 #include "nodes/makefuncs.h"
24 #include "nodes/nodeFuncs.h"
25 #include "optimizer/appendinfo.h"
26 #include "optimizer/clauses.h"
27 #include "optimizer/optimizer.h"
28 #include "optimizer/pathnode.h"
29 #include "optimizer/paths.h"
30 #include "optimizer/planmain.h"
31 #include "optimizer/restrictinfo.h"
32 #include "utils/lsyscache.h"
35 static EquivalenceMember *add_eq_member(EquivalenceClass *ec,
36 Expr *expr, Relids relids, Relids nullable_relids,
37 bool is_child, Oid datatype);
38 static bool is_exprlist_member(Expr *node, List *exprs);
39 static void generate_base_implied_equalities_const(PlannerInfo *root,
40 EquivalenceClass *ec);
41 static void generate_base_implied_equalities_no_const(PlannerInfo *root,
42 EquivalenceClass *ec);
43 static void generate_base_implied_equalities_broken(PlannerInfo *root,
44 EquivalenceClass *ec);
45 static List *generate_join_implied_equalities_normal(PlannerInfo *root,
46 EquivalenceClass *ec,
47 Relids join_relids,
48 Relids outer_relids,
49 Relids inner_relids);
50 static List *generate_join_implied_equalities_broken(PlannerInfo *root,
51 EquivalenceClass *ec,
52 Relids nominal_join_relids,
53 Relids outer_relids,
54 Relids nominal_inner_relids,
55 RelOptInfo *inner_rel);
56 static Oid select_equality_operator(EquivalenceClass *ec,
57 Oid lefttype, Oid righttype);
58 static RestrictInfo *create_join_clause(PlannerInfo *root,
59 EquivalenceClass *ec, Oid opno,
60 EquivalenceMember *leftem,
61 EquivalenceMember *rightem,
62 EquivalenceClass *parent_ec);
63 static bool reconsider_outer_join_clause(PlannerInfo *root,
64 RestrictInfo *rinfo,
65 bool outer_on_left);
66 static bool reconsider_full_join_clause(PlannerInfo *root,
67 RestrictInfo *rinfo);
68 static Bitmapset *get_eclass_indexes_for_relids(PlannerInfo *root,
69 Relids relids);
70 static Bitmapset *get_common_eclass_indexes(PlannerInfo *root, Relids relids1,
71 Relids relids2);
75 * process_equivalence
76 * The given clause has a mergejoinable operator and can be applied without
77 * any delay by an outer join, so its two sides can be considered equal
78 * anywhere they are both computable; moreover that equality can be
79 * extended transitively. Record this knowledge in the EquivalenceClass
80 * data structure, if applicable. Returns true if successful, false if not
81 * (in which case caller should treat the clause as ordinary, not an
82 * equivalence).
84 * In some cases, although we cannot convert a clause into EquivalenceClass
85 * knowledge, we can still modify it to a more useful form than the original.
86 * Then, *p_restrictinfo will be replaced by a new RestrictInfo, which is what
87 * the caller should use for further processing.
89 * If below_outer_join is true, then the clause was found below the nullable
90 * side of an outer join, so its sides might validly be both NULL rather than
91 * strictly equal. We can still deduce equalities in such cases, but we take
92 * care to mark an EquivalenceClass if it came from any such clauses. Also,
93 * we have to check that both sides are either pseudo-constants or strict
94 * functions of Vars, else they might not both go to NULL above the outer
95 * join. (This is the main reason why we need a failure return. It's more
96 * convenient to check this case here than at the call sites...)
98 * We also reject proposed equivalence clauses if they contain leaky functions
99 * and have security_level above zero. The EC evaluation rules require us to
100 * apply certain tests at certain joining levels, and we can't tolerate
101 * delaying any test on security_level grounds. By rejecting candidate clauses
102 * that might require security delays, we ensure it's safe to apply an EC
103 * clause as soon as it's supposed to be applied.
105 * On success return, we have also initialized the clause's left_ec/right_ec
106 * fields to point to the EquivalenceClass representing it. This saves lookup
107 * effort later.
109 * Note: constructing merged EquivalenceClasses is a standard UNION-FIND
110 * problem, for which there exist better data structures than simple lists.
111 * If this code ever proves to be a bottleneck then it could be sped up ---
112 * but for now, simple is beautiful.
114 * Note: this is only called during planner startup, not during GEQO
115 * exploration, so we need not worry about whether we're in the right
116 * memory context.
118 bool
119 process_equivalence(PlannerInfo *root,
120 RestrictInfo **p_restrictinfo,
121 bool below_outer_join)
123 RestrictInfo *restrictinfo = *p_restrictinfo;
124 Expr *clause = restrictinfo->clause;
125 Oid opno,
126 collation,
127 item1_type,
128 item2_type;
129 Expr *item1;
130 Expr *item2;
131 Relids item1_relids,
132 item2_relids,
133 item1_nullable_relids,
134 item2_nullable_relids;
135 List *opfamilies;
136 EquivalenceClass *ec1,
137 *ec2;
138 EquivalenceMember *em1,
139 *em2;
140 ListCell *lc1;
141 int ec2_idx;
143 /* Should not already be marked as having generated an eclass */
144 Assert(restrictinfo->left_ec == NULL);
145 Assert(restrictinfo->right_ec == NULL);
147 /* Reject if it is potentially postponable by security considerations */
148 if (restrictinfo->security_level > 0 && !restrictinfo->leakproof)
149 return false;
151 /* Extract info from given clause */
152 Assert(is_opclause(clause));
153 opno = ((OpExpr *) clause)->opno;
154 collation = ((OpExpr *) clause)->inputcollid;
155 item1 = (Expr *) get_leftop(clause);
156 item2 = (Expr *) get_rightop(clause);
157 item1_relids = restrictinfo->left_relids;
158 item2_relids = restrictinfo->right_relids;
161 * Ensure both input expressions expose the desired collation (their types
162 * should be OK already); see comments for canonicalize_ec_expression.
164 item1 = canonicalize_ec_expression(item1,
165 exprType((Node *) item1),
166 collation);
167 item2 = canonicalize_ec_expression(item2,
168 exprType((Node *) item2),
169 collation);
172 * Clauses of the form X=X cannot be translated into EquivalenceClasses.
173 * We'd either end up with a single-entry EC, losing the knowledge that
174 * the clause was present at all, or else make an EC with duplicate
175 * entries, causing other issues.
177 if (equal(item1, item2))
180 * If the operator is strict, then the clause can be treated as just
181 * "X IS NOT NULL". (Since we know we are considering a top-level
182 * qual, we can ignore the difference between FALSE and NULL results.)
183 * It's worth making the conversion because we'll typically get a much
184 * better selectivity estimate than we would for X=X.
186 * If the operator is not strict, we can't be sure what it will do
187 * with NULLs, so don't attempt to optimize it.
189 set_opfuncid((OpExpr *) clause);
190 if (func_strict(((OpExpr *) clause)->opfuncid))
192 NullTest *ntest = makeNode(NullTest);
194 ntest->arg = item1;
195 ntest->nulltesttype = IS_NOT_NULL;
196 ntest->argisrow = false; /* correct even if composite arg */
197 ntest->location = -1;
199 *p_restrictinfo =
200 make_restrictinfo(root,
201 (Expr *) ntest,
202 restrictinfo->is_pushed_down,
203 restrictinfo->outerjoin_delayed,
204 restrictinfo->pseudoconstant,
205 restrictinfo->security_level,
206 NULL,
207 restrictinfo->outer_relids,
208 restrictinfo->nullable_relids);
210 return false;
214 * If below outer join, check for strictness, else reject.
216 if (below_outer_join)
218 if (!bms_is_empty(item1_relids) &&
219 contain_nonstrict_functions((Node *) item1))
220 return false; /* LHS is non-strict but not constant */
221 if (!bms_is_empty(item2_relids) &&
222 contain_nonstrict_functions((Node *) item2))
223 return false; /* RHS is non-strict but not constant */
226 /* Calculate nullable-relid sets for each side of the clause */
227 item1_nullable_relids = bms_intersect(item1_relids,
228 restrictinfo->nullable_relids);
229 item2_nullable_relids = bms_intersect(item2_relids,
230 restrictinfo->nullable_relids);
233 * We use the declared input types of the operator, not exprType() of the
234 * inputs, as the nominal datatypes for opfamily lookup. This presumes
235 * that btree operators are always registered with amoplefttype and
236 * amoprighttype equal to their declared input types. We will need this
237 * info anyway to build EquivalenceMember nodes, and by extracting it now
238 * we can use type comparisons to short-circuit some equal() tests.
240 op_input_types(opno, &item1_type, &item2_type);
242 opfamilies = restrictinfo->mergeopfamilies;
245 * Sweep through the existing EquivalenceClasses looking for matches to
246 * item1 and item2. These are the possible outcomes:
248 * 1. We find both in the same EC. The equivalence is already known, so
249 * there's nothing to do.
251 * 2. We find both in different ECs. Merge the two ECs together.
253 * 3. We find just one. Add the other to its EC.
255 * 4. We find neither. Make a new, two-entry EC.
257 * Note: since all ECs are built through this process or the similar
258 * search in get_eclass_for_sort_expr(), it's impossible that we'd match
259 * an item in more than one existing nonvolatile EC. So it's okay to stop
260 * at the first match.
262 ec1 = ec2 = NULL;
263 em1 = em2 = NULL;
264 ec2_idx = -1;
265 foreach(lc1, root->eq_classes)
267 EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
268 ListCell *lc2;
270 /* Never match to a volatile EC */
271 if (cur_ec->ec_has_volatile)
272 continue;
275 * The collation has to match; check this first since it's cheaper
276 * than the opfamily comparison.
278 if (collation != cur_ec->ec_collation)
279 continue;
282 * A "match" requires matching sets of btree opfamilies. Use of
283 * equal() for this test has implications discussed in the comments
284 * for get_mergejoin_opfamilies().
286 if (!equal(opfamilies, cur_ec->ec_opfamilies))
287 continue;
289 foreach(lc2, cur_ec->ec_members)
291 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
293 Assert(!cur_em->em_is_child); /* no children yet */
296 * If below an outer join, don't match constants: they're not as
297 * constant as they look.
299 if ((below_outer_join || cur_ec->ec_below_outer_join) &&
300 cur_em->em_is_const)
301 continue;
303 if (!ec1 &&
304 item1_type == cur_em->em_datatype &&
305 equal(item1, cur_em->em_expr))
307 ec1 = cur_ec;
308 em1 = cur_em;
309 if (ec2)
310 break;
313 if (!ec2 &&
314 item2_type == cur_em->em_datatype &&
315 equal(item2, cur_em->em_expr))
317 ec2 = cur_ec;
318 ec2_idx = foreach_current_index(lc1);
319 em2 = cur_em;
320 if (ec1)
321 break;
325 if (ec1 && ec2)
326 break;
329 /* Sweep finished, what did we find? */
331 if (ec1 && ec2)
333 /* If case 1, nothing to do, except add to sources */
334 if (ec1 == ec2)
336 ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
337 ec1->ec_below_outer_join |= below_outer_join;
338 ec1->ec_min_security = Min(ec1->ec_min_security,
339 restrictinfo->security_level);
340 ec1->ec_max_security = Max(ec1->ec_max_security,
341 restrictinfo->security_level);
342 /* mark the RI as associated with this eclass */
343 restrictinfo->left_ec = ec1;
344 restrictinfo->right_ec = ec1;
345 /* mark the RI as usable with this pair of EMs */
346 restrictinfo->left_em = em1;
347 restrictinfo->right_em = em2;
348 return true;
352 * Case 2: need to merge ec1 and ec2. This should never happen after
353 * the ECs have reached canonical state; otherwise, pathkeys could be
354 * rendered non-canonical by the merge, and relation eclass indexes
355 * would get broken by removal of an eq_classes list entry.
357 if (root->ec_merging_done)
358 elog(ERROR, "too late to merge equivalence classes");
361 * We add ec2's items to ec1, then set ec2's ec_merged link to point
362 * to ec1 and remove ec2 from the eq_classes list. We cannot simply
363 * delete ec2 because that could leave dangling pointers in existing
364 * PathKeys. We leave it behind with a link so that the merged EC can
365 * be found.
367 ec1->ec_members = list_concat(ec1->ec_members, ec2->ec_members);
368 ec1->ec_sources = list_concat(ec1->ec_sources, ec2->ec_sources);
369 ec1->ec_derives = list_concat(ec1->ec_derives, ec2->ec_derives);
370 ec1->ec_relids = bms_join(ec1->ec_relids, ec2->ec_relids);
371 ec1->ec_has_const |= ec2->ec_has_const;
372 /* can't need to set has_volatile */
373 ec1->ec_below_outer_join |= ec2->ec_below_outer_join;
374 ec1->ec_min_security = Min(ec1->ec_min_security,
375 ec2->ec_min_security);
376 ec1->ec_max_security = Max(ec1->ec_max_security,
377 ec2->ec_max_security);
378 ec2->ec_merged = ec1;
379 root->eq_classes = list_delete_nth_cell(root->eq_classes, ec2_idx);
380 /* just to avoid debugging confusion w/ dangling pointers: */
381 ec2->ec_members = NIL;
382 ec2->ec_sources = NIL;
383 ec2->ec_derives = NIL;
384 ec2->ec_relids = NULL;
385 ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
386 ec1->ec_below_outer_join |= below_outer_join;
387 ec1->ec_min_security = Min(ec1->ec_min_security,
388 restrictinfo->security_level);
389 ec1->ec_max_security = Max(ec1->ec_max_security,
390 restrictinfo->security_level);
391 /* mark the RI as associated with this eclass */
392 restrictinfo->left_ec = ec1;
393 restrictinfo->right_ec = ec1;
394 /* mark the RI as usable with this pair of EMs */
395 restrictinfo->left_em = em1;
396 restrictinfo->right_em = em2;
398 else if (ec1)
400 /* Case 3: add item2 to ec1 */
401 em2 = add_eq_member(ec1, item2, item2_relids, item2_nullable_relids,
402 false, item2_type);
403 ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
404 ec1->ec_below_outer_join |= below_outer_join;
405 ec1->ec_min_security = Min(ec1->ec_min_security,
406 restrictinfo->security_level);
407 ec1->ec_max_security = Max(ec1->ec_max_security,
408 restrictinfo->security_level);
409 /* mark the RI as associated with this eclass */
410 restrictinfo->left_ec = ec1;
411 restrictinfo->right_ec = ec1;
412 /* mark the RI as usable with this pair of EMs */
413 restrictinfo->left_em = em1;
414 restrictinfo->right_em = em2;
416 else if (ec2)
418 /* Case 3: add item1 to ec2 */
419 em1 = add_eq_member(ec2, item1, item1_relids, item1_nullable_relids,
420 false, item1_type);
421 ec2->ec_sources = lappend(ec2->ec_sources, restrictinfo);
422 ec2->ec_below_outer_join |= below_outer_join;
423 ec2->ec_min_security = Min(ec2->ec_min_security,
424 restrictinfo->security_level);
425 ec2->ec_max_security = Max(ec2->ec_max_security,
426 restrictinfo->security_level);
427 /* mark the RI as associated with this eclass */
428 restrictinfo->left_ec = ec2;
429 restrictinfo->right_ec = ec2;
430 /* mark the RI as usable with this pair of EMs */
431 restrictinfo->left_em = em1;
432 restrictinfo->right_em = em2;
434 else
436 /* Case 4: make a new, two-entry EC */
437 EquivalenceClass *ec = makeNode(EquivalenceClass);
439 ec->ec_opfamilies = opfamilies;
440 ec->ec_collation = collation;
441 ec->ec_members = NIL;
442 ec->ec_sources = list_make1(restrictinfo);
443 ec->ec_derives = NIL;
444 ec->ec_relids = NULL;
445 ec->ec_has_const = false;
446 ec->ec_has_volatile = false;
447 ec->ec_below_outer_join = below_outer_join;
448 ec->ec_broken = false;
449 ec->ec_sortref = 0;
450 ec->ec_min_security = restrictinfo->security_level;
451 ec->ec_max_security = restrictinfo->security_level;
452 ec->ec_merged = NULL;
453 em1 = add_eq_member(ec, item1, item1_relids, item1_nullable_relids,
454 false, item1_type);
455 em2 = add_eq_member(ec, item2, item2_relids, item2_nullable_relids,
456 false, item2_type);
458 root->eq_classes = lappend(root->eq_classes, ec);
460 /* mark the RI as associated with this eclass */
461 restrictinfo->left_ec = ec;
462 restrictinfo->right_ec = ec;
463 /* mark the RI as usable with this pair of EMs */
464 restrictinfo->left_em = em1;
465 restrictinfo->right_em = em2;
468 return true;
472 * canonicalize_ec_expression
474 * This function ensures that the expression exposes the expected type and
475 * collation, so that it will be equal() to other equivalence-class expressions
476 * that it ought to be equal() to.
478 * The rule for datatypes is that the exposed type should match what it would
479 * be for an input to an operator of the EC's opfamilies; which is usually
480 * the declared input type of the operator, but in the case of polymorphic
481 * operators no relabeling is wanted (compare the behavior of parse_coerce.c).
482 * Expressions coming in from quals will generally have the right type
483 * already, but expressions coming from indexkeys may not (because they are
484 * represented without any explicit relabel in pg_index), and the same problem
485 * occurs for sort expressions (because the parser is likewise cavalier about
486 * putting relabels on them). Such cases will be binary-compatible with the
487 * real operators, so adding a RelabelType is sufficient.
489 * Also, the expression's exposed collation must match the EC's collation.
490 * This is important because in comparisons like "foo < bar COLLATE baz",
491 * only one of the expressions has the correct exposed collation as we receive
492 * it from the parser. Forcing both of them to have it ensures that all
493 * variant spellings of such a construct behave the same. Again, we can
494 * stick on a RelabelType to force the right exposed collation. (It might
495 * work to not label the collation at all in EC members, but this is risky
496 * since some parts of the system expect exprCollation() to deliver the
497 * right answer for a sort key.)
499 Expr *
500 canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
502 Oid expr_type = exprType((Node *) expr);
505 * For a polymorphic-input-type opclass, just keep the same exposed type.
506 * RECORD opclasses work like polymorphic-type ones for this purpose.
508 if (IsPolymorphicType(req_type) || req_type == RECORDOID)
509 req_type = expr_type;
512 * No work if the expression exposes the right type/collation already.
514 if (expr_type != req_type ||
515 exprCollation((Node *) expr) != req_collation)
518 * If we have to change the type of the expression, set typmod to -1,
519 * since the new type may not have the same typmod interpretation.
520 * When we only have to change collation, preserve the exposed typmod.
522 int32 req_typmod;
524 if (expr_type != req_type)
525 req_typmod = -1;
526 else
527 req_typmod = exprTypmod((Node *) expr);
530 * Use applyRelabelType so that we preserve const-flatness. This is
531 * important since eval_const_expressions has already been applied.
533 expr = (Expr *) applyRelabelType((Node *) expr,
534 req_type, req_typmod, req_collation,
535 COERCE_IMPLICIT_CAST, -1, false);
538 return expr;
542 * add_eq_member - build a new EquivalenceMember and add it to an EC
544 static EquivalenceMember *
545 add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids,
546 Relids nullable_relids, bool is_child, Oid datatype)
548 EquivalenceMember *em = makeNode(EquivalenceMember);
550 em->em_expr = expr;
551 em->em_relids = relids;
552 em->em_nullable_relids = nullable_relids;
553 em->em_is_const = false;
554 em->em_is_child = is_child;
555 em->em_datatype = datatype;
557 if (bms_is_empty(relids))
560 * No Vars, assume it's a pseudoconstant. This is correct for entries
561 * generated from process_equivalence(), because a WHERE clause can't
562 * contain aggregates or SRFs, and non-volatility was checked before
563 * process_equivalence() ever got called. But
564 * get_eclass_for_sort_expr() has to work harder. We put the tests
565 * there not here to save cycles in the equivalence case.
567 Assert(!is_child);
568 em->em_is_const = true;
569 ec->ec_has_const = true;
570 /* it can't affect ec_relids */
572 else if (!is_child) /* child members don't add to ec_relids */
574 ec->ec_relids = bms_add_members(ec->ec_relids, relids);
576 ec->ec_members = lappend(ec->ec_members, em);
578 return em;
583 * get_eclass_for_sort_expr
584 * Given an expression and opfamily/collation info, find an existing
585 * equivalence class it is a member of; if none, optionally build a new
586 * single-member EquivalenceClass for it.
588 * expr is the expression, and nullable_relids is the set of base relids
589 * that are potentially nullable below it. We actually only care about
590 * the set of such relids that are used in the expression; but for caller
591 * convenience, we perform that intersection step here. The caller need
592 * only be sure that nullable_relids doesn't omit any nullable rels that
593 * might appear in the expr.
595 * sortref is the SortGroupRef of the originating SortGroupClause, if any,
596 * or zero if not. (It should never be zero if the expression is volatile!)
598 * If rel is not NULL, it identifies a specific relation we're considering
599 * a path for, and indicates that child EC members for that relation can be
600 * considered. Otherwise child members are ignored. (Note: since child EC
601 * members aren't guaranteed unique, a non-NULL value means that there could
602 * be more than one EC that matches the expression; if so it's order-dependent
603 * which one you get. This is annoying but it only happens in corner cases,
604 * so for now we live with just reporting the first match. See also
605 * generate_implied_equalities_for_column and match_pathkeys_to_index.)
607 * If create_it is true, we'll build a new EquivalenceClass when there is no
608 * match. If create_it is false, we just return NULL when no match.
610 * This can be used safely both before and after EquivalenceClass merging;
611 * since it never causes merging it does not invalidate any existing ECs
612 * or PathKeys. However, ECs added after path generation has begun are
613 * of limited usefulness, so usually it's best to create them beforehand.
615 * Note: opfamilies must be chosen consistently with the way
616 * process_equivalence() would do; that is, generated from a mergejoinable
617 * equality operator. Else we might fail to detect valid equivalences,
618 * generating poor (but not incorrect) plans.
620 EquivalenceClass *
621 get_eclass_for_sort_expr(PlannerInfo *root,
622 Expr *expr,
623 Relids nullable_relids,
624 List *opfamilies,
625 Oid opcintype,
626 Oid collation,
627 Index sortref,
628 Relids rel,
629 bool create_it)
631 Relids expr_relids;
632 EquivalenceClass *newec;
633 EquivalenceMember *newem;
634 ListCell *lc1;
635 MemoryContext oldcontext;
638 * Ensure the expression exposes the correct type and collation.
640 expr = canonicalize_ec_expression(expr, opcintype, collation);
643 * Scan through the existing EquivalenceClasses for a match
645 foreach(lc1, root->eq_classes)
647 EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
648 ListCell *lc2;
651 * Never match to a volatile EC, except when we are looking at another
652 * reference to the same volatile SortGroupClause.
654 if (cur_ec->ec_has_volatile &&
655 (sortref == 0 || sortref != cur_ec->ec_sortref))
656 continue;
658 if (collation != cur_ec->ec_collation)
659 continue;
660 if (!equal(opfamilies, cur_ec->ec_opfamilies))
661 continue;
663 foreach(lc2, cur_ec->ec_members)
665 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
668 * Ignore child members unless they match the request.
670 if (cur_em->em_is_child &&
671 !bms_equal(cur_em->em_relids, rel))
672 continue;
675 * If below an outer join, don't match constants: they're not as
676 * constant as they look.
678 if (cur_ec->ec_below_outer_join &&
679 cur_em->em_is_const)
680 continue;
682 if (opcintype == cur_em->em_datatype &&
683 equal(expr, cur_em->em_expr))
684 return cur_ec; /* Match! */
688 /* No match; does caller want a NULL result? */
689 if (!create_it)
690 return NULL;
693 * OK, build a new single-member EC
695 * Here, we must be sure that we construct the EC in the right context.
697 oldcontext = MemoryContextSwitchTo(root->planner_cxt);
699 newec = makeNode(EquivalenceClass);
700 newec->ec_opfamilies = list_copy(opfamilies);
701 newec->ec_collation = collation;
702 newec->ec_members = NIL;
703 newec->ec_sources = NIL;
704 newec->ec_derives = NIL;
705 newec->ec_relids = NULL;
706 newec->ec_has_const = false;
707 newec->ec_has_volatile = contain_volatile_functions((Node *) expr);
708 newec->ec_below_outer_join = false;
709 newec->ec_broken = false;
710 newec->ec_sortref = sortref;
711 newec->ec_min_security = UINT_MAX;
712 newec->ec_max_security = 0;
713 newec->ec_merged = NULL;
715 if (newec->ec_has_volatile && sortref == 0) /* should not happen */
716 elog(ERROR, "volatile EquivalenceClass has no sortref");
719 * Get the precise set of nullable relids appearing in the expression.
721 expr_relids = pull_varnos(root, (Node *) expr);
722 nullable_relids = bms_intersect(nullable_relids, expr_relids);
724 newem = add_eq_member(newec, copyObject(expr), expr_relids,
725 nullable_relids, false, opcintype);
728 * add_eq_member doesn't check for volatile functions, set-returning
729 * functions, aggregates, or window functions, but such could appear in
730 * sort expressions; so we have to check whether its const-marking was
731 * correct.
733 if (newec->ec_has_const)
735 if (newec->ec_has_volatile ||
736 expression_returns_set((Node *) expr) ||
737 contain_agg_clause((Node *) expr) ||
738 contain_window_function((Node *) expr))
740 newec->ec_has_const = false;
741 newem->em_is_const = false;
745 root->eq_classes = lappend(root->eq_classes, newec);
748 * If EC merging is already complete, we have to mop up by adding the new
749 * EC to the eclass_indexes of the relation(s) mentioned in it.
751 if (root->ec_merging_done)
753 int ec_index = list_length(root->eq_classes) - 1;
754 int i = -1;
756 while ((i = bms_next_member(newec->ec_relids, i)) > 0)
758 RelOptInfo *rel = root->simple_rel_array[i];
760 Assert(rel->reloptkind == RELOPT_BASEREL ||
761 rel->reloptkind == RELOPT_DEADREL);
763 rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
764 ec_index);
768 MemoryContextSwitchTo(oldcontext);
770 return newec;
774 * find_ec_member_matching_expr
775 * Locate an EquivalenceClass member matching the given expr, if any;
776 * return NULL if no match.
778 * "Matching" is defined as "equal after stripping RelabelTypes".
779 * This is used for identifying sort expressions, and we need to allow
780 * binary-compatible relabeling for some cases involving binary-compatible
781 * sort operators.
783 * Child EC members are ignored unless they belong to given 'relids'.
785 EquivalenceMember *
786 find_ec_member_matching_expr(EquivalenceClass *ec,
787 Expr *expr,
788 Relids relids)
790 ListCell *lc;
792 /* We ignore binary-compatible relabeling on both ends */
793 while (expr && IsA(expr, RelabelType))
794 expr = ((RelabelType *) expr)->arg;
796 foreach(lc, ec->ec_members)
798 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
799 Expr *emexpr;
802 * We shouldn't be trying to sort by an equivalence class that
803 * contains a constant, so no need to consider such cases any further.
805 if (em->em_is_const)
806 continue;
809 * Ignore child members unless they belong to the requested rel.
811 if (em->em_is_child &&
812 !bms_is_subset(em->em_relids, relids))
813 continue;
816 * Match if same expression (after stripping relabel).
818 emexpr = em->em_expr;
819 while (emexpr && IsA(emexpr, RelabelType))
820 emexpr = ((RelabelType *) emexpr)->arg;
822 if (equal(emexpr, expr))
823 return em;
826 return NULL;
830 * find_computable_ec_member
831 * Locate an EquivalenceClass member that can be computed from the
832 * expressions appearing in "exprs"; return NULL if no match.
834 * "exprs" can be either a list of bare expression trees, or a list of
835 * TargetEntry nodes. Either way, it should contain Vars and possibly
836 * Aggrefs and WindowFuncs, which are matched to the corresponding elements
837 * of the EquivalenceClass's expressions.
839 * Unlike find_ec_member_matching_expr, there's no special provision here
840 * for binary-compatible relabeling. This is intentional: if we have to
841 * compute an expression in this way, setrefs.c is going to insist on exact
842 * matches of Vars to the source tlist.
844 * Child EC members are ignored unless they belong to given 'relids'.
845 * Also, non-parallel-safe expressions are ignored if 'require_parallel_safe'.
847 * Note: some callers pass root == NULL for notational reasons. This is OK
848 * when require_parallel_safe is false.
850 EquivalenceMember *
851 find_computable_ec_member(PlannerInfo *root,
852 EquivalenceClass *ec,
853 List *exprs,
854 Relids relids,
855 bool require_parallel_safe)
857 ListCell *lc;
859 foreach(lc, ec->ec_members)
861 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
862 List *exprvars;
863 ListCell *lc2;
866 * We shouldn't be trying to sort by an equivalence class that
867 * contains a constant, so no need to consider such cases any further.
869 if (em->em_is_const)
870 continue;
873 * Ignore child members unless they belong to the requested rel.
875 if (em->em_is_child &&
876 !bms_is_subset(em->em_relids, relids))
877 continue;
880 * Match if all Vars and quasi-Vars are available in "exprs".
882 exprvars = pull_var_clause((Node *) em->em_expr,
883 PVC_INCLUDE_AGGREGATES |
884 PVC_INCLUDE_WINDOWFUNCS |
885 PVC_INCLUDE_PLACEHOLDERS);
886 foreach(lc2, exprvars)
888 if (!is_exprlist_member(lfirst(lc2), exprs))
889 break;
891 list_free(exprvars);
892 if (lc2)
893 continue; /* we hit a non-available Var */
896 * If requested, reject expressions that are not parallel-safe. We
897 * check this last because it's a rather expensive test.
899 if (require_parallel_safe &&
900 !is_parallel_safe(root, (Node *) em->em_expr))
901 continue;
903 return em; /* found usable expression */
906 return NULL;
910 * is_exprlist_member
911 * Subroutine for find_computable_ec_member: is "node" in "exprs"?
913 * Per the requirements of that function, "exprs" might or might not have
914 * TargetEntry superstructure.
916 static bool
917 is_exprlist_member(Expr *node, List *exprs)
919 ListCell *lc;
921 foreach(lc, exprs)
923 Expr *expr = (Expr *) lfirst(lc);
925 if (expr && IsA(expr, TargetEntry))
926 expr = ((TargetEntry *) expr)->expr;
928 if (equal(node, expr))
929 return true;
931 return false;
935 * Find an equivalence class member expression, all of whose Vars, come from
936 * the indicated relation.
938 Expr *
939 find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
941 ListCell *lc_em;
943 foreach(lc_em, ec->ec_members)
945 EquivalenceMember *em = lfirst(lc_em);
947 if (bms_is_subset(em->em_relids, rel->relids) &&
948 !bms_is_empty(em->em_relids))
951 * If there is more than one equivalence member whose Vars are
952 * taken entirely from this relation, we'll be content to choose
953 * any one of those.
955 return em->em_expr;
959 /* We didn't find any suitable equivalence class expression */
960 return NULL;
964 * relation_can_be_sorted_early
965 * Can this relation be sorted on this EC before the final output step?
967 * To succeed, we must find an EC member that prepare_sort_from_pathkeys knows
968 * how to sort on, given the rel's reltarget as input. There are also a few
969 * additional constraints based on the fact that the desired sort will be done
970 * "early", within the scan/join part of the plan. Also, non-parallel-safe
971 * expressions are ignored if 'require_parallel_safe'.
973 * At some point we might want to return the identified EquivalenceMember,
974 * but for now, callers only want to know if there is one.
976 bool
977 relation_can_be_sorted_early(PlannerInfo *root, RelOptInfo *rel,
978 EquivalenceClass *ec, bool require_parallel_safe)
980 PathTarget *target = rel->reltarget;
981 EquivalenceMember *em;
982 ListCell *lc;
985 * Reject volatile ECs immediately; such sorts must always be postponed.
987 if (ec->ec_has_volatile)
988 return false;
991 * Try to find an EM directly matching some reltarget member.
993 foreach(lc, target->exprs)
995 Expr *targetexpr = (Expr *) lfirst(lc);
997 em = find_ec_member_matching_expr(ec, targetexpr, rel->relids);
998 if (!em)
999 continue;
1002 * Reject expressions involving set-returning functions, as those
1003 * can't be computed early either. (Note: this test and the following
1004 * one are effectively checking properties of targetexpr, so there's
1005 * no point in asking whether some other EC member would be better.)
1007 if (IS_SRF_CALL((Node *) em->em_expr))
1008 continue;
1011 * If requested, reject expressions that are not parallel-safe. We
1012 * check this last because it's a rather expensive test.
1014 if (require_parallel_safe &&
1015 !is_parallel_safe(root, (Node *) em->em_expr))
1016 continue;
1018 return true;
1022 * Try to find a expression computable from the reltarget.
1024 em = find_computable_ec_member(root, ec, target->exprs, rel->relids,
1025 require_parallel_safe);
1026 if (!em)
1027 return false;
1030 * Reject expressions involving set-returning functions, as those can't be
1031 * computed early either. (There's no point in looking for another EC
1032 * member in this case; since SRFs can't appear in WHERE, they cannot
1033 * belong to multi-member ECs.)
1035 if (IS_SRF_CALL((Node *) em->em_expr))
1036 return false;
1038 return true;
1042 * generate_base_implied_equalities
1043 * Generate any restriction clauses that we can deduce from equivalence
1044 * classes.
1046 * When an EC contains pseudoconstants, our strategy is to generate
1047 * "member = const1" clauses where const1 is the first constant member, for
1048 * every other member (including other constants). If we are able to do this
1049 * then we don't need any "var = var" comparisons because we've successfully
1050 * constrained all the vars at their points of creation. If we fail to
1051 * generate any of these clauses due to lack of cross-type operators, we fall
1052 * back to the "ec_broken" strategy described below. (XXX if there are
1053 * multiple constants of different types, it's possible that we might succeed
1054 * in forming all the required clauses if we started from a different const
1055 * member; but this seems a sufficiently hokey corner case to not be worth
1056 * spending lots of cycles on.)
1058 * For ECs that contain no pseudoconstants, we generate derived clauses
1059 * "member1 = member2" for each pair of members belonging to the same base
1060 * relation (actually, if there are more than two for the same base relation,
1061 * we only need enough clauses to link each to each other). This provides
1062 * the base case for the recursion: each row emitted by a base relation scan
1063 * will constrain all computable members of the EC to be equal. As each
1064 * join path is formed, we'll add additional derived clauses on-the-fly
1065 * to maintain this invariant (see generate_join_implied_equalities).
1067 * If the opfamilies used by the EC do not provide complete sets of cross-type
1068 * equality operators, it is possible that we will fail to generate a clause
1069 * that must be generated to maintain the invariant. (An example: given
1070 * "WHERE a.x = b.y AND b.y = a.z", the scheme breaks down if we cannot
1071 * generate "a.x = a.z" as a restriction clause for A.) In this case we mark
1072 * the EC "ec_broken" and fall back to regurgitating its original source
1073 * RestrictInfos at appropriate times. We do not try to retract any derived
1074 * clauses already generated from the broken EC, so the resulting plan could
1075 * be poor due to bad selectivity estimates caused by redundant clauses. But
1076 * the correct solution to that is to fix the opfamilies ...
1078 * Equality clauses derived by this function are passed off to
1079 * process_implied_equality (in plan/initsplan.c) to be inserted into the
1080 * restrictinfo datastructures. Note that this must be called after initial
1081 * scanning of the quals and before Path construction begins.
1083 * We make no attempt to avoid generating duplicate RestrictInfos here: we
1084 * don't search ec_sources or ec_derives for matches. It doesn't really
1085 * seem worth the trouble to do so.
1087 void
1088 generate_base_implied_equalities(PlannerInfo *root)
1090 int ec_index;
1091 ListCell *lc;
1094 * At this point, we're done absorbing knowledge of equivalences in the
1095 * query, so no further EC merging should happen, and ECs remaining in the
1096 * eq_classes list can be considered canonical. (But note that it's still
1097 * possible for new single-member ECs to be added through
1098 * get_eclass_for_sort_expr().)
1100 root->ec_merging_done = true;
1102 ec_index = 0;
1103 foreach(lc, root->eq_classes)
1105 EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
1106 bool can_generate_joinclause = false;
1107 int i;
1109 Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
1110 Assert(!ec->ec_broken); /* not yet anyway... */
1113 * Generate implied equalities that are restriction clauses.
1114 * Single-member ECs won't generate any deductions, either here or at
1115 * the join level.
1117 if (list_length(ec->ec_members) > 1)
1119 if (ec->ec_has_const)
1120 generate_base_implied_equalities_const(root, ec);
1121 else
1122 generate_base_implied_equalities_no_const(root, ec);
1124 /* Recover if we failed to generate required derived clauses */
1125 if (ec->ec_broken)
1126 generate_base_implied_equalities_broken(root, ec);
1128 /* Detect whether this EC might generate join clauses */
1129 can_generate_joinclause =
1130 (bms_membership(ec->ec_relids) == BMS_MULTIPLE);
1134 * Mark the base rels cited in each eclass (which should all exist by
1135 * now) with the eq_classes indexes of all eclasses mentioning them.
1136 * This will let us avoid searching in subsequent lookups. While
1137 * we're at it, we can mark base rels that have pending eclass joins;
1138 * this is a cheap version of has_relevant_eclass_joinclause().
1140 i = -1;
1141 while ((i = bms_next_member(ec->ec_relids, i)) > 0)
1143 RelOptInfo *rel = root->simple_rel_array[i];
1145 Assert(rel->reloptkind == RELOPT_BASEREL);
1147 rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
1148 ec_index);
1150 if (can_generate_joinclause)
1151 rel->has_eclass_joins = true;
1154 ec_index++;
1159 * generate_base_implied_equalities when EC contains pseudoconstant(s)
1161 static void
1162 generate_base_implied_equalities_const(PlannerInfo *root,
1163 EquivalenceClass *ec)
1165 EquivalenceMember *const_em = NULL;
1166 ListCell *lc;
1169 * In the trivial case where we just had one "var = const" clause, push
1170 * the original clause back into the main planner machinery. There is
1171 * nothing to be gained by doing it differently, and we save the effort to
1172 * re-build and re-analyze an equality clause that will be exactly
1173 * equivalent to the old one.
1175 if (list_length(ec->ec_members) == 2 &&
1176 list_length(ec->ec_sources) == 1)
1178 RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
1180 if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1182 distribute_restrictinfo_to_rels(root, restrictinfo);
1183 return;
1188 * Find the constant member to use. We prefer an actual constant to
1189 * pseudo-constants (such as Params), because the constraint exclusion
1190 * machinery might be able to exclude relations on the basis of generated
1191 * "var = const" equalities, but "var = param" won't work for that.
1193 foreach(lc, ec->ec_members)
1195 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1197 if (cur_em->em_is_const)
1199 const_em = cur_em;
1200 if (IsA(cur_em->em_expr, Const))
1201 break;
1204 Assert(const_em != NULL);
1206 /* Generate a derived equality against each other member */
1207 foreach(lc, ec->ec_members)
1209 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1210 Oid eq_op;
1211 RestrictInfo *rinfo;
1213 Assert(!cur_em->em_is_child); /* no children yet */
1214 if (cur_em == const_em)
1215 continue;
1216 eq_op = select_equality_operator(ec,
1217 cur_em->em_datatype,
1218 const_em->em_datatype);
1219 if (!OidIsValid(eq_op))
1221 /* failed... */
1222 ec->ec_broken = true;
1223 break;
1225 rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1226 cur_em->em_expr, const_em->em_expr,
1227 bms_copy(ec->ec_relids),
1228 bms_union(cur_em->em_nullable_relids,
1229 const_em->em_nullable_relids),
1230 ec->ec_min_security,
1231 ec->ec_below_outer_join,
1232 cur_em->em_is_const);
1235 * If the clause didn't degenerate to a constant, fill in the correct
1236 * markings for a mergejoinable clause, and save it in ec_derives. (We
1237 * will not re-use such clauses directly, but selectivity estimation
1238 * may consult the list later. Note that this use of ec_derives does
1239 * not overlap with its use for join clauses, since we never generate
1240 * join clauses from an ec_has_const eclass.)
1242 if (rinfo && rinfo->mergeopfamilies)
1244 /* it's not redundant, so don't set parent_ec */
1245 rinfo->left_ec = rinfo->right_ec = ec;
1246 rinfo->left_em = cur_em;
1247 rinfo->right_em = const_em;
1248 ec->ec_derives = lappend(ec->ec_derives, rinfo);
1254 * generate_base_implied_equalities when EC contains no pseudoconstants
1256 static void
1257 generate_base_implied_equalities_no_const(PlannerInfo *root,
1258 EquivalenceClass *ec)
1260 EquivalenceMember **prev_ems;
1261 ListCell *lc;
1264 * We scan the EC members once and track the last-seen member for each
1265 * base relation. When we see another member of the same base relation,
1266 * we generate "prev_em = cur_em". This results in the minimum number of
1267 * derived clauses, but it's possible that it will fail when a different
1268 * ordering would succeed. XXX FIXME: use a UNION-FIND algorithm similar
1269 * to the way we build merged ECs. (Use a list-of-lists for each rel.)
1271 prev_ems = (EquivalenceMember **)
1272 palloc0(root->simple_rel_array_size * sizeof(EquivalenceMember *));
1274 foreach(lc, ec->ec_members)
1276 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1277 int relid;
1279 Assert(!cur_em->em_is_child); /* no children yet */
1280 if (!bms_get_singleton_member(cur_em->em_relids, &relid))
1281 continue;
1282 Assert(relid < root->simple_rel_array_size);
1284 if (prev_ems[relid] != NULL)
1286 EquivalenceMember *prev_em = prev_ems[relid];
1287 Oid eq_op;
1288 RestrictInfo *rinfo;
1290 eq_op = select_equality_operator(ec,
1291 prev_em->em_datatype,
1292 cur_em->em_datatype);
1293 if (!OidIsValid(eq_op))
1295 /* failed... */
1296 ec->ec_broken = true;
1297 break;
1299 rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1300 prev_em->em_expr, cur_em->em_expr,
1301 bms_copy(ec->ec_relids),
1302 bms_union(prev_em->em_nullable_relids,
1303 cur_em->em_nullable_relids),
1304 ec->ec_min_security,
1305 ec->ec_below_outer_join,
1306 false);
1309 * If the clause didn't degenerate to a constant, fill in the
1310 * correct markings for a mergejoinable clause. We don't put it
1311 * in ec_derives however; we don't currently need to re-find such
1312 * clauses, and we don't want to clutter that list with non-join
1313 * clauses.
1315 if (rinfo && rinfo->mergeopfamilies)
1317 /* it's not redundant, so don't set parent_ec */
1318 rinfo->left_ec = rinfo->right_ec = ec;
1319 rinfo->left_em = prev_em;
1320 rinfo->right_em = cur_em;
1323 prev_ems[relid] = cur_em;
1326 pfree(prev_ems);
1329 * We also have to make sure that all the Vars used in the member clauses
1330 * will be available at any join node we might try to reference them at.
1331 * For the moment we force all the Vars to be available at all join nodes
1332 * for this eclass. Perhaps this could be improved by doing some
1333 * pre-analysis of which members we prefer to join, but it's no worse than
1334 * what happened in the pre-8.3 code.
1336 foreach(lc, ec->ec_members)
1338 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1339 List *vars = pull_var_clause((Node *) cur_em->em_expr,
1340 PVC_RECURSE_AGGREGATES |
1341 PVC_RECURSE_WINDOWFUNCS |
1342 PVC_INCLUDE_PLACEHOLDERS);
1344 add_vars_to_targetlist(root, vars, ec->ec_relids, false);
1345 list_free(vars);
1350 * generate_base_implied_equalities cleanup after failure
1352 * What we must do here is push any zero- or one-relation source RestrictInfos
1353 * of the EC back into the main restrictinfo datastructures. Multi-relation
1354 * clauses will be regurgitated later by generate_join_implied_equalities().
1355 * (We do it this way to maintain continuity with the case that ec_broken
1356 * becomes set only after we've gone up a join level or two.) However, for
1357 * an EC that contains constants, we can adopt a simpler strategy and just
1358 * throw back all the source RestrictInfos immediately; that works because
1359 * we know that such an EC can't become broken later. (This rule justifies
1360 * ignoring ec_has_const ECs in generate_join_implied_equalities, even when
1361 * they are broken.)
1363 static void
1364 generate_base_implied_equalities_broken(PlannerInfo *root,
1365 EquivalenceClass *ec)
1367 ListCell *lc;
1369 foreach(lc, ec->ec_sources)
1371 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1373 if (ec->ec_has_const ||
1374 bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1375 distribute_restrictinfo_to_rels(root, restrictinfo);
1381 * generate_join_implied_equalities
1382 * Generate any join clauses that we can deduce from equivalence classes.
1384 * At a join node, we must enforce restriction clauses sufficient to ensure
1385 * that all equivalence-class members computable at that node are equal.
1386 * Since the set of clauses to enforce can vary depending on which subset
1387 * relations are the inputs, we have to compute this afresh for each join
1388 * relation pair. Hence a fresh List of RestrictInfo nodes is built and
1389 * passed back on each call.
1391 * In addition to its use at join nodes, this can be applied to generate
1392 * eclass-based join clauses for use in a parameterized scan of a base rel.
1393 * The reason for the asymmetry of specifying the inner rel as a RelOptInfo
1394 * and the outer rel by Relids is that this usage occurs before we have
1395 * built any join RelOptInfos.
1397 * An annoying special case for parameterized scans is that the inner rel can
1398 * be an appendrel child (an "other rel"). In this case we must generate
1399 * appropriate clauses using child EC members. add_child_rel_equivalences
1400 * must already have been done for the child rel.
1402 * The results are sufficient for use in merge, hash, and plain nestloop join
1403 * methods. We do not worry here about selecting clauses that are optimal
1404 * for use in a parameterized indexscan. indxpath.c makes its own selections
1405 * of clauses to use, and if the ones we pick here are redundant with those,
1406 * the extras will be eliminated at createplan time, using the parent_ec
1407 * markers that we provide (see is_redundant_derived_clause()).
1409 * Because the same join clauses are likely to be needed multiple times as
1410 * we consider different join paths, we avoid generating multiple copies:
1411 * whenever we select a particular pair of EquivalenceMembers to join,
1412 * we check to see if the pair matches any original clause (in ec_sources)
1413 * or previously-built clause (in ec_derives). This saves memory and allows
1414 * re-use of information cached in RestrictInfos.
1416 * join_relids should always equal bms_union(outer_relids, inner_rel->relids).
1417 * We could simplify this function's API by computing it internally, but in
1418 * most current uses, the caller has the value at hand anyway.
1420 List *
1421 generate_join_implied_equalities(PlannerInfo *root,
1422 Relids join_relids,
1423 Relids outer_relids,
1424 RelOptInfo *inner_rel)
1426 List *result = NIL;
1427 Relids inner_relids = inner_rel->relids;
1428 Relids nominal_inner_relids;
1429 Relids nominal_join_relids;
1430 Bitmapset *matching_ecs;
1431 int i;
1433 /* If inner rel is a child, extra setup work is needed */
1434 if (IS_OTHER_REL(inner_rel))
1436 Assert(!bms_is_empty(inner_rel->top_parent_relids));
1438 /* Fetch relid set for the topmost parent rel */
1439 nominal_inner_relids = inner_rel->top_parent_relids;
1440 /* ECs will be marked with the parent's relid, not the child's */
1441 nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1443 else
1445 nominal_inner_relids = inner_relids;
1446 nominal_join_relids = join_relids;
1450 * Get all eclasses that mention both inner and outer sides of the join
1452 matching_ecs = get_common_eclass_indexes(root, nominal_inner_relids,
1453 outer_relids);
1455 i = -1;
1456 while ((i = bms_next_member(matching_ecs, i)) >= 0)
1458 EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
1459 List *sublist = NIL;
1461 /* ECs containing consts do not need any further enforcement */
1462 if (ec->ec_has_const)
1463 continue;
1465 /* Single-member ECs won't generate any deductions */
1466 if (list_length(ec->ec_members) <= 1)
1467 continue;
1469 /* Sanity check that this eclass overlaps the join */
1470 Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
1472 if (!ec->ec_broken)
1473 sublist = generate_join_implied_equalities_normal(root,
1475 join_relids,
1476 outer_relids,
1477 inner_relids);
1479 /* Recover if we failed to generate required derived clauses */
1480 if (ec->ec_broken)
1481 sublist = generate_join_implied_equalities_broken(root,
1483 nominal_join_relids,
1484 outer_relids,
1485 nominal_inner_relids,
1486 inner_rel);
1488 result = list_concat(result, sublist);
1491 return result;
1495 * generate_join_implied_equalities_for_ecs
1496 * As above, but consider only the listed ECs.
1498 List *
1499 generate_join_implied_equalities_for_ecs(PlannerInfo *root,
1500 List *eclasses,
1501 Relids join_relids,
1502 Relids outer_relids,
1503 RelOptInfo *inner_rel)
1505 List *result = NIL;
1506 Relids inner_relids = inner_rel->relids;
1507 Relids nominal_inner_relids;
1508 Relids nominal_join_relids;
1509 ListCell *lc;
1511 /* If inner rel is a child, extra setup work is needed */
1512 if (IS_OTHER_REL(inner_rel))
1514 Assert(!bms_is_empty(inner_rel->top_parent_relids));
1516 /* Fetch relid set for the topmost parent rel */
1517 nominal_inner_relids = inner_rel->top_parent_relids;
1518 /* ECs will be marked with the parent's relid, not the child's */
1519 nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1521 else
1523 nominal_inner_relids = inner_relids;
1524 nominal_join_relids = join_relids;
1527 foreach(lc, eclasses)
1529 EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
1530 List *sublist = NIL;
1532 /* ECs containing consts do not need any further enforcement */
1533 if (ec->ec_has_const)
1534 continue;
1536 /* Single-member ECs won't generate any deductions */
1537 if (list_length(ec->ec_members) <= 1)
1538 continue;
1540 /* We can quickly ignore any that don't overlap the join, too */
1541 if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1542 continue;
1544 if (!ec->ec_broken)
1545 sublist = generate_join_implied_equalities_normal(root,
1547 join_relids,
1548 outer_relids,
1549 inner_relids);
1551 /* Recover if we failed to generate required derived clauses */
1552 if (ec->ec_broken)
1553 sublist = generate_join_implied_equalities_broken(root,
1555 nominal_join_relids,
1556 outer_relids,
1557 nominal_inner_relids,
1558 inner_rel);
1560 result = list_concat(result, sublist);
1563 return result;
1567 * generate_join_implied_equalities for a still-valid EC
1569 static List *
1570 generate_join_implied_equalities_normal(PlannerInfo *root,
1571 EquivalenceClass *ec,
1572 Relids join_relids,
1573 Relids outer_relids,
1574 Relids inner_relids)
1576 List *result = NIL;
1577 List *new_members = NIL;
1578 List *outer_members = NIL;
1579 List *inner_members = NIL;
1580 ListCell *lc1;
1583 * First, scan the EC to identify member values that are computable at the
1584 * outer rel, at the inner rel, or at this relation but not in either
1585 * input rel. The outer-rel members should already be enforced equal,
1586 * likewise for the inner-rel members. We'll need to create clauses to
1587 * enforce that any newly computable members are all equal to each other
1588 * as well as to at least one input member, plus enforce at least one
1589 * outer-rel member equal to at least one inner-rel member.
1591 foreach(lc1, ec->ec_members)
1593 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1596 * We don't need to check explicitly for child EC members. This test
1597 * against join_relids will cause them to be ignored except when
1598 * considering a child inner rel, which is what we want.
1600 if (!bms_is_subset(cur_em->em_relids, join_relids))
1601 continue; /* not computable yet, or wrong child */
1603 if (bms_is_subset(cur_em->em_relids, outer_relids))
1604 outer_members = lappend(outer_members, cur_em);
1605 else if (bms_is_subset(cur_em->em_relids, inner_relids))
1606 inner_members = lappend(inner_members, cur_em);
1607 else
1608 new_members = lappend(new_members, cur_em);
1612 * First, select the joinclause if needed. We can equate any one outer
1613 * member to any one inner member, but we have to find a datatype
1614 * combination for which an opfamily member operator exists. If we have
1615 * choices, we prefer simple Var members (possibly with RelabelType) since
1616 * these are (a) cheapest to compute at runtime and (b) most likely to
1617 * have useful statistics. Also, prefer operators that are also
1618 * hashjoinable.
1620 if (outer_members && inner_members)
1622 EquivalenceMember *best_outer_em = NULL;
1623 EquivalenceMember *best_inner_em = NULL;
1624 Oid best_eq_op = InvalidOid;
1625 int best_score = -1;
1626 RestrictInfo *rinfo;
1628 foreach(lc1, outer_members)
1630 EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
1631 ListCell *lc2;
1633 foreach(lc2, inner_members)
1635 EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
1636 Oid eq_op;
1637 int score;
1639 eq_op = select_equality_operator(ec,
1640 outer_em->em_datatype,
1641 inner_em->em_datatype);
1642 if (!OidIsValid(eq_op))
1643 continue;
1644 score = 0;
1645 if (IsA(outer_em->em_expr, Var) ||
1646 (IsA(outer_em->em_expr, RelabelType) &&
1647 IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
1648 score++;
1649 if (IsA(inner_em->em_expr, Var) ||
1650 (IsA(inner_em->em_expr, RelabelType) &&
1651 IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
1652 score++;
1653 if (op_hashjoinable(eq_op,
1654 exprType((Node *) outer_em->em_expr)))
1655 score++;
1656 if (score > best_score)
1658 best_outer_em = outer_em;
1659 best_inner_em = inner_em;
1660 best_eq_op = eq_op;
1661 best_score = score;
1662 if (best_score == 3)
1663 break; /* no need to look further */
1666 if (best_score == 3)
1667 break; /* no need to look further */
1669 if (best_score < 0)
1671 /* failed... */
1672 ec->ec_broken = true;
1673 return NIL;
1677 * Create clause, setting parent_ec to mark it as redundant with other
1678 * joinclauses
1680 rinfo = create_join_clause(root, ec, best_eq_op,
1681 best_outer_em, best_inner_em,
1682 ec);
1684 result = lappend(result, rinfo);
1688 * Now deal with building restrictions for any expressions that involve
1689 * Vars from both sides of the join. We have to equate all of these to
1690 * each other as well as to at least one old member (if any).
1692 * XXX as in generate_base_implied_equalities_no_const, we could be a lot
1693 * smarter here to avoid unnecessary failures in cross-type situations.
1694 * For now, use the same left-to-right method used there.
1696 if (new_members)
1698 List *old_members = list_concat(outer_members, inner_members);
1699 EquivalenceMember *prev_em = NULL;
1700 RestrictInfo *rinfo;
1702 /* For now, arbitrarily take the first old_member as the one to use */
1703 if (old_members)
1704 new_members = lappend(new_members, linitial(old_members));
1706 foreach(lc1, new_members)
1708 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1710 if (prev_em != NULL)
1712 Oid eq_op;
1714 eq_op = select_equality_operator(ec,
1715 prev_em->em_datatype,
1716 cur_em->em_datatype);
1717 if (!OidIsValid(eq_op))
1719 /* failed... */
1720 ec->ec_broken = true;
1721 return NIL;
1723 /* do NOT set parent_ec, this qual is not redundant! */
1724 rinfo = create_join_clause(root, ec, eq_op,
1725 prev_em, cur_em,
1726 NULL);
1728 result = lappend(result, rinfo);
1730 prev_em = cur_em;
1734 return result;
1738 * generate_join_implied_equalities cleanup after failure
1740 * Return any original RestrictInfos that are enforceable at this join.
1742 * In the case of a child inner relation, we have to translate the
1743 * original RestrictInfos from parent to child Vars.
1745 static List *
1746 generate_join_implied_equalities_broken(PlannerInfo *root,
1747 EquivalenceClass *ec,
1748 Relids nominal_join_relids,
1749 Relids outer_relids,
1750 Relids nominal_inner_relids,
1751 RelOptInfo *inner_rel)
1753 List *result = NIL;
1754 ListCell *lc;
1756 foreach(lc, ec->ec_sources)
1758 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1759 Relids clause_relids = restrictinfo->required_relids;
1761 if (bms_is_subset(clause_relids, nominal_join_relids) &&
1762 !bms_is_subset(clause_relids, outer_relids) &&
1763 !bms_is_subset(clause_relids, nominal_inner_relids))
1764 result = lappend(result, restrictinfo);
1768 * If we have to translate, just brute-force apply adjust_appendrel_attrs
1769 * to all the RestrictInfos at once. This will result in returning
1770 * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1771 * any duplication, and it's a sufficiently narrow corner case that we
1772 * shouldn't sweat too much over it anyway.
1774 * Since inner_rel might be an indirect descendant of the baserel
1775 * mentioned in the ec_sources clauses, we have to be prepared to apply
1776 * multiple levels of Var translation.
1778 if (IS_OTHER_REL(inner_rel) && result != NIL)
1779 result = (List *) adjust_appendrel_attrs_multilevel(root,
1780 (Node *) result,
1781 inner_rel->relids,
1782 inner_rel->top_parent_relids);
1784 return result;
1789 * select_equality_operator
1790 * Select a suitable equality operator for comparing two EC members
1792 * Returns InvalidOid if no operator can be found for this datatype combination
1794 static Oid
1795 select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
1797 ListCell *lc;
1799 foreach(lc, ec->ec_opfamilies)
1801 Oid opfamily = lfirst_oid(lc);
1802 Oid opno;
1804 opno = get_opfamily_member(opfamily, lefttype, righttype,
1805 BTEqualStrategyNumber);
1806 if (!OidIsValid(opno))
1807 continue;
1808 /* If no barrier quals in query, don't worry about leaky operators */
1809 if (ec->ec_max_security == 0)
1810 return opno;
1811 /* Otherwise, insist that selected operators be leakproof */
1812 if (get_func_leakproof(get_opcode(opno)))
1813 return opno;
1815 return InvalidOid;
1820 * create_join_clause
1821 * Find or make a RestrictInfo comparing the two given EC members
1822 * with the given operator.
1824 * parent_ec is either equal to ec (if the clause is a potentially-redundant
1825 * join clause) or NULL (if not). We have to treat this as part of the
1826 * match requirements --- it's possible that a clause comparing the same two
1827 * EMs is a join clause in one join path and a restriction clause in another.
1829 static RestrictInfo *
1830 create_join_clause(PlannerInfo *root,
1831 EquivalenceClass *ec, Oid opno,
1832 EquivalenceMember *leftem,
1833 EquivalenceMember *rightem,
1834 EquivalenceClass *parent_ec)
1836 RestrictInfo *rinfo;
1837 ListCell *lc;
1838 MemoryContext oldcontext;
1841 * Search to see if we already built a RestrictInfo for this pair of
1842 * EquivalenceMembers. We can use either original source clauses or
1843 * previously-derived clauses. The check on opno is probably redundant,
1844 * but be safe ...
1846 foreach(lc, ec->ec_sources)
1848 rinfo = (RestrictInfo *) lfirst(lc);
1849 if (rinfo->left_em == leftem &&
1850 rinfo->right_em == rightem &&
1851 rinfo->parent_ec == parent_ec &&
1852 opno == ((OpExpr *) rinfo->clause)->opno)
1853 return rinfo;
1856 foreach(lc, ec->ec_derives)
1858 rinfo = (RestrictInfo *) lfirst(lc);
1859 if (rinfo->left_em == leftem &&
1860 rinfo->right_em == rightem &&
1861 rinfo->parent_ec == parent_ec &&
1862 opno == ((OpExpr *) rinfo->clause)->opno)
1863 return rinfo;
1867 * Not there, so build it, in planner context so we can re-use it. (Not
1868 * important in normal planning, but definitely so in GEQO.)
1870 oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1872 rinfo = build_implied_join_equality(root,
1873 opno,
1874 ec->ec_collation,
1875 leftem->em_expr,
1876 rightem->em_expr,
1877 bms_union(leftem->em_relids,
1878 rightem->em_relids),
1879 bms_union(leftem->em_nullable_relids,
1880 rightem->em_nullable_relids),
1881 ec->ec_min_security);
1883 /* Mark the clause as redundant, or not */
1884 rinfo->parent_ec = parent_ec;
1887 * We know the correct values for left_ec/right_ec, ie this particular EC,
1888 * so we can just set them directly instead of forcing another lookup.
1890 rinfo->left_ec = ec;
1891 rinfo->right_ec = ec;
1893 /* Mark it as usable with these EMs */
1894 rinfo->left_em = leftem;
1895 rinfo->right_em = rightem;
1896 /* and save it for possible re-use */
1897 ec->ec_derives = lappend(ec->ec_derives, rinfo);
1899 MemoryContextSwitchTo(oldcontext);
1901 return rinfo;
1906 * reconsider_outer_join_clauses
1907 * Re-examine any outer-join clauses that were set aside by
1908 * distribute_qual_to_rels(), and see if we can derive any
1909 * EquivalenceClasses from them. Then, if they were not made
1910 * redundant, push them out into the regular join-clause lists.
1912 * When we have mergejoinable clauses A = B that are outer-join clauses,
1913 * we can't blindly combine them with other clauses A = C to deduce B = C,
1914 * since in fact the "equality" A = B won't necessarily hold above the
1915 * outer join (one of the variables might be NULL instead). Nonetheless
1916 * there are cases where we can add qual clauses using transitivity.
1918 * One case that we look for here is an outer-join clause OUTERVAR = INNERVAR
1919 * for which there is also an equivalence clause OUTERVAR = CONSTANT.
1920 * It is safe and useful to push a clause INNERVAR = CONSTANT into the
1921 * evaluation of the inner (nullable) relation, because any inner rows not
1922 * meeting this condition will not contribute to the outer-join result anyway.
1923 * (Any outer rows they could join to will be eliminated by the pushed-down
1924 * equivalence clause.)
1926 * Note that the above rule does not work for full outer joins; nor is it
1927 * very interesting to consider cases where the generated equivalence clause
1928 * would involve relations outside the outer join, since such clauses couldn't
1929 * be pushed into the inner side's scan anyway. So the restriction to
1930 * outervar = pseudoconstant is not really giving up anything.
1932 * For full-join cases, we can only do something useful if it's a FULL JOIN
1933 * USING and a merged column has an equivalence MERGEDVAR = CONSTANT.
1934 * By the time it gets here, the merged column will look like
1935 * COALESCE(LEFTVAR, RIGHTVAR)
1936 * and we will have a full-join clause LEFTVAR = RIGHTVAR that we can match
1937 * the COALESCE expression to. In this situation we can push LEFTVAR = CONSTANT
1938 * and RIGHTVAR = CONSTANT into the input relations, since any rows not
1939 * meeting these conditions cannot contribute to the join result.
1941 * Again, there isn't any traction to be gained by trying to deal with
1942 * clauses comparing a mergedvar to a non-pseudoconstant. So we can make
1943 * use of the EquivalenceClasses to search for matching variables that were
1944 * equivalenced to constants. The interesting outer-join clauses were
1945 * accumulated for us by distribute_qual_to_rels.
1947 * When we find one of these cases, we implement the changes we want by
1948 * generating a new equivalence clause INNERVAR = CONSTANT (or LEFTVAR, etc)
1949 * and pushing it into the EquivalenceClass structures. This is because we
1950 * may already know that INNERVAR is equivalenced to some other var(s), and
1951 * we'd like the constant to propagate to them too. Note that it would be
1952 * unsafe to merge any existing EC for INNERVAR with the OUTERVAR's EC ---
1953 * that could result in propagating constant restrictions from
1954 * INNERVAR to OUTERVAR, which would be very wrong.
1956 * It's possible that the INNERVAR is also an OUTERVAR for some other
1957 * outer-join clause, in which case the process can be repeated. So we repeat
1958 * looping over the lists of clauses until no further deductions can be made.
1959 * Whenever we do make a deduction, we remove the generating clause from the
1960 * lists, since we don't want to make the same deduction twice.
1962 * If we don't find any match for a set-aside outer join clause, we must
1963 * throw it back into the regular joinclause processing by passing it to
1964 * distribute_restrictinfo_to_rels(). If we do generate a derived clause,
1965 * however, the outer-join clause is redundant. We still throw it back,
1966 * because otherwise the join will be seen as a clauseless join and avoided
1967 * during join order searching; but we mark it as redundant to keep from
1968 * messing up the joinrel's size estimate. (This behavior means that the
1969 * API for this routine is uselessly complex: we could have just put all
1970 * the clauses into the regular processing initially. We keep it because
1971 * someday we might want to do something else, such as inserting "dummy"
1972 * joinclauses instead of real ones.)
1974 * Outer join clauses that are marked outerjoin_delayed are special: this
1975 * condition means that one or both VARs might go to null due to a lower
1976 * outer join. We can still push a constant through the clause, but only
1977 * if its operator is strict; and we *have to* throw the clause back into
1978 * regular joinclause processing. By keeping the strict join clause,
1979 * we ensure that any null-extended rows that are mistakenly generated due
1980 * to suppressing rows not matching the constant will be rejected at the
1981 * upper outer join. (This doesn't work for full-join clauses.)
1983 void
1984 reconsider_outer_join_clauses(PlannerInfo *root)
1986 bool found;
1987 ListCell *cell;
1989 /* Outer loop repeats until we find no more deductions */
1992 found = false;
1994 /* Process the LEFT JOIN clauses */
1995 foreach(cell, root->left_join_clauses)
1997 RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1999 if (reconsider_outer_join_clause(root, rinfo, true))
2001 found = true;
2002 /* remove it from the list */
2003 root->left_join_clauses =
2004 foreach_delete_current(root->left_join_clauses, cell);
2005 /* we throw it back anyway (see notes above) */
2006 /* but the thrown-back clause has no extra selectivity */
2007 rinfo->norm_selec = 2.0;
2008 rinfo->outer_selec = 1.0;
2009 distribute_restrictinfo_to_rels(root, rinfo);
2013 /* Process the RIGHT JOIN clauses */
2014 foreach(cell, root->right_join_clauses)
2016 RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2018 if (reconsider_outer_join_clause(root, rinfo, false))
2020 found = true;
2021 /* remove it from the list */
2022 root->right_join_clauses =
2023 foreach_delete_current(root->right_join_clauses, cell);
2024 /* we throw it back anyway (see notes above) */
2025 /* but the thrown-back clause has no extra selectivity */
2026 rinfo->norm_selec = 2.0;
2027 rinfo->outer_selec = 1.0;
2028 distribute_restrictinfo_to_rels(root, rinfo);
2032 /* Process the FULL JOIN clauses */
2033 foreach(cell, root->full_join_clauses)
2035 RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2037 if (reconsider_full_join_clause(root, rinfo))
2039 found = true;
2040 /* remove it from the list */
2041 root->full_join_clauses =
2042 foreach_delete_current(root->full_join_clauses, cell);
2043 /* we throw it back anyway (see notes above) */
2044 /* but the thrown-back clause has no extra selectivity */
2045 rinfo->norm_selec = 2.0;
2046 rinfo->outer_selec = 1.0;
2047 distribute_restrictinfo_to_rels(root, rinfo);
2050 } while (found);
2052 /* Now, any remaining clauses have to be thrown back */
2053 foreach(cell, root->left_join_clauses)
2055 RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2057 distribute_restrictinfo_to_rels(root, rinfo);
2059 foreach(cell, root->right_join_clauses)
2061 RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2063 distribute_restrictinfo_to_rels(root, rinfo);
2065 foreach(cell, root->full_join_clauses)
2067 RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2069 distribute_restrictinfo_to_rels(root, rinfo);
2074 * reconsider_outer_join_clauses for a single LEFT/RIGHT JOIN clause
2076 * Returns true if we were able to propagate a constant through the clause.
2078 static bool
2079 reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo,
2080 bool outer_on_left)
2082 Expr *outervar,
2083 *innervar;
2084 Oid opno,
2085 collation,
2086 left_type,
2087 right_type,
2088 inner_datatype;
2089 Relids inner_relids,
2090 inner_nullable_relids;
2091 ListCell *lc1;
2093 Assert(is_opclause(rinfo->clause));
2094 opno = ((OpExpr *) rinfo->clause)->opno;
2095 collation = ((OpExpr *) rinfo->clause)->inputcollid;
2097 /* If clause is outerjoin_delayed, operator must be strict */
2098 if (rinfo->outerjoin_delayed && !op_strict(opno))
2099 return false;
2101 /* Extract needed info from the clause */
2102 op_input_types(opno, &left_type, &right_type);
2103 if (outer_on_left)
2105 outervar = (Expr *) get_leftop(rinfo->clause);
2106 innervar = (Expr *) get_rightop(rinfo->clause);
2107 inner_datatype = right_type;
2108 inner_relids = rinfo->right_relids;
2110 else
2112 outervar = (Expr *) get_rightop(rinfo->clause);
2113 innervar = (Expr *) get_leftop(rinfo->clause);
2114 inner_datatype = left_type;
2115 inner_relids = rinfo->left_relids;
2117 inner_nullable_relids = bms_intersect(inner_relids,
2118 rinfo->nullable_relids);
2120 /* Scan EquivalenceClasses for a match to outervar */
2121 foreach(lc1, root->eq_classes)
2123 EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2124 bool match;
2125 ListCell *lc2;
2127 /* Ignore EC unless it contains pseudoconstants */
2128 if (!cur_ec->ec_has_const)
2129 continue;
2130 /* Never match to a volatile EC */
2131 if (cur_ec->ec_has_volatile)
2132 continue;
2133 /* It has to match the outer-join clause as to semantics, too */
2134 if (collation != cur_ec->ec_collation)
2135 continue;
2136 if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
2137 continue;
2138 /* Does it contain a match to outervar? */
2139 match = false;
2140 foreach(lc2, cur_ec->ec_members)
2142 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2144 Assert(!cur_em->em_is_child); /* no children yet */
2145 if (equal(outervar, cur_em->em_expr))
2147 match = true;
2148 break;
2151 if (!match)
2152 continue; /* no match, so ignore this EC */
2155 * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
2156 * CONSTANT in the EC. Note that we must succeed with at least one
2157 * constant before we can decide to throw away the outer-join clause.
2159 match = false;
2160 foreach(lc2, cur_ec->ec_members)
2162 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2163 Oid eq_op;
2164 RestrictInfo *newrinfo;
2166 if (!cur_em->em_is_const)
2167 continue; /* ignore non-const members */
2168 eq_op = select_equality_operator(cur_ec,
2169 inner_datatype,
2170 cur_em->em_datatype);
2171 if (!OidIsValid(eq_op))
2172 continue; /* can't generate equality */
2173 newrinfo = build_implied_join_equality(root,
2174 eq_op,
2175 cur_ec->ec_collation,
2176 innervar,
2177 cur_em->em_expr,
2178 bms_copy(inner_relids),
2179 bms_copy(inner_nullable_relids),
2180 cur_ec->ec_min_security);
2181 if (process_equivalence(root, &newrinfo, true))
2182 match = true;
2186 * If we were able to equate INNERVAR to any constant, report success.
2187 * Otherwise, fall out of the search loop, since we know the OUTERVAR
2188 * appears in at most one EC.
2190 if (match)
2191 return true;
2192 else
2193 break;
2196 return false; /* failed to make any deduction */
2200 * reconsider_outer_join_clauses for a single FULL JOIN clause
2202 * Returns true if we were able to propagate a constant through the clause.
2204 static bool
2205 reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
2207 Expr *leftvar;
2208 Expr *rightvar;
2209 Oid opno,
2210 collation,
2211 left_type,
2212 right_type;
2213 Relids left_relids,
2214 right_relids,
2215 left_nullable_relids,
2216 right_nullable_relids;
2217 ListCell *lc1;
2219 /* Can't use an outerjoin_delayed clause here */
2220 if (rinfo->outerjoin_delayed)
2221 return false;
2223 /* Extract needed info from the clause */
2224 Assert(is_opclause(rinfo->clause));
2225 opno = ((OpExpr *) rinfo->clause)->opno;
2226 collation = ((OpExpr *) rinfo->clause)->inputcollid;
2227 op_input_types(opno, &left_type, &right_type);
2228 leftvar = (Expr *) get_leftop(rinfo->clause);
2229 rightvar = (Expr *) get_rightop(rinfo->clause);
2230 left_relids = rinfo->left_relids;
2231 right_relids = rinfo->right_relids;
2232 left_nullable_relids = bms_intersect(left_relids,
2233 rinfo->nullable_relids);
2234 right_nullable_relids = bms_intersect(right_relids,
2235 rinfo->nullable_relids);
2237 foreach(lc1, root->eq_classes)
2239 EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2240 EquivalenceMember *coal_em = NULL;
2241 bool match;
2242 bool matchleft;
2243 bool matchright;
2244 ListCell *lc2;
2245 int coal_idx = -1;
2247 /* Ignore EC unless it contains pseudoconstants */
2248 if (!cur_ec->ec_has_const)
2249 continue;
2250 /* Never match to a volatile EC */
2251 if (cur_ec->ec_has_volatile)
2252 continue;
2253 /* It has to match the outer-join clause as to semantics, too */
2254 if (collation != cur_ec->ec_collation)
2255 continue;
2256 if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
2257 continue;
2260 * Does it contain a COALESCE(leftvar, rightvar) construct?
2262 * We can assume the COALESCE() inputs are in the same order as the
2263 * join clause, since both were automatically generated in the cases
2264 * we care about.
2266 * XXX currently this may fail to match in cross-type cases because
2267 * the COALESCE will contain typecast operations while the join clause
2268 * may not (if there is a cross-type mergejoin operator available for
2269 * the two column types). Is it OK to strip implicit coercions from
2270 * the COALESCE arguments?
2272 match = false;
2273 foreach(lc2, cur_ec->ec_members)
2275 coal_em = (EquivalenceMember *) lfirst(lc2);
2276 Assert(!coal_em->em_is_child); /* no children yet */
2277 if (IsA(coal_em->em_expr, CoalesceExpr))
2279 CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
2280 Node *cfirst;
2281 Node *csecond;
2283 if (list_length(cexpr->args) != 2)
2284 continue;
2285 cfirst = (Node *) linitial(cexpr->args);
2286 csecond = (Node *) lsecond(cexpr->args);
2288 if (equal(leftvar, cfirst) && equal(rightvar, csecond))
2290 coal_idx = foreach_current_index(lc2);
2291 match = true;
2292 break;
2296 if (!match)
2297 continue; /* no match, so ignore this EC */
2300 * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
2301 * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
2302 * succeed with at least one constant for each var before we can
2303 * decide to throw away the outer-join clause.
2305 matchleft = matchright = false;
2306 foreach(lc2, cur_ec->ec_members)
2308 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2309 Oid eq_op;
2310 RestrictInfo *newrinfo;
2312 if (!cur_em->em_is_const)
2313 continue; /* ignore non-const members */
2314 eq_op = select_equality_operator(cur_ec,
2315 left_type,
2316 cur_em->em_datatype);
2317 if (OidIsValid(eq_op))
2319 newrinfo = build_implied_join_equality(root,
2320 eq_op,
2321 cur_ec->ec_collation,
2322 leftvar,
2323 cur_em->em_expr,
2324 bms_copy(left_relids),
2325 bms_copy(left_nullable_relids),
2326 cur_ec->ec_min_security);
2327 if (process_equivalence(root, &newrinfo, true))
2328 matchleft = true;
2330 eq_op = select_equality_operator(cur_ec,
2331 right_type,
2332 cur_em->em_datatype);
2333 if (OidIsValid(eq_op))
2335 newrinfo = build_implied_join_equality(root,
2336 eq_op,
2337 cur_ec->ec_collation,
2338 rightvar,
2339 cur_em->em_expr,
2340 bms_copy(right_relids),
2341 bms_copy(right_nullable_relids),
2342 cur_ec->ec_min_security);
2343 if (process_equivalence(root, &newrinfo, true))
2344 matchright = true;
2349 * If we were able to equate both vars to constants, we're done, and
2350 * we can throw away the full-join clause as redundant. Moreover, we
2351 * can remove the COALESCE entry from the EC, since the added
2352 * restrictions ensure it will always have the expected value. (We
2353 * don't bother trying to update ec_relids or ec_sources.)
2355 if (matchleft && matchright)
2357 cur_ec->ec_members = list_delete_nth_cell(cur_ec->ec_members, coal_idx);
2358 return true;
2362 * Otherwise, fall out of the search loop, since we know the COALESCE
2363 * appears in at most one EC (XXX might stop being true if we allow
2364 * stripping of coercions above?)
2366 break;
2369 return false; /* failed to make any deduction */
2374 * exprs_known_equal
2375 * Detect whether two expressions are known equal due to equivalence
2376 * relationships.
2378 * Actually, this only shows that the expressions are equal according
2379 * to some opfamily's notion of equality --- but we only use it for
2380 * selectivity estimation, so a fuzzy idea of equality is OK.
2382 * Note: does not bother to check for "equal(item1, item2)"; caller must
2383 * check that case if it's possible to pass identical items.
2385 bool
2386 exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2)
2388 ListCell *lc1;
2390 foreach(lc1, root->eq_classes)
2392 EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2393 bool item1member = false;
2394 bool item2member = false;
2395 ListCell *lc2;
2397 /* Never match to a volatile EC */
2398 if (ec->ec_has_volatile)
2399 continue;
2401 foreach(lc2, ec->ec_members)
2403 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
2405 if (em->em_is_child)
2406 continue; /* ignore children here */
2407 if (equal(item1, em->em_expr))
2408 item1member = true;
2409 else if (equal(item2, em->em_expr))
2410 item2member = true;
2411 /* Exit as soon as equality is proven */
2412 if (item1member && item2member)
2413 return true;
2416 return false;
2421 * match_eclasses_to_foreign_key_col
2422 * See whether a foreign key column match is proven by any eclass.
2424 * If the referenced and referencing Vars of the fkey's colno'th column are
2425 * known equal due to any eclass, return that eclass; otherwise return NULL.
2426 * (In principle there might be more than one matching eclass if multiple
2427 * collations are involved, but since collation doesn't matter for equality,
2428 * we ignore that fine point here.) This is much like exprs_known_equal,
2429 * except that we insist on the comparison operator matching the eclass, so
2430 * that the result is definite not approximate.
2432 * On success, we also set fkinfo->eclass[colno] to the matching eclass,
2433 * and set fkinfo->fk_eclass_member[colno] to the eclass member for the
2434 * referencing Var.
2436 EquivalenceClass *
2437 match_eclasses_to_foreign_key_col(PlannerInfo *root,
2438 ForeignKeyOptInfo *fkinfo,
2439 int colno)
2441 Index var1varno = fkinfo->con_relid;
2442 AttrNumber var1attno = fkinfo->conkey[colno];
2443 Index var2varno = fkinfo->ref_relid;
2444 AttrNumber var2attno = fkinfo->confkey[colno];
2445 Oid eqop = fkinfo->conpfeqop[colno];
2446 RelOptInfo *rel1 = root->simple_rel_array[var1varno];
2447 RelOptInfo *rel2 = root->simple_rel_array[var2varno];
2448 List *opfamilies = NIL; /* compute only if needed */
2449 Bitmapset *matching_ecs;
2450 int i;
2452 /* Consider only eclasses mentioning both relations */
2453 Assert(root->ec_merging_done);
2454 Assert(IS_SIMPLE_REL(rel1));
2455 Assert(IS_SIMPLE_REL(rel2));
2456 matching_ecs = bms_intersect(rel1->eclass_indexes,
2457 rel2->eclass_indexes);
2459 i = -1;
2460 while ((i = bms_next_member(matching_ecs, i)) >= 0)
2462 EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
2464 EquivalenceMember *item1_em = NULL;
2465 EquivalenceMember *item2_em = NULL;
2466 ListCell *lc2;
2468 /* Never match to a volatile EC */
2469 if (ec->ec_has_volatile)
2470 continue;
2471 /* Note: it seems okay to match to "broken" eclasses here */
2473 foreach(lc2, ec->ec_members)
2475 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
2476 Var *var;
2478 if (em->em_is_child)
2479 continue; /* ignore children here */
2481 /* EM must be a Var, possibly with RelabelType */
2482 var = (Var *) em->em_expr;
2483 while (var && IsA(var, RelabelType))
2484 var = (Var *) ((RelabelType *) var)->arg;
2485 if (!(var && IsA(var, Var)))
2486 continue;
2488 /* Match? */
2489 if (var->varno == var1varno && var->varattno == var1attno)
2490 item1_em = em;
2491 else if (var->varno == var2varno && var->varattno == var2attno)
2492 item2_em = em;
2494 /* Have we found both PK and FK column in this EC? */
2495 if (item1_em && item2_em)
2498 * Succeed if eqop matches EC's opfamilies. We could test
2499 * this before scanning the members, but it's probably cheaper
2500 * to test for member matches first.
2502 if (opfamilies == NIL) /* compute if we didn't already */
2503 opfamilies = get_mergejoin_opfamilies(eqop);
2504 if (equal(opfamilies, ec->ec_opfamilies))
2506 fkinfo->eclass[colno] = ec;
2507 fkinfo->fk_eclass_member[colno] = item2_em;
2508 return ec;
2510 /* Otherwise, done with this EC, move on to the next */
2511 break;
2515 return NULL;
2519 * find_derived_clause_for_ec_member
2520 * Search for a previously-derived clause mentioning the given EM.
2522 * The eclass should be an ec_has_const EC, of which the EM is a non-const
2523 * member. This should ensure there is just one derived clause mentioning
2524 * the EM (and equating it to a constant).
2525 * Returns NULL if no such clause can be found.
2527 RestrictInfo *
2528 find_derived_clause_for_ec_member(EquivalenceClass *ec,
2529 EquivalenceMember *em)
2531 ListCell *lc;
2533 Assert(ec->ec_has_const);
2534 Assert(!em->em_is_const);
2535 foreach(lc, ec->ec_derives)
2537 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2540 * generate_base_implied_equalities_const will have put non-const
2541 * members on the left side of derived clauses.
2543 if (rinfo->left_em == em)
2544 return rinfo;
2546 return NULL;
2551 * add_child_rel_equivalences
2552 * Search for EC members that reference the root parent of child_rel, and
2553 * add transformed members referencing the child_rel.
2555 * Note that this function won't be called at all unless we have at least some
2556 * reason to believe that the EC members it generates will be useful.
2558 * parent_rel and child_rel could be derived from appinfo, but since the
2559 * caller has already computed them, we might as well just pass them in.
2561 * The passed-in AppendRelInfo is not used when the parent_rel is not a
2562 * top-level baserel, since it shows the mapping from the parent_rel but
2563 * we need to translate EC expressions that refer to the top-level parent.
2564 * Using it is faster than using adjust_appendrel_attrs_multilevel(), though,
2565 * so we prefer it when we can.
2567 void
2568 add_child_rel_equivalences(PlannerInfo *root,
2569 AppendRelInfo *appinfo,
2570 RelOptInfo *parent_rel,
2571 RelOptInfo *child_rel)
2573 Relids top_parent_relids = child_rel->top_parent_relids;
2574 Relids child_relids = child_rel->relids;
2575 int i;
2578 * EC merging should be complete already, so we can use the parent rel's
2579 * eclass_indexes to avoid searching all of root->eq_classes.
2581 Assert(root->ec_merging_done);
2582 Assert(IS_SIMPLE_REL(parent_rel));
2584 i = -1;
2585 while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
2587 EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2588 int num_members;
2591 * If this EC contains a volatile expression, then generating child
2592 * EMs would be downright dangerous, so skip it. We rely on a
2593 * volatile EC having only one EM.
2595 if (cur_ec->ec_has_volatile)
2596 continue;
2598 /* Sanity check eclass_indexes only contain ECs for parent_rel */
2599 Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
2602 * We don't use foreach() here because there's no point in scanning
2603 * newly-added child members, so we can stop after the last
2604 * pre-existing EC member.
2606 num_members = list_length(cur_ec->ec_members);
2607 for (int pos = 0; pos < num_members; pos++)
2609 EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2611 if (cur_em->em_is_const)
2612 continue; /* ignore consts here */
2615 * We consider only original EC members here, not
2616 * already-transformed child members. Otherwise, if some original
2617 * member expression references more than one appendrel, we'd get
2618 * an O(N^2) explosion of useless derived expressions for
2619 * combinations of children. (But add_child_join_rel_equivalences
2620 * may add targeted combinations for partitionwise-join purposes.)
2622 if (cur_em->em_is_child)
2623 continue; /* ignore children here */
2625 /* Does this member reference child's topmost parent rel? */
2626 if (bms_overlap(cur_em->em_relids, top_parent_relids))
2628 /* Yes, generate transformed child version */
2629 Expr *child_expr;
2630 Relids new_relids;
2631 Relids new_nullable_relids;
2633 if (parent_rel->reloptkind == RELOPT_BASEREL)
2635 /* Simple single-level transformation */
2636 child_expr = (Expr *)
2637 adjust_appendrel_attrs(root,
2638 (Node *) cur_em->em_expr,
2639 1, &appinfo);
2641 else
2643 /* Must do multi-level transformation */
2644 child_expr = (Expr *)
2645 adjust_appendrel_attrs_multilevel(root,
2646 (Node *) cur_em->em_expr,
2647 child_relids,
2648 top_parent_relids);
2652 * Transform em_relids to match. Note we do *not* do
2653 * pull_varnos(child_expr) here, as for example the
2654 * transformation might have substituted a constant, but we
2655 * don't want the child member to be marked as constant.
2657 new_relids = bms_difference(cur_em->em_relids,
2658 top_parent_relids);
2659 new_relids = bms_add_members(new_relids, child_relids);
2662 * And likewise for nullable_relids. Note this code assumes
2663 * parent and child relids are singletons.
2665 new_nullable_relids = cur_em->em_nullable_relids;
2666 if (bms_overlap(new_nullable_relids, top_parent_relids))
2668 new_nullable_relids = bms_difference(new_nullable_relids,
2669 top_parent_relids);
2670 new_nullable_relids = bms_add_members(new_nullable_relids,
2671 child_relids);
2674 (void) add_eq_member(cur_ec, child_expr,
2675 new_relids, new_nullable_relids,
2676 true, cur_em->em_datatype);
2678 /* Record this EC index for the child rel */
2679 child_rel->eclass_indexes = bms_add_member(child_rel->eclass_indexes, i);
2686 * add_child_join_rel_equivalences
2687 * Like add_child_rel_equivalences(), but for joinrels
2689 * Here we find the ECs relevant to the top parent joinrel and add transformed
2690 * member expressions that refer to this child joinrel.
2692 * Note that this function won't be called at all unless we have at least some
2693 * reason to believe that the EC members it generates will be useful.
2695 void
2696 add_child_join_rel_equivalences(PlannerInfo *root,
2697 int nappinfos, AppendRelInfo **appinfos,
2698 RelOptInfo *parent_joinrel,
2699 RelOptInfo *child_joinrel)
2701 Relids top_parent_relids = child_joinrel->top_parent_relids;
2702 Relids child_relids = child_joinrel->relids;
2703 Bitmapset *matching_ecs;
2704 MemoryContext oldcontext;
2705 int i;
2707 Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
2709 /* We need consider only ECs that mention the parent joinrel */
2710 matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
2713 * If we're being called during GEQO join planning, we still have to
2714 * create any new EC members in the main planner context, to avoid having
2715 * a corrupt EC data structure after the GEQO context is reset. This is
2716 * problematic since we'll leak memory across repeated GEQO cycles. For
2717 * now, though, bloat is better than crash. If it becomes a real issue
2718 * we'll have to do something to avoid generating duplicate EC members.
2720 oldcontext = MemoryContextSwitchTo(root->planner_cxt);
2722 i = -1;
2723 while ((i = bms_next_member(matching_ecs, i)) >= 0)
2725 EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2726 int num_members;
2729 * If this EC contains a volatile expression, then generating child
2730 * EMs would be downright dangerous, so skip it. We rely on a
2731 * volatile EC having only one EM.
2733 if (cur_ec->ec_has_volatile)
2734 continue;
2736 /* Sanity check on get_eclass_indexes_for_relids result */
2737 Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
2740 * We don't use foreach() here because there's no point in scanning
2741 * newly-added child members, so we can stop after the last
2742 * pre-existing EC member.
2744 num_members = list_length(cur_ec->ec_members);
2745 for (int pos = 0; pos < num_members; pos++)
2747 EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2749 if (cur_em->em_is_const)
2750 continue; /* ignore consts here */
2753 * We consider only original EC members here, not
2754 * already-transformed child members.
2756 if (cur_em->em_is_child)
2757 continue; /* ignore children here */
2760 * We may ignore expressions that reference a single baserel,
2761 * because add_child_rel_equivalences should have handled them.
2763 if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
2764 continue;
2766 /* Does this member reference child's topmost parent rel? */
2767 if (bms_overlap(cur_em->em_relids, top_parent_relids))
2769 /* Yes, generate transformed child version */
2770 Expr *child_expr;
2771 Relids new_relids;
2772 Relids new_nullable_relids;
2774 if (parent_joinrel->reloptkind == RELOPT_JOINREL)
2776 /* Simple single-level transformation */
2777 child_expr = (Expr *)
2778 adjust_appendrel_attrs(root,
2779 (Node *) cur_em->em_expr,
2780 nappinfos, appinfos);
2782 else
2784 /* Must do multi-level transformation */
2785 Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
2786 child_expr = (Expr *)
2787 adjust_appendrel_attrs_multilevel(root,
2788 (Node *) cur_em->em_expr,
2789 child_relids,
2790 top_parent_relids);
2794 * Transform em_relids to match. Note we do *not* do
2795 * pull_varnos(child_expr) here, as for example the
2796 * transformation might have substituted a constant, but we
2797 * don't want the child member to be marked as constant.
2799 new_relids = bms_difference(cur_em->em_relids,
2800 top_parent_relids);
2801 new_relids = bms_add_members(new_relids, child_relids);
2804 * For nullable_relids, we must selectively replace parent
2805 * nullable relids with child ones.
2807 new_nullable_relids = cur_em->em_nullable_relids;
2808 if (bms_overlap(new_nullable_relids, top_parent_relids))
2809 new_nullable_relids =
2810 adjust_child_relids_multilevel(root,
2811 new_nullable_relids,
2812 child_relids,
2813 top_parent_relids);
2815 (void) add_eq_member(cur_ec, child_expr,
2816 new_relids, new_nullable_relids,
2817 true, cur_em->em_datatype);
2822 MemoryContextSwitchTo(oldcontext);
2827 * generate_implied_equalities_for_column
2828 * Create EC-derived joinclauses usable with a specific column.
2830 * This is used by indxpath.c to extract potentially indexable joinclauses
2831 * from ECs, and can be used by foreign data wrappers for similar purposes.
2832 * We assume that only expressions in Vars of a single table are of interest,
2833 * but the caller provides a callback function to identify exactly which
2834 * such expressions it would like to know about.
2836 * We assume that any given table/index column could appear in only one EC.
2837 * (This should be true in all but the most pathological cases, and if it
2838 * isn't, we stop on the first match anyway.) Therefore, what we return
2839 * is a redundant list of clauses equating the table/index column to each of
2840 * the other-relation values it is known to be equal to. Any one of
2841 * these clauses can be used to create a parameterized path, and there
2842 * is no value in using more than one. (But it *is* worthwhile to create
2843 * a separate parameterized path for each one, since that leads to different
2844 * join orders.)
2846 * The caller can pass a Relids set of rels we aren't interested in joining
2847 * to, so as to save the work of creating useless clauses.
2849 List *
2850 generate_implied_equalities_for_column(PlannerInfo *root,
2851 RelOptInfo *rel,
2852 ec_matches_callback_type callback,
2853 void *callback_arg,
2854 Relids prohibited_rels)
2856 List *result = NIL;
2857 bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2858 Relids parent_relids;
2859 int i;
2861 /* Should be OK to rely on eclass_indexes */
2862 Assert(root->ec_merging_done);
2864 /* Indexes are available only on base or "other" member relations. */
2865 Assert(IS_SIMPLE_REL(rel));
2867 /* If it's a child rel, we'll need to know what its parent(s) are */
2868 if (is_child_rel)
2869 parent_relids = find_childrel_parents(root, rel);
2870 else
2871 parent_relids = NULL; /* not used, but keep compiler quiet */
2873 i = -1;
2874 while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
2876 EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2877 EquivalenceMember *cur_em;
2878 ListCell *lc2;
2880 /* Sanity check eclass_indexes only contain ECs for rel */
2881 Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
2884 * Won't generate joinclauses if const or single-member (the latter
2885 * test covers the volatile case too)
2887 if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2888 continue;
2891 * Scan members, looking for a match to the target column. Note that
2892 * child EC members are considered, but only when they belong to the
2893 * target relation. (Unlike regular members, the same expression
2894 * could be a child member of more than one EC. Therefore, it's
2895 * potentially order-dependent which EC a child relation's target
2896 * column gets matched to. This is annoying but it only happens in
2897 * corner cases, so for now we live with just reporting the first
2898 * match. See also get_eclass_for_sort_expr.)
2900 cur_em = NULL;
2901 foreach(lc2, cur_ec->ec_members)
2903 cur_em = (EquivalenceMember *) lfirst(lc2);
2904 if (bms_equal(cur_em->em_relids, rel->relids) &&
2905 callback(root, rel, cur_ec, cur_em, callback_arg))
2906 break;
2907 cur_em = NULL;
2910 if (!cur_em)
2911 continue;
2914 * Found our match. Scan the other EC members and attempt to generate
2915 * joinclauses.
2917 foreach(lc2, cur_ec->ec_members)
2919 EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2920 Oid eq_op;
2921 RestrictInfo *rinfo;
2923 if (other_em->em_is_child)
2924 continue; /* ignore children here */
2926 /* Make sure it'll be a join to a different rel */
2927 if (other_em == cur_em ||
2928 bms_overlap(other_em->em_relids, rel->relids))
2929 continue;
2931 /* Forget it if caller doesn't want joins to this rel */
2932 if (bms_overlap(other_em->em_relids, prohibited_rels))
2933 continue;
2936 * Also, if this is a child rel, avoid generating a useless join
2937 * to its parent rel(s).
2939 if (is_child_rel &&
2940 bms_overlap(parent_relids, other_em->em_relids))
2941 continue;
2943 eq_op = select_equality_operator(cur_ec,
2944 cur_em->em_datatype,
2945 other_em->em_datatype);
2946 if (!OidIsValid(eq_op))
2947 continue;
2949 /* set parent_ec to mark as redundant with other joinclauses */
2950 rinfo = create_join_clause(root, cur_ec, eq_op,
2951 cur_em, other_em,
2952 cur_ec);
2954 result = lappend(result, rinfo);
2958 * If somehow we failed to create any join clauses, we might as well
2959 * keep scanning the ECs for another match. But if we did make any,
2960 * we're done, because we don't want to return non-redundant clauses.
2962 if (result)
2963 break;
2966 return result;
2970 * have_relevant_eclass_joinclause
2971 * Detect whether there is an EquivalenceClass that could produce
2972 * a joinclause involving the two given relations.
2974 * This is essentially a very cut-down version of
2975 * generate_join_implied_equalities(). Note it's OK to occasionally say "yes"
2976 * incorrectly. Hence we don't bother with details like whether the lack of a
2977 * cross-type operator might prevent the clause from actually being generated.
2979 bool
2980 have_relevant_eclass_joinclause(PlannerInfo *root,
2981 RelOptInfo *rel1, RelOptInfo *rel2)
2983 Bitmapset *matching_ecs;
2984 int i;
2986 /* Examine only eclasses mentioning both rel1 and rel2 */
2987 matching_ecs = get_common_eclass_indexes(root, rel1->relids,
2988 rel2->relids);
2990 i = -1;
2991 while ((i = bms_next_member(matching_ecs, i)) >= 0)
2993 EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
2997 * Sanity check that get_common_eclass_indexes gave only ECs
2998 * containing both rels.
3000 Assert(bms_overlap(rel1->relids, ec->ec_relids));
3001 Assert(bms_overlap(rel2->relids, ec->ec_relids));
3004 * Won't generate joinclauses if single-member (this test covers the
3005 * volatile case too)
3007 if (list_length(ec->ec_members) <= 1)
3008 continue;
3011 * We do not need to examine the individual members of the EC, because
3012 * all that we care about is whether each rel overlaps the relids of
3013 * at least one member, and get_common_eclass_indexes() and the single
3014 * member check above are sufficient to prove that. (As with
3015 * have_relevant_joinclause(), it is not necessary that the EC be able
3016 * to form a joinclause relating exactly the two given rels, only that
3017 * it be able to form a joinclause mentioning both, and this will
3018 * surely be true if both of them overlap ec_relids.)
3020 * Note we don't test ec_broken; if we did, we'd need a separate code
3021 * path to look through ec_sources. Checking the membership anyway is
3022 * OK as a possibly-overoptimistic heuristic.
3024 * We don't test ec_has_const either, even though a const eclass won't
3025 * generate real join clauses. This is because if we had "WHERE a.x =
3026 * b.y and a.x = 42", it is worth considering a join between a and b,
3027 * since the join result is likely to be small even though it'll end
3028 * up being an unqualified nestloop.
3031 return true;
3034 return false;
3039 * has_relevant_eclass_joinclause
3040 * Detect whether there is an EquivalenceClass that could produce
3041 * a joinclause involving the given relation and anything else.
3043 * This is the same as have_relevant_eclass_joinclause with the other rel
3044 * implicitly defined as "everything else in the query".
3046 bool
3047 has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
3049 Bitmapset *matched_ecs;
3050 int i;
3052 /* Examine only eclasses mentioning rel1 */
3053 matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
3055 i = -1;
3056 while ((i = bms_next_member(matched_ecs, i)) >= 0)
3058 EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
3062 * Won't generate joinclauses if single-member (this test covers the
3063 * volatile case too)
3065 if (list_length(ec->ec_members) <= 1)
3066 continue;
3069 * Per the comment in have_relevant_eclass_joinclause, it's sufficient
3070 * to find an EC that mentions both this rel and some other rel.
3072 if (!bms_is_subset(ec->ec_relids, rel1->relids))
3073 return true;
3076 return false;
3081 * eclass_useful_for_merging
3082 * Detect whether the EC could produce any mergejoinable join clauses
3083 * against the specified relation.
3085 * This is just a heuristic test and doesn't have to be exact; it's better
3086 * to say "yes" incorrectly than "no". Hence we don't bother with details
3087 * like whether the lack of a cross-type operator might prevent the clause
3088 * from actually being generated.
3090 bool
3091 eclass_useful_for_merging(PlannerInfo *root,
3092 EquivalenceClass *eclass,
3093 RelOptInfo *rel)
3095 Relids relids;
3096 ListCell *lc;
3098 Assert(!eclass->ec_merged);
3101 * Won't generate joinclauses if const or single-member (the latter test
3102 * covers the volatile case too)
3104 if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
3105 return false;
3108 * Note we don't test ec_broken; if we did, we'd need a separate code path
3109 * to look through ec_sources. Checking the members anyway is OK as a
3110 * possibly-overoptimistic heuristic.
3113 /* If specified rel is a child, we must consider the topmost parent rel */
3114 if (IS_OTHER_REL(rel))
3116 Assert(!bms_is_empty(rel->top_parent_relids));
3117 relids = rel->top_parent_relids;
3119 else
3120 relids = rel->relids;
3122 /* If rel already includes all members of eclass, no point in searching */
3123 if (bms_is_subset(eclass->ec_relids, relids))
3124 return false;
3126 /* To join, we need a member not in the given rel */
3127 foreach(lc, eclass->ec_members)
3129 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
3131 if (cur_em->em_is_child)
3132 continue; /* ignore children here */
3134 if (!bms_overlap(cur_em->em_relids, relids))
3135 return true;
3138 return false;
3143 * is_redundant_derived_clause
3144 * Test whether rinfo is derived from same EC as any clause in clauselist;
3145 * if so, it can be presumed to represent a condition that's redundant
3146 * with that member of the list.
3148 bool
3149 is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
3151 EquivalenceClass *parent_ec = rinfo->parent_ec;
3152 ListCell *lc;
3154 /* Fail if it's not a potentially-redundant clause from some EC */
3155 if (parent_ec == NULL)
3156 return false;
3158 foreach(lc, clauselist)
3160 RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
3162 if (otherrinfo->parent_ec == parent_ec)
3163 return true;
3166 return false;
3170 * is_redundant_with_indexclauses
3171 * Test whether rinfo is redundant with any clause in the IndexClause
3172 * list. Here, for convenience, we test both simple identity and
3173 * whether it is derived from the same EC as any member of the list.
3175 bool
3176 is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
3178 EquivalenceClass *parent_ec = rinfo->parent_ec;
3179 ListCell *lc;
3181 foreach(lc, indexclauses)
3183 IndexClause *iclause = lfirst_node(IndexClause, lc);
3184 RestrictInfo *otherrinfo = iclause->rinfo;
3186 /* If indexclause is lossy, it won't enforce the condition exactly */
3187 if (iclause->lossy)
3188 continue;
3190 /* Match if it's same clause (pointer equality should be enough) */
3191 if (rinfo == otherrinfo)
3192 return true;
3193 /* Match if derived from same EC */
3194 if (parent_ec && otherrinfo->parent_ec == parent_ec)
3195 return true;
3198 * No need to look at the derived clauses in iclause->indexquals; they
3199 * couldn't match if the parent clause didn't.
3203 return false;
3207 * get_eclass_indexes_for_relids
3208 * Build and return a Bitmapset containing the indexes into root's
3209 * eq_classes list for all eclasses that mention any of these relids
3211 static Bitmapset *
3212 get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
3214 Bitmapset *ec_indexes = NULL;
3215 int i = -1;
3217 /* Should be OK to rely on eclass_indexes */
3218 Assert(root->ec_merging_done);
3220 while ((i = bms_next_member(relids, i)) > 0)
3222 RelOptInfo *rel = root->simple_rel_array[i];
3224 ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
3226 return ec_indexes;
3230 * get_common_eclass_indexes
3231 * Build and return a Bitmapset containing the indexes into root's
3232 * eq_classes list for all eclasses that mention rels in both
3233 * relids1 and relids2.
3235 static Bitmapset *
3236 get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
3238 Bitmapset *rel1ecs;
3239 Bitmapset *rel2ecs;
3240 int relid;
3242 rel1ecs = get_eclass_indexes_for_relids(root, relids1);
3245 * We can get away with just using the relation's eclass_indexes directly
3246 * when relids2 is a singleton set.
3248 if (bms_get_singleton_member(relids2, &relid))
3249 rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
3250 else
3251 rel2ecs = get_eclass_indexes_for_relids(root, relids2);
3253 /* Calculate and return the common EC indexes, recycling the left input. */
3254 return bms_int_members(rel1ecs, rel2ecs);