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 (inner_rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
1064  {
1065  /* Fetch relid set for the topmost parent rel */
1066  nominal_inner_relids = find_childrel_top_parent(root, inner_rel)->relids;
1067  /* ECs will be marked with the parent's relid, not the child's */
1068  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1069  }
1070  else
1071  {
1072  nominal_inner_relids = inner_relids;
1073  nominal_join_relids = join_relids;
1074  }
1075 
1076  foreach(lc, eclasses)
1077  {
1079  List *sublist = NIL;
1080 
1081  /* ECs containing consts do not need any further enforcement */
1082  if (ec->ec_has_const)
1083  continue;
1084 
1085  /* Single-member ECs won't generate any deductions */
1086  if (list_length(ec->ec_members) <= 1)
1087  continue;
1088 
1089  /* We can quickly ignore any that don't overlap the join, too */
1090  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1091  continue;
1092 
1093  if (!ec->ec_broken)
1095  ec,
1096  join_relids,
1097  outer_relids,
1098  inner_relids);
1099 
1100  /* Recover if we failed to generate required derived clauses */
1101  if (ec->ec_broken)
1103  ec,
1104  nominal_join_relids,
1105  outer_relids,
1106  nominal_inner_relids,
1107  inner_rel);
1108 
1109  result = list_concat(result, sublist);
1110  }
1111 
1112  return result;
1113 }
1114 
1115 /*
1116  * generate_join_implied_equalities for a still-valid EC
1117  */
1118 static List *
1120  EquivalenceClass *ec,
1121  Relids join_relids,
1122  Relids outer_relids,
1123  Relids inner_relids)
1124 {
1125  List *result = NIL;
1126  List *new_members = NIL;
1127  List *outer_members = NIL;
1128  List *inner_members = NIL;
1129  ListCell *lc1;
1130 
1131  /*
1132  * First, scan the EC to identify member values that are computable at the
1133  * outer rel, at the inner rel, or at this relation but not in either
1134  * input rel. The outer-rel members should already be enforced equal,
1135  * likewise for the inner-rel members. We'll need to create clauses to
1136  * enforce that any newly computable members are all equal to each other
1137  * as well as to at least one input member, plus enforce at least one
1138  * outer-rel member equal to at least one inner-rel member.
1139  */
1140  foreach(lc1, ec->ec_members)
1141  {
1142  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1143 
1144  /*
1145  * We don't need to check explicitly for child EC members. This test
1146  * against join_relids will cause them to be ignored except when
1147  * considering a child inner rel, which is what we want.
1148  */
1149  if (!bms_is_subset(cur_em->em_relids, join_relids))
1150  continue; /* not computable yet, or wrong child */
1151 
1152  if (bms_is_subset(cur_em->em_relids, outer_relids))
1153  outer_members = lappend(outer_members, cur_em);
1154  else if (bms_is_subset(cur_em->em_relids, inner_relids))
1155  inner_members = lappend(inner_members, cur_em);
1156  else
1157  new_members = lappend(new_members, cur_em);
1158  }
1159 
1160  /*
1161  * First, select the joinclause if needed. We can equate any one outer
1162  * member to any one inner member, but we have to find a datatype
1163  * combination for which an opfamily member operator exists. If we have
1164  * choices, we prefer simple Var members (possibly with RelabelType) since
1165  * these are (a) cheapest to compute at runtime and (b) most likely to
1166  * have useful statistics. Also, prefer operators that are also
1167  * hashjoinable.
1168  */
1169  if (outer_members && inner_members)
1170  {
1171  EquivalenceMember *best_outer_em = NULL;
1172  EquivalenceMember *best_inner_em = NULL;
1173  Oid best_eq_op = InvalidOid;
1174  int best_score = -1;
1175  RestrictInfo *rinfo;
1176 
1177  foreach(lc1, outer_members)
1178  {
1179  EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
1180  ListCell *lc2;
1181 
1182  foreach(lc2, inner_members)
1183  {
1184  EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
1185  Oid eq_op;
1186  int score;
1187 
1188  eq_op = select_equality_operator(ec,
1189  outer_em->em_datatype,
1190  inner_em->em_datatype);
1191  if (!OidIsValid(eq_op))
1192  continue;
1193  score = 0;
1194  if (IsA(outer_em->em_expr, Var) ||
1195  (IsA(outer_em->em_expr, RelabelType) &&
1196  IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
1197  score++;
1198  if (IsA(inner_em->em_expr, Var) ||
1199  (IsA(inner_em->em_expr, RelabelType) &&
1200  IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
1201  score++;
1202  if (op_hashjoinable(eq_op,
1203  exprType((Node *) outer_em->em_expr)))
1204  score++;
1205  if (score > best_score)
1206  {
1207  best_outer_em = outer_em;
1208  best_inner_em = inner_em;
1209  best_eq_op = eq_op;
1210  best_score = score;
1211  if (best_score == 3)
1212  break; /* no need to look further */
1213  }
1214  }
1215  if (best_score == 3)
1216  break; /* no need to look further */
1217  }
1218  if (best_score < 0)
1219  {
1220  /* failed... */
1221  ec->ec_broken = true;
1222  return NIL;
1223  }
1224 
1225  /*
1226  * Create clause, setting parent_ec to mark it as redundant with other
1227  * joinclauses
1228  */
1229  rinfo = create_join_clause(root, ec, best_eq_op,
1230  best_outer_em, best_inner_em,
1231  ec);
1232 
1233  result = lappend(result, rinfo);
1234  }
1235 
1236  /*
1237  * Now deal with building restrictions for any expressions that involve
1238  * Vars from both sides of the join. We have to equate all of these to
1239  * each other as well as to at least one old member (if any).
1240  *
1241  * XXX as in generate_base_implied_equalities_no_const, we could be a lot
1242  * smarter here to avoid unnecessary failures in cross-type situations.
1243  * For now, use the same left-to-right method used there.
1244  */
1245  if (new_members)
1246  {
1247  List *old_members = list_concat(outer_members, inner_members);
1248  EquivalenceMember *prev_em = NULL;
1249  RestrictInfo *rinfo;
1250 
1251  /* For now, arbitrarily take the first old_member as the one to use */
1252  if (old_members)
1253  new_members = lappend(new_members, linitial(old_members));
1254 
1255  foreach(lc1, new_members)
1256  {
1257  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1258 
1259  if (prev_em != NULL)
1260  {
1261  Oid eq_op;
1262 
1263  eq_op = select_equality_operator(ec,
1264  prev_em->em_datatype,
1265  cur_em->em_datatype);
1266  if (!OidIsValid(eq_op))
1267  {
1268  /* failed... */
1269  ec->ec_broken = true;
1270  return NIL;
1271  }
1272  /* do NOT set parent_ec, this qual is not redundant! */
1273  rinfo = create_join_clause(root, ec, eq_op,
1274  prev_em, cur_em,
1275  NULL);
1276 
1277  result = lappend(result, rinfo);
1278  }
1279  prev_em = cur_em;
1280  }
1281  }
1282 
1283  return result;
1284 }
1285 
1286 /*
1287  * generate_join_implied_equalities cleanup after failure
1288  *
1289  * Return any original RestrictInfos that are enforceable at this join.
1290  *
1291  * In the case of a child inner relation, we have to translate the
1292  * original RestrictInfos from parent to child Vars.
1293  */
1294 static List *
1296  EquivalenceClass *ec,
1297  Relids nominal_join_relids,
1298  Relids outer_relids,
1299  Relids nominal_inner_relids,
1300  RelOptInfo *inner_rel)
1301 {
1302  List *result = NIL;
1303  ListCell *lc;
1304 
1305  foreach(lc, ec->ec_sources)
1306  {
1307  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1308  Relids clause_relids = restrictinfo->required_relids;
1309 
1310  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1311  !bms_is_subset(clause_relids, outer_relids) &&
1312  !bms_is_subset(clause_relids, nominal_inner_relids))
1313  result = lappend(result, restrictinfo);
1314  }
1315 
1316  /*
1317  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1318  * to all the RestrictInfos at once. This will result in returning
1319  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1320  * any duplication, and it's a sufficiently narrow corner case that we
1321  * shouldn't sweat too much over it anyway.
1322  *
1323  * Since inner_rel might be an indirect descendant of the baserel
1324  * mentioned in the ec_sources clauses, we have to be prepared to apply
1325  * multiple levels of Var translation.
1326  */
1327  if (inner_rel->reloptkind == RELOPT_OTHER_MEMBER_REL &&
1328  result != NIL)
1329  result = (List *) adjust_appendrel_attrs_multilevel(root,
1330  (Node *) result,
1331  inner_rel);
1332 
1333  return result;
1334 }
1335 
1336 
1337 /*
1338  * select_equality_operator
1339  * Select a suitable equality operator for comparing two EC members
1340  *
1341  * Returns InvalidOid if no operator can be found for this datatype combination
1342  */
1343 static Oid
1345 {
1346  ListCell *lc;
1347 
1348  foreach(lc, ec->ec_opfamilies)
1349  {
1350  Oid opfamily = lfirst_oid(lc);
1351  Oid opno;
1352 
1353  opno = get_opfamily_member(opfamily, lefttype, righttype,
1355  if (!OidIsValid(opno))
1356  continue;
1357  /* If no barrier quals in query, don't worry about leaky operators */
1358  if (ec->ec_max_security == 0)
1359  return opno;
1360  /* Otherwise, insist that selected operators be leakproof */
1361  if (get_func_leakproof(get_opcode(opno)))
1362  return opno;
1363  }
1364  return InvalidOid;
1365 }
1366 
1367 
1368 /*
1369  * create_join_clause
1370  * Find or make a RestrictInfo comparing the two given EC members
1371  * with the given operator.
1372  *
1373  * parent_ec is either equal to ec (if the clause is a potentially-redundant
1374  * join clause) or NULL (if not). We have to treat this as part of the
1375  * match requirements --- it's possible that a clause comparing the same two
1376  * EMs is a join clause in one join path and a restriction clause in another.
1377  */
1378 static RestrictInfo *
1380  EquivalenceClass *ec, Oid opno,
1381  EquivalenceMember *leftem,
1382  EquivalenceMember *rightem,
1383  EquivalenceClass *parent_ec)
1384 {
1385  RestrictInfo *rinfo;
1386  ListCell *lc;
1387  MemoryContext oldcontext;
1388 
1389  /*
1390  * Search to see if we already built a RestrictInfo for this pair of
1391  * EquivalenceMembers. We can use either original source clauses or
1392  * previously-derived clauses. The check on opno is probably redundant,
1393  * but be safe ...
1394  */
1395  foreach(lc, ec->ec_sources)
1396  {
1397  rinfo = (RestrictInfo *) lfirst(lc);
1398  if (rinfo->left_em == leftem &&
1399  rinfo->right_em == rightem &&
1400  rinfo->parent_ec == parent_ec &&
1401  opno == ((OpExpr *) rinfo->clause)->opno)
1402  return rinfo;
1403  }
1404 
1405  foreach(lc, ec->ec_derives)
1406  {
1407  rinfo = (RestrictInfo *) lfirst(lc);
1408  if (rinfo->left_em == leftem &&
1409  rinfo->right_em == rightem &&
1410  rinfo->parent_ec == parent_ec &&
1411  opno == ((OpExpr *) rinfo->clause)->opno)
1412  return rinfo;
1413  }
1414 
1415  /*
1416  * Not there, so build it, in planner context so we can re-use it. (Not
1417  * important in normal planning, but definitely so in GEQO.)
1418  */
1419  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1420 
1421  rinfo = build_implied_join_equality(opno,
1422  ec->ec_collation,
1423  leftem->em_expr,
1424  rightem->em_expr,
1425  bms_union(leftem->em_relids,
1426  rightem->em_relids),
1427  bms_union(leftem->em_nullable_relids,
1428  rightem->em_nullable_relids),
1429  ec->ec_min_security);
1430 
1431  /* Mark the clause as redundant, or not */
1432  rinfo->parent_ec = parent_ec;
1433 
1434  /*
1435  * We know the correct values for left_ec/right_ec, ie this particular EC,
1436  * so we can just set them directly instead of forcing another lookup.
1437  */
1438  rinfo->left_ec = ec;
1439  rinfo->right_ec = ec;
1440 
1441  /* Mark it as usable with these EMs */
1442  rinfo->left_em = leftem;
1443  rinfo->right_em = rightem;
1444  /* and save it for possible re-use */
1445  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1446 
1447  MemoryContextSwitchTo(oldcontext);
1448 
1449  return rinfo;
1450 }
1451 
1452 
1453 /*
1454  * reconsider_outer_join_clauses
1455  * Re-examine any outer-join clauses that were set aside by
1456  * distribute_qual_to_rels(), and see if we can derive any
1457  * EquivalenceClasses from them. Then, if they were not made
1458  * redundant, push them out into the regular join-clause lists.
1459  *
1460  * When we have mergejoinable clauses A = B that are outer-join clauses,
1461  * we can't blindly combine them with other clauses A = C to deduce B = C,
1462  * since in fact the "equality" A = B won't necessarily hold above the
1463  * outer join (one of the variables might be NULL instead). Nonetheless
1464  * there are cases where we can add qual clauses using transitivity.
1465  *
1466  * One case that we look for here is an outer-join clause OUTERVAR = INNERVAR
1467  * for which there is also an equivalence clause OUTERVAR = CONSTANT.
1468  * It is safe and useful to push a clause INNERVAR = CONSTANT into the
1469  * evaluation of the inner (nullable) relation, because any inner rows not
1470  * meeting this condition will not contribute to the outer-join result anyway.
1471  * (Any outer rows they could join to will be eliminated by the pushed-down
1472  * equivalence clause.)
1473  *
1474  * Note that the above rule does not work for full outer joins; nor is it
1475  * very interesting to consider cases where the generated equivalence clause
1476  * would involve relations outside the outer join, since such clauses couldn't
1477  * be pushed into the inner side's scan anyway. So the restriction to
1478  * outervar = pseudoconstant is not really giving up anything.
1479  *
1480  * For full-join cases, we can only do something useful if it's a FULL JOIN
1481  * USING and a merged column has an equivalence MERGEDVAR = CONSTANT.
1482  * By the time it gets here, the merged column will look like
1483  * COALESCE(LEFTVAR, RIGHTVAR)
1484  * and we will have a full-join clause LEFTVAR = RIGHTVAR that we can match
1485  * the COALESCE expression to. In this situation we can push LEFTVAR = CONSTANT
1486  * and RIGHTVAR = CONSTANT into the input relations, since any rows not
1487  * meeting these conditions cannot contribute to the join result.
1488  *
1489  * Again, there isn't any traction to be gained by trying to deal with
1490  * clauses comparing a mergedvar to a non-pseudoconstant. So we can make
1491  * use of the EquivalenceClasses to search for matching variables that were
1492  * equivalenced to constants. The interesting outer-join clauses were
1493  * accumulated for us by distribute_qual_to_rels.
1494  *
1495  * When we find one of these cases, we implement the changes we want by
1496  * generating a new equivalence clause INNERVAR = CONSTANT (or LEFTVAR, etc)
1497  * and pushing it into the EquivalenceClass structures. This is because we
1498  * may already know that INNERVAR is equivalenced to some other var(s), and
1499  * we'd like the constant to propagate to them too. Note that it would be
1500  * unsafe to merge any existing EC for INNERVAR with the OUTERVAR's EC ---
1501  * that could result in propagating constant restrictions from
1502  * INNERVAR to OUTERVAR, which would be very wrong.
1503  *
1504  * It's possible that the INNERVAR is also an OUTERVAR for some other
1505  * outer-join clause, in which case the process can be repeated. So we repeat
1506  * looping over the lists of clauses until no further deductions can be made.
1507  * Whenever we do make a deduction, we remove the generating clause from the
1508  * lists, since we don't want to make the same deduction twice.
1509  *
1510  * If we don't find any match for a set-aside outer join clause, we must
1511  * throw it back into the regular joinclause processing by passing it to
1512  * distribute_restrictinfo_to_rels(). If we do generate a derived clause,
1513  * however, the outer-join clause is redundant. We still throw it back,
1514  * because otherwise the join will be seen as a clauseless join and avoided
1515  * during join order searching; but we mark it as redundant to keep from
1516  * messing up the joinrel's size estimate. (This behavior means that the
1517  * API for this routine is uselessly complex: we could have just put all
1518  * the clauses into the regular processing initially. We keep it because
1519  * someday we might want to do something else, such as inserting "dummy"
1520  * joinclauses instead of real ones.)
1521  *
1522  * Outer join clauses that are marked outerjoin_delayed are special: this
1523  * condition means that one or both VARs might go to null due to a lower
1524  * outer join. We can still push a constant through the clause, but only
1525  * if its operator is strict; and we *have to* throw the clause back into
1526  * regular joinclause processing. By keeping the strict join clause,
1527  * we ensure that any null-extended rows that are mistakenly generated due
1528  * to suppressing rows not matching the constant will be rejected at the
1529  * upper outer join. (This doesn't work for full-join clauses.)
1530  */
1531 void
1533 {
1534  bool found;
1535  ListCell *cell;
1536  ListCell *prev;
1537  ListCell *next;
1538 
1539  /* Outer loop repeats until we find no more deductions */
1540  do
1541  {
1542  found = false;
1543 
1544  /* Process the LEFT JOIN clauses */
1545  prev = NULL;
1546  for (cell = list_head(root->left_join_clauses); cell; cell = next)
1547  {
1548  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1549 
1550  next = lnext(cell);
1551  if (reconsider_outer_join_clause(root, rinfo, true))
1552  {
1553  found = true;
1554  /* remove it from the list */
1555  root->left_join_clauses =
1556  list_delete_cell(root->left_join_clauses, cell, prev);
1557  /* we throw it back anyway (see notes above) */
1558  /* but the thrown-back clause has no extra selectivity */
1559  rinfo->norm_selec = 2.0;
1560  rinfo->outer_selec = 1.0;
1561  distribute_restrictinfo_to_rels(root, rinfo);
1562  }
1563  else
1564  prev = cell;
1565  }
1566 
1567  /* Process the RIGHT JOIN clauses */
1568  prev = NULL;
1569  for (cell = list_head(root->right_join_clauses); cell; cell = next)
1570  {
1571  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1572 
1573  next = lnext(cell);
1574  if (reconsider_outer_join_clause(root, rinfo, false))
1575  {
1576  found = true;
1577  /* remove it from the list */
1578  root->right_join_clauses =
1579  list_delete_cell(root->right_join_clauses, cell, prev);
1580  /* we throw it back anyway (see notes above) */
1581  /* but the thrown-back clause has no extra selectivity */
1582  rinfo->norm_selec = 2.0;
1583  rinfo->outer_selec = 1.0;
1584  distribute_restrictinfo_to_rels(root, rinfo);
1585  }
1586  else
1587  prev = cell;
1588  }
1589 
1590  /* Process the FULL JOIN clauses */
1591  prev = NULL;
1592  for (cell = list_head(root->full_join_clauses); cell; cell = next)
1593  {
1594  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1595 
1596  next = lnext(cell);
1597  if (reconsider_full_join_clause(root, rinfo))
1598  {
1599  found = true;
1600  /* remove it from the list */
1601  root->full_join_clauses =
1602  list_delete_cell(root->full_join_clauses, cell, prev);
1603  /* we throw it back anyway (see notes above) */
1604  /* but the thrown-back clause has no extra selectivity */
1605  rinfo->norm_selec = 2.0;
1606  rinfo->outer_selec = 1.0;
1607  distribute_restrictinfo_to_rels(root, rinfo);
1608  }
1609  else
1610  prev = cell;
1611  }
1612  } while (found);
1613 
1614  /* Now, any remaining clauses have to be thrown back */
1615  foreach(cell, root->left_join_clauses)
1616  {
1617  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1618 
1619  distribute_restrictinfo_to_rels(root, rinfo);
1620  }
1621  foreach(cell, root->right_join_clauses)
1622  {
1623  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1624 
1625  distribute_restrictinfo_to_rels(root, rinfo);
1626  }
1627  foreach(cell, root->full_join_clauses)
1628  {
1629  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1630 
1631  distribute_restrictinfo_to_rels(root, rinfo);
1632  }
1633 }
1634 
1635 /*
1636  * reconsider_outer_join_clauses for a single LEFT/RIGHT JOIN clause
1637  *
1638  * Returns TRUE if we were able to propagate a constant through the clause.
1639  */
1640 static bool
1642  bool outer_on_left)
1643 {
1644  Expr *outervar,
1645  *innervar;
1646  Oid opno,
1647  collation,
1648  left_type,
1649  right_type,
1650  inner_datatype;
1651  Relids inner_relids,
1652  inner_nullable_relids;
1653  ListCell *lc1;
1654 
1655  Assert(is_opclause(rinfo->clause));
1656  opno = ((OpExpr *) rinfo->clause)->opno;
1657  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1658 
1659  /* If clause is outerjoin_delayed, operator must be strict */
1660  if (rinfo->outerjoin_delayed && !op_strict(opno))
1661  return false;
1662 
1663  /* Extract needed info from the clause */
1664  op_input_types(opno, &left_type, &right_type);
1665  if (outer_on_left)
1666  {
1667  outervar = (Expr *) get_leftop(rinfo->clause);
1668  innervar = (Expr *) get_rightop(rinfo->clause);
1669  inner_datatype = right_type;
1670  inner_relids = rinfo->right_relids;
1671  }
1672  else
1673  {
1674  outervar = (Expr *) get_rightop(rinfo->clause);
1675  innervar = (Expr *) get_leftop(rinfo->clause);
1676  inner_datatype = left_type;
1677  inner_relids = rinfo->left_relids;
1678  }
1679  inner_nullable_relids = bms_intersect(inner_relids,
1680  rinfo->nullable_relids);
1681 
1682  /* Scan EquivalenceClasses for a match to outervar */
1683  foreach(lc1, root->eq_classes)
1684  {
1685  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1686  bool match;
1687  ListCell *lc2;
1688 
1689  /* Ignore EC unless it contains pseudoconstants */
1690  if (!cur_ec->ec_has_const)
1691  continue;
1692  /* Never match to a volatile EC */
1693  if (cur_ec->ec_has_volatile)
1694  continue;
1695  /* It has to match the outer-join clause as to semantics, too */
1696  if (collation != cur_ec->ec_collation)
1697  continue;
1698  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1699  continue;
1700  /* Does it contain a match to outervar? */
1701  match = false;
1702  foreach(lc2, cur_ec->ec_members)
1703  {
1704  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1705 
1706  Assert(!cur_em->em_is_child); /* no children yet */
1707  if (equal(outervar, cur_em->em_expr))
1708  {
1709  match = true;
1710  break;
1711  }
1712  }
1713  if (!match)
1714  continue; /* no match, so ignore this EC */
1715 
1716  /*
1717  * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
1718  * CONSTANT in the EC. Note that we must succeed with at least one
1719  * constant before we can decide to throw away the outer-join clause.
1720  */
1721  match = false;
1722  foreach(lc2, cur_ec->ec_members)
1723  {
1724  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1725  Oid eq_op;
1726  RestrictInfo *newrinfo;
1727 
1728  if (!cur_em->em_is_const)
1729  continue; /* ignore non-const members */
1730  eq_op = select_equality_operator(cur_ec,
1731  inner_datatype,
1732  cur_em->em_datatype);
1733  if (!OidIsValid(eq_op))
1734  continue; /* can't generate equality */
1735  newrinfo = build_implied_join_equality(eq_op,
1736  cur_ec->ec_collation,
1737  innervar,
1738  cur_em->em_expr,
1739  bms_copy(inner_relids),
1740  bms_copy(inner_nullable_relids),
1741  cur_ec->ec_min_security);
1742  if (process_equivalence(root, newrinfo, true))
1743  match = true;
1744  }
1745 
1746  /*
1747  * If we were able to equate INNERVAR to any constant, report success.
1748  * Otherwise, fall out of the search loop, since we know the OUTERVAR
1749  * appears in at most one EC.
1750  */
1751  if (match)
1752  return true;
1753  else
1754  break;
1755  }
1756 
1757  return false; /* failed to make any deduction */
1758 }
1759 
1760 /*
1761  * reconsider_outer_join_clauses for a single FULL JOIN clause
1762  *
1763  * Returns TRUE if we were able to propagate a constant through the clause.
1764  */
1765 static bool
1767 {
1768  Expr *leftvar;
1769  Expr *rightvar;
1770  Oid opno,
1771  collation,
1772  left_type,
1773  right_type;
1774  Relids left_relids,
1775  right_relids,
1776  left_nullable_relids,
1777  right_nullable_relids;
1778  ListCell *lc1;
1779 
1780  /* Can't use an outerjoin_delayed clause here */
1781  if (rinfo->outerjoin_delayed)
1782  return false;
1783 
1784  /* Extract needed info from the clause */
1785  Assert(is_opclause(rinfo->clause));
1786  opno = ((OpExpr *) rinfo->clause)->opno;
1787  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1788  op_input_types(opno, &left_type, &right_type);
1789  leftvar = (Expr *) get_leftop(rinfo->clause);
1790  rightvar = (Expr *) get_rightop(rinfo->clause);
1791  left_relids = rinfo->left_relids;
1792  right_relids = rinfo->right_relids;
1793  left_nullable_relids = bms_intersect(left_relids,
1794  rinfo->nullable_relids);
1795  right_nullable_relids = bms_intersect(right_relids,
1796  rinfo->nullable_relids);
1797 
1798  foreach(lc1, root->eq_classes)
1799  {
1800  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1801  EquivalenceMember *coal_em = NULL;
1802  bool match;
1803  bool matchleft;
1804  bool matchright;
1805  ListCell *lc2;
1806 
1807  /* Ignore EC unless it contains pseudoconstants */
1808  if (!cur_ec->ec_has_const)
1809  continue;
1810  /* Never match to a volatile EC */
1811  if (cur_ec->ec_has_volatile)
1812  continue;
1813  /* It has to match the outer-join clause as to semantics, too */
1814  if (collation != cur_ec->ec_collation)
1815  continue;
1816  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1817  continue;
1818 
1819  /*
1820  * Does it contain a COALESCE(leftvar, rightvar) construct?
1821  *
1822  * We can assume the COALESCE() inputs are in the same order as the
1823  * join clause, since both were automatically generated in the cases
1824  * we care about.
1825  *
1826  * XXX currently this may fail to match in cross-type cases because
1827  * the COALESCE will contain typecast operations while the join clause
1828  * may not (if there is a cross-type mergejoin operator available for
1829  * the two column types). Is it OK to strip implicit coercions from
1830  * the COALESCE arguments?
1831  */
1832  match = false;
1833  foreach(lc2, cur_ec->ec_members)
1834  {
1835  coal_em = (EquivalenceMember *) lfirst(lc2);
1836  Assert(!coal_em->em_is_child); /* no children yet */
1837  if (IsA(coal_em->em_expr, CoalesceExpr))
1838  {
1839  CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
1840  Node *cfirst;
1841  Node *csecond;
1842 
1843  if (list_length(cexpr->args) != 2)
1844  continue;
1845  cfirst = (Node *) linitial(cexpr->args);
1846  csecond = (Node *) lsecond(cexpr->args);
1847 
1848  if (equal(leftvar, cfirst) && equal(rightvar, csecond))
1849  {
1850  match = true;
1851  break;
1852  }
1853  }
1854  }
1855  if (!match)
1856  continue; /* no match, so ignore this EC */
1857 
1858  /*
1859  * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
1860  * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
1861  * succeed with at least one constant for each var before we can
1862  * decide to throw away the outer-join clause.
1863  */
1864  matchleft = matchright = false;
1865  foreach(lc2, cur_ec->ec_members)
1866  {
1867  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1868  Oid eq_op;
1869  RestrictInfo *newrinfo;
1870 
1871  if (!cur_em->em_is_const)
1872  continue; /* ignore non-const members */
1873  eq_op = select_equality_operator(cur_ec,
1874  left_type,
1875  cur_em->em_datatype);
1876  if (OidIsValid(eq_op))
1877  {
1878  newrinfo = build_implied_join_equality(eq_op,
1879  cur_ec->ec_collation,
1880  leftvar,
1881  cur_em->em_expr,
1882  bms_copy(left_relids),
1883  bms_copy(left_nullable_relids),
1884  cur_ec->ec_min_security);
1885  if (process_equivalence(root, newrinfo, true))
1886  matchleft = true;
1887  }
1888  eq_op = select_equality_operator(cur_ec,
1889  right_type,
1890  cur_em->em_datatype);
1891  if (OidIsValid(eq_op))
1892  {
1893  newrinfo = build_implied_join_equality(eq_op,
1894  cur_ec->ec_collation,
1895  rightvar,
1896  cur_em->em_expr,
1897  bms_copy(right_relids),
1898  bms_copy(right_nullable_relids),
1899  cur_ec->ec_min_security);
1900  if (process_equivalence(root, newrinfo, true))
1901  matchright = true;
1902  }
1903  }
1904 
1905  /*
1906  * If we were able to equate both vars to constants, we're done, and
1907  * we can throw away the full-join clause as redundant. Moreover, we
1908  * can remove the COALESCE entry from the EC, since the added
1909  * restrictions ensure it will always have the expected value. (We
1910  * don't bother trying to update ec_relids or ec_sources.)
1911  */
1912  if (matchleft && matchright)
1913  {
1914  cur_ec->ec_members = list_delete_ptr(cur_ec->ec_members, coal_em);
1915  return true;
1916  }
1917 
1918  /*
1919  * Otherwise, fall out of the search loop, since we know the COALESCE
1920  * appears in at most one EC (XXX might stop being true if we allow
1921  * stripping of coercions above?)
1922  */
1923  break;
1924  }
1925 
1926  return false; /* failed to make any deduction */
1927 }
1928 
1929 
1930 /*
1931  * exprs_known_equal
1932  * Detect whether two expressions are known equal due to equivalence
1933  * relationships.
1934  *
1935  * Actually, this only shows that the expressions are equal according
1936  * to some opfamily's notion of equality --- but we only use it for
1937  * selectivity estimation, so a fuzzy idea of equality is OK.
1938  *
1939  * Note: does not bother to check for "equal(item1, item2)"; caller must
1940  * check that case if it's possible to pass identical items.
1941  */
1942 bool
1943 exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2)
1944 {
1945  ListCell *lc1;
1946 
1947  foreach(lc1, root->eq_classes)
1948  {
1949  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
1950  bool item1member = false;
1951  bool item2member = false;
1952  ListCell *lc2;
1953 
1954  /* Never match to a volatile EC */
1955  if (ec->ec_has_volatile)
1956  continue;
1957 
1958  foreach(lc2, ec->ec_members)
1959  {
1961 
1962  if (em->em_is_child)
1963  continue; /* ignore children here */
1964  if (equal(item1, em->em_expr))
1965  item1member = true;
1966  else if (equal(item2, em->em_expr))
1967  item2member = true;
1968  /* Exit as soon as equality is proven */
1969  if (item1member && item2member)
1970  return true;
1971  }
1972  }
1973  return false;
1974 }
1975 
1976 
1977 /*
1978  * match_eclasses_to_foreign_key_col
1979  * See whether a foreign key column match is proven by any eclass.
1980  *
1981  * If the referenced and referencing Vars of the fkey's colno'th column are
1982  * known equal due to any eclass, return that eclass; otherwise return NULL.
1983  * (In principle there might be more than one matching eclass if multiple
1984  * collations are involved, but since collation doesn't matter for equality,
1985  * we ignore that fine point here.) This is much like exprs_known_equal,
1986  * except that we insist on the comparison operator matching the eclass, so
1987  * that the result is definite not approximate.
1988  */
1991  ForeignKeyOptInfo *fkinfo,
1992  int colno)
1993 {
1994  Index var1varno = fkinfo->con_relid;
1995  AttrNumber var1attno = fkinfo->conkey[colno];
1996  Index var2varno = fkinfo->ref_relid;
1997  AttrNumber var2attno = fkinfo->confkey[colno];
1998  Oid eqop = fkinfo->conpfeqop[colno];
1999  List *opfamilies = NIL; /* compute only if needed */
2000  ListCell *lc1;
2001 
2002  foreach(lc1, root->eq_classes)
2003  {
2004  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2005  bool item1member = false;
2006  bool item2member = false;
2007  ListCell *lc2;
2008 
2009  /* Never match to a volatile EC */
2010  if (ec->ec_has_volatile)
2011  continue;
2012  /* Note: it seems okay to match to "broken" eclasses here */
2013 
2014  foreach(lc2, ec->ec_members)
2015  {
2017  Var *var;
2018 
2019  if (em->em_is_child)
2020  continue; /* ignore children here */
2021 
2022  /* EM must be a Var, possibly with RelabelType */
2023  var = (Var *) em->em_expr;
2024  while (var && IsA(var, RelabelType))
2025  var = (Var *) ((RelabelType *) var)->arg;
2026  if (!(var && IsA(var, Var)))
2027  continue;
2028 
2029  /* Match? */
2030  if (var->varno == var1varno && var->varattno == var1attno)
2031  item1member = true;
2032  else if (var->varno == var2varno && var->varattno == var2attno)
2033  item2member = true;
2034 
2035  /* Have we found both PK and FK column in this EC? */
2036  if (item1member && item2member)
2037  {
2038  /*
2039  * Succeed if eqop matches EC's opfamilies. We could test
2040  * this before scanning the members, but it's probably cheaper
2041  * to test for member matches first.
2042  */
2043  if (opfamilies == NIL) /* compute if we didn't already */
2044  opfamilies = get_mergejoin_opfamilies(eqop);
2045  if (equal(opfamilies, ec->ec_opfamilies))
2046  return ec;
2047  /* Otherwise, done with this EC, move on to the next */
2048  break;
2049  }
2050  }
2051  }
2052  return NULL;
2053 }
2054 
2055 
2056 /*
2057  * add_child_rel_equivalences
2058  * Search for EC members that reference the parent_rel, and
2059  * add transformed members referencing the child_rel.
2060  *
2061  * Note that this function won't be called at all unless we have at least some
2062  * reason to believe that the EC members it generates will be useful.
2063  *
2064  * parent_rel and child_rel could be derived from appinfo, but since the
2065  * caller has already computed them, we might as well just pass them in.
2066  */
2067 void
2069  AppendRelInfo *appinfo,
2070  RelOptInfo *parent_rel,
2071  RelOptInfo *child_rel)
2072 {
2073  ListCell *lc1;
2074 
2075  foreach(lc1, root->eq_classes)
2076  {
2077  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2078  ListCell *lc2;
2079 
2080  /*
2081  * If this EC contains a volatile expression, then generating child
2082  * EMs would be downright dangerous, so skip it. We rely on a
2083  * volatile EC having only one EM.
2084  */
2085  if (cur_ec->ec_has_volatile)
2086  continue;
2087 
2088  /*
2089  * No point in searching if parent rel not mentioned in eclass; but we
2090  * can't tell that for sure if parent rel is itself a child.
2091  */
2092  if (parent_rel->reloptkind == RELOPT_BASEREL &&
2093  !bms_is_subset(parent_rel->relids, cur_ec->ec_relids))
2094  continue;
2095 
2096  foreach(lc2, cur_ec->ec_members)
2097  {
2098  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2099 
2100  if (cur_em->em_is_const)
2101  continue; /* ignore consts here */
2102 
2103  /* Does it reference parent_rel? */
2104  if (bms_overlap(cur_em->em_relids, parent_rel->relids))
2105  {
2106  /* Yes, generate transformed child version */
2107  Expr *child_expr;
2108  Relids new_relids;
2109  Relids new_nullable_relids;
2110 
2111  child_expr = (Expr *)
2113  (Node *) cur_em->em_expr,
2114  appinfo);
2115 
2116  /*
2117  * Transform em_relids to match. Note we do *not* do
2118  * pull_varnos(child_expr) here, as for example the
2119  * transformation might have substituted a constant, but we
2120  * don't want the child member to be marked as constant.
2121  */
2122  new_relids = bms_difference(cur_em->em_relids,
2123  parent_rel->relids);
2124  new_relids = bms_add_members(new_relids, child_rel->relids);
2125 
2126  /*
2127  * And likewise for nullable_relids. Note this code assumes
2128  * parent and child relids are singletons.
2129  */
2130  new_nullable_relids = cur_em->em_nullable_relids;
2131  if (bms_overlap(new_nullable_relids, parent_rel->relids))
2132  {
2133  new_nullable_relids = bms_difference(new_nullable_relids,
2134  parent_rel->relids);
2135  new_nullable_relids = bms_add_members(new_nullable_relids,
2136  child_rel->relids);
2137  }
2138 
2139  (void) add_eq_member(cur_ec, child_expr,
2140  new_relids, new_nullable_relids,
2141  true, cur_em->em_datatype);
2142  }
2143  }
2144  }
2145 }
2146 
2147 
2148 /*
2149  * generate_implied_equalities_for_column
2150  * Create EC-derived joinclauses usable with a specific column.
2151  *
2152  * This is used by indxpath.c to extract potentially indexable joinclauses
2153  * from ECs, and can be used by foreign data wrappers for similar purposes.
2154  * We assume that only expressions in Vars of a single table are of interest,
2155  * but the caller provides a callback function to identify exactly which
2156  * such expressions it would like to know about.
2157  *
2158  * We assume that any given table/index column could appear in only one EC.
2159  * (This should be true in all but the most pathological cases, and if it
2160  * isn't, we stop on the first match anyway.) Therefore, what we return
2161  * is a redundant list of clauses equating the table/index column to each of
2162  * the other-relation values it is known to be equal to. Any one of
2163  * these clauses can be used to create a parameterized path, and there
2164  * is no value in using more than one. (But it *is* worthwhile to create
2165  * a separate parameterized path for each one, since that leads to different
2166  * join orders.)
2167  *
2168  * The caller can pass a Relids set of rels we aren't interested in joining
2169  * to, so as to save the work of creating useless clauses.
2170  */
2171 List *
2173  RelOptInfo *rel,
2175  void *callback_arg,
2176  Relids prohibited_rels)
2177 {
2178  List *result = NIL;
2179  bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2180  Relids parent_relids;
2181  ListCell *lc1;
2182 
2183  /* If it's a child rel, we'll need to know what its parent(s) are */
2184  if (is_child_rel)
2185  parent_relids = find_childrel_parents(root, rel);
2186  else
2187  parent_relids = NULL; /* not used, but keep compiler quiet */
2188 
2189  foreach(lc1, root->eq_classes)
2190  {
2191  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2192  EquivalenceMember *cur_em;
2193  ListCell *lc2;
2194 
2195  /*
2196  * Won't generate joinclauses if const or single-member (the latter
2197  * test covers the volatile case too)
2198  */
2199  if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2200  continue;
2201 
2202  /*
2203  * No point in searching if rel not mentioned in eclass (but we can't
2204  * tell that for a child rel).
2205  */
2206  if (!is_child_rel &&
2207  !bms_is_subset(rel->relids, cur_ec->ec_relids))
2208  continue;
2209 
2210  /*
2211  * Scan members, looking for a match to the target column. Note that
2212  * child EC members are considered, but only when they belong to the
2213  * target relation. (Unlike regular members, the same expression
2214  * could be a child member of more than one EC. Therefore, it's
2215  * potentially order-dependent which EC a child relation's target
2216  * column gets matched to. This is annoying but it only happens in
2217  * corner cases, so for now we live with just reporting the first
2218  * match. See also get_eclass_for_sort_expr.)
2219  */
2220  cur_em = NULL;
2221  foreach(lc2, cur_ec->ec_members)
2222  {
2223  cur_em = (EquivalenceMember *) lfirst(lc2);
2224  if (bms_equal(cur_em->em_relids, rel->relids) &&
2225  callback(root, rel, cur_ec, cur_em, callback_arg))
2226  break;
2227  cur_em = NULL;
2228  }
2229 
2230  if (!cur_em)
2231  continue;
2232 
2233  /*
2234  * Found our match. Scan the other EC members and attempt to generate
2235  * joinclauses.
2236  */
2237  foreach(lc2, cur_ec->ec_members)
2238  {
2239  EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2240  Oid eq_op;
2241  RestrictInfo *rinfo;
2242 
2243  if (other_em->em_is_child)
2244  continue; /* ignore children here */
2245 
2246  /* Make sure it'll be a join to a different rel */
2247  if (other_em == cur_em ||
2248  bms_overlap(other_em->em_relids, rel->relids))
2249  continue;
2250 
2251  /* Forget it if caller doesn't want joins to this rel */
2252  if (bms_overlap(other_em->em_relids, prohibited_rels))
2253  continue;
2254 
2255  /*
2256  * Also, if this is a child rel, avoid generating a useless join
2257  * to its parent rel(s).
2258  */
2259  if (is_child_rel &&
2260  bms_overlap(parent_relids, other_em->em_relids))
2261  continue;
2262 
2263  eq_op = select_equality_operator(cur_ec,
2264  cur_em->em_datatype,
2265  other_em->em_datatype);
2266  if (!OidIsValid(eq_op))
2267  continue;
2268 
2269  /* set parent_ec to mark as redundant with other joinclauses */
2270  rinfo = create_join_clause(root, cur_ec, eq_op,
2271  cur_em, other_em,
2272  cur_ec);
2273 
2274  result = lappend(result, rinfo);
2275  }
2276 
2277  /*
2278  * If somehow we failed to create any join clauses, we might as well
2279  * keep scanning the ECs for another match. But if we did make any,
2280  * we're done, because we don't want to return non-redundant clauses.
2281  */
2282  if (result)
2283  break;
2284  }
2285 
2286  return result;
2287 }
2288 
2289 /*
2290  * have_relevant_eclass_joinclause
2291  * Detect whether there is an EquivalenceClass that could produce
2292  * a joinclause involving the two given relations.
2293  *
2294  * This is essentially a very cut-down version of
2295  * generate_join_implied_equalities(). Note it's OK to occasionally say "yes"
2296  * incorrectly. Hence we don't bother with details like whether the lack of a
2297  * cross-type operator might prevent the clause from actually being generated.
2298  */
2299 bool
2301  RelOptInfo *rel1, RelOptInfo *rel2)
2302 {
2303  ListCell *lc1;
2304 
2305  foreach(lc1, root->eq_classes)
2306  {
2307  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2308 
2309  /*
2310  * Won't generate joinclauses if single-member (this test covers the
2311  * volatile case too)
2312  */
2313  if (list_length(ec->ec_members) <= 1)
2314  continue;
2315 
2316  /*
2317  * We do not need to examine the individual members of the EC, because
2318  * all that we care about is whether each rel overlaps the relids of
2319  * at least one member, and a test on ec_relids is sufficient to prove
2320  * that. (As with have_relevant_joinclause(), it is not necessary
2321  * that the EC be able to form a joinclause relating exactly the two
2322  * given rels, only that it be able to form a joinclause mentioning
2323  * both, and this will surely be true if both of them overlap
2324  * ec_relids.)
2325  *
2326  * Note we don't test ec_broken; if we did, we'd need a separate code
2327  * path to look through ec_sources. Checking the membership anyway is
2328  * OK as a possibly-overoptimistic heuristic.
2329  *
2330  * We don't test ec_has_const either, even though a const eclass won't
2331  * generate real join clauses. This is because if we had "WHERE a.x =
2332  * b.y and a.x = 42", it is worth considering a join between a and b,
2333  * since the join result is likely to be small even though it'll end
2334  * up being an unqualified nestloop.
2335  */
2336  if (bms_overlap(rel1->relids, ec->ec_relids) &&
2337  bms_overlap(rel2->relids, ec->ec_relids))
2338  return true;
2339  }
2340 
2341  return false;
2342 }
2343 
2344 
2345 /*
2346  * has_relevant_eclass_joinclause
2347  * Detect whether there is an EquivalenceClass that could produce
2348  * a joinclause involving the given relation and anything else.
2349  *
2350  * This is the same as have_relevant_eclass_joinclause with the other rel
2351  * implicitly defined as "everything else in the query".
2352  */
2353 bool
2355 {
2356  ListCell *lc1;
2357 
2358  foreach(lc1, root->eq_classes)
2359  {
2360  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2361 
2362  /*
2363  * Won't generate joinclauses if single-member (this test covers the
2364  * volatile case too)
2365  */
2366  if (list_length(ec->ec_members) <= 1)
2367  continue;
2368 
2369  /*
2370  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
2371  * to find an EC that mentions both this rel and some other rel.
2372  */
2373  if (bms_overlap(rel1->relids, ec->ec_relids) &&
2374  !bms_is_subset(ec->ec_relids, rel1->relids))
2375  return true;
2376  }
2377 
2378  return false;
2379 }
2380 
2381 
2382 /*
2383  * eclass_useful_for_merging
2384  * Detect whether the EC could produce any mergejoinable join clauses
2385  * against the specified relation.
2386  *
2387  * This is just a heuristic test and doesn't have to be exact; it's better
2388  * to say "yes" incorrectly than "no". Hence we don't bother with details
2389  * like whether the lack of a cross-type operator might prevent the clause
2390  * from actually being generated.
2391  */
2392 bool
2395  RelOptInfo *rel)
2396 {
2397  Relids relids;
2398  ListCell *lc;
2399 
2400  Assert(!eclass->ec_merged);
2401 
2402  /*
2403  * Won't generate joinclauses if const or single-member (the latter test
2404  * covers the volatile case too)
2405  */
2406  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
2407  return false;
2408 
2409  /*
2410  * Note we don't test ec_broken; if we did, we'd need a separate code path
2411  * to look through ec_sources. Checking the members anyway is OK as a
2412  * possibly-overoptimistic heuristic.
2413  */
2414 
2415  /* If specified rel is a child, we must consider the topmost parent rel */
2416  if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
2417  relids = find_childrel_top_parent(root, rel)->relids;
2418  else
2419  relids = rel->relids;
2420 
2421  /* If rel already includes all members of eclass, no point in searching */
2422  if (bms_is_subset(eclass->ec_relids, relids))
2423  return false;
2424 
2425  /* To join, we need a member not in the given rel */
2426  foreach(lc, eclass->ec_members)
2427  {
2428  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
2429 
2430  if (cur_em->em_is_child)
2431  continue; /* ignore children here */
2432 
2433  if (!bms_overlap(cur_em->em_relids, relids))
2434  return true;
2435  }
2436 
2437  return false;
2438 }
2439 
2440 
2441 /*
2442  * is_redundant_derived_clause
2443  * Test whether rinfo is derived from same EC as any clause in clauselist;
2444  * if so, it can be presumed to represent a condition that's redundant
2445  * with that member of the list.
2446  */
2447 bool
2449 {
2450  EquivalenceClass *parent_ec = rinfo->parent_ec;
2451  ListCell *lc;
2452 
2453  /* Fail if it's not a potentially-redundant clause from some EC */
2454  if (parent_ec == NULL)
2455  return false;
2456 
2457  foreach(lc, clauselist)
2458  {
2459  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
2460 
2461  if (otherrinfo->parent_ec == parent_ec)
2462  return true;
2463  }
2464 
2465  return false;
2466 }
bool has_eclass_joins
Definition: relation.h:551
RelOptInfo * find_childrel_top_parent(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:951
#define NIL
Definition: pg_list.h:69
bool exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2)
Definition: equivclass.c:1943
static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
Definition: equivclass.c:1766
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:1590
bool op_strict(Oid opno)
Definition: lsyscache.c:1249
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1379
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
Index security_level
Definition: relation.h:1649
bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
Definition: equivclass.c:2448
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:110
#define PVC_RECURSE_AGGREGATES
Definition: var.h:21
RelOptKind reloptkind
Definition: relation.h:487
static int32 next
Definition: blutils.c:210
Relids required_relids
Definition: relation.h:1655
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2870
bool leakproof
Definition: relation.h:1647
Index ec_min_security
Definition: relation.h:724
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
bool eclass_useful_for_merging(PlannerInfo *root, EquivalenceClass *eclass, RelOptInfo *rel)
Definition: equivclass.c:2393
Relids em_nullable_relids
Definition: relation.h:764
List * ec_derives
Definition: relation.h:716
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:363
#define Min(x, y)
Definition: c.h:802
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:667
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:283
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:723
Definition: nodes.h:508
Relids left_relids
Definition: relation.h:1664
AttrNumber varattno
Definition: primnodes.h:146
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:539
List * list_concat(List *list1, List *list2)
Definition: list.c:321
bool ec_below_outer_join
Definition: relation.h:721
EquivalenceClass * right_ec
Definition: relation.h:1686
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:1641
void add_child_rel_equivalences(PlannerInfo *root, AppendRelInfo *appinfo, RelOptInfo *parent_rel, RelOptInfo *child_rel)
Definition: equivclass.c:2068
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
Index ec_max_security
Definition: relation.h:725
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:141
#define OidIsValid(objectId)
Definition: c.h:534
List * mergeopfamilies
Definition: relation.h:1682
#define lsecond(l)
Definition: pg_list.h:114
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:2287
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:508
Selectivity norm_selec
Definition: relation.h:1675
bool(* ec_matches_callback_type)(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
Definition: paths.h:117
struct RelOptInfo ** simple_rel_array
Definition: relation.h:176
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
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:133
#define PVC_INCLUDE_PLACEHOLDERS
Definition: var.h:24
void pfree(void *pointer)
Definition: mcxt.c:992
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2203
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1214
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
#define IsPolymorphicType(typid)
Definition: pg_type.h:733
#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:1687
List * args
Definition: primnodes.h:1023
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:808
Oid conpfeqop[INDEX_MAX_KEYS]
Definition: relation.h:655
EquivalenceClass * parent_ec
Definition: relation.h:1671
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:49
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:307
bool outerjoin_delayed
Definition: relation.h:1641
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1344
List * ec_sources
Definition: relation.h:715
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, AppendRelInfo *appinfo)
Definition: prepunion.c:1733
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:1295
Relids ec_relids
Definition: relation.h:717
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:399
List * left_join_clauses
Definition: relation.h:236
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
List * full_join_clauses
Definition: relation.h:244
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:490
Selectivity outer_selec
Definition: relation.h:1678
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1135
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:2354
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1119
int simple_rel_array_size
Definition: relation.h:177
#define lnext(lc)
Definition: pg_list.h:105
List * canon_pathkeys
Definition: relation.h:234
Relids pull_varnos(Node *node)
Definition: var.c:95
bool contain_window_function(Node *clause)
Definition: clauses.c:726
List * lappend(List *list, void *datum)
Definition: list.c:128
EquivalenceMember * right_em
Definition: relation.h:1688
Expr * clause
Definition: relation.h:1637
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:633
Index varno
Definition: primnodes.h:144
List * ec_opfamilies
Definition: relation.h:712
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
Relids nullable_relids
Definition: relation.h:1661
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:604
void * palloc0(Size size)
Definition: mcxt.c:920
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:653
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
void generate_base_implied_equalities(PlannerInfo *root)
Definition: equivclass.c:762
Relids em_relids
Definition: relation.h:763
bool have_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: equivclass.c:2300
unsigned int Index
Definition: c.h:362
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:977
EquivalenceClass * match_eclasses_to_foreign_key_col(PlannerInfo *root, ForeignKeyOptInfo *fkinfo, int colno)
Definition: equivclass.c:1990
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
void reconsider_outer_join_clauses(PlannerInfo *root)
Definition: equivclass.c:1532
#define Max(x, y)
Definition: c.h:796
#define makeNode(_type_)
Definition: nodes.h:556
Relids right_relids
Definition: relation.h:1665
bool process_equivalence(PlannerInfo *root, RestrictInfo *restrictinfo, bool below_outer_join)
Definition: equivclass.c:107
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:232
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:217
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:720
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2352
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:442
bool contain_agg_clause(Node *clause)
Definition: clauses.c:416
EquivalenceClass * left_ec
Definition: relation.h:1685
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:2100
void * arg
static void generate_base_implied_equalities_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:807
MemoryContext planner_cxt
Definition: relation.h:282
List * right_join_clauses
Definition: relation.h:240
#define elog
Definition: elog.h:219
bool contain_nonstrict_functions(Node *clause)
Definition: clauses.c:1234
Definition: regcomp.c:226
Definition: pg_list.h:45
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: relation.h:654
int16 AttrNumber
Definition: attnum.h:21
struct EquivalenceClass * ec_merged
Definition: relation.h:726
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:725
#define lfirst_oid(lc)
Definition: pg_list.h:108
List * ec_members
Definition: relation.h:714
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:130
List * generate_implied_equalities_for_column(PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, void *callback_arg, Relids prohibited_rels)
Definition: equivclass.c:2172