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