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