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