PostgreSQL Source Code  git master
equivclass.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * equivclass.c
4  * Routines for managing EquivalenceClasses
5  *
6  * See src/backend/optimizer/README for discussion of EquivalenceClasses.
7  *
8  *
9  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * IDENTIFICATION
13  * src/backend/optimizer/path/equivclass.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 #include "postgres.h"
18 
19 #include <limits.h>
20 
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"
33 
34 
36  Expr *expr, Relids relids, Relids nullable_relids,
37  bool is_child, Oid datatype);
39  EquivalenceClass *ec);
41  EquivalenceClass *ec);
43  EquivalenceClass *ec);
45  EquivalenceClass *ec,
46  Relids join_relids,
47  Relids outer_relids,
48  Relids inner_relids);
50  EquivalenceClass *ec,
51  Relids nominal_join_relids,
52  Relids outer_relids,
53  Relids nominal_inner_relids,
54  RelOptInfo *inner_rel);
56  Oid lefttype, Oid righttype);
58  EquivalenceClass *ec, Oid opno,
59  EquivalenceMember *leftem,
60  EquivalenceMember *rightem,
61  EquivalenceClass *parent_ec);
63  RestrictInfo *rinfo,
64  bool outer_on_left);
66  RestrictInfo *rinfo);
68  Relids relids);
70  Relids relids2);
71 
72 
73 /*
74  * process_equivalence
75  * The given clause has a mergejoinable operator and can be applied without
76  * any delay by an outer join, so its two sides can be considered equal
77  * anywhere they are both computable; moreover that equality can be
78  * extended transitively. Record this knowledge in the EquivalenceClass
79  * data structure, if applicable. Returns true if successful, false if not
80  * (in which case caller should treat the clause as ordinary, not an
81  * equivalence).
82  *
83  * In some cases, although we cannot convert a clause into EquivalenceClass
84  * knowledge, we can still modify it to a more useful form than the original.
85  * Then, *p_restrictinfo will be replaced by a new RestrictInfo, which is what
86  * the caller should use for further processing.
87  *
88  * If below_outer_join is true, then the clause was found below the nullable
89  * side of an outer join, so its sides might validly be both NULL rather than
90  * strictly equal. We can still deduce equalities in such cases, but we take
91  * care to mark an EquivalenceClass if it came from any such clauses. Also,
92  * we have to check that both sides are either pseudo-constants or strict
93  * functions of Vars, else they might not both go to NULL above the outer
94  * join. (This is the main reason why we need a failure return. It's more
95  * convenient to check this case here than at the call sites...)
96  *
97  * We also reject proposed equivalence clauses if they contain leaky functions
98  * and have security_level above zero. The EC evaluation rules require us to
99  * apply certain tests at certain joining levels, and we can't tolerate
100  * delaying any test on security_level grounds. By rejecting candidate clauses
101  * that might require security delays, we ensure it's safe to apply an EC
102  * clause as soon as it's supposed to be applied.
103  *
104  * On success return, we have also initialized the clause's left_ec/right_ec
105  * fields to point to the EquivalenceClass representing it. This saves lookup
106  * effort later.
107  *
108  * Note: constructing merged EquivalenceClasses is a standard UNION-FIND
109  * problem, for which there exist better data structures than simple lists.
110  * If this code ever proves to be a bottleneck then it could be sped up ---
111  * but for now, simple is beautiful.
112  *
113  * Note: this is only called during planner startup, not during GEQO
114  * exploration, so we need not worry about whether we're in the right
115  * memory context.
116  */
117 bool
119  RestrictInfo **p_restrictinfo,
120  bool below_outer_join)
121 {
122  RestrictInfo *restrictinfo = *p_restrictinfo;
123  Expr *clause = restrictinfo->clause;
124  Oid opno,
125  collation,
126  item1_type,
127  item2_type;
128  Expr *item1;
129  Expr *item2;
130  Relids item1_relids,
131  item2_relids,
132  item1_nullable_relids,
133  item2_nullable_relids;
134  List *opfamilies;
135  EquivalenceClass *ec1,
136  *ec2;
137  EquivalenceMember *em1,
138  *em2;
139  ListCell *lc1;
140 
141  /* Should not already be marked as having generated an eclass */
142  Assert(restrictinfo->left_ec == NULL);
143  Assert(restrictinfo->right_ec == NULL);
144 
145  /* Reject if it is potentially postponable by security considerations */
146  if (restrictinfo->security_level > 0 && !restrictinfo->leakproof)
147  return false;
148 
149  /* Extract info from given clause */
150  Assert(is_opclause(clause));
151  opno = ((OpExpr *) clause)->opno;
152  collation = ((OpExpr *) clause)->inputcollid;
153  item1 = (Expr *) get_leftop(clause);
154  item2 = (Expr *) get_rightop(clause);
155  item1_relids = restrictinfo->left_relids;
156  item2_relids = restrictinfo->right_relids;
157 
158  /*
159  * Ensure both input expressions expose the desired collation (their types
160  * should be OK already); see comments for canonicalize_ec_expression.
161  */
162  item1 = canonicalize_ec_expression(item1,
163  exprType((Node *) item1),
164  collation);
165  item2 = canonicalize_ec_expression(item2,
166  exprType((Node *) item2),
167  collation);
168 
169  /*
170  * Clauses of the form X=X cannot be translated into EquivalenceClasses.
171  * We'd either end up with a single-entry EC, losing the knowledge that
172  * the clause was present at all, or else make an EC with duplicate
173  * entries, causing other issues.
174  */
175  if (equal(item1, item2))
176  {
177  /*
178  * If the operator is strict, then the clause can be treated as just
179  * "X IS NOT NULL". (Since we know we are considering a top-level
180  * qual, we can ignore the difference between FALSE and NULL results.)
181  * It's worth making the conversion because we'll typically get a much
182  * better selectivity estimate than we would for X=X.
183  *
184  * If the operator is not strict, we can't be sure what it will do
185  * with NULLs, so don't attempt to optimize it.
186  */
187  set_opfuncid((OpExpr *) clause);
188  if (func_strict(((OpExpr *) clause)->opfuncid))
189  {
190  NullTest *ntest = makeNode(NullTest);
191 
192  ntest->arg = item1;
193  ntest->nulltesttype = IS_NOT_NULL;
194  ntest->argisrow = false; /* correct even if composite arg */
195  ntest->location = -1;
196 
197  *p_restrictinfo =
198  make_restrictinfo((Expr *) ntest,
199  restrictinfo->is_pushed_down,
200  restrictinfo->outerjoin_delayed,
201  restrictinfo->pseudoconstant,
202  restrictinfo->security_level,
203  NULL,
204  restrictinfo->outer_relids,
205  restrictinfo->nullable_relids);
206  }
207  return false;
208  }
209 
210  /*
211  * If below outer join, check for strictness, else reject.
212  */
213  if (below_outer_join)
214  {
215  if (!bms_is_empty(item1_relids) &&
217  return false; /* LHS is non-strict but not constant */
218  if (!bms_is_empty(item2_relids) &&
220  return false; /* RHS is non-strict but not constant */
221  }
222 
223  /* Calculate nullable-relid sets for each side of the clause */
224  item1_nullable_relids = bms_intersect(item1_relids,
225  restrictinfo->nullable_relids);
226  item2_nullable_relids = bms_intersect(item2_relids,
227  restrictinfo->nullable_relids);
228 
229  /*
230  * We use the declared input types of the operator, not exprType() of the
231  * inputs, as the nominal datatypes for opfamily lookup. This presumes
232  * that btree operators are always registered with amoplefttype and
233  * amoprighttype equal to their declared input types. We will need this
234  * info anyway to build EquivalenceMember nodes, and by extracting it now
235  * we can use type comparisons to short-circuit some equal() tests.
236  */
237  op_input_types(opno, &item1_type, &item2_type);
238 
239  opfamilies = restrictinfo->mergeopfamilies;
240 
241  /*
242  * Sweep through the existing EquivalenceClasses looking for matches to
243  * item1 and item2. These are the possible outcomes:
244  *
245  * 1. We find both in the same EC. The equivalence is already known, so
246  * there's nothing to do.
247  *
248  * 2. We find both in different ECs. Merge the two ECs together.
249  *
250  * 3. We find just one. Add the other to its EC.
251  *
252  * 4. We find neither. Make a new, two-entry EC.
253  *
254  * Note: since all ECs are built through this process or the similar
255  * search in get_eclass_for_sort_expr(), it's impossible that we'd match
256  * an item in more than one existing nonvolatile EC. So it's okay to stop
257  * at the first match.
258  */
259  ec1 = ec2 = NULL;
260  em1 = em2 = NULL;
261  foreach(lc1, root->eq_classes)
262  {
263  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
264  ListCell *lc2;
265 
266  /* Never match to a volatile EC */
267  if (cur_ec->ec_has_volatile)
268  continue;
269 
270  /*
271  * The collation has to match; check this first since it's cheaper
272  * than the opfamily comparison.
273  */
274  if (collation != cur_ec->ec_collation)
275  continue;
276 
277  /*
278  * A "match" requires matching sets of btree opfamilies. Use of
279  * equal() for this test has implications discussed in the comments
280  * for get_mergejoin_opfamilies().
281  */
282  if (!equal(opfamilies, cur_ec->ec_opfamilies))
283  continue;
284 
285  foreach(lc2, cur_ec->ec_members)
286  {
287  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
288 
289  Assert(!cur_em->em_is_child); /* no children yet */
290 
291  /*
292  * If below an outer join, don't match constants: they're not as
293  * constant as they look.
294  */
295  if ((below_outer_join || cur_ec->ec_below_outer_join) &&
296  cur_em->em_is_const)
297  continue;
298 
299  if (!ec1 &&
300  item1_type == cur_em->em_datatype &&
301  equal(item1, cur_em->em_expr))
302  {
303  ec1 = cur_ec;
304  em1 = cur_em;
305  if (ec2)
306  break;
307  }
308 
309  if (!ec2 &&
310  item2_type == cur_em->em_datatype &&
311  equal(item2, cur_em->em_expr))
312  {
313  ec2 = cur_ec;
314  em2 = cur_em;
315  if (ec1)
316  break;
317  }
318  }
319 
320  if (ec1 && ec2)
321  break;
322  }
323 
324  /* Sweep finished, what did we find? */
325 
326  if (ec1 && ec2)
327  {
328  /* If case 1, nothing to do, except add to sources */
329  if (ec1 == ec2)
330  {
331  ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
332  ec1->ec_below_outer_join |= below_outer_join;
333  ec1->ec_min_security = Min(ec1->ec_min_security,
334  restrictinfo->security_level);
335  ec1->ec_max_security = Max(ec1->ec_max_security,
336  restrictinfo->security_level);
337  /* mark the RI as associated with this eclass */
338  restrictinfo->left_ec = ec1;
339  restrictinfo->right_ec = ec1;
340  /* mark the RI as usable with this pair of EMs */
341  restrictinfo->left_em = em1;
342  restrictinfo->right_em = em2;
343  return true;
344  }
345 
346  /*
347  * Case 2: need to merge ec1 and ec2. This should never happen after
348  * the ECs have reached canonical state; otherwise, pathkeys could be
349  * rendered non-canonical by the merge, and relation eclass indexes
350  * would get broken by removal of an eq_classes list entry.
351  */
352  if (root->ec_merging_done)
353  elog(ERROR, "too late to merge equivalence classes");
354 
355  /*
356  * We add ec2's items to ec1, then set ec2's ec_merged link to point
357  * to ec1 and remove ec2 from the eq_classes list. We cannot simply
358  * delete ec2 because that could leave dangling pointers in existing
359  * PathKeys. We leave it behind with a link so that the merged EC can
360  * be found.
361  */
362  ec1->ec_members = list_concat(ec1->ec_members, ec2->ec_members);
363  ec1->ec_sources = list_concat(ec1->ec_sources, ec2->ec_sources);
364  ec1->ec_derives = list_concat(ec1->ec_derives, ec2->ec_derives);
365  ec1->ec_relids = bms_join(ec1->ec_relids, ec2->ec_relids);
366  ec1->ec_has_const |= ec2->ec_has_const;
367  /* can't need to set has_volatile */
369  ec1->ec_min_security = Min(ec1->ec_min_security,
370  ec2->ec_min_security);
371  ec1->ec_max_security = Max(ec1->ec_max_security,
372  ec2->ec_max_security);
373  ec2->ec_merged = ec1;
374  root->eq_classes = list_delete_ptr(root->eq_classes, ec2);
375  /* just to avoid debugging confusion w/ dangling pointers: */
376  ec2->ec_members = NIL;
377  ec2->ec_sources = NIL;
378  ec2->ec_derives = NIL;
379  ec2->ec_relids = NULL;
380  ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
381  ec1->ec_below_outer_join |= below_outer_join;
382  ec1->ec_min_security = Min(ec1->ec_min_security,
383  restrictinfo->security_level);
384  ec1->ec_max_security = Max(ec1->ec_max_security,
385  restrictinfo->security_level);
386  /* mark the RI as associated with this eclass */
387  restrictinfo->left_ec = ec1;
388  restrictinfo->right_ec = ec1;
389  /* mark the RI as usable with this pair of EMs */
390  restrictinfo->left_em = em1;
391  restrictinfo->right_em = em2;
392  }
393  else if (ec1)
394  {
395  /* Case 3: add item2 to ec1 */
396  em2 = add_eq_member(ec1, item2, item2_relids, item2_nullable_relids,
397  false, item2_type);
398  ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
399  ec1->ec_below_outer_join |= below_outer_join;
400  ec1->ec_min_security = Min(ec1->ec_min_security,
401  restrictinfo->security_level);
402  ec1->ec_max_security = Max(ec1->ec_max_security,
403  restrictinfo->security_level);
404  /* mark the RI as associated with this eclass */
405  restrictinfo->left_ec = ec1;
406  restrictinfo->right_ec = ec1;
407  /* mark the RI as usable with this pair of EMs */
408  restrictinfo->left_em = em1;
409  restrictinfo->right_em = em2;
410  }
411  else if (ec2)
412  {
413  /* Case 3: add item1 to ec2 */
414  em1 = add_eq_member(ec2, item1, item1_relids, item1_nullable_relids,
415  false, item1_type);
416  ec2->ec_sources = lappend(ec2->ec_sources, restrictinfo);
417  ec2->ec_below_outer_join |= below_outer_join;
418  ec2->ec_min_security = Min(ec2->ec_min_security,
419  restrictinfo->security_level);
420  ec2->ec_max_security = Max(ec2->ec_max_security,
421  restrictinfo->security_level);
422  /* mark the RI as associated with this eclass */
423  restrictinfo->left_ec = ec2;
424  restrictinfo->right_ec = ec2;
425  /* mark the RI as usable with this pair of EMs */
426  restrictinfo->left_em = em1;
427  restrictinfo->right_em = em2;
428  }
429  else
430  {
431  /* Case 4: make a new, two-entry EC */
433 
434  ec->ec_opfamilies = opfamilies;
435  ec->ec_collation = collation;
436  ec->ec_members = NIL;
437  ec->ec_sources = list_make1(restrictinfo);
438  ec->ec_derives = NIL;
439  ec->ec_relids = NULL;
440  ec->ec_has_const = false;
441  ec->ec_has_volatile = false;
442  ec->ec_below_outer_join = below_outer_join;
443  ec->ec_broken = false;
444  ec->ec_sortref = 0;
445  ec->ec_min_security = restrictinfo->security_level;
446  ec->ec_max_security = restrictinfo->security_level;
447  ec->ec_merged = NULL;
448  em1 = add_eq_member(ec, item1, item1_relids, item1_nullable_relids,
449  false, item1_type);
450  em2 = add_eq_member(ec, item2, item2_relids, item2_nullable_relids,
451  false, item2_type);
452 
453  root->eq_classes = lappend(root->eq_classes, ec);
454 
455  /* mark the RI as associated with this eclass */
456  restrictinfo->left_ec = ec;
457  restrictinfo->right_ec = ec;
458  /* mark the RI as usable with this pair of EMs */
459  restrictinfo->left_em = em1;
460  restrictinfo->right_em = em2;
461  }
462 
463  return true;
464 }
465 
466 /*
467  * canonicalize_ec_expression
468  *
469  * This function ensures that the expression exposes the expected type and
470  * collation, so that it will be equal() to other equivalence-class expressions
471  * that it ought to be equal() to.
472  *
473  * The rule for datatypes is that the exposed type should match what it would
474  * be for an input to an operator of the EC's opfamilies; which is usually
475  * the declared input type of the operator, but in the case of polymorphic
476  * operators no relabeling is wanted (compare the behavior of parse_coerce.c).
477  * Expressions coming in from quals will generally have the right type
478  * already, but expressions coming from indexkeys may not (because they are
479  * represented without any explicit relabel in pg_index), and the same problem
480  * occurs for sort expressions (because the parser is likewise cavalier about
481  * putting relabels on them). Such cases will be binary-compatible with the
482  * real operators, so adding a RelabelType is sufficient.
483  *
484  * Also, the expression's exposed collation must match the EC's collation.
485  * This is important because in comparisons like "foo < bar COLLATE baz",
486  * only one of the expressions has the correct exposed collation as we receive
487  * it from the parser. Forcing both of them to have it ensures that all
488  * variant spellings of such a construct behave the same. Again, we can
489  * stick on a RelabelType to force the right exposed collation. (It might
490  * work to not label the collation at all in EC members, but this is risky
491  * since some parts of the system expect exprCollation() to deliver the
492  * right answer for a sort key.)
493  *
494  * Note this code assumes that the expression has already been through
495  * eval_const_expressions, so there are no CollateExprs and no redundant
496  * RelabelTypes.
497  */
498 Expr *
499 canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
500 {
501  Oid expr_type = exprType((Node *) expr);
502 
503  /*
504  * For a polymorphic-input-type opclass, just keep the same exposed type.
505  * RECORD opclasses work like polymorphic-type ones for this purpose.
506  */
507  if (IsPolymorphicType(req_type) || req_type == RECORDOID)
508  req_type = expr_type;
509 
510  /*
511  * No work if the expression exposes the right type/collation already.
512  */
513  if (expr_type != req_type ||
514  exprCollation((Node *) expr) != req_collation)
515  {
516  /*
517  * Strip any existing RelabelType, then add a new one if needed. This
518  * is to preserve the invariant of no redundant RelabelTypes.
519  *
520  * If we have to change the exposed type of the stripped expression,
521  * set typmod to -1 (since the new type may not have the same typmod
522  * interpretation). If we only have to change collation, preserve the
523  * exposed typmod.
524  */
525  while (expr && IsA(expr, RelabelType))
526  expr = (Expr *) ((RelabelType *) expr)->arg;
527 
528  if (exprType((Node *) expr) != req_type)
529  expr = (Expr *) makeRelabelType(expr,
530  req_type,
531  -1,
532  req_collation,
534  else if (exprCollation((Node *) expr) != req_collation)
535  expr = (Expr *) makeRelabelType(expr,
536  req_type,
537  exprTypmod((Node *) expr),
538  req_collation,
540  }
541 
542  return expr;
543 }
544 
545 /*
546  * add_eq_member - build a new EquivalenceMember and add it to an EC
547  */
548 static EquivalenceMember *
550  Relids nullable_relids, bool is_child, Oid datatype)
551 {
553 
554  em->em_expr = expr;
555  em->em_relids = relids;
556  em->em_nullable_relids = nullable_relids;
557  em->em_is_const = false;
558  em->em_is_child = is_child;
559  em->em_datatype = datatype;
560 
561  if (bms_is_empty(relids))
562  {
563  /*
564  * No Vars, assume it's a pseudoconstant. This is correct for entries
565  * generated from process_equivalence(), because a WHERE clause can't
566  * contain aggregates or SRFs, and non-volatility was checked before
567  * process_equivalence() ever got called. But
568  * get_eclass_for_sort_expr() has to work harder. We put the tests
569  * there not here to save cycles in the equivalence case.
570  */
571  Assert(!is_child);
572  em->em_is_const = true;
573  ec->ec_has_const = true;
574  /* it can't affect ec_relids */
575  }
576  else if (!is_child) /* child members don't add to ec_relids */
577  {
578  ec->ec_relids = bms_add_members(ec->ec_relids, relids);
579  }
580  ec->ec_members = lappend(ec->ec_members, em);
581 
582  return em;
583 }
584 
585 
586 /*
587  * get_eclass_for_sort_expr
588  * Given an expression and opfamily/collation info, find an existing
589  * equivalence class it is a member of; if none, optionally build a new
590  * single-member EquivalenceClass for it.
591  *
592  * expr is the expression, and nullable_relids is the set of base relids
593  * that are potentially nullable below it. We actually only care about
594  * the set of such relids that are used in the expression; but for caller
595  * convenience, we perform that intersection step here. The caller need
596  * only be sure that nullable_relids doesn't omit any nullable rels that
597  * might appear in the expr.
598  *
599  * sortref is the SortGroupRef of the originating SortGroupClause, if any,
600  * or zero if not. (It should never be zero if the expression is volatile!)
601  *
602  * If rel is not NULL, it identifies a specific relation we're considering
603  * a path for, and indicates that child EC members for that relation can be
604  * considered. Otherwise child members are ignored. (Note: since child EC
605  * members aren't guaranteed unique, a non-NULL value means that there could
606  * be more than one EC that matches the expression; if so it's order-dependent
607  * which one you get. This is annoying but it only happens in corner cases,
608  * so for now we live with just reporting the first match. See also
609  * generate_implied_equalities_for_column and match_pathkeys_to_index.)
610  *
611  * If create_it is true, we'll build a new EquivalenceClass when there is no
612  * match. If create_it is false, we just return NULL when no match.
613  *
614  * This can be used safely both before and after EquivalenceClass merging;
615  * since it never causes merging it does not invalidate any existing ECs
616  * or PathKeys. However, ECs added after path generation has begun are
617  * of limited usefulness, so usually it's best to create them beforehand.
618  *
619  * Note: opfamilies must be chosen consistently with the way
620  * process_equivalence() would do; that is, generated from a mergejoinable
621  * equality operator. Else we might fail to detect valid equivalences,
622  * generating poor (but not incorrect) plans.
623  */
626  Expr *expr,
627  Relids nullable_relids,
628  List *opfamilies,
629  Oid opcintype,
630  Oid collation,
631  Index sortref,
632  Relids rel,
633  bool create_it)
634 {
635  Relids expr_relids;
636  EquivalenceClass *newec;
637  EquivalenceMember *newem;
638  ListCell *lc1;
639  MemoryContext oldcontext;
640 
641  /*
642  * Ensure the expression exposes the correct type and collation.
643  */
644  expr = canonicalize_ec_expression(expr, opcintype, collation);
645 
646  /*
647  * Get the precise set of nullable relids appearing in the expression.
648  */
649  expr_relids = pull_varnos((Node *) expr);
650  nullable_relids = bms_intersect(nullable_relids, expr_relids);
651 
652  /*
653  * Scan through the existing EquivalenceClasses for a match
654  */
655  foreach(lc1, root->eq_classes)
656  {
657  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
658  ListCell *lc2;
659 
660  /*
661  * Never match to a volatile EC, except when we are looking at another
662  * reference to the same volatile SortGroupClause.
663  */
664  if (cur_ec->ec_has_volatile &&
665  (sortref == 0 || sortref != cur_ec->ec_sortref))
666  continue;
667 
668  if (collation != cur_ec->ec_collation)
669  continue;
670  if (!equal(opfamilies, cur_ec->ec_opfamilies))
671  continue;
672 
673  foreach(lc2, cur_ec->ec_members)
674  {
675  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
676 
677  /*
678  * Ignore child members unless they match the request.
679  */
680  if (cur_em->em_is_child &&
681  !bms_equal(cur_em->em_relids, rel))
682  continue;
683 
684  /*
685  * If below an outer join, don't match constants: they're not as
686  * constant as they look.
687  */
688  if (cur_ec->ec_below_outer_join &&
689  cur_em->em_is_const)
690  continue;
691 
692  if (opcintype == cur_em->em_datatype &&
693  equal(expr, cur_em->em_expr))
694  return cur_ec; /* Match! */
695  }
696  }
697 
698  /* No match; does caller want a NULL result? */
699  if (!create_it)
700  return NULL;
701 
702  /*
703  * OK, build a new single-member EC
704  *
705  * Here, we must be sure that we construct the EC in the right context.
706  */
707  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
708 
709  newec = makeNode(EquivalenceClass);
710  newec->ec_opfamilies = list_copy(opfamilies);
711  newec->ec_collation = collation;
712  newec->ec_members = NIL;
713  newec->ec_sources = NIL;
714  newec->ec_derives = NIL;
715  newec->ec_relids = NULL;
716  newec->ec_has_const = false;
718  newec->ec_below_outer_join = false;
719  newec->ec_broken = false;
720  newec->ec_sortref = sortref;
721  newec->ec_min_security = UINT_MAX;
722  newec->ec_max_security = 0;
723  newec->ec_merged = NULL;
724 
725  if (newec->ec_has_volatile && sortref == 0) /* should not happen */
726  elog(ERROR, "volatile EquivalenceClass has no sortref");
727 
728  newem = add_eq_member(newec, copyObject(expr), expr_relids,
729  nullable_relids, false, opcintype);
730 
731  /*
732  * add_eq_member doesn't check for volatile functions, set-returning
733  * functions, aggregates, or window functions, but such could appear in
734  * sort expressions; so we have to check whether its const-marking was
735  * correct.
736  */
737  if (newec->ec_has_const)
738  {
739  if (newec->ec_has_volatile ||
740  expression_returns_set((Node *) expr) ||
741  contain_agg_clause((Node *) expr) ||
742  contain_window_function((Node *) expr))
743  {
744  newec->ec_has_const = false;
745  newem->em_is_const = false;
746  }
747  }
748 
749  root->eq_classes = lappend(root->eq_classes, newec);
750 
751  /*
752  * If EC merging is already complete, we have to mop up by adding the new
753  * EC to the eclass_indexes of the relation(s) mentioned in it.
754  */
755  if (root->ec_merging_done)
756  {
757  int ec_index = list_length(root->eq_classes) - 1;
758  int i = -1;
759 
760  while ((i = bms_next_member(newec->ec_relids, i)) > 0)
761  {
762  RelOptInfo *rel = root->simple_rel_array[i];
763 
764  Assert(rel->reloptkind == RELOPT_BASEREL ||
765  rel->reloptkind == RELOPT_DEADREL);
766 
768  ec_index);
769  }
770  }
771 
772  MemoryContextSwitchTo(oldcontext);
773 
774  return newec;
775 }
776 
777 /*
778  * Find an equivalence class member expression, all of whose Vars, come from
779  * the indicated relation.
780  */
781 Expr *
783 {
784  ListCell *lc_em;
785 
786  foreach(lc_em, ec->ec_members)
787  {
788  EquivalenceMember *em = lfirst(lc_em);
789 
790  if (bms_is_subset(em->em_relids, rel->relids) &&
791  !bms_is_empty(em->em_relids))
792  {
793  /*
794  * If there is more than one equivalence member whose Vars are
795  * taken entirely from this relation, we'll be content to choose
796  * any one of those.
797  */
798  return em->em_expr;
799  }
800  }
801 
802  /* We didn't find any suitable equivalence class expression */
803  return NULL;
804 }
805 
806 /*
807  * generate_base_implied_equalities
808  * Generate any restriction clauses that we can deduce from equivalence
809  * classes.
810  *
811  * When an EC contains pseudoconstants, our strategy is to generate
812  * "member = const1" clauses where const1 is the first constant member, for
813  * every other member (including other constants). If we are able to do this
814  * then we don't need any "var = var" comparisons because we've successfully
815  * constrained all the vars at their points of creation. If we fail to
816  * generate any of these clauses due to lack of cross-type operators, we fall
817  * back to the "ec_broken" strategy described below. (XXX if there are
818  * multiple constants of different types, it's possible that we might succeed
819  * in forming all the required clauses if we started from a different const
820  * member; but this seems a sufficiently hokey corner case to not be worth
821  * spending lots of cycles on.)
822  *
823  * For ECs that contain no pseudoconstants, we generate derived clauses
824  * "member1 = member2" for each pair of members belonging to the same base
825  * relation (actually, if there are more than two for the same base relation,
826  * we only need enough clauses to link each to each other). This provides
827  * the base case for the recursion: each row emitted by a base relation scan
828  * will constrain all computable members of the EC to be equal. As each
829  * join path is formed, we'll add additional derived clauses on-the-fly
830  * to maintain this invariant (see generate_join_implied_equalities).
831  *
832  * If the opfamilies used by the EC do not provide complete sets of cross-type
833  * equality operators, it is possible that we will fail to generate a clause
834  * that must be generated to maintain the invariant. (An example: given
835  * "WHERE a.x = b.y AND b.y = a.z", the scheme breaks down if we cannot
836  * generate "a.x = a.z" as a restriction clause for A.) In this case we mark
837  * the EC "ec_broken" and fall back to regurgitating its original source
838  * RestrictInfos at appropriate times. We do not try to retract any derived
839  * clauses already generated from the broken EC, so the resulting plan could
840  * be poor due to bad selectivity estimates caused by redundant clauses. But
841  * the correct solution to that is to fix the opfamilies ...
842  *
843  * Equality clauses derived by this function are passed off to
844  * process_implied_equality (in plan/initsplan.c) to be inserted into the
845  * restrictinfo datastructures. Note that this must be called after initial
846  * scanning of the quals and before Path construction begins.
847  *
848  * We make no attempt to avoid generating duplicate RestrictInfos here: we
849  * don't search ec_sources for matches, nor put the created RestrictInfos
850  * into ec_derives. Doing so would require some slightly ugly changes in
851  * initsplan.c's API, and there's no real advantage, because the clauses
852  * generated here can't duplicate anything we will generate for joins anyway.
853  */
854 void
856 {
857  int ec_index;
858  ListCell *lc;
859 
860  /*
861  * At this point, we're done absorbing knowledge of equivalences in the
862  * query, so no further EC merging should happen, and ECs remaining in the
863  * eq_classes list can be considered canonical. (But note that it's still
864  * possible for new single-member ECs to be added through
865  * get_eclass_for_sort_expr().)
866  */
867  root->ec_merging_done = true;
868 
869  ec_index = 0;
870  foreach(lc, root->eq_classes)
871  {
873  bool can_generate_joinclause = false;
874  int i;
875 
876  Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
877  Assert(!ec->ec_broken); /* not yet anyway... */
878 
879  /*
880  * Generate implied equalities that are restriction clauses.
881  * Single-member ECs won't generate any deductions, either here or at
882  * the join level.
883  */
884  if (list_length(ec->ec_members) > 1)
885  {
886  if (ec->ec_has_const)
888  else
890 
891  /* Recover if we failed to generate required derived clauses */
892  if (ec->ec_broken)
894 
895  /* Detect whether this EC might generate join clauses */
896  can_generate_joinclause =
898  }
899 
900  /*
901  * Mark the base rels cited in each eclass (which should all exist by
902  * now) with the eq_classes indexes of all eclasses mentioning them.
903  * This will let us avoid searching in subsequent lookups. While
904  * we're at it, we can mark base rels that have pending eclass joins;
905  * this is a cheap version of has_relevant_eclass_joinclause().
906  */
907  i = -1;
908  while ((i = bms_next_member(ec->ec_relids, i)) > 0)
909  {
910  RelOptInfo *rel = root->simple_rel_array[i];
911 
913 
915  ec_index);
916 
917  if (can_generate_joinclause)
918  rel->has_eclass_joins = true;
919  }
920 
921  ec_index++;
922  }
923 }
924 
925 /*
926  * generate_base_implied_equalities when EC contains pseudoconstant(s)
927  */
928 static void
930  EquivalenceClass *ec)
931 {
932  EquivalenceMember *const_em = NULL;
933  ListCell *lc;
934 
935  /*
936  * In the trivial case where we just had one "var = const" clause, push
937  * the original clause back into the main planner machinery. There is
938  * nothing to be gained by doing it differently, and we save the effort to
939  * re-build and re-analyze an equality clause that will be exactly
940  * equivalent to the old one.
941  */
942  if (list_length(ec->ec_members) == 2 &&
943  list_length(ec->ec_sources) == 1)
944  {
945  RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
946 
947  if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
948  {
949  distribute_restrictinfo_to_rels(root, restrictinfo);
950  return;
951  }
952  }
953 
954  /*
955  * Find the constant member to use. We prefer an actual constant to
956  * pseudo-constants (such as Params), because the constraint exclusion
957  * machinery might be able to exclude relations on the basis of generated
958  * "var = const" equalities, but "var = param" won't work for that.
959  */
960  foreach(lc, ec->ec_members)
961  {
962  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
963 
964  if (cur_em->em_is_const)
965  {
966  const_em = cur_em;
967  if (IsA(cur_em->em_expr, Const))
968  break;
969  }
970  }
971  Assert(const_em != NULL);
972 
973  /* Generate a derived equality against each other member */
974  foreach(lc, ec->ec_members)
975  {
976  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
977  Oid eq_op;
978 
979  Assert(!cur_em->em_is_child); /* no children yet */
980  if (cur_em == const_em)
981  continue;
982  eq_op = select_equality_operator(ec,
983  cur_em->em_datatype,
984  const_em->em_datatype);
985  if (!OidIsValid(eq_op))
986  {
987  /* failed... */
988  ec->ec_broken = true;
989  break;
990  }
991  process_implied_equality(root, eq_op, ec->ec_collation,
992  cur_em->em_expr, const_em->em_expr,
993  bms_copy(ec->ec_relids),
995  const_em->em_nullable_relids),
996  ec->ec_min_security,
998  cur_em->em_is_const);
999  }
1000 }
1001 
1002 /*
1003  * generate_base_implied_equalities when EC contains no pseudoconstants
1004  */
1005 static void
1007  EquivalenceClass *ec)
1008 {
1009  EquivalenceMember **prev_ems;
1010  ListCell *lc;
1011 
1012  /*
1013  * We scan the EC members once and track the last-seen member for each
1014  * base relation. When we see another member of the same base relation,
1015  * we generate "prev_em = cur_em". This results in the minimum number of
1016  * derived clauses, but it's possible that it will fail when a different
1017  * ordering would succeed. XXX FIXME: use a UNION-FIND algorithm similar
1018  * to the way we build merged ECs. (Use a list-of-lists for each rel.)
1019  */
1020  prev_ems = (EquivalenceMember **)
1021  palloc0(root->simple_rel_array_size * sizeof(EquivalenceMember *));
1022 
1023  foreach(lc, ec->ec_members)
1024  {
1025  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1026  int relid;
1027 
1028  Assert(!cur_em->em_is_child); /* no children yet */
1029  if (!bms_get_singleton_member(cur_em->em_relids, &relid))
1030  continue;
1031  Assert(relid < root->simple_rel_array_size);
1032 
1033  if (prev_ems[relid] != NULL)
1034  {
1035  EquivalenceMember *prev_em = prev_ems[relid];
1036  Oid eq_op;
1037 
1038  eq_op = select_equality_operator(ec,
1039  prev_em->em_datatype,
1040  cur_em->em_datatype);
1041  if (!OidIsValid(eq_op))
1042  {
1043  /* failed... */
1044  ec->ec_broken = true;
1045  break;
1046  }
1047  process_implied_equality(root, eq_op, ec->ec_collation,
1048  prev_em->em_expr, cur_em->em_expr,
1049  bms_copy(ec->ec_relids),
1050  bms_union(prev_em->em_nullable_relids,
1051  cur_em->em_nullable_relids),
1052  ec->ec_min_security,
1053  ec->ec_below_outer_join,
1054  false);
1055  }
1056  prev_ems[relid] = cur_em;
1057  }
1058 
1059  pfree(prev_ems);
1060 
1061  /*
1062  * We also have to make sure that all the Vars used in the member clauses
1063  * will be available at any join node we might try to reference them at.
1064  * For the moment we force all the Vars to be available at all join nodes
1065  * for this eclass. Perhaps this could be improved by doing some
1066  * pre-analysis of which members we prefer to join, but it's no worse than
1067  * what happened in the pre-8.3 code.
1068  */
1069  foreach(lc, ec->ec_members)
1070  {
1071  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1072  List *vars = pull_var_clause((Node *) cur_em->em_expr,
1076 
1077  add_vars_to_targetlist(root, vars, ec->ec_relids, false);
1078  list_free(vars);
1079  }
1080 }
1081 
1082 /*
1083  * generate_base_implied_equalities cleanup after failure
1084  *
1085  * What we must do here is push any zero- or one-relation source RestrictInfos
1086  * of the EC back into the main restrictinfo datastructures. Multi-relation
1087  * clauses will be regurgitated later by generate_join_implied_equalities().
1088  * (We do it this way to maintain continuity with the case that ec_broken
1089  * becomes set only after we've gone up a join level or two.) However, for
1090  * an EC that contains constants, we can adopt a simpler strategy and just
1091  * throw back all the source RestrictInfos immediately; that works because
1092  * we know that such an EC can't become broken later. (This rule justifies
1093  * ignoring ec_has_const ECs in generate_join_implied_equalities, even when
1094  * they are broken.)
1095  */
1096 static void
1098  EquivalenceClass *ec)
1099 {
1100  ListCell *lc;
1101 
1102  foreach(lc, ec->ec_sources)
1103  {
1104  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1105 
1106  if (ec->ec_has_const ||
1107  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1108  distribute_restrictinfo_to_rels(root, restrictinfo);
1109  }
1110 }
1111 
1112 
1113 /*
1114  * generate_join_implied_equalities
1115  * Generate any join clauses that we can deduce from equivalence classes.
1116  *
1117  * At a join node, we must enforce restriction clauses sufficient to ensure
1118  * that all equivalence-class members computable at that node are equal.
1119  * Since the set of clauses to enforce can vary depending on which subset
1120  * relations are the inputs, we have to compute this afresh for each join
1121  * relation pair. Hence a fresh List of RestrictInfo nodes is built and
1122  * passed back on each call.
1123  *
1124  * In addition to its use at join nodes, this can be applied to generate
1125  * eclass-based join clauses for use in a parameterized scan of a base rel.
1126  * The reason for the asymmetry of specifying the inner rel as a RelOptInfo
1127  * and the outer rel by Relids is that this usage occurs before we have
1128  * built any join RelOptInfos.
1129  *
1130  * An annoying special case for parameterized scans is that the inner rel can
1131  * be an appendrel child (an "other rel"). In this case we must generate
1132  * appropriate clauses using child EC members. add_child_rel_equivalences
1133  * must already have been done for the child rel.
1134  *
1135  * The results are sufficient for use in merge, hash, and plain nestloop join
1136  * methods. We do not worry here about selecting clauses that are optimal
1137  * for use in a parameterized indexscan. indxpath.c makes its own selections
1138  * of clauses to use, and if the ones we pick here are redundant with those,
1139  * the extras will be eliminated at createplan time, using the parent_ec
1140  * markers that we provide (see is_redundant_derived_clause()).
1141  *
1142  * Because the same join clauses are likely to be needed multiple times as
1143  * we consider different join paths, we avoid generating multiple copies:
1144  * whenever we select a particular pair of EquivalenceMembers to join,
1145  * we check to see if the pair matches any original clause (in ec_sources)
1146  * or previously-built clause (in ec_derives). This saves memory and allows
1147  * re-use of information cached in RestrictInfos.
1148  *
1149  * join_relids should always equal bms_union(outer_relids, inner_rel->relids).
1150  * We could simplify this function's API by computing it internally, but in
1151  * most current uses, the caller has the value at hand anyway.
1152  */
1153 List *
1155  Relids join_relids,
1156  Relids outer_relids,
1157  RelOptInfo *inner_rel)
1158 {
1159  List *result = NIL;
1160  Relids inner_relids = inner_rel->relids;
1161  Relids nominal_inner_relids;
1162  Relids nominal_join_relids;
1163  Bitmapset *matching_ecs;
1164  int i;
1165 
1166  /* If inner rel is a child, extra setup work is needed */
1167  if (IS_OTHER_REL(inner_rel))
1168  {
1169  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1170 
1171  /* Fetch relid set for the topmost parent rel */
1172  nominal_inner_relids = inner_rel->top_parent_relids;
1173  /* ECs will be marked with the parent's relid, not the child's */
1174  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1175  }
1176  else
1177  {
1178  nominal_inner_relids = inner_relids;
1179  nominal_join_relids = join_relids;
1180  }
1181 
1182  /*
1183  * Get all eclasses in common between inner_rel's relids and outer_relids
1184  */
1185  matching_ecs = get_common_eclass_indexes(root, inner_rel->relids,
1186  outer_relids);
1187 
1188  i = -1;
1189  while ((i = bms_next_member(matching_ecs, i)) >= 0)
1190  {
1192  List *sublist = NIL;
1193 
1194  /* ECs containing consts do not need any further enforcement */
1195  if (ec->ec_has_const)
1196  continue;
1197 
1198  /* Single-member ECs won't generate any deductions */
1199  if (list_length(ec->ec_members) <= 1)
1200  continue;
1201 
1202  /* Sanity check that this eclass overlaps the join */
1203  Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
1204 
1205  if (!ec->ec_broken)
1207  ec,
1208  join_relids,
1209  outer_relids,
1210  inner_relids);
1211 
1212  /* Recover if we failed to generate required derived clauses */
1213  if (ec->ec_broken)
1215  ec,
1216  nominal_join_relids,
1217  outer_relids,
1218  nominal_inner_relids,
1219  inner_rel);
1220 
1221  result = list_concat(result, sublist);
1222  }
1223 
1224  return result;
1225 }
1226 
1227 /*
1228  * generate_join_implied_equalities_for_ecs
1229  * As above, but consider only the listed ECs.
1230  */
1231 List *
1233  List *eclasses,
1234  Relids join_relids,
1235  Relids outer_relids,
1236  RelOptInfo *inner_rel)
1237 {
1238  List *result = NIL;
1239  Relids inner_relids = inner_rel->relids;
1240  Relids nominal_inner_relids;
1241  Relids nominal_join_relids;
1242  ListCell *lc;
1243 
1244  /* If inner rel is a child, extra setup work is needed */
1245  if (IS_OTHER_REL(inner_rel))
1246  {
1247  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1248 
1249  /* Fetch relid set for the topmost parent rel */
1250  nominal_inner_relids = inner_rel->top_parent_relids;
1251  /* ECs will be marked with the parent's relid, not the child's */
1252  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1253  }
1254  else
1255  {
1256  nominal_inner_relids = inner_relids;
1257  nominal_join_relids = join_relids;
1258  }
1259 
1260  foreach(lc, eclasses)
1261  {
1263  List *sublist = NIL;
1264 
1265  /* ECs containing consts do not need any further enforcement */
1266  if (ec->ec_has_const)
1267  continue;
1268 
1269  /* Single-member ECs won't generate any deductions */
1270  if (list_length(ec->ec_members) <= 1)
1271  continue;
1272 
1273  /* We can quickly ignore any that don't overlap the join, too */
1274  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1275  continue;
1276 
1277  if (!ec->ec_broken)
1279  ec,
1280  join_relids,
1281  outer_relids,
1282  inner_relids);
1283 
1284  /* Recover if we failed to generate required derived clauses */
1285  if (ec->ec_broken)
1287  ec,
1288  nominal_join_relids,
1289  outer_relids,
1290  nominal_inner_relids,
1291  inner_rel);
1292 
1293  result = list_concat(result, sublist);
1294  }
1295 
1296  return result;
1297 }
1298 
1299 /*
1300  * generate_join_implied_equalities for a still-valid EC
1301  */
1302 static List *
1304  EquivalenceClass *ec,
1305  Relids join_relids,
1306  Relids outer_relids,
1307  Relids inner_relids)
1308 {
1309  List *result = NIL;
1310  List *new_members = NIL;
1311  List *outer_members = NIL;
1312  List *inner_members = NIL;
1313  ListCell *lc1;
1314 
1315  /*
1316  * First, scan the EC to identify member values that are computable at the
1317  * outer rel, at the inner rel, or at this relation but not in either
1318  * input rel. The outer-rel members should already be enforced equal,
1319  * likewise for the inner-rel members. We'll need to create clauses to
1320  * enforce that any newly computable members are all equal to each other
1321  * as well as to at least one input member, plus enforce at least one
1322  * outer-rel member equal to at least one inner-rel member.
1323  */
1324  foreach(lc1, ec->ec_members)
1325  {
1326  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1327 
1328  /*
1329  * We don't need to check explicitly for child EC members. This test
1330  * against join_relids will cause them to be ignored except when
1331  * considering a child inner rel, which is what we want.
1332  */
1333  if (!bms_is_subset(cur_em->em_relids, join_relids))
1334  continue; /* not computable yet, or wrong child */
1335 
1336  if (bms_is_subset(cur_em->em_relids, outer_relids))
1337  outer_members = lappend(outer_members, cur_em);
1338  else if (bms_is_subset(cur_em->em_relids, inner_relids))
1339  inner_members = lappend(inner_members, cur_em);
1340  else
1341  new_members = lappend(new_members, cur_em);
1342  }
1343 
1344  /*
1345  * First, select the joinclause if needed. We can equate any one outer
1346  * member to any one inner member, but we have to find a datatype
1347  * combination for which an opfamily member operator exists. If we have
1348  * choices, we prefer simple Var members (possibly with RelabelType) since
1349  * these are (a) cheapest to compute at runtime and (b) most likely to
1350  * have useful statistics. Also, prefer operators that are also
1351  * hashjoinable.
1352  */
1353  if (outer_members && inner_members)
1354  {
1355  EquivalenceMember *best_outer_em = NULL;
1356  EquivalenceMember *best_inner_em = NULL;
1357  Oid best_eq_op = InvalidOid;
1358  int best_score = -1;
1359  RestrictInfo *rinfo;
1360 
1361  foreach(lc1, outer_members)
1362  {
1363  EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
1364  ListCell *lc2;
1365 
1366  foreach(lc2, inner_members)
1367  {
1368  EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
1369  Oid eq_op;
1370  int score;
1371 
1372  eq_op = select_equality_operator(ec,
1373  outer_em->em_datatype,
1374  inner_em->em_datatype);
1375  if (!OidIsValid(eq_op))
1376  continue;
1377  score = 0;
1378  if (IsA(outer_em->em_expr, Var) ||
1379  (IsA(outer_em->em_expr, RelabelType) &&
1380  IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
1381  score++;
1382  if (IsA(inner_em->em_expr, Var) ||
1383  (IsA(inner_em->em_expr, RelabelType) &&
1384  IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
1385  score++;
1386  if (op_hashjoinable(eq_op,
1387  exprType((Node *) outer_em->em_expr)))
1388  score++;
1389  if (score > best_score)
1390  {
1391  best_outer_em = outer_em;
1392  best_inner_em = inner_em;
1393  best_eq_op = eq_op;
1394  best_score = score;
1395  if (best_score == 3)
1396  break; /* no need to look further */
1397  }
1398  }
1399  if (best_score == 3)
1400  break; /* no need to look further */
1401  }
1402  if (best_score < 0)
1403  {
1404  /* failed... */
1405  ec->ec_broken = true;
1406  return NIL;
1407  }
1408 
1409  /*
1410  * Create clause, setting parent_ec to mark it as redundant with other
1411  * joinclauses
1412  */
1413  rinfo = create_join_clause(root, ec, best_eq_op,
1414  best_outer_em, best_inner_em,
1415  ec);
1416 
1417  result = lappend(result, rinfo);
1418  }
1419 
1420  /*
1421  * Now deal with building restrictions for any expressions that involve
1422  * Vars from both sides of the join. We have to equate all of these to
1423  * each other as well as to at least one old member (if any).
1424  *
1425  * XXX as in generate_base_implied_equalities_no_const, we could be a lot
1426  * smarter here to avoid unnecessary failures in cross-type situations.
1427  * For now, use the same left-to-right method used there.
1428  */
1429  if (new_members)
1430  {
1431  List *old_members = list_concat(outer_members, inner_members);
1432  EquivalenceMember *prev_em = NULL;
1433  RestrictInfo *rinfo;
1434 
1435  /* For now, arbitrarily take the first old_member as the one to use */
1436  if (old_members)
1437  new_members = lappend(new_members, linitial(old_members));
1438 
1439  foreach(lc1, new_members)
1440  {
1441  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1442 
1443  if (prev_em != NULL)
1444  {
1445  Oid eq_op;
1446 
1447  eq_op = select_equality_operator(ec,
1448  prev_em->em_datatype,
1449  cur_em->em_datatype);
1450  if (!OidIsValid(eq_op))
1451  {
1452  /* failed... */
1453  ec->ec_broken = true;
1454  return NIL;
1455  }
1456  /* do NOT set parent_ec, this qual is not redundant! */
1457  rinfo = create_join_clause(root, ec, eq_op,
1458  prev_em, cur_em,
1459  NULL);
1460 
1461  result = lappend(result, rinfo);
1462  }
1463  prev_em = cur_em;
1464  }
1465  }
1466 
1467  return result;
1468 }
1469 
1470 /*
1471  * generate_join_implied_equalities cleanup after failure
1472  *
1473  * Return any original RestrictInfos that are enforceable at this join.
1474  *
1475  * In the case of a child inner relation, we have to translate the
1476  * original RestrictInfos from parent to child Vars.
1477  */
1478 static List *
1480  EquivalenceClass *ec,
1481  Relids nominal_join_relids,
1482  Relids outer_relids,
1483  Relids nominal_inner_relids,
1484  RelOptInfo *inner_rel)
1485 {
1486  List *result = NIL;
1487  ListCell *lc;
1488 
1489  foreach(lc, ec->ec_sources)
1490  {
1491  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1492  Relids clause_relids = restrictinfo->required_relids;
1493 
1494  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1495  !bms_is_subset(clause_relids, outer_relids) &&
1496  !bms_is_subset(clause_relids, nominal_inner_relids))
1497  result = lappend(result, restrictinfo);
1498  }
1499 
1500  /*
1501  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1502  * to all the RestrictInfos at once. This will result in returning
1503  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1504  * any duplication, and it's a sufficiently narrow corner case that we
1505  * shouldn't sweat too much over it anyway.
1506  *
1507  * Since inner_rel might be an indirect descendant of the baserel
1508  * mentioned in the ec_sources clauses, we have to be prepared to apply
1509  * multiple levels of Var translation.
1510  */
1511  if (IS_OTHER_REL(inner_rel) && result != NIL)
1512  result = (List *) adjust_appendrel_attrs_multilevel(root,
1513  (Node *) result,
1514  inner_rel->relids,
1515  inner_rel->top_parent_relids);
1516 
1517  return result;
1518 }
1519 
1520 
1521 /*
1522  * select_equality_operator
1523  * Select a suitable equality operator for comparing two EC members
1524  *
1525  * Returns InvalidOid if no operator can be found for this datatype combination
1526  */
1527 static Oid
1529 {
1530  ListCell *lc;
1531 
1532  foreach(lc, ec->ec_opfamilies)
1533  {
1534  Oid opfamily = lfirst_oid(lc);
1535  Oid opno;
1536 
1537  opno = get_opfamily_member(opfamily, lefttype, righttype,
1539  if (!OidIsValid(opno))
1540  continue;
1541  /* If no barrier quals in query, don't worry about leaky operators */
1542  if (ec->ec_max_security == 0)
1543  return opno;
1544  /* Otherwise, insist that selected operators be leakproof */
1545  if (get_func_leakproof(get_opcode(opno)))
1546  return opno;
1547  }
1548  return InvalidOid;
1549 }
1550 
1551 
1552 /*
1553  * create_join_clause
1554  * Find or make a RestrictInfo comparing the two given EC members
1555  * with the given operator.
1556  *
1557  * parent_ec is either equal to ec (if the clause is a potentially-redundant
1558  * join clause) or NULL (if not). We have to treat this as part of the
1559  * match requirements --- it's possible that a clause comparing the same two
1560  * EMs is a join clause in one join path and a restriction clause in another.
1561  */
1562 static RestrictInfo *
1564  EquivalenceClass *ec, Oid opno,
1565  EquivalenceMember *leftem,
1566  EquivalenceMember *rightem,
1567  EquivalenceClass *parent_ec)
1568 {
1569  RestrictInfo *rinfo;
1570  ListCell *lc;
1571  MemoryContext oldcontext;
1572 
1573  /*
1574  * Search to see if we already built a RestrictInfo for this pair of
1575  * EquivalenceMembers. We can use either original source clauses or
1576  * previously-derived clauses. The check on opno is probably redundant,
1577  * but be safe ...
1578  */
1579  foreach(lc, ec->ec_sources)
1580  {
1581  rinfo = (RestrictInfo *) lfirst(lc);
1582  if (rinfo->left_em == leftem &&
1583  rinfo->right_em == rightem &&
1584  rinfo->parent_ec == parent_ec &&
1585  opno == ((OpExpr *) rinfo->clause)->opno)
1586  return rinfo;
1587  }
1588 
1589  foreach(lc, ec->ec_derives)
1590  {
1591  rinfo = (RestrictInfo *) lfirst(lc);
1592  if (rinfo->left_em == leftem &&
1593  rinfo->right_em == rightem &&
1594  rinfo->parent_ec == parent_ec &&
1595  opno == ((OpExpr *) rinfo->clause)->opno)
1596  return rinfo;
1597  }
1598 
1599  /*
1600  * Not there, so build it, in planner context so we can re-use it. (Not
1601  * important in normal planning, but definitely so in GEQO.)
1602  */
1603  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1604 
1605  rinfo = build_implied_join_equality(opno,
1606  ec->ec_collation,
1607  leftem->em_expr,
1608  rightem->em_expr,
1609  bms_union(leftem->em_relids,
1610  rightem->em_relids),
1611  bms_union(leftem->em_nullable_relids,
1612  rightem->em_nullable_relids),
1613  ec->ec_min_security);
1614 
1615  /* Mark the clause as redundant, or not */
1616  rinfo->parent_ec = parent_ec;
1617 
1618  /*
1619  * We know the correct values for left_ec/right_ec, ie this particular EC,
1620  * so we can just set them directly instead of forcing another lookup.
1621  */
1622  rinfo->left_ec = ec;
1623  rinfo->right_ec = ec;
1624 
1625  /* Mark it as usable with these EMs */
1626  rinfo->left_em = leftem;
1627  rinfo->right_em = rightem;
1628  /* and save it for possible re-use */
1629  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1630 
1631  MemoryContextSwitchTo(oldcontext);
1632 
1633  return rinfo;
1634 }
1635 
1636 
1637 /*
1638  * reconsider_outer_join_clauses
1639  * Re-examine any outer-join clauses that were set aside by
1640  * distribute_qual_to_rels(), and see if we can derive any
1641  * EquivalenceClasses from them. Then, if they were not made
1642  * redundant, push them out into the regular join-clause lists.
1643  *
1644  * When we have mergejoinable clauses A = B that are outer-join clauses,
1645  * we can't blindly combine them with other clauses A = C to deduce B = C,
1646  * since in fact the "equality" A = B won't necessarily hold above the
1647  * outer join (one of the variables might be NULL instead). Nonetheless
1648  * there are cases where we can add qual clauses using transitivity.
1649  *
1650  * One case that we look for here is an outer-join clause OUTERVAR = INNERVAR
1651  * for which there is also an equivalence clause OUTERVAR = CONSTANT.
1652  * It is safe and useful to push a clause INNERVAR = CONSTANT into the
1653  * evaluation of the inner (nullable) relation, because any inner rows not
1654  * meeting this condition will not contribute to the outer-join result anyway.
1655  * (Any outer rows they could join to will be eliminated by the pushed-down
1656  * equivalence clause.)
1657  *
1658  * Note that the above rule does not work for full outer joins; nor is it
1659  * very interesting to consider cases where the generated equivalence clause
1660  * would involve relations outside the outer join, since such clauses couldn't
1661  * be pushed into the inner side's scan anyway. So the restriction to
1662  * outervar = pseudoconstant is not really giving up anything.
1663  *
1664  * For full-join cases, we can only do something useful if it's a FULL JOIN
1665  * USING and a merged column has an equivalence MERGEDVAR = CONSTANT.
1666  * By the time it gets here, the merged column will look like
1667  * COALESCE(LEFTVAR, RIGHTVAR)
1668  * and we will have a full-join clause LEFTVAR = RIGHTVAR that we can match
1669  * the COALESCE expression to. In this situation we can push LEFTVAR = CONSTANT
1670  * and RIGHTVAR = CONSTANT into the input relations, since any rows not
1671  * meeting these conditions cannot contribute to the join result.
1672  *
1673  * Again, there isn't any traction to be gained by trying to deal with
1674  * clauses comparing a mergedvar to a non-pseudoconstant. So we can make
1675  * use of the EquivalenceClasses to search for matching variables that were
1676  * equivalenced to constants. The interesting outer-join clauses were
1677  * accumulated for us by distribute_qual_to_rels.
1678  *
1679  * When we find one of these cases, we implement the changes we want by
1680  * generating a new equivalence clause INNERVAR = CONSTANT (or LEFTVAR, etc)
1681  * and pushing it into the EquivalenceClass structures. This is because we
1682  * may already know that INNERVAR is equivalenced to some other var(s), and
1683  * we'd like the constant to propagate to them too. Note that it would be
1684  * unsafe to merge any existing EC for INNERVAR with the OUTERVAR's EC ---
1685  * that could result in propagating constant restrictions from
1686  * INNERVAR to OUTERVAR, which would be very wrong.
1687  *
1688  * It's possible that the INNERVAR is also an OUTERVAR for some other
1689  * outer-join clause, in which case the process can be repeated. So we repeat
1690  * looping over the lists of clauses until no further deductions can be made.
1691  * Whenever we do make a deduction, we remove the generating clause from the
1692  * lists, since we don't want to make the same deduction twice.
1693  *
1694  * If we don't find any match for a set-aside outer join clause, we must
1695  * throw it back into the regular joinclause processing by passing it to
1696  * distribute_restrictinfo_to_rels(). If we do generate a derived clause,
1697  * however, the outer-join clause is redundant. We still throw it back,
1698  * because otherwise the join will be seen as a clauseless join and avoided
1699  * during join order searching; but we mark it as redundant to keep from
1700  * messing up the joinrel's size estimate. (This behavior means that the
1701  * API for this routine is uselessly complex: we could have just put all
1702  * the clauses into the regular processing initially. We keep it because
1703  * someday we might want to do something else, such as inserting "dummy"
1704  * joinclauses instead of real ones.)
1705  *
1706  * Outer join clauses that are marked outerjoin_delayed are special: this
1707  * condition means that one or both VARs might go to null due to a lower
1708  * outer join. We can still push a constant through the clause, but only
1709  * if its operator is strict; and we *have to* throw the clause back into
1710  * regular joinclause processing. By keeping the strict join clause,
1711  * we ensure that any null-extended rows that are mistakenly generated due
1712  * to suppressing rows not matching the constant will be rejected at the
1713  * upper outer join. (This doesn't work for full-join clauses.)
1714  */
1715 void
1717 {
1718  bool found;
1719  ListCell *cell;
1720 
1721  /* Outer loop repeats until we find no more deductions */
1722  do
1723  {
1724  found = false;
1725 
1726  /* Process the LEFT JOIN clauses */
1727  foreach(cell, root->left_join_clauses)
1728  {
1729  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1730 
1731  if (reconsider_outer_join_clause(root, rinfo, true))
1732  {
1733  found = true;
1734  /* remove it from the list */
1735  root->left_join_clauses =
1737  /* we throw it back anyway (see notes above) */
1738  /* but the thrown-back clause has no extra selectivity */
1739  rinfo->norm_selec = 2.0;
1740  rinfo->outer_selec = 1.0;
1741  distribute_restrictinfo_to_rels(root, rinfo);
1742  }
1743  }
1744 
1745  /* Process the RIGHT JOIN clauses */
1746  foreach(cell, root->right_join_clauses)
1747  {
1748  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1749 
1750  if (reconsider_outer_join_clause(root, rinfo, false))
1751  {
1752  found = true;
1753  /* remove it from the list */
1754  root->right_join_clauses =
1756  /* we throw it back anyway (see notes above) */
1757  /* but the thrown-back clause has no extra selectivity */
1758  rinfo->norm_selec = 2.0;
1759  rinfo->outer_selec = 1.0;
1760  distribute_restrictinfo_to_rels(root, rinfo);
1761  }
1762  }
1763 
1764  /* Process the FULL JOIN clauses */
1765  foreach(cell, root->full_join_clauses)
1766  {
1767  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1768 
1769  if (reconsider_full_join_clause(root, rinfo))
1770  {
1771  found = true;
1772  /* remove it from the list */
1773  root->full_join_clauses =
1775  /* we throw it back anyway (see notes above) */
1776  /* but the thrown-back clause has no extra selectivity */
1777  rinfo->norm_selec = 2.0;
1778  rinfo->outer_selec = 1.0;
1779  distribute_restrictinfo_to_rels(root, rinfo);
1780  }
1781  }
1782  } while (found);
1783 
1784  /* Now, any remaining clauses have to be thrown back */
1785  foreach(cell, root->left_join_clauses)
1786  {
1787  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1788 
1789  distribute_restrictinfo_to_rels(root, rinfo);
1790  }
1791  foreach(cell, root->right_join_clauses)
1792  {
1793  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1794 
1795  distribute_restrictinfo_to_rels(root, rinfo);
1796  }
1797  foreach(cell, root->full_join_clauses)
1798  {
1799  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1800 
1801  distribute_restrictinfo_to_rels(root, rinfo);
1802  }
1803 }
1804 
1805 /*
1806  * reconsider_outer_join_clauses for a single LEFT/RIGHT JOIN clause
1807  *
1808  * Returns true if we were able to propagate a constant through the clause.
1809  */
1810 static bool
1812  bool outer_on_left)
1813 {
1814  Expr *outervar,
1815  *innervar;
1816  Oid opno,
1817  collation,
1818  left_type,
1819  right_type,
1820  inner_datatype;
1821  Relids inner_relids,
1822  inner_nullable_relids;
1823  ListCell *lc1;
1824 
1825  Assert(is_opclause(rinfo->clause));
1826  opno = ((OpExpr *) rinfo->clause)->opno;
1827  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1828 
1829  /* If clause is outerjoin_delayed, operator must be strict */
1830  if (rinfo->outerjoin_delayed && !op_strict(opno))
1831  return false;
1832 
1833  /* Extract needed info from the clause */
1834  op_input_types(opno, &left_type, &right_type);
1835  if (outer_on_left)
1836  {
1837  outervar = (Expr *) get_leftop(rinfo->clause);
1838  innervar = (Expr *) get_rightop(rinfo->clause);
1839  inner_datatype = right_type;
1840  inner_relids = rinfo->right_relids;
1841  }
1842  else
1843  {
1844  outervar = (Expr *) get_rightop(rinfo->clause);
1845  innervar = (Expr *) get_leftop(rinfo->clause);
1846  inner_datatype = left_type;
1847  inner_relids = rinfo->left_relids;
1848  }
1849  inner_nullable_relids = bms_intersect(inner_relids,
1850  rinfo->nullable_relids);
1851 
1852  /* Scan EquivalenceClasses for a match to outervar */
1853  foreach(lc1, root->eq_classes)
1854  {
1855  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1856  bool match;
1857  ListCell *lc2;
1858 
1859  /* Ignore EC unless it contains pseudoconstants */
1860  if (!cur_ec->ec_has_const)
1861  continue;
1862  /* Never match to a volatile EC */
1863  if (cur_ec->ec_has_volatile)
1864  continue;
1865  /* It has to match the outer-join clause as to semantics, too */
1866  if (collation != cur_ec->ec_collation)
1867  continue;
1868  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1869  continue;
1870  /* Does it contain a match to outervar? */
1871  match = false;
1872  foreach(lc2, cur_ec->ec_members)
1873  {
1874  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1875 
1876  Assert(!cur_em->em_is_child); /* no children yet */
1877  if (equal(outervar, cur_em->em_expr))
1878  {
1879  match = true;
1880  break;
1881  }
1882  }
1883  if (!match)
1884  continue; /* no match, so ignore this EC */
1885 
1886  /*
1887  * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
1888  * CONSTANT in the EC. Note that we must succeed with at least one
1889  * constant before we can decide to throw away the outer-join clause.
1890  */
1891  match = false;
1892  foreach(lc2, cur_ec->ec_members)
1893  {
1894  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1895  Oid eq_op;
1896  RestrictInfo *newrinfo;
1897 
1898  if (!cur_em->em_is_const)
1899  continue; /* ignore non-const members */
1900  eq_op = select_equality_operator(cur_ec,
1901  inner_datatype,
1902  cur_em->em_datatype);
1903  if (!OidIsValid(eq_op))
1904  continue; /* can't generate equality */
1905  newrinfo = build_implied_join_equality(eq_op,
1906  cur_ec->ec_collation,
1907  innervar,
1908  cur_em->em_expr,
1909  bms_copy(inner_relids),
1910  bms_copy(inner_nullable_relids),
1911  cur_ec->ec_min_security);
1912  if (process_equivalence(root, &newrinfo, true))
1913  match = true;
1914  }
1915 
1916  /*
1917  * If we were able to equate INNERVAR to any constant, report success.
1918  * Otherwise, fall out of the search loop, since we know the OUTERVAR
1919  * appears in at most one EC.
1920  */
1921  if (match)
1922  return true;
1923  else
1924  break;
1925  }
1926 
1927  return false; /* failed to make any deduction */
1928 }
1929 
1930 /*
1931  * reconsider_outer_join_clauses for a single FULL JOIN clause
1932  *
1933  * Returns true if we were able to propagate a constant through the clause.
1934  */
1935 static bool
1937 {
1938  Expr *leftvar;
1939  Expr *rightvar;
1940  Oid opno,
1941  collation,
1942  left_type,
1943  right_type;
1944  Relids left_relids,
1945  right_relids,
1946  left_nullable_relids,
1947  right_nullable_relids;
1948  ListCell *lc1;
1949 
1950  /* Can't use an outerjoin_delayed clause here */
1951  if (rinfo->outerjoin_delayed)
1952  return false;
1953 
1954  /* Extract needed info from the clause */
1955  Assert(is_opclause(rinfo->clause));
1956  opno = ((OpExpr *) rinfo->clause)->opno;
1957  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1958  op_input_types(opno, &left_type, &right_type);
1959  leftvar = (Expr *) get_leftop(rinfo->clause);
1960  rightvar = (Expr *) get_rightop(rinfo->clause);
1961  left_relids = rinfo->left_relids;
1962  right_relids = rinfo->right_relids;
1963  left_nullable_relids = bms_intersect(left_relids,
1964  rinfo->nullable_relids);
1965  right_nullable_relids = bms_intersect(right_relids,
1966  rinfo->nullable_relids);
1967 
1968  foreach(lc1, root->eq_classes)
1969  {
1970  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1971  EquivalenceMember *coal_em = NULL;
1972  bool match;
1973  bool matchleft;
1974  bool matchright;
1975  ListCell *lc2;
1976 
1977  /* Ignore EC unless it contains pseudoconstants */
1978  if (!cur_ec->ec_has_const)
1979  continue;
1980  /* Never match to a volatile EC */
1981  if (cur_ec->ec_has_volatile)
1982  continue;
1983  /* It has to match the outer-join clause as to semantics, too */
1984  if (collation != cur_ec->ec_collation)
1985  continue;
1986  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1987  continue;
1988 
1989  /*
1990  * Does it contain a COALESCE(leftvar, rightvar) construct?
1991  *
1992  * We can assume the COALESCE() inputs are in the same order as the
1993  * join clause, since both were automatically generated in the cases
1994  * we care about.
1995  *
1996  * XXX currently this may fail to match in cross-type cases because
1997  * the COALESCE will contain typecast operations while the join clause
1998  * may not (if there is a cross-type mergejoin operator available for
1999  * the two column types). Is it OK to strip implicit coercions from
2000  * the COALESCE arguments?
2001  */
2002  match = false;
2003  foreach(lc2, cur_ec->ec_members)
2004  {
2005  coal_em = (EquivalenceMember *) lfirst(lc2);
2006  Assert(!coal_em->em_is_child); /* no children yet */
2007  if (IsA(coal_em->em_expr, CoalesceExpr))
2008  {
2009  CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
2010  Node *cfirst;
2011  Node *csecond;
2012 
2013  if (list_length(cexpr->args) != 2)
2014  continue;
2015  cfirst = (Node *) linitial(cexpr->args);
2016  csecond = (Node *) lsecond(cexpr->args);
2017 
2018  if (equal(leftvar, cfirst) && equal(rightvar, csecond))
2019  {
2020  match = true;
2021  break;
2022  }
2023  }
2024  }
2025  if (!match)
2026  continue; /* no match, so ignore this EC */
2027 
2028  /*
2029  * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
2030  * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
2031  * succeed with at least one constant for each var before we can
2032  * decide to throw away the outer-join clause.
2033  */
2034  matchleft = matchright = false;
2035  foreach(lc2, cur_ec->ec_members)
2036  {
2037  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2038  Oid eq_op;
2039  RestrictInfo *newrinfo;
2040 
2041  if (!cur_em->em_is_const)
2042  continue; /* ignore non-const members */
2043  eq_op = select_equality_operator(cur_ec,
2044  left_type,
2045  cur_em->em_datatype);
2046  if (OidIsValid(eq_op))
2047  {
2048  newrinfo = build_implied_join_equality(eq_op,
2049  cur_ec->ec_collation,
2050  leftvar,
2051  cur_em->em_expr,
2052  bms_copy(left_relids),
2053  bms_copy(left_nullable_relids),
2054  cur_ec->ec_min_security);
2055  if (process_equivalence(root, &newrinfo, true))
2056  matchleft = true;
2057  }
2058  eq_op = select_equality_operator(cur_ec,
2059  right_type,
2060  cur_em->em_datatype);
2061  if (OidIsValid(eq_op))
2062  {
2063  newrinfo = build_implied_join_equality(eq_op,
2064  cur_ec->ec_collation,
2065  rightvar,
2066  cur_em->em_expr,
2067  bms_copy(right_relids),
2068  bms_copy(right_nullable_relids),
2069  cur_ec->ec_min_security);
2070  if (process_equivalence(root, &newrinfo, true))
2071  matchright = true;
2072  }
2073  }
2074 
2075  /*
2076  * If we were able to equate both vars to constants, we're done, and
2077  * we can throw away the full-join clause as redundant. Moreover, we
2078  * can remove the COALESCE entry from the EC, since the added
2079  * restrictions ensure it will always have the expected value. (We
2080  * don't bother trying to update ec_relids or ec_sources.)
2081  */
2082  if (matchleft && matchright)
2083  {
2084  cur_ec->ec_members = list_delete_ptr(cur_ec->ec_members, coal_em);
2085  return true;
2086  }
2087 
2088  /*
2089  * Otherwise, fall out of the search loop, since we know the COALESCE
2090  * appears in at most one EC (XXX might stop being true if we allow
2091  * stripping of coercions above?)
2092  */
2093  break;
2094  }
2095 
2096  return false; /* failed to make any deduction */
2097 }
2098 
2099 
2100 /*
2101  * exprs_known_equal
2102  * Detect whether two expressions are known equal due to equivalence
2103  * relationships.
2104  *
2105  * Actually, this only shows that the expressions are equal according
2106  * to some opfamily's notion of equality --- but we only use it for
2107  * selectivity estimation, so a fuzzy idea of equality is OK.
2108  *
2109  * Note: does not bother to check for "equal(item1, item2)"; caller must
2110  * check that case if it's possible to pass identical items.
2111  */
2112 bool
2113 exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2)
2114 {
2115  ListCell *lc1;
2116 
2117  foreach(lc1, root->eq_classes)
2118  {
2119  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2120  bool item1member = false;
2121  bool item2member = false;
2122  ListCell *lc2;
2123 
2124  /* Never match to a volatile EC */
2125  if (ec->ec_has_volatile)
2126  continue;
2127 
2128  foreach(lc2, ec->ec_members)
2129  {
2131 
2132  if (em->em_is_child)
2133  continue; /* ignore children here */
2134  if (equal(item1, em->em_expr))
2135  item1member = true;
2136  else if (equal(item2, em->em_expr))
2137  item2member = true;
2138  /* Exit as soon as equality is proven */
2139  if (item1member && item2member)
2140  return true;
2141  }
2142  }
2143  return false;
2144 }
2145 
2146 
2147 /*
2148  * match_eclasses_to_foreign_key_col
2149  * See whether a foreign key column match is proven by any eclass.
2150  *
2151  * If the referenced and referencing Vars of the fkey's colno'th column are
2152  * known equal due to any eclass, return that eclass; otherwise return NULL.
2153  * (In principle there might be more than one matching eclass if multiple
2154  * collations are involved, but since collation doesn't matter for equality,
2155  * we ignore that fine point here.) This is much like exprs_known_equal,
2156  * except that we insist on the comparison operator matching the eclass, so
2157  * that the result is definite not approximate.
2158  */
2161  ForeignKeyOptInfo *fkinfo,
2162  int colno)
2163 {
2164  Index var1varno = fkinfo->con_relid;
2165  AttrNumber var1attno = fkinfo->conkey[colno];
2166  Index var2varno = fkinfo->ref_relid;
2167  AttrNumber var2attno = fkinfo->confkey[colno];
2168  Oid eqop = fkinfo->conpfeqop[colno];
2169  RelOptInfo *rel1 = root->simple_rel_array[var1varno];
2170  RelOptInfo *rel2 = root->simple_rel_array[var2varno];
2171  List *opfamilies = NIL; /* compute only if needed */
2172  Bitmapset *matching_ecs;
2173  int i;
2174 
2175  /* Consider only eclasses mentioning both relations */
2176  Assert(root->ec_merging_done);
2177  Assert(IS_SIMPLE_REL(rel1));
2178  Assert(IS_SIMPLE_REL(rel2));
2179  matching_ecs = bms_intersect(rel1->eclass_indexes,
2180  rel2->eclass_indexes);
2181 
2182  i = -1;
2183  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2184  {
2186  i);
2187  bool item1member = false;
2188  bool item2member = false;
2189  ListCell *lc2;
2190 
2191  /* Never match to a volatile EC */
2192  if (ec->ec_has_volatile)
2193  continue;
2194  /* Note: it seems okay to match to "broken" eclasses here */
2195 
2196  foreach(lc2, ec->ec_members)
2197  {
2199  Var *var;
2200 
2201  if (em->em_is_child)
2202  continue; /* ignore children here */
2203 
2204  /* EM must be a Var, possibly with RelabelType */
2205  var = (Var *) em->em_expr;
2206  while (var && IsA(var, RelabelType))
2207  var = (Var *) ((RelabelType *) var)->arg;
2208  if (!(var && IsA(var, Var)))
2209  continue;
2210 
2211  /* Match? */
2212  if (var->varno == var1varno && var->varattno == var1attno)
2213  item1member = true;
2214  else if (var->varno == var2varno && var->varattno == var2attno)
2215  item2member = true;
2216 
2217  /* Have we found both PK and FK column in this EC? */
2218  if (item1member && item2member)
2219  {
2220  /*
2221  * Succeed if eqop matches EC's opfamilies. We could test
2222  * this before scanning the members, but it's probably cheaper
2223  * to test for member matches first.
2224  */
2225  if (opfamilies == NIL) /* compute if we didn't already */
2226  opfamilies = get_mergejoin_opfamilies(eqop);
2227  if (equal(opfamilies, ec->ec_opfamilies))
2228  return ec;
2229  /* Otherwise, done with this EC, move on to the next */
2230  break;
2231  }
2232  }
2233  }
2234  return NULL;
2235 }
2236 
2237 
2238 /*
2239  * add_child_rel_equivalences
2240  * Search for EC members that reference the root parent of child_rel, and
2241  * add transformed members referencing the child_rel.
2242  *
2243  * Note that this function won't be called at all unless we have at least some
2244  * reason to believe that the EC members it generates will be useful.
2245  *
2246  * parent_rel and child_rel could be derived from appinfo, but since the
2247  * caller has already computed them, we might as well just pass them in.
2248  *
2249  * The passed-in AppendRelInfo is not used when the parent_rel is not a
2250  * top-level baserel, since it shows the mapping from the parent_rel but
2251  * we need to translate EC expressions that refer to the top-level parent.
2252  * Using it is faster than using adjust_appendrel_attrs_multilevel(), though,
2253  * so we prefer it when we can.
2254  */
2255 void
2257  AppendRelInfo *appinfo,
2258  RelOptInfo *parent_rel,
2259  RelOptInfo *child_rel)
2260 {
2261  Relids top_parent_relids = child_rel->top_parent_relids;
2262  Relids child_relids = child_rel->relids;
2263  int i;
2264 
2265  /*
2266  * EC merging should be complete already, so we can use the parent rel's
2267  * eclass_indexes to avoid searching all of root->eq_classes.
2268  */
2269  Assert(root->ec_merging_done);
2270  Assert(IS_SIMPLE_REL(parent_rel));
2271 
2272  i = -1;
2273  while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
2274  {
2275  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2276  int num_members;
2277 
2278  /*
2279  * If this EC contains a volatile expression, then generating child
2280  * EMs would be downright dangerous, so skip it. We rely on a
2281  * volatile EC having only one EM.
2282  */
2283  if (cur_ec->ec_has_volatile)
2284  continue;
2285 
2286  /* Sanity check eclass_indexes only contain ECs for parent_rel */
2287  Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
2288 
2289  /*
2290  * We don't use foreach() here because there's no point in scanning
2291  * newly-added child members, so we can stop after the last
2292  * pre-existing EC member.
2293  */
2294  num_members = list_length(cur_ec->ec_members);
2295  for (int pos = 0; pos < num_members; pos++)
2296  {
2297  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2298 
2299  if (cur_em->em_is_const)
2300  continue; /* ignore consts here */
2301 
2302  /*
2303  * We consider only original EC members here, not
2304  * already-transformed child members. Otherwise, if some original
2305  * member expression references more than one appendrel, we'd get
2306  * an O(N^2) explosion of useless derived expressions for
2307  * combinations of children. (But add_child_join_rel_equivalences
2308  * may add targeted combinations for partitionwise-join purposes.)
2309  */
2310  if (cur_em->em_is_child)
2311  continue; /* ignore children here */
2312 
2313  /* Does this member reference child's topmost parent rel? */
2314  if (bms_overlap(cur_em->em_relids, top_parent_relids))
2315  {
2316  /* Yes, generate transformed child version */
2317  Expr *child_expr;
2318  Relids new_relids;
2319  Relids new_nullable_relids;
2320 
2321  if (parent_rel->reloptkind == RELOPT_BASEREL)
2322  {
2323  /* Simple single-level transformation */
2324  child_expr = (Expr *)
2326  (Node *) cur_em->em_expr,
2327  1, &appinfo);
2328  }
2329  else
2330  {
2331  /* Must do multi-level transformation */
2332  child_expr = (Expr *)
2334  (Node *) cur_em->em_expr,
2335  child_relids,
2336  top_parent_relids);
2337  }
2338 
2339  /*
2340  * Transform em_relids to match. Note we do *not* do
2341  * pull_varnos(child_expr) here, as for example the
2342  * transformation might have substituted a constant, but we
2343  * don't want the child member to be marked as constant.
2344  */
2345  new_relids = bms_difference(cur_em->em_relids,
2346  top_parent_relids);
2347  new_relids = bms_add_members(new_relids, child_relids);
2348 
2349  /*
2350  * And likewise for nullable_relids. Note this code assumes
2351  * parent and child relids are singletons.
2352  */
2353  new_nullable_relids = cur_em->em_nullable_relids;
2354  if (bms_overlap(new_nullable_relids, top_parent_relids))
2355  {
2356  new_nullable_relids = bms_difference(new_nullable_relids,
2357  top_parent_relids);
2358  new_nullable_relids = bms_add_members(new_nullable_relids,
2359  child_relids);
2360  }
2361 
2362  (void) add_eq_member(cur_ec, child_expr,
2363  new_relids, new_nullable_relids,
2364  true, cur_em->em_datatype);
2365 
2366  /* Record this EC index for the child rel */
2367  child_rel->eclass_indexes = bms_add_member(child_rel->eclass_indexes, i);
2368  }
2369  }
2370  }
2371 }
2372 
2373 /*
2374  * add_child_join_rel_equivalences
2375  * Like add_child_rel_equivalences(), but for joinrels
2376  *
2377  * Here we find the ECs relevant to the top parent joinrel and add transformed
2378  * member expressions that refer to this child joinrel.
2379  *
2380  * Note that this function won't be called at all unless we have at least some
2381  * reason to believe that the EC members it generates will be useful.
2382  */
2383 void
2385  int nappinfos, AppendRelInfo **appinfos,
2386  RelOptInfo *parent_joinrel,
2387  RelOptInfo *child_joinrel)
2388 {
2389  Relids top_parent_relids = child_joinrel->top_parent_relids;
2390  Relids child_relids = child_joinrel->relids;
2391  Bitmapset *matching_ecs;
2392  int i;
2393 
2394  Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
2395 
2396  /* We need consider only ECs that mention the parent joinrel */
2397  matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
2398 
2399  i = -1;
2400  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2401  {
2402  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2403  int num_members;
2404 
2405  /*
2406  * If this EC contains a volatile expression, then generating child
2407  * EMs would be downright dangerous, so skip it. We rely on a
2408  * volatile EC having only one EM.
2409  */
2410  if (cur_ec->ec_has_volatile)
2411  continue;
2412 
2413  /* Sanity check on get_eclass_indexes_for_relids result */
2414  Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
2415 
2416  /*
2417  * We don't use foreach() here because there's no point in scanning
2418  * newly-added child members, so we can stop after the last
2419  * pre-existing EC member.
2420  */
2421  num_members = list_length(cur_ec->ec_members);
2422  for (int pos = 0; pos < num_members; pos++)
2423  {
2424  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2425 
2426  if (cur_em->em_is_const)
2427  continue; /* ignore consts here */
2428 
2429  /*
2430  * We consider only original EC members here, not
2431  * already-transformed child members.
2432  */
2433  if (cur_em->em_is_child)
2434  continue; /* ignore children here */
2435 
2436  /*
2437  * We may ignore expressions that reference a single baserel,
2438  * because add_child_rel_equivalences should have handled them.
2439  */
2440  if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
2441  continue;
2442 
2443  /* Does this member reference child's topmost parent rel? */
2444  if (bms_overlap(cur_em->em_relids, top_parent_relids))
2445  {
2446  /* Yes, generate transformed child version */
2447  Expr *child_expr;
2448  Relids new_relids;
2449  Relids new_nullable_relids;
2450 
2451  if (parent_joinrel->reloptkind == RELOPT_JOINREL)
2452  {
2453  /* Simple single-level transformation */
2454  child_expr = (Expr *)
2456  (Node *) cur_em->em_expr,
2457  nappinfos, appinfos);
2458  }
2459  else
2460  {
2461  /* Must do multi-level transformation */
2462  Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
2463  child_expr = (Expr *)
2465  (Node *) cur_em->em_expr,
2466  child_relids,
2467  top_parent_relids);
2468  }
2469 
2470  /*
2471  * Transform em_relids to match. Note we do *not* do
2472  * pull_varnos(child_expr) here, as for example the
2473  * transformation might have substituted a constant, but we
2474  * don't want the child member to be marked as constant.
2475  */
2476  new_relids = bms_difference(cur_em->em_relids,
2477  top_parent_relids);
2478  new_relids = bms_add_members(new_relids, child_relids);
2479 
2480  /*
2481  * For nullable_relids, we must selectively replace parent
2482  * nullable relids with child ones.
2483  */
2484  new_nullable_relids = cur_em->em_nullable_relids;
2485  if (bms_overlap(new_nullable_relids, top_parent_relids))
2486  new_nullable_relids =
2488  new_nullable_relids,
2489  child_relids,
2490  top_parent_relids);
2491 
2492  (void) add_eq_member(cur_ec, child_expr,
2493  new_relids, new_nullable_relids,
2494  true, cur_em->em_datatype);
2495  }
2496  }
2497  }
2498 }
2499 
2500 
2501 /*
2502  * generate_implied_equalities_for_column
2503  * Create EC-derived joinclauses usable with a specific column.
2504  *
2505  * This is used by indxpath.c to extract potentially indexable joinclauses
2506  * from ECs, and can be used by foreign data wrappers for similar purposes.
2507  * We assume that only expressions in Vars of a single table are of interest,
2508  * but the caller provides a callback function to identify exactly which
2509  * such expressions it would like to know about.
2510  *
2511  * We assume that any given table/index column could appear in only one EC.
2512  * (This should be true in all but the most pathological cases, and if it
2513  * isn't, we stop on the first match anyway.) Therefore, what we return
2514  * is a redundant list of clauses equating the table/index column to each of
2515  * the other-relation values it is known to be equal to. Any one of
2516  * these clauses can be used to create a parameterized path, and there
2517  * is no value in using more than one. (But it *is* worthwhile to create
2518  * a separate parameterized path for each one, since that leads to different
2519  * join orders.)
2520  *
2521  * The caller can pass a Relids set of rels we aren't interested in joining
2522  * to, so as to save the work of creating useless clauses.
2523  */
2524 List *
2526  RelOptInfo *rel,
2528  void *callback_arg,
2529  Relids prohibited_rels)
2530 {
2531  List *result = NIL;
2532  bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2533  Relids parent_relids;
2534  int i;
2535 
2536  /* Should be OK to rely on eclass_indexes */
2537  Assert(root->ec_merging_done);
2538 
2539  /* Indexes are available only on base or "other" member relations. */
2540  Assert(IS_SIMPLE_REL(rel));
2541 
2542  /* If it's a child rel, we'll need to know what its parent(s) are */
2543  if (is_child_rel)
2544  parent_relids = find_childrel_parents(root, rel);
2545  else
2546  parent_relids = NULL; /* not used, but keep compiler quiet */
2547 
2548  i = -1;
2549  while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
2550  {
2551  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2552  EquivalenceMember *cur_em;
2553  ListCell *lc2;
2554 
2555  /* Sanity check eclass_indexes only contain ECs for rel */
2556  Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
2557 
2558  /*
2559  * Won't generate joinclauses if const or single-member (the latter
2560  * test covers the volatile case too)
2561  */
2562  if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2563  continue;
2564 
2565  /*
2566  * Scan members, looking for a match to the target column. Note that
2567  * child EC members are considered, but only when they belong to the
2568  * target relation. (Unlike regular members, the same expression
2569  * could be a child member of more than one EC. Therefore, it's
2570  * potentially order-dependent which EC a child relation's target
2571  * column gets matched to. This is annoying but it only happens in
2572  * corner cases, so for now we live with just reporting the first
2573  * match. See also get_eclass_for_sort_expr.)
2574  */
2575  cur_em = NULL;
2576  foreach(lc2, cur_ec->ec_members)
2577  {
2578  cur_em = (EquivalenceMember *) lfirst(lc2);
2579  if (bms_equal(cur_em->em_relids, rel->relids) &&
2580  callback(root, rel, cur_ec, cur_em, callback_arg))
2581  break;
2582  cur_em = NULL;
2583  }
2584 
2585  if (!cur_em)
2586  continue;
2587 
2588  /*
2589  * Found our match. Scan the other EC members and attempt to generate
2590  * joinclauses.
2591  */
2592  foreach(lc2, cur_ec->ec_members)
2593  {
2594  EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2595  Oid eq_op;
2596  RestrictInfo *rinfo;
2597 
2598  if (other_em->em_is_child)
2599  continue; /* ignore children here */
2600 
2601  /* Make sure it'll be a join to a different rel */
2602  if (other_em == cur_em ||
2603  bms_overlap(other_em->em_relids, rel->relids))
2604  continue;
2605 
2606  /* Forget it if caller doesn't want joins to this rel */
2607  if (bms_overlap(other_em->em_relids, prohibited_rels))
2608  continue;
2609 
2610  /*
2611  * Also, if this is a child rel, avoid generating a useless join
2612  * to its parent rel(s).
2613  */
2614  if (is_child_rel &&
2615  bms_overlap(parent_relids, other_em->em_relids))
2616  continue;
2617 
2618  eq_op = select_equality_operator(cur_ec,
2619  cur_em->em_datatype,
2620  other_em->em_datatype);
2621  if (!OidIsValid(eq_op))
2622  continue;
2623 
2624  /* set parent_ec to mark as redundant with other joinclauses */
2625  rinfo = create_join_clause(root, cur_ec, eq_op,
2626  cur_em, other_em,
2627  cur_ec);
2628 
2629  result = lappend(result, rinfo);
2630  }
2631 
2632  /*
2633  * If somehow we failed to create any join clauses, we might as well
2634  * keep scanning the ECs for another match. But if we did make any,
2635  * we're done, because we don't want to return non-redundant clauses.
2636  */
2637  if (result)
2638  break;
2639  }
2640 
2641  return result;
2642 }
2643 
2644 /*
2645  * have_relevant_eclass_joinclause
2646  * Detect whether there is an EquivalenceClass that could produce
2647  * a joinclause involving the two given relations.
2648  *
2649  * This is essentially a very cut-down version of
2650  * generate_join_implied_equalities(). Note it's OK to occasionally say "yes"
2651  * incorrectly. Hence we don't bother with details like whether the lack of a
2652  * cross-type operator might prevent the clause from actually being generated.
2653  */
2654 bool
2656  RelOptInfo *rel1, RelOptInfo *rel2)
2657 {
2658  Bitmapset *matching_ecs;
2659  int i;
2660 
2661  /* Examine only eclasses mentioning both rel1 and rel2 */
2662  matching_ecs = get_common_eclass_indexes(root, rel1->relids,
2663  rel2->relids);
2664 
2665  i = -1;
2666  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2667  {
2669  i);
2670 
2671  /*
2672  * Sanity check that get_common_eclass_indexes gave only ECs
2673  * containing both rels.
2674  */
2675  Assert(bms_overlap(rel1->relids, ec->ec_relids));
2676  Assert(bms_overlap(rel2->relids, ec->ec_relids));
2677 
2678  /*
2679  * Won't generate joinclauses if single-member (this test covers the
2680  * volatile case too)
2681  */
2682  if (list_length(ec->ec_members) <= 1)
2683  continue;
2684 
2685  /*
2686  * We do not need to examine the individual members of the EC, because
2687  * all that we care about is whether each rel overlaps the relids of
2688  * at least one member, and get_common_eclass_indexes() and the single
2689  * member check above are sufficient to prove that. (As with
2690  * have_relevant_joinclause(), it is not necessary that the EC be able
2691  * to form a joinclause relating exactly the two given rels, only that
2692  * it be able to form a joinclause mentioning both, and this will
2693  * surely be true if both of them overlap ec_relids.)
2694  *
2695  * Note we don't test ec_broken; if we did, we'd need a separate code
2696  * path to look through ec_sources. Checking the membership anyway is
2697  * OK as a possibly-overoptimistic heuristic.
2698  *
2699  * We don't test ec_has_const either, even though a const eclass won't
2700  * generate real join clauses. This is because if we had "WHERE a.x =
2701  * b.y and a.x = 42", it is worth considering a join between a and b,
2702  * since the join result is likely to be small even though it'll end
2703  * up being an unqualified nestloop.
2704  */
2705 
2706  return true;
2707  }
2708 
2709  return false;
2710 }
2711 
2712 
2713 /*
2714  * has_relevant_eclass_joinclause
2715  * Detect whether there is an EquivalenceClass that could produce
2716  * a joinclause involving the given relation and anything else.
2717  *
2718  * This is the same as have_relevant_eclass_joinclause with the other rel
2719  * implicitly defined as "everything else in the query".
2720  */
2721 bool
2723 {
2724  Bitmapset *matched_ecs;
2725  int i;
2726 
2727  /* Examine only eclasses mentioning rel1 */
2728  matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
2729 
2730  i = -1;
2731  while ((i = bms_next_member(matched_ecs, i)) >= 0)
2732  {
2734  i);
2735 
2736  /*
2737  * Won't generate joinclauses if single-member (this test covers the
2738  * volatile case too)
2739  */
2740  if (list_length(ec->ec_members) <= 1)
2741  continue;
2742 
2743  /*
2744  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
2745  * to find an EC that mentions both this rel and some other rel.
2746  */
2747  if (!bms_is_subset(ec->ec_relids, rel1->relids))
2748  return true;
2749  }
2750 
2751  return false;
2752 }
2753 
2754 
2755 /*
2756  * eclass_useful_for_merging
2757  * Detect whether the EC could produce any mergejoinable join clauses
2758  * against the specified relation.
2759  *
2760  * This is just a heuristic test and doesn't have to be exact; it's better
2761  * to say "yes" incorrectly than "no". Hence we don't bother with details
2762  * like whether the lack of a cross-type operator might prevent the clause
2763  * from actually being generated.
2764  */
2765 bool
2768  RelOptInfo *rel)
2769 {
2770  Relids relids;
2771  ListCell *lc;
2772 
2773  Assert(!eclass->ec_merged);
2774 
2775  /*
2776  * Won't generate joinclauses if const or single-member (the latter test
2777  * covers the volatile case too)
2778  */
2779  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
2780  return false;
2781 
2782  /*
2783  * Note we don't test ec_broken; if we did, we'd need a separate code path
2784  * to look through ec_sources. Checking the members anyway is OK as a
2785  * possibly-overoptimistic heuristic.
2786  */
2787 
2788  /* If specified rel is a child, we must consider the topmost parent rel */
2789  if (IS_OTHER_REL(rel))
2790  {
2792  relids = rel->top_parent_relids;
2793  }
2794  else
2795  relids = rel->relids;
2796 
2797  /* If rel already includes all members of eclass, no point in searching */
2798  if (bms_is_subset(eclass->ec_relids, relids))
2799  return false;
2800 
2801  /* To join, we need a member not in the given rel */
2802  foreach(lc, eclass->ec_members)
2803  {
2804  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
2805 
2806  if (cur_em->em_is_child)
2807  continue; /* ignore children here */
2808 
2809  if (!bms_overlap(cur_em->em_relids, relids))
2810  return true;
2811  }
2812 
2813  return false;
2814 }
2815 
2816 
2817 /*
2818  * is_redundant_derived_clause
2819  * Test whether rinfo is derived from same EC as any clause in clauselist;
2820  * if so, it can be presumed to represent a condition that's redundant
2821  * with that member of the list.
2822  */
2823 bool
2825 {
2826  EquivalenceClass *parent_ec = rinfo->parent_ec;
2827  ListCell *lc;
2828 
2829  /* Fail if it's not a potentially-redundant clause from some EC */
2830  if (parent_ec == NULL)
2831  return false;
2832 
2833  foreach(lc, clauselist)
2834  {
2835  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
2836 
2837  if (otherrinfo->parent_ec == parent_ec)
2838  return true;
2839  }
2840 
2841  return false;
2842 }
2843 
2844 /*
2845  * is_redundant_with_indexclauses
2846  * Test whether rinfo is redundant with any clause in the IndexClause
2847  * list. Here, for convenience, we test both simple identity and
2848  * whether it is derived from the same EC as any member of the list.
2849  */
2850 bool
2852 {
2853  EquivalenceClass *parent_ec = rinfo->parent_ec;
2854  ListCell *lc;
2855 
2856  foreach(lc, indexclauses)
2857  {
2858  IndexClause *iclause = lfirst_node(IndexClause, lc);
2859  RestrictInfo *otherrinfo = iclause->rinfo;
2860 
2861  /* If indexclause is lossy, it won't enforce the condition exactly */
2862  if (iclause->lossy)
2863  continue;
2864 
2865  /* Match if it's same clause (pointer equality should be enough) */
2866  if (rinfo == otherrinfo)
2867  return true;
2868  /* Match if derived from same EC */
2869  if (parent_ec && otherrinfo->parent_ec == parent_ec)
2870  return true;
2871 
2872  /*
2873  * No need to look at the derived clauses in iclause->indexquals; they
2874  * couldn't match if the parent clause didn't.
2875  */
2876  }
2877 
2878  return false;
2879 }
2880 
2881 /*
2882  * get_eclass_indexes_for_relids
2883  * Build and return a Bitmapset containing the indexes into root's
2884  * eq_classes list for all eclasses that mention any of these relids
2885  */
2886 static Bitmapset *
2888 {
2889  Bitmapset *ec_indexes = NULL;
2890  int i = -1;
2891 
2892  /* Should be OK to rely on eclass_indexes */
2893  Assert(root->ec_merging_done);
2894 
2895  while ((i = bms_next_member(relids, i)) > 0)
2896  {
2897  RelOptInfo *rel = root->simple_rel_array[i];
2898 
2899  ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
2900  }
2901  return ec_indexes;
2902 }
2903 
2904 /*
2905  * get_common_eclass_indexes
2906  * Build and return a Bitmapset containing the indexes into root's
2907  * eq_classes list for all eclasses that mention rels in both
2908  * relids1 and relids2.
2909  */
2910 static Bitmapset *
2912 {
2913  Bitmapset *rel1ecs;
2914  Bitmapset *rel2ecs;
2915  int relid;
2916 
2917  rel1ecs = get_eclass_indexes_for_relids(root, relids1);
2918 
2919  /*
2920  * We can get away with just using the relation's eclass_indexes directly
2921  * when relids2 is a singleton set.
2922  */
2923  if (bms_get_singleton_member(relids2, &relid))
2924  rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
2925  else
2926  rel2ecs = get_eclass_indexes_for_relids(root, relids2);
2927 
2928  /* Calculate and return the common EC indexes, recycling the left input. */
2929  return bms_int_members(rel1ecs, rel2ecs);
2930 }
bool has_eclass_joins
Definition: pathnodes.h:733
#define NIL
Definition: pg_list.h:65
bool exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2)
Definition: equivclass.c:2113
static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
Definition: equivclass.c:1936
static void generate_base_implied_equalities_no_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1006
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1749
bool op_strict(Oid opno)
Definition: lsyscache.c:1389
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1563
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Index security_level
Definition: pathnodes.h:1997
bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
Definition: equivclass.c:2824
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
RelOptKind reloptkind
Definition: pathnodes.h:662
RestrictInfo * make_restrictinfo(Expr *clause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
Definition: restrictinfo.c:59
bool ec_merging_done
Definition: pathnodes.h:268
Relids required_relids
Definition: pathnodes.h:2003
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3033
bool leakproof
Definition: pathnodes.h:1995
Index ec_min_security
Definition: pathnodes.h:974
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:653
bool eclass_useful_for_merging(PlannerInfo *root, EquivalenceClass *eclass, RelOptInfo *rel)
Definition: equivclass.c:2766
Relids em_nullable_relids
Definition: pathnodes.h:1014
List * ec_derives
Definition: pathnodes.h:966
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:364
#define Min(x, y)
Definition: c.h:920
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:669
EquivalenceClass * get_eclass_for_sort_expr(PlannerInfo *root, Expr *expr, Relids nullable_relids, List *opfamilies, Oid opcintype, Oid collation, Index sortref, Relids rel, bool create_it)
Definition: equivclass.c:625
bool pseudoconstant
Definition: pathnodes.h:1993
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static void generate_base_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1097
List * list_copy(const List *oldlist)
Definition: list.c:1403
Definition: nodes.h:529
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:643
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
Relids left_relids
Definition: pathnodes.h:2012
AttrNumber varattno
Definition: primnodes.h:186
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
bool ec_below_outer_join
Definition: pathnodes.h:971
Relids adjust_child_relids_multilevel(PlannerInfo *root, Relids relids, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:576
EquivalenceClass * right_ec
Definition: pathnodes.h:2034
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
static bool reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
Definition: equivclass.c:1811
void add_child_rel_equivalences(PlannerInfo *root, AppendRelInfo *appinfo, RelOptInfo *parent_rel, RelOptInfo *child_rel)
Definition: equivclass.c:2256
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:726
Index ec_max_security
Definition: pathnodes.h:975
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:796
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:181
#define OidIsValid(objectId)
Definition: c.h:644
List * mergeopfamilies
Definition: pathnodes.h:2030
#define lsecond(l)
Definition: pg_list.h:200
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:638
Relids outer_relids
Definition: pathnodes.h:2006
void process_implied_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level, bool below_outer_join, bool both_const)
Definition: initsplan.c:2288
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:508
Selectivity norm_selec
Definition: pathnodes.h:2023
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:203
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:502
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:368
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:229
#define list_make1(x1)
Definition: pg_list.h:227
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
Definition: equivclass.c:2384
void pfree(void *pointer)
Definition: mcxt.c:1056
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2204
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1354
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
Definition: equivclass.c:118
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1154
EquivalenceMember * left_em
Definition: pathnodes.h:2035
List * args
Definition: primnodes.h:1086
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2887
Oid conpfeqop[INDEX_MAX_KEYS]
Definition: pathnodes.h:889
EquivalenceClass * parent_ec
Definition: pathnodes.h:2019
Expr * arg
Definition: primnodes.h:1219
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
bool is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
Definition: equivclass.c:2851
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:499
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
bool outerjoin_delayed
Definition: pathnodes.h:1989
#define lfirst_node(type, lc)
Definition: pg_list.h:193
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1528
List * ec_sources
Definition: pathnodes.h:965
static List * generate_join_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec, Relids nominal_join_relids, Relids outer_relids, Relids nominal_inner_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1479
struct RestrictInfo * rinfo
Definition: pathnodes.h:1253
Relids ec_relids
Definition: pathnodes.h:967
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:402
List * left_join_clauses
Definition: pathnodes.h:272
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:164
List * full_join_clauses
Definition: pathnodes.h:280
Relids relids
Definition: pathnodes.h:665
Selectivity outer_selec
Definition: pathnodes.h:2026
Expr * find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
Definition: equivclass.c:782
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1275
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:70
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:2722
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1303
int simple_rel_array_size
Definition: pathnodes.h:204
Relids pull_varnos(Node *node)
Definition: var.c:95
bool contain_window_function(Node *clause)
Definition: clauses.c:496
List * lappend(List *list, void *datum)
Definition: list.c:321
EquivalenceMember * right_em
Definition: pathnodes.h:2036
Expr * clause
Definition: pathnodes.h:1985
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Index varno
Definition: primnodes.h:184
List * ec_opfamilies
Definition: pathnodes.h:962
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:175
Relids nullable_relids
Definition: pathnodes.h:2009
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
void * palloc0(Size size)
Definition: mcxt.c:980
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1232
AttrNumber conkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:887
static Bitmapset * get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
Definition: equivclass.c:2911
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
void generate_base_implied_equalities(PlannerInfo *root)
Definition: equivclass.c:855
bool have_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: equivclass.c:2655
unsigned int Index
Definition: c.h:475
NullTestType nulltesttype
Definition: primnodes.h:1220
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1243
EquivalenceClass * match_eclasses_to_foreign_key_col(PlannerInfo *root, ForeignKeyOptInfo *fkinfo, int colno)
Definition: equivclass.c:2160
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:82
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1202
bool is_pushed_down
Definition: pathnodes.h:1987
void reconsider_outer_join_clauses(PlannerInfo *root)
Definition: equivclass.c:1716
#define Max(x, y)
Definition: c.h:914
#define makeNode(_type_)
Definition: nodes.h:577
Relids right_relids
Definition: pathnodes.h:2013
bool(* ec_matches_callback_type)(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
Definition: paths.h:116
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:266
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
int location
Definition: primnodes.h:1222
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:169
bool ec_has_volatile
Definition: pathnodes.h:970
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2353
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
bool func_strict(Oid funcid)
Definition: lsyscache.c:1673
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
bool contain_agg_clause(Node *clause)
Definition: clauses.c:181
EquivalenceClass * left_ec
Definition: pathnodes.h:2033
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1618
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:174
void list_free(List *list)
Definition: list.c:1376
#define elog(elevel,...)
Definition: elog.h:214
int i
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:549
void * arg
bool argisrow
Definition: primnodes.h:1221
static void generate_base_implied_equalities_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:929
MemoryContext planner_cxt
Definition: pathnodes.h:331
List * right_join_clauses
Definition: pathnodes.h:276
bool contain_nonstrict_functions(Node *clause)
Definition: clauses.c:1096
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
#define copyObject(obj)
Definition: nodes.h:645
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:902
Definition: regcomp.c:224
Definition: pg_list.h:50
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:888
int16 AttrNumber
Definition: attnum.h:21
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:172
Bitmapset * eclass_indexes
Definition: pathnodes.h:707
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:976
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
#define lfirst_oid(lc)
Definition: pg_list.h:192
List * ec_members
Definition: pathnodes.h:964
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194
Relids top_parent_relids
Definition: pathnodes.h:738
List * generate_implied_equalities_for_column(PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, void *callback_arg, Relids prohibited_rels)
Definition: equivclass.c:2525