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