PostgreSQL Source Code  git master
analyzejoins.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * analyzejoins.c
4  * Routines for simplifying joins after initial query analysis
5  *
6  * While we do a great deal of join simplification in prep/prepjointree.c,
7  * certain optimizations cannot be performed at that stage for lack of
8  * detailed information about the query. The routines here are invoked
9  * after initsplan.c has done its work, and can do additional join removal
10  * and simplification steps based on the information extracted. The penalty
11  * is that we have to work harder to clean up after ourselves when we modify
12  * the query, since the derived data structures have to be updated too.
13  *
14  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
15  * Portions Copyright (c) 1994, Regents of the University of California
16  *
17  *
18  * IDENTIFICATION
19  * src/backend/optimizer/plan/analyzejoins.c
20  *
21  *-------------------------------------------------------------------------
22  */
23 #include "postgres.h"
24 
25 #include "nodes/nodeFuncs.h"
26 #include "optimizer/clauses.h"
27 #include "optimizer/joininfo.h"
28 #include "optimizer/optimizer.h"
29 #include "optimizer/pathnode.h"
30 #include "optimizer/paths.h"
31 #include "optimizer/planmain.h"
32 #include "optimizer/restrictinfo.h"
33 #include "optimizer/tlist.h"
34 #include "utils/lsyscache.h"
35 
36 /* local functions */
37 static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo);
38 static void remove_rel_from_query(PlannerInfo *root, int relid, int ojrelid,
39  Relids joinrelids);
41  int relid, int ojrelid);
42 static List *remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved);
43 static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel);
44 static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel,
45  List *clause_list);
46 static Oid distinct_col_search(int colno, List *colnos, List *opids);
47 static bool is_innerrel_unique_for(PlannerInfo *root,
48  Relids joinrelids,
49  Relids outerrelids,
50  RelOptInfo *innerrel,
51  JoinType jointype,
52  List *restrictlist);
53 
54 
55 /*
56  * remove_useless_joins
57  * Check for relations that don't actually need to be joined at all,
58  * and remove them from the query.
59  *
60  * We are passed the current joinlist and return the updated list. Other
61  * data structures that have to be updated are accessible via "root".
62  */
63 List *
65 {
66  ListCell *lc;
67 
68  /*
69  * We are only interested in relations that are left-joined to, so we can
70  * scan the join_info_list to find them easily.
71  */
72 restart:
73  foreach(lc, root->join_info_list)
74  {
75  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
76  Relids joinrelids;
77  int innerrelid;
78  int nremoved;
79 
80  /* Skip if not removable */
81  if (!join_is_removable(root, sjinfo))
82  continue;
83 
84  /*
85  * Currently, join_is_removable can only succeed when the sjinfo's
86  * righthand is a single baserel. Remove that rel from the query and
87  * joinlist.
88  */
89  innerrelid = bms_singleton_member(sjinfo->min_righthand);
90 
91  /* Compute the relid set for the join we are considering */
92  joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
93  if (sjinfo->ojrelid != 0)
94  joinrelids = bms_add_member(joinrelids, sjinfo->ojrelid);
95 
96  remove_rel_from_query(root, innerrelid, sjinfo->ojrelid, joinrelids);
97 
98  /* We verify that exactly one reference gets removed from joinlist */
99  nremoved = 0;
100  joinlist = remove_rel_from_joinlist(joinlist, innerrelid, &nremoved);
101  if (nremoved != 1)
102  elog(ERROR, "failed to find relation %d in joinlist", innerrelid);
103 
104  /*
105  * We can delete this SpecialJoinInfo from the list too, since it's no
106  * longer of interest. (Since we'll restart the foreach loop
107  * immediately, we don't bother with foreach_delete_current.)
108  */
110 
111  /*
112  * Restart the scan. This is necessary to ensure we find all
113  * removable joins independently of ordering of the join_info_list
114  * (note that removal of attr_needed bits may make a join appear
115  * removable that did not before).
116  */
117  goto restart;
118  }
119 
120  return joinlist;
121 }
122 
123 /*
124  * clause_sides_match_join
125  * Determine whether a join clause is of the right form to use in this join.
126  *
127  * We already know that the clause is a binary opclause referencing only the
128  * rels in the current join. The point here is to check whether it has the
129  * form "outerrel_expr op innerrel_expr" or "innerrel_expr op outerrel_expr",
130  * rather than mixing outer and inner vars on either side. If it matches,
131  * we set the transient flag outer_is_left to identify which side is which.
132  */
133 static inline bool
135  Relids innerrelids)
136 {
137  if (bms_is_subset(rinfo->left_relids, outerrelids) &&
138  bms_is_subset(rinfo->right_relids, innerrelids))
139  {
140  /* lefthand side is outer */
141  rinfo->outer_is_left = true;
142  return true;
143  }
144  else if (bms_is_subset(rinfo->left_relids, innerrelids) &&
145  bms_is_subset(rinfo->right_relids, outerrelids))
146  {
147  /* righthand side is outer */
148  rinfo->outer_is_left = false;
149  return true;
150  }
151  return false; /* no good for these input relations */
152 }
153 
154 /*
155  * join_is_removable
156  * Check whether we need not perform this special join at all, because
157  * it will just duplicate its left input.
158  *
159  * This is true for a left join for which the join condition cannot match
160  * more than one inner-side row. (There are other possibly interesting
161  * cases, but we don't have the infrastructure to prove them.) We also
162  * have to check that the inner side doesn't generate any variables needed
163  * above the join.
164  */
165 static bool
167 {
168  int innerrelid;
169  RelOptInfo *innerrel;
170  Relids inputrelids;
171  Relids joinrelids;
172  List *clause_list = NIL;
173  ListCell *l;
174  int attroff;
175 
176  /*
177  * Must be a left join to a single baserel, else we aren't going to be
178  * able to do anything with it.
179  */
180  if (sjinfo->jointype != JOIN_LEFT)
181  return false;
182 
183  if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
184  return false;
185 
186  /*
187  * Never try to eliminate a left join to the query result rel. Although
188  * the case is syntactically impossible in standard SQL, MERGE will build
189  * a join tree that looks exactly like that.
190  */
191  if (innerrelid == root->parse->resultRelation)
192  return false;
193 
194  innerrel = find_base_rel(root, innerrelid);
195 
196  /*
197  * Before we go to the effort of checking whether any innerrel variables
198  * are needed above the join, make a quick check to eliminate cases in
199  * which we will surely be unable to prove uniqueness of the innerrel.
200  */
201  if (!rel_supports_distinctness(root, innerrel))
202  return false;
203 
204  /* Compute the relid set for the join we are considering */
205  inputrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
206  Assert(sjinfo->ojrelid != 0);
207  joinrelids = bms_copy(inputrelids);
208  joinrelids = bms_add_member(joinrelids, sjinfo->ojrelid);
209 
210  /*
211  * We can't remove the join if any inner-rel attributes are used above the
212  * join. Here, "above" the join includes pushed-down conditions, so we
213  * should reject if attr_needed includes the OJ's own relid; therefore,
214  * compare to inputrelids not joinrelids.
215  *
216  * As a micro-optimization, it seems better to start with max_attr and
217  * count down rather than starting with min_attr and counting up, on the
218  * theory that the system attributes are somewhat less likely to be wanted
219  * and should be tested last.
220  */
221  for (attroff = innerrel->max_attr - innerrel->min_attr;
222  attroff >= 0;
223  attroff--)
224  {
225  if (!bms_is_subset(innerrel->attr_needed[attroff], inputrelids))
226  return false;
227  }
228 
229  /*
230  * Similarly check that the inner rel isn't needed by any PlaceHolderVars
231  * that will be used above the join. The PHV case is a little bit more
232  * complicated, because PHVs may have been assigned a ph_eval_at location
233  * that includes the innerrel, yet their contained expression might not
234  * actually reference the innerrel (it could be just a constant, for
235  * instance). If such a PHV is due to be evaluated above the join then it
236  * needn't prevent join removal.
237  */
238  foreach(l, root->placeholder_list)
239  {
240  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
241 
242  if (bms_overlap(phinfo->ph_lateral, innerrel->relids))
243  return false; /* it references innerrel laterally */
244  if (!bms_overlap(phinfo->ph_eval_at, innerrel->relids))
245  continue; /* it definitely doesn't reference innerrel */
246  if (bms_is_subset(phinfo->ph_needed, inputrelids))
247  continue; /* PHV is not used above the join */
248  if (!bms_is_member(sjinfo->ojrelid, phinfo->ph_eval_at))
249  return false; /* it has to be evaluated below the join */
250 
251  /*
252  * We need to be sure there will still be a place to evaluate the PHV
253  * if we remove the join, ie that ph_eval_at wouldn't become empty.
254  */
255  if (!bms_overlap(sjinfo->min_lefthand, phinfo->ph_eval_at))
256  return false; /* there isn't any other place to eval PHV */
257  /* Check contained expression last, since this is a bit expensive */
258  if (bms_overlap(pull_varnos(root, (Node *) phinfo->ph_var->phexpr),
259  innerrel->relids))
260  return false; /* contained expression references innerrel */
261  }
262 
263  /*
264  * Search for mergejoinable clauses that constrain the inner rel against
265  * either the outer rel or a pseudoconstant. If an operator is
266  * mergejoinable then it behaves like equality for some btree opclass, so
267  * it's what we want. The mergejoinability test also eliminates clauses
268  * containing volatile functions, which we couldn't depend on.
269  */
270  foreach(l, innerrel->joininfo)
271  {
272  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
273 
274  /*
275  * If the current join commutes with some other outer join(s) via
276  * outer join identity 3, there will be multiple clones of its join
277  * clauses in the joininfo list. We want to consider only the
278  * has_clone form of such clauses. Processing more than one form
279  * would be wasteful, and also some of the others would confuse the
280  * RINFO_IS_PUSHED_DOWN test below.
281  */
282  if (restrictinfo->is_clone)
283  continue; /* ignore it */
284 
285  /*
286  * If it's not a join clause for this outer join, we can't use it.
287  * Note that if the clause is pushed-down, then it is logically from
288  * above the outer join, even if it references no other rels (it might
289  * be from WHERE, for example).
290  */
291  if (RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
292  continue; /* ignore; not useful here */
293 
294  /* Ignore if it's not a mergejoinable clause */
295  if (!restrictinfo->can_join ||
296  restrictinfo->mergeopfamilies == NIL)
297  continue; /* not mergejoinable */
298 
299  /*
300  * Check if clause has the form "outer op inner" or "inner op outer",
301  * and if so mark which side is inner.
302  */
303  if (!clause_sides_match_join(restrictinfo, sjinfo->min_lefthand,
304  innerrel->relids))
305  continue; /* no good for these input relations */
306 
307  /* OK, add to list */
308  clause_list = lappend(clause_list, restrictinfo);
309  }
310 
311  /*
312  * Now that we have the relevant equality join clauses, try to prove the
313  * innerrel distinct.
314  */
315  if (rel_is_distinct_for(root, innerrel, clause_list))
316  return true;
317 
318  /*
319  * Some day it would be nice to check for other methods of establishing
320  * distinctness.
321  */
322  return false;
323 }
324 
325 
326 /*
327  * Remove the target relid from the planner's data structures, having
328  * determined that there is no need to include it in the query.
329  *
330  * We are not terribly thorough here. We only bother to update parts of
331  * the planner's data structures that will actually be consulted later.
332  */
333 static void
334 remove_rel_from_query(PlannerInfo *root, int relid, int ojrelid,
335  Relids joinrelids)
336 {
337  RelOptInfo *rel = find_base_rel(root, relid);
338  List *joininfos;
339  Index rti;
340  ListCell *l;
341 
342  /*
343  * Remove references to the rel from other baserels' attr_needed arrays.
344  */
345  for (rti = 1; rti < root->simple_rel_array_size; rti++)
346  {
347  RelOptInfo *otherrel = root->simple_rel_array[rti];
348  int attroff;
349 
350  /* there may be empty slots corresponding to non-baserel RTEs */
351  if (otherrel == NULL)
352  continue;
353 
354  Assert(otherrel->relid == rti); /* sanity check on array */
355 
356  /* no point in processing target rel itself */
357  if (otherrel == rel)
358  continue;
359 
360  for (attroff = otherrel->max_attr - otherrel->min_attr;
361  attroff >= 0;
362  attroff--)
363  {
364  otherrel->attr_needed[attroff] =
365  bms_del_member(otherrel->attr_needed[attroff], relid);
366  otherrel->attr_needed[attroff] =
367  bms_del_member(otherrel->attr_needed[attroff], ojrelid);
368  }
369  }
370 
371  /*
372  * Update all_baserels and related relid sets.
373  */
374  root->all_baserels = bms_del_member(root->all_baserels, relid);
375  root->outer_join_rels = bms_del_member(root->outer_join_rels, ojrelid);
376  root->all_query_rels = bms_del_member(root->all_query_rels, relid);
377  root->all_query_rels = bms_del_member(root->all_query_rels, ojrelid);
378 
379  /*
380  * Likewise remove references from SpecialJoinInfo data structures.
381  *
382  * This is relevant in case the outer join we're deleting is nested inside
383  * other outer joins: the upper joins' relid sets have to be adjusted. The
384  * RHS of the target outer join will be made empty here, but that's OK
385  * since caller will delete that SpecialJoinInfo entirely.
386  */
387  foreach(l, root->join_info_list)
388  {
389  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
390 
391  sjinfo->min_lefthand = bms_del_member(sjinfo->min_lefthand, relid);
392  sjinfo->min_righthand = bms_del_member(sjinfo->min_righthand, relid);
393  sjinfo->syn_lefthand = bms_del_member(sjinfo->syn_lefthand, relid);
394  sjinfo->syn_righthand = bms_del_member(sjinfo->syn_righthand, relid);
395  sjinfo->min_lefthand = bms_del_member(sjinfo->min_lefthand, ojrelid);
396  sjinfo->min_righthand = bms_del_member(sjinfo->min_righthand, ojrelid);
397  sjinfo->syn_lefthand = bms_del_member(sjinfo->syn_lefthand, ojrelid);
398  sjinfo->syn_righthand = bms_del_member(sjinfo->syn_righthand, ojrelid);
399  /* relid cannot appear in these fields, but ojrelid can: */
400  sjinfo->commute_above_l = bms_del_member(sjinfo->commute_above_l, ojrelid);
401  sjinfo->commute_above_r = bms_del_member(sjinfo->commute_above_r, ojrelid);
402  sjinfo->commute_below = bms_del_member(sjinfo->commute_below, ojrelid);
403  }
404 
405  /*
406  * Likewise remove references from PlaceHolderVar data structures,
407  * removing any no-longer-needed placeholders entirely.
408  *
409  * Removal is a bit trickier than it might seem: we can remove PHVs that
410  * are used at the target rel and/or in the join qual, but not those that
411  * are used at join partner rels or above the join. It's not that easy to
412  * distinguish PHVs used at partner rels from those used in the join qual,
413  * since they will both have ph_needed sets that are subsets of
414  * joinrelids. However, a PHV used at a partner rel could not have the
415  * target rel in ph_eval_at, so we check that while deciding whether to
416  * remove or just update the PHV. There is no corresponding test in
417  * join_is_removable because it doesn't need to distinguish those cases.
418  */
419  foreach(l, root->placeholder_list)
420  {
421  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
422 
423  Assert(!bms_is_member(relid, phinfo->ph_lateral));
424  if (bms_is_subset(phinfo->ph_needed, joinrelids) &&
425  bms_is_member(relid, phinfo->ph_eval_at))
426  {
428  l);
429  root->placeholder_array[phinfo->phid] = NULL;
430  }
431  else
432  {
433  PlaceHolderVar *phv = phinfo->ph_var;
434 
435  phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, relid);
436  phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, ojrelid);
437  Assert(!bms_is_empty(phinfo->ph_eval_at)); /* checked previously */
438  phinfo->ph_needed = bms_del_member(phinfo->ph_needed, relid);
439  phinfo->ph_needed = bms_del_member(phinfo->ph_needed, ojrelid);
440  /* ph_needed might or might not become empty */
441  phv->phrels = bms_del_member(phv->phrels, relid);
442  phv->phrels = bms_del_member(phv->phrels, ojrelid);
443  Assert(!bms_is_empty(phv->phrels));
444  Assert(phv->phnullingrels == NULL); /* no need to adjust */
445  }
446  }
447 
448  /*
449  * Remove any joinquals referencing the rel from the joininfo lists.
450  *
451  * In some cases, a joinqual has to be put back after deleting its
452  * reference to the target rel. This can occur for pseudoconstant and
453  * outerjoin-delayed quals, which can get marked as requiring the rel in
454  * order to force them to be evaluated at or above the join. We can't
455  * just discard them, though. Only quals that logically belonged to the
456  * outer join being discarded should be removed from the query.
457  *
458  * We must make a copy of the rel's old joininfo list before starting the
459  * loop, because otherwise remove_join_clause_from_rels would destroy the
460  * list while we're scanning it.
461  */
462  joininfos = list_copy(rel->joininfo);
463  foreach(l, joininfos)
464  {
465  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
466 
467  remove_join_clause_from_rels(root, rinfo, rinfo->required_relids);
468 
469  /*
470  * If the qual lists ojrelid in its required_relids, it must have come
471  * from above the outer join we're removing (so we need to keep it);
472  * if it does not, then it didn't and we can discard it.
473  */
474  if (bms_is_member(ojrelid, rinfo->required_relids))
475  {
476  /*
477  * There might be references to relid or ojrelid in the
478  * RestrictInfo, as a consequence of PHVs having ph_eval_at sets
479  * that include those. We already checked above that any such PHV
480  * is safe, so we can just drop those references.
481  */
482  remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
483  /* Now throw it back into the joininfo lists */
484  distribute_restrictinfo_to_rels(root, rinfo);
485  }
486  }
487 
488  /*
489  * There may be references to the rel in root->fkey_list, but if so,
490  * match_foreign_keys_to_quals() will get rid of them.
491  */
492 
493  /*
494  * Finally, remove the rel from the baserel array to prevent it from being
495  * referenced again. (We can't do this earlier because
496  * remove_join_clause_from_rels will touch it.)
497  */
498  root->simple_rel_array[relid] = NULL;
499 
500  /* And nuke the RelOptInfo, just in case there's another access path */
501  pfree(rel);
502 }
503 
504 /*
505  * Remove any references to relid or ojrelid from the RestrictInfo.
506  *
507  * We only bother to clean out bits in clause_relids and required_relids,
508  * not nullingrel bits in contained Vars and PHVs. (This might have to be
509  * improved sometime.) However, if the RestrictInfo contains an OR clause
510  * we have to also clean up the sub-clauses.
511  */
512 static void
513 remove_rel_from_restrictinfo(RestrictInfo *rinfo, int relid, int ojrelid)
514 {
515  /*
516  * The clause_relids probably aren't shared with anything else, but let's
517  * copy them just to be sure.
518  */
519  rinfo->clause_relids = bms_copy(rinfo->clause_relids);
520  rinfo->clause_relids = bms_del_member(rinfo->clause_relids, relid);
521  rinfo->clause_relids = bms_del_member(rinfo->clause_relids, ojrelid);
522  /* Likewise for required_relids */
523  rinfo->required_relids = bms_copy(rinfo->required_relids);
524  rinfo->required_relids = bms_del_member(rinfo->required_relids, relid);
525  rinfo->required_relids = bms_del_member(rinfo->required_relids, ojrelid);
526 
527  /* If it's an OR, recurse to clean up sub-clauses */
528  if (restriction_is_or_clause(rinfo))
529  {
530  ListCell *lc;
531 
532  Assert(is_orclause(rinfo->orclause));
533  foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
534  {
535  Node *orarg = (Node *) lfirst(lc);
536 
537  /* OR arguments should be ANDs or sub-RestrictInfos */
538  if (is_andclause(orarg))
539  {
540  List *andargs = ((BoolExpr *) orarg)->args;
541  ListCell *lc2;
542 
543  foreach(lc2, andargs)
544  {
545  RestrictInfo *rinfo2 = lfirst_node(RestrictInfo, lc2);
546 
547  remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
548  }
549  }
550  else
551  {
552  RestrictInfo *rinfo2 = castNode(RestrictInfo, orarg);
553 
554  remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
555  }
556  }
557  }
558 }
559 
560 /*
561  * Remove any occurrences of the target relid from a joinlist structure.
562  *
563  * It's easiest to build a whole new list structure, so we handle it that
564  * way. Efficiency is not a big deal here.
565  *
566  * *nremoved is incremented by the number of occurrences removed (there
567  * should be exactly one, but the caller checks that).
568  */
569 static List *
570 remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
571 {
572  List *result = NIL;
573  ListCell *jl;
574 
575  foreach(jl, joinlist)
576  {
577  Node *jlnode = (Node *) lfirst(jl);
578 
579  if (IsA(jlnode, RangeTblRef))
580  {
581  int varno = ((RangeTblRef *) jlnode)->rtindex;
582 
583  if (varno == relid)
584  (*nremoved)++;
585  else
586  result = lappend(result, jlnode);
587  }
588  else if (IsA(jlnode, List))
589  {
590  /* Recurse to handle subproblem */
591  List *sublist;
592 
593  sublist = remove_rel_from_joinlist((List *) jlnode,
594  relid, nremoved);
595  /* Avoid including empty sub-lists in the result */
596  if (sublist)
597  result = lappend(result, sublist);
598  }
599  else
600  {
601  elog(ERROR, "unrecognized joinlist node type: %d",
602  (int) nodeTag(jlnode));
603  }
604  }
605 
606  return result;
607 }
608 
609 
610 /*
611  * reduce_unique_semijoins
612  * Check for semijoins that can be simplified to plain inner joins
613  * because the inner relation is provably unique for the join clauses.
614  *
615  * Ideally this would happen during reduce_outer_joins, but we don't have
616  * enough information at that point.
617  *
618  * To perform the strength reduction when applicable, we need only delete
619  * the semijoin's SpecialJoinInfo from root->join_info_list. (We don't
620  * bother fixing the join type attributed to it in the query jointree,
621  * since that won't be consulted again.)
622  */
623 void
625 {
626  ListCell *lc;
627 
628  /*
629  * Scan the join_info_list to find semijoins.
630  */
631  foreach(lc, root->join_info_list)
632  {
633  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
634  int innerrelid;
635  RelOptInfo *innerrel;
636  Relids joinrelids;
637  List *restrictlist;
638 
639  /*
640  * Must be a semijoin to a single baserel, else we aren't going to be
641  * able to do anything with it.
642  */
643  if (sjinfo->jointype != JOIN_SEMI)
644  continue;
645 
646  if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
647  continue;
648 
649  innerrel = find_base_rel(root, innerrelid);
650 
651  /*
652  * Before we trouble to run generate_join_implied_equalities, make a
653  * quick check to eliminate cases in which we will surely be unable to
654  * prove uniqueness of the innerrel.
655  */
656  if (!rel_supports_distinctness(root, innerrel))
657  continue;
658 
659  /* Compute the relid set for the join we are considering */
660  joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
661  Assert(sjinfo->ojrelid == 0); /* SEMI joins don't have RT indexes */
662 
663  /*
664  * Since we're only considering a single-rel RHS, any join clauses it
665  * has must be clauses linking it to the semijoin's min_lefthand. We
666  * can also consider EC-derived join clauses.
667  */
668  restrictlist =
670  joinrelids,
671  sjinfo->min_lefthand,
672  innerrel,
673  0),
674  innerrel->joininfo);
675 
676  /* Test whether the innerrel is unique for those clauses. */
677  if (!innerrel_is_unique(root,
678  joinrelids, sjinfo->min_lefthand, innerrel,
679  JOIN_SEMI, restrictlist, true))
680  continue;
681 
682  /* OK, remove the SpecialJoinInfo from the list. */
684  }
685 }
686 
687 
688 /*
689  * rel_supports_distinctness
690  * Could the relation possibly be proven distinct on some set of columns?
691  *
692  * This is effectively a pre-checking function for rel_is_distinct_for().
693  * It must return true if rel_is_distinct_for() could possibly return true
694  * with this rel, but it should not expend a lot of cycles. The idea is
695  * that callers can avoid doing possibly-expensive processing to compute
696  * rel_is_distinct_for()'s argument lists if the call could not possibly
697  * succeed.
698  */
699 static bool
701 {
702  /* We only know about baserels ... */
703  if (rel->reloptkind != RELOPT_BASEREL)
704  return false;
705  if (rel->rtekind == RTE_RELATION)
706  {
707  /*
708  * For a plain relation, we only know how to prove uniqueness by
709  * reference to unique indexes. Make sure there's at least one
710  * suitable unique index. It must be immediately enforced, and if
711  * it's a partial index, it must match the query. (Keep these
712  * conditions in sync with relation_has_unique_index_for!)
713  */
714  ListCell *lc;
715 
716  foreach(lc, rel->indexlist)
717  {
718  IndexOptInfo *ind = (IndexOptInfo *) lfirst(lc);
719 
720  if (ind->unique && ind->immediate &&
721  (ind->indpred == NIL || ind->predOK))
722  return true;
723  }
724  }
725  else if (rel->rtekind == RTE_SUBQUERY)
726  {
727  Query *subquery = root->simple_rte_array[rel->relid]->subquery;
728 
729  /* Check if the subquery has any qualities that support distinctness */
730  if (query_supports_distinctness(subquery))
731  return true;
732  }
733  /* We have no proof rules for any other rtekinds. */
734  return false;
735 }
736 
737 /*
738  * rel_is_distinct_for
739  * Does the relation return only distinct rows according to clause_list?
740  *
741  * clause_list is a list of join restriction clauses involving this rel and
742  * some other one. Return true if no two rows emitted by this rel could
743  * possibly join to the same row of the other rel.
744  *
745  * The caller must have already determined that each condition is a
746  * mergejoinable equality with an expression in this relation on one side, and
747  * an expression not involving this relation on the other. The transient
748  * outer_is_left flag is used to identify which side references this relation:
749  * left side if outer_is_left is false, right side if it is true.
750  *
751  * Note that the passed-in clause_list may be destructively modified! This
752  * is OK for current uses, because the clause_list is built by the caller for
753  * the sole purpose of passing to this function.
754  */
755 static bool
756 rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list)
757 {
758  /*
759  * We could skip a couple of tests here if we assume all callers checked
760  * rel_supports_distinctness first, but it doesn't seem worth taking any
761  * risk for.
762  */
763  if (rel->reloptkind != RELOPT_BASEREL)
764  return false;
765  if (rel->rtekind == RTE_RELATION)
766  {
767  /*
768  * Examine the indexes to see if we have a matching unique index.
769  * relation_has_unique_index_for automatically adds any usable
770  * restriction clauses for the rel, so we needn't do that here.
771  */
772  if (relation_has_unique_index_for(root, rel, clause_list, NIL, NIL))
773  return true;
774  }
775  else if (rel->rtekind == RTE_SUBQUERY)
776  {
777  Index relid = rel->relid;
778  Query *subquery = root->simple_rte_array[relid]->subquery;
779  List *colnos = NIL;
780  List *opids = NIL;
781  ListCell *l;
782 
783  /*
784  * Build the argument lists for query_is_distinct_for: a list of
785  * output column numbers that the query needs to be distinct over, and
786  * a list of equality operators that the output columns need to be
787  * distinct according to.
788  *
789  * (XXX we are not considering restriction clauses attached to the
790  * subquery; is that worth doing?)
791  */
792  foreach(l, clause_list)
793  {
795  Oid op;
796  Var *var;
797 
798  /*
799  * Get the equality operator we need uniqueness according to.
800  * (This might be a cross-type operator and thus not exactly the
801  * same operator the subquery would consider; that's all right
802  * since query_is_distinct_for can resolve such cases.) The
803  * caller's mergejoinability test should have selected only
804  * OpExprs.
805  */
806  op = castNode(OpExpr, rinfo->clause)->opno;
807 
808  /* caller identified the inner side for us */
809  if (rinfo->outer_is_left)
810  var = (Var *) get_rightop(rinfo->clause);
811  else
812  var = (Var *) get_leftop(rinfo->clause);
813 
814  /*
815  * We may ignore any RelabelType node above the operand. (There
816  * won't be more than one, since eval_const_expressions() has been
817  * applied already.)
818  */
819  if (var && IsA(var, RelabelType))
820  var = (Var *) ((RelabelType *) var)->arg;
821 
822  /*
823  * If inner side isn't a Var referencing a subquery output column,
824  * this clause doesn't help us.
825  */
826  if (!var || !IsA(var, Var) ||
827  var->varno != relid || var->varlevelsup != 0)
828  continue;
829 
830  colnos = lappend_int(colnos, var->varattno);
831  opids = lappend_oid(opids, op);
832  }
833 
834  if (query_is_distinct_for(subquery, colnos, opids))
835  return true;
836  }
837  return false;
838 }
839 
840 
841 /*
842  * query_supports_distinctness - could the query possibly be proven distinct
843  * on some set of output columns?
844  *
845  * This is effectively a pre-checking function for query_is_distinct_for().
846  * It must return true if query_is_distinct_for() could possibly return true
847  * with this query, but it should not expend a lot of cycles. The idea is
848  * that callers can avoid doing possibly-expensive processing to compute
849  * query_is_distinct_for()'s argument lists if the call could not possibly
850  * succeed.
851  */
852 bool
854 {
855  /* SRFs break distinctness except with DISTINCT, see below */
856  if (query->hasTargetSRFs && query->distinctClause == NIL)
857  return false;
858 
859  /* check for features we can prove distinctness with */
860  if (query->distinctClause != NIL ||
861  query->groupClause != NIL ||
862  query->groupingSets != NIL ||
863  query->hasAggs ||
864  query->havingQual ||
865  query->setOperations)
866  return true;
867 
868  return false;
869 }
870 
871 /*
872  * query_is_distinct_for - does query never return duplicates of the
873  * specified columns?
874  *
875  * query is a not-yet-planned subquery (in current usage, it's always from
876  * a subquery RTE, which the planner avoids scribbling on).
877  *
878  * colnos is an integer list of output column numbers (resno's). We are
879  * interested in whether rows consisting of just these columns are certain
880  * to be distinct. "Distinctness" is defined according to whether the
881  * corresponding upper-level equality operators listed in opids would think
882  * the values are distinct. (Note: the opids entries could be cross-type
883  * operators, and thus not exactly the equality operators that the subquery
884  * would use itself. We use equality_ops_are_compatible() to check
885  * compatibility. That looks at btree or hash opfamily membership, and so
886  * should give trustworthy answers for all operators that we might need
887  * to deal with here.)
888  */
889 bool
890 query_is_distinct_for(Query *query, List *colnos, List *opids)
891 {
892  ListCell *l;
893  Oid opid;
894 
895  Assert(list_length(colnos) == list_length(opids));
896 
897  /*
898  * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
899  * columns in the DISTINCT clause appear in colnos and operator semantics
900  * match. This is true even if there are SRFs in the DISTINCT columns or
901  * elsewhere in the tlist.
902  */
903  if (query->distinctClause)
904  {
905  foreach(l, query->distinctClause)
906  {
907  SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
909  query->targetList);
910 
911  opid = distinct_col_search(tle->resno, colnos, opids);
912  if (!OidIsValid(opid) ||
913  !equality_ops_are_compatible(opid, sgc->eqop))
914  break; /* exit early if no match */
915  }
916  if (l == NULL) /* had matches for all? */
917  return true;
918  }
919 
920  /*
921  * Otherwise, a set-returning function in the query's targetlist can
922  * result in returning duplicate rows, despite any grouping that might
923  * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
924  * columns, it would be safe because they'd be expanded before grouping.
925  * But it doesn't currently seem worth the effort to check for that.)
926  */
927  if (query->hasTargetSRFs)
928  return false;
929 
930  /*
931  * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
932  * the grouped columns appear in colnos and operator semantics match.
933  */
934  if (query->groupClause && !query->groupingSets)
935  {
936  foreach(l, query->groupClause)
937  {
938  SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
940  query->targetList);
941 
942  opid = distinct_col_search(tle->resno, colnos, opids);
943  if (!OidIsValid(opid) ||
944  !equality_ops_are_compatible(opid, sgc->eqop))
945  break; /* exit early if no match */
946  }
947  if (l == NULL) /* had matches for all? */
948  return true;
949  }
950  else if (query->groupingSets)
951  {
952  /*
953  * If we have grouping sets with expressions, we probably don't have
954  * uniqueness and analysis would be hard. Punt.
955  */
956  if (query->groupClause)
957  return false;
958 
959  /*
960  * If we have no groupClause (therefore no grouping expressions), we
961  * might have one or many empty grouping sets. If there's just one,
962  * then we're returning only one row and are certainly unique. But
963  * otherwise, we know we're certainly not unique.
964  */
965  if (list_length(query->groupingSets) == 1 &&
966  ((GroupingSet *) linitial(query->groupingSets))->kind == GROUPING_SET_EMPTY)
967  return true;
968  else
969  return false;
970  }
971  else
972  {
973  /*
974  * If we have no GROUP BY, but do have aggregates or HAVING, then the
975  * result is at most one row so it's surely unique, for any operators.
976  */
977  if (query->hasAggs || query->havingQual)
978  return true;
979  }
980 
981  /*
982  * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
983  * except with ALL.
984  */
985  if (query->setOperations)
986  {
988 
989  Assert(topop->op != SETOP_NONE);
990 
991  if (!topop->all)
992  {
993  ListCell *lg;
994 
995  /* We're good if all the nonjunk output columns are in colnos */
996  lg = list_head(topop->groupClauses);
997  foreach(l, query->targetList)
998  {
999  TargetEntry *tle = (TargetEntry *) lfirst(l);
1000  SortGroupClause *sgc;
1001 
1002  if (tle->resjunk)
1003  continue; /* ignore resjunk columns */
1004 
1005  /* non-resjunk columns should have grouping clauses */
1006  Assert(lg != NULL);
1007  sgc = (SortGroupClause *) lfirst(lg);
1008  lg = lnext(topop->groupClauses, lg);
1009 
1010  opid = distinct_col_search(tle->resno, colnos, opids);
1011  if (!OidIsValid(opid) ||
1012  !equality_ops_are_compatible(opid, sgc->eqop))
1013  break; /* exit early if no match */
1014  }
1015  if (l == NULL) /* had matches for all? */
1016  return true;
1017  }
1018  }
1019 
1020  /*
1021  * XXX Are there any other cases in which we can easily see the result
1022  * must be distinct?
1023  *
1024  * If you do add more smarts to this function, be sure to update
1025  * query_supports_distinctness() to match.
1026  */
1027 
1028  return false;
1029 }
1030 
1031 /*
1032  * distinct_col_search - subroutine for query_is_distinct_for
1033  *
1034  * If colno is in colnos, return the corresponding element of opids,
1035  * else return InvalidOid. (Ordinarily colnos would not contain duplicates,
1036  * but if it does, we arbitrarily select the first match.)
1037  */
1038 static Oid
1039 distinct_col_search(int colno, List *colnos, List *opids)
1040 {
1041  ListCell *lc1,
1042  *lc2;
1043 
1044  forboth(lc1, colnos, lc2, opids)
1045  {
1046  if (colno == lfirst_int(lc1))
1047  return lfirst_oid(lc2);
1048  }
1049  return InvalidOid;
1050 }
1051 
1052 
1053 /*
1054  * innerrel_is_unique
1055  * Check if the innerrel provably contains at most one tuple matching any
1056  * tuple from the outerrel, based on join clauses in the 'restrictlist'.
1057  *
1058  * We need an actual RelOptInfo for the innerrel, but it's sufficient to
1059  * identify the outerrel by its Relids. This asymmetry supports use of this
1060  * function before joinrels have been built. (The caller is expected to
1061  * also supply the joinrelids, just to save recalculating that.)
1062  *
1063  * The proof must be made based only on clauses that will be "joinquals"
1064  * rather than "otherquals" at execution. For an inner join there's no
1065  * difference; but if the join is outer, we must ignore pushed-down quals,
1066  * as those will become "otherquals". Note that this means the answer might
1067  * vary depending on whether IS_OUTER_JOIN(jointype); since we cache the
1068  * answer without regard to that, callers must take care not to call this
1069  * with jointypes that would be classified differently by IS_OUTER_JOIN().
1070  *
1071  * The actual proof is undertaken by is_innerrel_unique_for(); this function
1072  * is a frontend that is mainly concerned with caching the answers.
1073  * In particular, the force_cache argument allows overriding the internal
1074  * heuristic about whether to cache negative answers; it should be "true"
1075  * if making an inquiry that is not part of the normal bottom-up join search
1076  * sequence.
1077  */
1078 bool
1080  Relids joinrelids,
1081  Relids outerrelids,
1082  RelOptInfo *innerrel,
1083  JoinType jointype,
1084  List *restrictlist,
1085  bool force_cache)
1086 {
1087  MemoryContext old_context;
1088  ListCell *lc;
1089 
1090  /* Certainly can't prove uniqueness when there are no joinclauses */
1091  if (restrictlist == NIL)
1092  return false;
1093 
1094  /*
1095  * Make a quick check to eliminate cases in which we will surely be unable
1096  * to prove uniqueness of the innerrel.
1097  */
1098  if (!rel_supports_distinctness(root, innerrel))
1099  return false;
1100 
1101  /*
1102  * Query the cache to see if we've managed to prove that innerrel is
1103  * unique for any subset of this outerrel. We don't need an exact match,
1104  * as extra outerrels can't make the innerrel any less unique (or more
1105  * formally, the restrictlist for a join to a superset outerrel must be a
1106  * superset of the conditions we successfully used before).
1107  */
1108  foreach(lc, innerrel->unique_for_rels)
1109  {
1110  Relids unique_for_rels = (Relids) lfirst(lc);
1111 
1112  if (bms_is_subset(unique_for_rels, outerrelids))
1113  return true; /* Success! */
1114  }
1115 
1116  /*
1117  * Conversely, we may have already determined that this outerrel, or some
1118  * superset thereof, cannot prove this innerrel to be unique.
1119  */
1120  foreach(lc, innerrel->non_unique_for_rels)
1121  {
1122  Relids unique_for_rels = (Relids) lfirst(lc);
1123 
1124  if (bms_is_subset(outerrelids, unique_for_rels))
1125  return false;
1126  }
1127 
1128  /* No cached information, so try to make the proof. */
1129  if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1130  jointype, restrictlist))
1131  {
1132  /*
1133  * Cache the positive result for future probes, being sure to keep it
1134  * in the planner_cxt even if we are working in GEQO.
1135  *
1136  * Note: one might consider trying to isolate the minimal subset of
1137  * the outerrels that proved the innerrel unique. But it's not worth
1138  * the trouble, because the planner builds up joinrels incrementally
1139  * and so we'll see the minimally sufficient outerrels before any
1140  * supersets of them anyway.
1141  */
1142  old_context = MemoryContextSwitchTo(root->planner_cxt);
1143  innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1144  bms_copy(outerrelids));
1145  MemoryContextSwitchTo(old_context);
1146 
1147  return true; /* Success! */
1148  }
1149  else
1150  {
1151  /*
1152  * None of the join conditions for outerrel proved innerrel unique, so
1153  * we can safely reject this outerrel or any subset of it in future
1154  * checks.
1155  *
1156  * However, in normal planning mode, caching this knowledge is totally
1157  * pointless; it won't be queried again, because we build up joinrels
1158  * from smaller to larger. It is useful in GEQO mode, where the
1159  * knowledge can be carried across successive planning attempts; and
1160  * it's likely to be useful when using join-search plugins, too. Hence
1161  * cache when join_search_private is non-NULL. (Yeah, that's a hack,
1162  * but it seems reasonable.)
1163  *
1164  * Also, allow callers to override that heuristic and force caching;
1165  * that's useful for reduce_unique_semijoins, which calls here before
1166  * the normal join search starts.
1167  */
1168  if (force_cache || root->join_search_private)
1169  {
1170  old_context = MemoryContextSwitchTo(root->planner_cxt);
1171  innerrel->non_unique_for_rels =
1172  lappend(innerrel->non_unique_for_rels,
1173  bms_copy(outerrelids));
1174  MemoryContextSwitchTo(old_context);
1175  }
1176 
1177  return false;
1178  }
1179 }
1180 
1181 /*
1182  * is_innerrel_unique_for
1183  * Check if the innerrel provably contains at most one tuple matching any
1184  * tuple from the outerrel, based on join clauses in the 'restrictlist'.
1185  */
1186 static bool
1188  Relids joinrelids,
1189  Relids outerrelids,
1190  RelOptInfo *innerrel,
1191  JoinType jointype,
1192  List *restrictlist)
1193 {
1194  List *clause_list = NIL;
1195  ListCell *lc;
1196 
1197  /*
1198  * Search for mergejoinable clauses that constrain the inner rel against
1199  * the outer rel. If an operator is mergejoinable then it behaves like
1200  * equality for some btree opclass, so it's what we want. The
1201  * mergejoinability test also eliminates clauses containing volatile
1202  * functions, which we couldn't depend on.
1203  */
1204  foreach(lc, restrictlist)
1205  {
1206  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1207 
1208  /*
1209  * As noted above, if it's a pushed-down clause and we're at an outer
1210  * join, we can't use it.
1211  */
1212  if (IS_OUTER_JOIN(jointype) &&
1213  RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
1214  continue;
1215 
1216  /* Ignore if it's not a mergejoinable clause */
1217  if (!restrictinfo->can_join ||
1218  restrictinfo->mergeopfamilies == NIL)
1219  continue; /* not mergejoinable */
1220 
1221  /*
1222  * Check if clause has the form "outer op inner" or "inner op outer",
1223  * and if so mark which side is inner.
1224  */
1225  if (!clause_sides_match_join(restrictinfo, outerrelids,
1226  innerrel->relids))
1227  continue; /* no good for these input relations */
1228 
1229  /* OK, add to list */
1230  clause_list = lappend(clause_list, restrictinfo);
1231  }
1232 
1233  /* Let rel_is_distinct_for() do the hard work */
1234  return rel_is_distinct_for(root, innerrel, clause_list);
1235 }
List * remove_useless_joins(PlannerInfo *root, List *joinlist)
Definition: analyzejoins.c:64
static bool clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids, Relids innerrelids)
Definition: analyzejoins.c:134
static void remove_rel_from_query(PlannerInfo *root, int relid, int ojrelid, Relids joinrelids)
Definition: analyzejoins.c:334
bool innerrel_is_unique(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
Definition: analyzejoins.c:570
bool query_is_distinct_for(Query *query, List *colnos, List *opids)
Definition: analyzejoins.c:890
static Oid distinct_col_search(int colno, List *colnos, List *opids)
bool query_supports_distinctness(Query *query)
Definition: analyzejoins.c:853
static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list)
Definition: analyzejoins.c:756
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:166
void reduce_unique_semijoins(PlannerInfo *root)
Definition: analyzejoins.c:624
static bool is_innerrel_unique_for(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist)
static void remove_rel_from_restrictinfo(RestrictInfo *rinfo, int relid, int ojrelid)
Definition: analyzejoins.c:513
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:700
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:332
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:596
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:444
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_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:792
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
unsigned int Index
Definition: c.h:598
#define OidIsValid(objectId)
Definition: c.h:759
#define ERROR
Definition: elog.h:39
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, Index ojrelid)
Definition: equivclass.c:1377
bool relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List *exprlist, List *oprlist)
Definition: indxpath.c:3491
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2596
void remove_join_clause_from_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition: joininfo.c:125
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
List * lappend_int(List *list, int datum)
Definition: list.c:356
List * lappend_oid(List *list, Oid datum)
Definition: list.c:374
List * list_copy(const List *oldlist)
Definition: list.c:1572
List * list_delete_cell(List *list, ListCell *cell)
Definition: list.c:840
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition: lsyscache.c:697
void pfree(void *pointer)
Definition: mcxt.c:1436
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:114
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 nodeTag(nodeptr)
Definition: nodes.h:133
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:347
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
JoinType
Definition: nodes.h:299
@ JOIN_SEMI
Definition: nodes.h:318
@ JOIN_LEFT
Definition: nodes.h:305
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
@ GROUPING_SET_EMPTY
Definition: parsenodes.h:1453
@ SETOP_NONE
Definition: parsenodes.h:1827
@ RTE_SUBQUERY
Definition: parsenodes.h:1015
@ RTE_RELATION
Definition: parsenodes.h:1014
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2664
Bitmapset * Relids
Definition: pathnodes.h:30
@ RELOPT_BASEREL
Definition: pathnodes.h:818
#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 NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:467
#define lfirst_int(lc)
Definition: pg_list.h:173
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
#define linitial(l)
Definition: pg_list.h:178
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:390
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:404
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:386
Definition: pg_list.h:54
Definition: nodes.h:129
Relids ph_lateral
Definition: pathnodes.h:3025
Relids ph_needed
Definition: pathnodes.h:3028
Relids ph_eval_at
Definition: pathnodes.h:3022
PlaceHolderVar * ph_var
Definition: pathnodes.h:3019
Relids phnullingrels
Definition: pathnodes.h:2734
int simple_rel_array_size
Definition: pathnodes.h:232
Relids all_query_rels
Definition: pathnodes.h:269
Relids outer_join_rels
Definition: pathnodes.h:261
List * placeholder_list
Definition: pathnodes.h:374
Query * parse
Definition: pathnodes.h:202
List * join_info_list
Definition: pathnodes.h:340
Relids all_baserels
Definition: pathnodes.h:255
Node * setOperations
Definition: parsenodes.h:217
List * groupClause
Definition: parsenodes.h:198
Node * havingQual
Definition: parsenodes.h:203
List * targetList
Definition: parsenodes.h:189
List * groupingSets
Definition: parsenodes.h:201
List * distinctClause
Definition: parsenodes.h:207
List * joininfo
Definition: pathnodes.h:976
Relids relids
Definition: pathnodes.h:862
Index relid
Definition: pathnodes.h:909
List * unique_for_rels
Definition: pathnodes.h:962
RelOptKind reloptkind
Definition: pathnodes.h:856
List * indexlist
Definition: pathnodes.h:929
List * non_unique_for_rels
Definition: pathnodes.h:964
AttrNumber max_attr
Definition: pathnodes.h:917
AttrNumber min_attr
Definition: pathnodes.h:915
RTEKind rtekind
Definition: pathnodes.h:913
Relids required_relids
Definition: pathnodes.h:2544
Expr * clause
Definition: pathnodes.h:2513
SetOperation op
Definition: parsenodes.h:1905
Relids commute_above_r
Definition: pathnodes.h:2836
Relids syn_lefthand
Definition: pathnodes.h:2831
Relids min_righthand
Definition: pathnodes.h:2830
Relids commute_above_l
Definition: pathnodes.h:2835
JoinType jointype
Definition: pathnodes.h:2833
Relids commute_below
Definition: pathnodes.h:2837
Relids min_lefthand
Definition: pathnodes.h:2829
Relids syn_righthand
Definition: pathnodes.h:2832
AttrNumber resno
Definition: primnodes.h:1733
Definition: primnodes.h:226
AttrNumber varattno
Definition: primnodes.h:238
int varno
Definition: primnodes.h:233
Index varlevelsup
Definition: primnodes.h:258
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:108