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