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