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