PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
relnode.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * relnode.c
4  * Relation-node lookup/construction routines
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/optimizer/util/relnode.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <limits.h>
18 
19 #include "miscadmin.h"
20 #include "optimizer/clauses.h"
21 #include "optimizer/cost.h"
22 #include "optimizer/pathnode.h"
23 #include "optimizer/paths.h"
24 #include "optimizer/placeholder.h"
25 #include "optimizer/plancat.h"
26 #include "optimizer/restrictinfo.h"
27 #include "optimizer/tlist.h"
28 #include "utils/hsearch.h"
29 
30 
31 typedef struct JoinHashEntry
32 {
33  Relids join_relids; /* hash key --- MUST BE FIRST */
36 
37 static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel,
38  RelOptInfo *input_rel);
40  RelOptInfo *joinrel,
41  RelOptInfo *outer_rel,
42  RelOptInfo *inner_rel);
43 static void build_joinrel_joinlist(RelOptInfo *joinrel,
44  RelOptInfo *outer_rel,
45  RelOptInfo *inner_rel);
47  List *joininfo_list,
48  List *new_restrictlist);
50  List *joininfo_list,
51  List *new_joininfo);
52 
53 
54 /*
55  * setup_simple_rel_arrays
56  * Prepare the arrays we use for quickly accessing base relations.
57  */
58 void
60 {
61  Index rti;
62  ListCell *lc;
63 
64  /* Arrays are accessed using RT indexes (1..N) */
65  root->simple_rel_array_size = list_length(root->parse->rtable) + 1;
66 
67  /* simple_rel_array is initialized to all NULLs */
68  root->simple_rel_array = (RelOptInfo **)
69  palloc0(root->simple_rel_array_size * sizeof(RelOptInfo *));
70 
71  /* simple_rte_array is an array equivalent of the rtable list */
72  root->simple_rte_array = (RangeTblEntry **)
73  palloc0(root->simple_rel_array_size * sizeof(RangeTblEntry *));
74  rti = 1;
75  foreach(lc, root->parse->rtable)
76  {
77  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
78 
79  root->simple_rte_array[rti++] = rte;
80  }
81 }
82 
83 /*
84  * build_simple_rel
85  * Construct a new RelOptInfo for a base relation or 'other' relation.
86  */
87 RelOptInfo *
88 build_simple_rel(PlannerInfo *root, int relid, RelOptKind reloptkind)
89 {
90  RelOptInfo *rel;
91  RangeTblEntry *rte;
92 
93  /* Rel should not exist already */
94  Assert(relid > 0 && relid < root->simple_rel_array_size);
95  if (root->simple_rel_array[relid] != NULL)
96  elog(ERROR, "rel %d already exists", relid);
97 
98  /* Fetch RTE for relation */
99  rte = root->simple_rte_array[relid];
100  Assert(rte != NULL);
101 
102  rel = makeNode(RelOptInfo);
103  rel->reloptkind = reloptkind;
104  rel->relids = bms_make_singleton(relid);
105  rel->rows = 0;
106  /* cheap startup cost is interesting iff not all tuples to be retrieved */
107  rel->consider_startup = (root->tuple_fraction > 0);
108  rel->consider_param_startup = false; /* might get changed later */
109  rel->consider_parallel = false; /* might get changed later */
111  rel->pathlist = NIL;
112  rel->ppilist = NIL;
113  rel->partial_pathlist = NIL;
115  rel->cheapest_total_path = NULL;
116  rel->cheapest_unique_path = NULL;
119  rel->lateral_relids = NULL;
120  rel->relid = relid;
121  rel->rtekind = rte->rtekind;
122  /* min_attr, max_attr, attr_needed, attr_widths are set below */
123  rel->lateral_vars = NIL;
124  rel->lateral_referencers = NULL;
125  rel->indexlist = NIL;
126  rel->pages = 0;
127  rel->tuples = 0;
128  rel->allvisfrac = 0;
129  rel->subroot = NULL;
130  rel->subplan_params = NIL;
131  rel->rel_parallel_workers = -1; /* set up in get_relation_info */
132  rel->serverid = InvalidOid;
133  rel->userid = rte->checkAsUser;
134  rel->useridiscurrent = false;
135  rel->fdwroutine = NULL;
136  rel->fdw_private = NULL;
137  rel->baserestrictinfo = NIL;
138  rel->baserestrictcost.startup = 0;
139  rel->baserestrictcost.per_tuple = 0;
140  rel->baserestrict_min_security = UINT_MAX;
141  rel->joininfo = NIL;
142  rel->has_eclass_joins = false;
143 
144  /* Check type of rtable entry */
145  switch (rte->rtekind)
146  {
147  case RTE_RELATION:
148  /* Table --- retrieve statistics from the system catalogs */
149  get_relation_info(root, rte->relid, rte->inh, rel);
150  break;
151  case RTE_SUBQUERY:
152  case RTE_FUNCTION:
153  case RTE_VALUES:
154  case RTE_CTE:
155 
156  /*
157  * Subquery, function, or values list --- set up attr range and
158  * arrays
159  *
160  * Note: 0 is included in range to support whole-row Vars
161  */
162  rel->min_attr = 0;
163  rel->max_attr = list_length(rte->eref->colnames);
164  rel->attr_needed = (Relids *)
165  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
166  rel->attr_widths = (int32 *)
167  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
168  break;
169  default:
170  elog(ERROR, "unrecognized RTE kind: %d",
171  (int) rte->rtekind);
172  break;
173  }
174 
175  /* Save the finished struct in the query's simple_rel_array */
176  root->simple_rel_array[relid] = rel;
177 
178  /*
179  * This is a convenient spot at which to note whether rels participating
180  * in the query have any securityQuals attached. If so, increase
181  * root->qual_security_level to ensure it's larger than the maximum
182  * security level needed for securityQuals.
183  */
184  if (rte->securityQuals)
186  list_length(rte->securityQuals));
187 
188  /*
189  * If this rel is an appendrel parent, recurse to build "other rel"
190  * RelOptInfos for its children. They are "other rels" because they are
191  * not in the main join tree, but we will need RelOptInfos to plan access
192  * to them.
193  */
194  if (rte->inh)
195  {
196  ListCell *l;
197 
198  foreach(l, root->append_rel_list)
199  {
200  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
201 
202  /* append_rel_list contains all append rels; ignore others */
203  if (appinfo->parent_relid != relid)
204  continue;
205 
206  (void) build_simple_rel(root, appinfo->child_relid,
208  }
209  }
210 
211  return rel;
212 }
213 
214 /*
215  * find_base_rel
216  * Find a base or other relation entry, which must already exist.
217  */
218 RelOptInfo *
219 find_base_rel(PlannerInfo *root, int relid)
220 {
221  RelOptInfo *rel;
222 
223  Assert(relid > 0);
224 
225  if (relid < root->simple_rel_array_size)
226  {
227  rel = root->simple_rel_array[relid];
228  if (rel)
229  return rel;
230  }
231 
232  elog(ERROR, "no relation entry for relid %d", relid);
233 
234  return NULL; /* keep compiler quiet */
235 }
236 
237 /*
238  * build_join_rel_hash
239  * Construct the auxiliary hash table for join relations.
240  */
241 static void
243 {
244  HTAB *hashtab;
245  HASHCTL hash_ctl;
246  ListCell *l;
247 
248  /* Create the hash table */
249  MemSet(&hash_ctl, 0, sizeof(hash_ctl));
250  hash_ctl.keysize = sizeof(Relids);
251  hash_ctl.entrysize = sizeof(JoinHashEntry);
252  hash_ctl.hash = bitmap_hash;
253  hash_ctl.match = bitmap_match;
254  hash_ctl.hcxt = CurrentMemoryContext;
255  hashtab = hash_create("JoinRelHashTable",
256  256L,
257  &hash_ctl,
259 
260  /* Insert all the already-existing joinrels */
261  foreach(l, root->join_rel_list)
262  {
263  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
264  JoinHashEntry *hentry;
265  bool found;
266 
267  hentry = (JoinHashEntry *) hash_search(hashtab,
268  &(rel->relids),
269  HASH_ENTER,
270  &found);
271  Assert(!found);
272  hentry->join_rel = rel;
273  }
274 
275  root->join_rel_hash = hashtab;
276 }
277 
278 /*
279  * find_join_rel
280  * Returns relation entry corresponding to 'relids' (a set of RT indexes),
281  * or NULL if none exists. This is for join relations.
282  */
283 RelOptInfo *
285 {
286  /*
287  * Switch to using hash lookup when list grows "too long". The threshold
288  * is arbitrary and is known only here.
289  */
290  if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
291  build_join_rel_hash(root);
292 
293  /*
294  * Use either hashtable lookup or linear search, as appropriate.
295  *
296  * Note: the seemingly redundant hashkey variable is used to avoid taking
297  * the address of relids; unless the compiler is exceedingly smart, doing
298  * so would force relids out of a register and thus probably slow down the
299  * list-search case.
300  */
301  if (root->join_rel_hash)
302  {
303  Relids hashkey = relids;
304  JoinHashEntry *hentry;
305 
306  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
307  &hashkey,
308  HASH_FIND,
309  NULL);
310  if (hentry)
311  return hentry->join_rel;
312  }
313  else
314  {
315  ListCell *l;
316 
317  foreach(l, root->join_rel_list)
318  {
319  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
320 
321  if (bms_equal(rel->relids, relids))
322  return rel;
323  }
324  }
325 
326  return NULL;
327 }
328 
329 /*
330  * build_join_rel
331  * Returns relation entry corresponding to the union of two given rels,
332  * creating a new relation entry if none already exists.
333  *
334  * 'joinrelids' is the Relids set that uniquely identifies the join
335  * 'outer_rel' and 'inner_rel' are relation nodes for the relations to be
336  * joined
337  * 'sjinfo': join context info
338  * 'restrictlist_ptr': result variable. If not NULL, *restrictlist_ptr
339  * receives the list of RestrictInfo nodes that apply to this
340  * particular pair of joinable relations.
341  *
342  * restrictlist_ptr makes the routine's API a little grotty, but it saves
343  * duplicated calculation of the restrictlist...
344  */
345 RelOptInfo *
347  Relids joinrelids,
348  RelOptInfo *outer_rel,
349  RelOptInfo *inner_rel,
350  SpecialJoinInfo *sjinfo,
351  List **restrictlist_ptr)
352 {
353  RelOptInfo *joinrel;
354  List *restrictlist;
355 
356  /*
357  * See if we already have a joinrel for this set of base rels.
358  */
359  joinrel = find_join_rel(root, joinrelids);
360 
361  if (joinrel)
362  {
363  /*
364  * Yes, so we only need to figure the restrictlist for this particular
365  * pair of component relations.
366  */
367  if (restrictlist_ptr)
368  *restrictlist_ptr = build_joinrel_restrictlist(root,
369  joinrel,
370  outer_rel,
371  inner_rel);
372  return joinrel;
373  }
374 
375  /*
376  * Nope, so make one.
377  */
378  joinrel = makeNode(RelOptInfo);
379  joinrel->reloptkind = RELOPT_JOINREL;
380  joinrel->relids = bms_copy(joinrelids);
381  joinrel->rows = 0;
382  /* cheap startup cost is interesting iff not all tuples to be retrieved */
383  joinrel->consider_startup = (root->tuple_fraction > 0);
384  joinrel->consider_param_startup = false;
385  joinrel->consider_parallel = false;
386  joinrel->reltarget = create_empty_pathtarget();
387  joinrel->pathlist = NIL;
388  joinrel->ppilist = NIL;
389  joinrel->partial_pathlist = NIL;
390  joinrel->cheapest_startup_path = NULL;
391  joinrel->cheapest_total_path = NULL;
392  joinrel->cheapest_unique_path = NULL;
394  /* init direct_lateral_relids from children; we'll finish it up below */
395  joinrel->direct_lateral_relids =
396  bms_union(outer_rel->direct_lateral_relids,
397  inner_rel->direct_lateral_relids);
398  joinrel->lateral_relids = min_join_parameterization(root, joinrel->relids,
399  outer_rel, inner_rel);
400  joinrel->relid = 0; /* indicates not a baserel */
401  joinrel->rtekind = RTE_JOIN;
402  joinrel->min_attr = 0;
403  joinrel->max_attr = 0;
404  joinrel->attr_needed = NULL;
405  joinrel->attr_widths = NULL;
406  joinrel->lateral_vars = NIL;
407  joinrel->lateral_referencers = NULL;
408  joinrel->indexlist = NIL;
409  joinrel->pages = 0;
410  joinrel->tuples = 0;
411  joinrel->allvisfrac = 0;
412  joinrel->subroot = NULL;
413  joinrel->subplan_params = NIL;
414  joinrel->rel_parallel_workers = -1;
415  joinrel->serverid = InvalidOid;
416  joinrel->userid = InvalidOid;
417  joinrel->useridiscurrent = false;
418  joinrel->fdwroutine = NULL;
419  joinrel->fdw_private = NULL;
420  joinrel->baserestrictinfo = NIL;
421  joinrel->baserestrictcost.startup = 0;
422  joinrel->baserestrictcost.per_tuple = 0;
423  joinrel->baserestrict_min_security = UINT_MAX;
424  joinrel->joininfo = NIL;
425  joinrel->has_eclass_joins = false;
426 
427  /*
428  * Set up foreign-join fields if outer and inner relation are foreign
429  * tables (or joins) belonging to the same server and assigned to the same
430  * user to check access permissions as. In addition to an exact match of
431  * userid, we allow the case where one side has zero userid (implying
432  * current user) and the other side has explicit userid that happens to
433  * equal the current user; but in that case, pushdown of the join is only
434  * valid for the current user. The useridiscurrent field records whether
435  * we had to make such an assumption for this join or any sub-join.
436  *
437  * Otherwise these fields are left invalid, so GetForeignJoinPaths will
438  * not be called for the join relation.
439  */
440  if (OidIsValid(outer_rel->serverid) &&
441  inner_rel->serverid == outer_rel->serverid)
442  {
443  if (inner_rel->userid == outer_rel->userid)
444  {
445  joinrel->serverid = outer_rel->serverid;
446  joinrel->userid = outer_rel->userid;
447  joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
448  joinrel->fdwroutine = outer_rel->fdwroutine;
449  }
450  else if (!OidIsValid(inner_rel->userid) &&
451  outer_rel->userid == GetUserId())
452  {
453  joinrel->serverid = outer_rel->serverid;
454  joinrel->userid = outer_rel->userid;
455  joinrel->useridiscurrent = true;
456  joinrel->fdwroutine = outer_rel->fdwroutine;
457  }
458  else if (!OidIsValid(outer_rel->userid) &&
459  inner_rel->userid == GetUserId())
460  {
461  joinrel->serverid = outer_rel->serverid;
462  joinrel->userid = inner_rel->userid;
463  joinrel->useridiscurrent = true;
464  joinrel->fdwroutine = outer_rel->fdwroutine;
465  }
466  }
467 
468  /*
469  * Create a new tlist containing just the vars that need to be output from
470  * this join (ie, are needed for higher joinclauses or final output).
471  *
472  * NOTE: the tlist order for a join rel will depend on which pair of outer
473  * and inner rels we first try to build it from. But the contents should
474  * be the same regardless.
475  */
476  build_joinrel_tlist(root, joinrel, outer_rel);
477  build_joinrel_tlist(root, joinrel, inner_rel);
478  add_placeholders_to_joinrel(root, joinrel, outer_rel, inner_rel);
479 
480  /*
481  * add_placeholders_to_joinrel also took care of adding the ph_lateral
482  * sets of any PlaceHolderVars computed here to direct_lateral_relids, so
483  * now we can finish computing that. This is much like the computation of
484  * the transitively-closed lateral_relids in min_join_parameterization,
485  * except that here we *do* have to consider the added PHVs.
486  */
487  joinrel->direct_lateral_relids =
488  bms_del_members(joinrel->direct_lateral_relids, joinrel->relids);
489  if (bms_is_empty(joinrel->direct_lateral_relids))
490  joinrel->direct_lateral_relids = NULL;
491 
492  /*
493  * Construct restrict and join clause lists for the new joinrel. (The
494  * caller might or might not need the restrictlist, but I need it anyway
495  * for set_joinrel_size_estimates().)
496  */
497  restrictlist = build_joinrel_restrictlist(root, joinrel,
498  outer_rel, inner_rel);
499  if (restrictlist_ptr)
500  *restrictlist_ptr = restrictlist;
501  build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
502 
503  /*
504  * This is also the right place to check whether the joinrel has any
505  * pending EquivalenceClass joins.
506  */
507  joinrel->has_eclass_joins = has_relevant_eclass_joinclause(root, joinrel);
508 
509  /*
510  * Set estimates of the joinrel's size.
511  */
512  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
513  sjinfo, restrictlist);
514 
515  /*
516  * Set the consider_parallel flag if this joinrel could potentially be
517  * scanned within a parallel worker. If this flag is false for either
518  * inner_rel or outer_rel, then it must be false for the joinrel also.
519  * Even if both are true, there might be parallel-restricted expressions
520  * in the targetlist or quals.
521  *
522  * Note that if there are more than two rels in this relation, they could
523  * be divided between inner_rel and outer_rel in any arbitrary way. We
524  * assume this doesn't matter, because we should hit all the same baserels
525  * and joinclauses while building up to this joinrel no matter which we
526  * take; therefore, we should make the same decision here however we get
527  * here.
528  */
529  if (inner_rel->consider_parallel && outer_rel->consider_parallel &&
530  is_parallel_safe(root, (Node *) restrictlist) &&
531  is_parallel_safe(root, (Node *) joinrel->reltarget->exprs))
532  joinrel->consider_parallel = true;
533 
534  /*
535  * Add the joinrel to the query's joinrel list, and store it into the
536  * auxiliary hashtable if there is one. NB: GEQO requires us to append
537  * the new joinrel to the end of the list!
538  */
539  root->join_rel_list = lappend(root->join_rel_list, joinrel);
540 
541  if (root->join_rel_hash)
542  {
543  JoinHashEntry *hentry;
544  bool found;
545 
546  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
547  &(joinrel->relids),
548  HASH_ENTER,
549  &found);
550  Assert(!found);
551  hentry->join_rel = joinrel;
552  }
553 
554  /*
555  * Also, if dynamic-programming join search is active, add the new joinrel
556  * to the appropriate sublist. Note: you might think the Assert on number
557  * of members should be for equality, but some of the level 1 rels might
558  * have been joinrels already, so we can only assert <=.
559  */
560  if (root->join_rel_level)
561  {
562  Assert(root->join_cur_level > 0);
563  Assert(root->join_cur_level <= bms_num_members(joinrel->relids));
564  root->join_rel_level[root->join_cur_level] =
565  lappend(root->join_rel_level[root->join_cur_level], joinrel);
566  }
567 
568  return joinrel;
569 }
570 
571 /*
572  * min_join_parameterization
573  *
574  * Determine the minimum possible parameterization of a joinrel, that is, the
575  * set of other rels it contains LATERAL references to. We save this value in
576  * the join's RelOptInfo. This function is split out of build_join_rel()
577  * because join_is_legal() needs the value to check a prospective join.
578  */
579 Relids
581  Relids joinrelids,
582  RelOptInfo *outer_rel,
583  RelOptInfo *inner_rel)
584 {
585  Relids result;
586 
587  /*
588  * Basically we just need the union of the inputs' lateral_relids, less
589  * whatever is already in the join.
590  *
591  * It's not immediately obvious that this is a valid way to compute the
592  * result, because it might seem that we're ignoring possible lateral refs
593  * of PlaceHolderVars that are due to be computed at the join but not in
594  * either input. However, because create_lateral_join_info() already
595  * charged all such PHV refs to each member baserel of the join, they'll
596  * be accounted for already in the inputs' lateral_relids. Likewise, we
597  * do not need to worry about doing transitive closure here, because that
598  * was already accounted for in the original baserel lateral_relids.
599  */
600  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
601  result = bms_del_members(result, joinrelids);
602 
603  /* Maintain invariant that result is exactly NULL if empty */
604  if (bms_is_empty(result))
605  result = NULL;
606 
607  return result;
608 }
609 
610 /*
611  * build_joinrel_tlist
612  * Builds a join relation's target list from an input relation.
613  * (This is invoked twice to handle the two input relations.)
614  *
615  * The join's targetlist includes all Vars of its member relations that
616  * will still be needed above the join. This subroutine adds all such
617  * Vars from the specified input rel's tlist to the join rel's tlist.
618  *
619  * We also compute the expected width of the join's output, making use
620  * of data that was cached at the baserel level by set_rel_width().
621  */
622 static void
624  RelOptInfo *input_rel)
625 {
626  Relids relids = joinrel->relids;
627  ListCell *vars;
628 
629  foreach(vars, input_rel->reltarget->exprs)
630  {
631  Var *var = (Var *) lfirst(vars);
632  RelOptInfo *baserel;
633  int ndx;
634 
635  /*
636  * Ignore PlaceHolderVars in the input tlists; we'll make our own
637  * decisions about whether to copy them.
638  */
639  if (IsA(var, PlaceHolderVar))
640  continue;
641 
642  /*
643  * Otherwise, anything in a baserel or joinrel targetlist ought to be
644  * a Var. (More general cases can only appear in appendrel child
645  * rels, which will never be seen here.)
646  */
647  if (!IsA(var, Var))
648  elog(ERROR, "unexpected node type in rel targetlist: %d",
649  (int) nodeTag(var));
650 
651  /* Get the Var's original base rel */
652  baserel = find_base_rel(root, var->varno);
653 
654  /* Is it still needed above this joinrel? */
655  ndx = var->varattno - baserel->min_attr;
656  if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
657  {
658  /* Yup, add it to the output */
659  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs, var);
660  /* Vars have cost zero, so no need to adjust reltarget->cost */
661  joinrel->reltarget->width += baserel->attr_widths[ndx];
662  }
663  }
664 }
665 
666 /*
667  * build_joinrel_restrictlist
668  * build_joinrel_joinlist
669  * These routines build lists of restriction and join clauses for a
670  * join relation from the joininfo lists of the relations it joins.
671  *
672  * These routines are separate because the restriction list must be
673  * built afresh for each pair of input sub-relations we consider, whereas
674  * the join list need only be computed once for any join RelOptInfo.
675  * The join list is fully determined by the set of rels making up the
676  * joinrel, so we should get the same results (up to ordering) from any
677  * candidate pair of sub-relations. But the restriction list is whatever
678  * is not handled in the sub-relations, so it depends on which
679  * sub-relations are considered.
680  *
681  * If a join clause from an input relation refers to base rels still not
682  * present in the joinrel, then it is still a join clause for the joinrel;
683  * we put it into the joininfo list for the joinrel. Otherwise,
684  * the clause is now a restrict clause for the joined relation, and we
685  * return it to the caller of build_joinrel_restrictlist() to be stored in
686  * join paths made from this pair of sub-relations. (It will not need to
687  * be considered further up the join tree.)
688  *
689  * In many case we will find the same RestrictInfos in both input
690  * relations' joinlists, so be careful to eliminate duplicates.
691  * Pointer equality should be a sufficient test for dups, since all
692  * the various joinlist entries ultimately refer to RestrictInfos
693  * pushed into them by distribute_restrictinfo_to_rels().
694  *
695  * 'joinrel' is a join relation node
696  * 'outer_rel' and 'inner_rel' are a pair of relations that can be joined
697  * to form joinrel.
698  *
699  * build_joinrel_restrictlist() returns a list of relevant restrictinfos,
700  * whereas build_joinrel_joinlist() stores its results in the joinrel's
701  * joininfo list. One or the other must accept each given clause!
702  *
703  * NB: Formerly, we made deep(!) copies of each input RestrictInfo to pass
704  * up to the join relation. I believe this is no longer necessary, because
705  * RestrictInfo nodes are no longer context-dependent. Instead, just include
706  * the original nodes in the lists made for the join relation.
707  */
708 static List *
710  RelOptInfo *joinrel,
711  RelOptInfo *outer_rel,
712  RelOptInfo *inner_rel)
713 {
714  List *result;
715 
716  /*
717  * Collect all the clauses that syntactically belong at this level,
718  * eliminating any duplicates (important since we will see many of the
719  * same clauses arriving from both input relations).
720  */
721  result = subbuild_joinrel_restrictlist(joinrel, outer_rel->joininfo, NIL);
722  result = subbuild_joinrel_restrictlist(joinrel, inner_rel->joininfo, result);
723 
724  /*
725  * Add on any clauses derived from EquivalenceClasses. These cannot be
726  * redundant with the clauses in the joininfo lists, so don't bother
727  * checking.
728  */
729  result = list_concat(result,
731  joinrel->relids,
732  outer_rel->relids,
733  inner_rel));
734 
735  return result;
736 }
737 
738 static void
740  RelOptInfo *outer_rel,
741  RelOptInfo *inner_rel)
742 {
743  List *result;
744 
745  /*
746  * Collect all the clauses that syntactically belong above this level,
747  * eliminating any duplicates (important since we will see many of the
748  * same clauses arriving from both input relations).
749  */
750  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
751  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
752 
753  joinrel->joininfo = result;
754 }
755 
756 static List *
758  List *joininfo_list,
759  List *new_restrictlist)
760 {
761  ListCell *l;
762 
763  foreach(l, joininfo_list)
764  {
765  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
766 
767  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
768  {
769  /*
770  * This clause becomes a restriction clause for the joinrel, since
771  * it refers to no outside rels. Add it to the list, being
772  * careful to eliminate duplicates. (Since RestrictInfo nodes in
773  * different joinlists will have been multiply-linked rather than
774  * copied, pointer equality should be a sufficient test.)
775  */
776  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
777  }
778  else
779  {
780  /*
781  * This clause is still a join clause at this level, so we ignore
782  * it in this routine.
783  */
784  }
785  }
786 
787  return new_restrictlist;
788 }
789 
790 static List *
792  List *joininfo_list,
793  List *new_joininfo)
794 {
795  ListCell *l;
796 
797  foreach(l, joininfo_list)
798  {
799  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
800 
801  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
802  {
803  /*
804  * This clause becomes a restriction clause for the joinrel, since
805  * it refers to no outside rels. So we can ignore it in this
806  * routine.
807  */
808  }
809  else
810  {
811  /*
812  * This clause is still a join clause at this level, so add it to
813  * the new joininfo list, being careful to eliminate duplicates.
814  * (Since RestrictInfo nodes in different joinlists will have been
815  * multiply-linked rather than copied, pointer equality should be
816  * a sufficient test.)
817  */
818  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
819  }
820  }
821 
822  return new_joininfo;
823 }
824 
825 
826 /*
827  * build_empty_join_rel
828  * Build a dummy join relation describing an empty set of base rels.
829  *
830  * This is used for queries with empty FROM clauses, such as "SELECT 2+2" or
831  * "INSERT INTO foo VALUES(...)". We don't try very hard to make the empty
832  * joinrel completely valid, since no real planning will be done with it ---
833  * we just need it to carry a simple Result path out of query_planner().
834  */
835 RelOptInfo *
837 {
838  RelOptInfo *joinrel;
839 
840  /* The dummy join relation should be the only one ... */
841  Assert(root->join_rel_list == NIL);
842 
843  joinrel = makeNode(RelOptInfo);
844  joinrel->reloptkind = RELOPT_JOINREL;
845  joinrel->relids = NULL; /* empty set */
846  joinrel->rows = 1; /* we produce one row for such cases */
847  joinrel->rtekind = RTE_JOIN;
848  joinrel->reltarget = create_empty_pathtarget();
849 
850  root->join_rel_list = lappend(root->join_rel_list, joinrel);
851 
852  return joinrel;
853 }
854 
855 
856 /*
857  * fetch_upper_rel
858  * Build a RelOptInfo describing some post-scan/join query processing,
859  * or return a pre-existing one if somebody already built it.
860  *
861  * An "upper" relation is identified by an UpperRelationKind and a Relids set.
862  * The meaning of the Relids set is not specified here, and very likely will
863  * vary for different relation kinds.
864  *
865  * Most of the fields in an upper-level RelOptInfo are not used and are not
866  * set here (though makeNode should ensure they're zeroes). We basically only
867  * care about fields that are of interest to add_path() and set_cheapest().
868  */
869 RelOptInfo *
871 {
872  RelOptInfo *upperrel;
873  ListCell *lc;
874 
875  /*
876  * For the moment, our indexing data structure is just a List for each
877  * relation kind. If we ever get so many of one kind that this stops
878  * working well, we can improve it. No code outside this function should
879  * assume anything about how to find a particular upperrel.
880  */
881 
882  /* If we already made this upperrel for the query, return it */
883  foreach(lc, root->upper_rels[kind])
884  {
885  upperrel = (RelOptInfo *) lfirst(lc);
886 
887  if (bms_equal(upperrel->relids, relids))
888  return upperrel;
889  }
890 
891  upperrel = makeNode(RelOptInfo);
892  upperrel->reloptkind = RELOPT_UPPER_REL;
893  upperrel->relids = bms_copy(relids);
894 
895  /* cheap startup cost is interesting iff not all tuples to be retrieved */
896  upperrel->consider_startup = (root->tuple_fraction > 0);
897  upperrel->consider_param_startup = false;
898  upperrel->consider_parallel = false; /* might get changed later */
899  upperrel->reltarget = create_empty_pathtarget();
900  upperrel->pathlist = NIL;
901  upperrel->cheapest_startup_path = NULL;
902  upperrel->cheapest_total_path = NULL;
903  upperrel->cheapest_unique_path = NULL;
904  upperrel->cheapest_parameterized_paths = NIL;
905 
906  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
907 
908  return upperrel;
909 }
910 
911 
912 /*
913  * find_childrel_appendrelinfo
914  * Get the AppendRelInfo associated with an appendrel child rel.
915  *
916  * This search could be eliminated by storing a link in child RelOptInfos,
917  * but for now it doesn't seem performance-critical. (Also, it might be
918  * difficult to maintain such a link during mutation of the append_rel_list.)
919  */
922 {
923  Index relid = rel->relid;
924  ListCell *lc;
925 
926  /* Should only be called on child rels */
928 
929  foreach(lc, root->append_rel_list)
930  {
931  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
932 
933  if (appinfo->child_relid == relid)
934  return appinfo;
935  }
936  /* should have found the entry ... */
937  elog(ERROR, "child rel %d not found in append_rel_list", relid);
938  return NULL; /* not reached */
939 }
940 
941 
942 /*
943  * find_childrel_top_parent
944  * Fetch the topmost appendrel parent rel of an appendrel child rel.
945  *
946  * Since appendrels can be nested, a child could have multiple levels of
947  * appendrel ancestors. This function locates the topmost ancestor,
948  * which will be a regular baserel not an otherrel.
949  */
950 RelOptInfo *
952 {
953  do
954  {
955  AppendRelInfo *appinfo = find_childrel_appendrelinfo(root, rel);
956  Index prelid = appinfo->parent_relid;
957 
958  /* traverse up to the parent rel, loop if it's also a child rel */
959  rel = find_base_rel(root, prelid);
960  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
961 
963 
964  return rel;
965 }
966 
967 
968 /*
969  * find_childrel_parents
970  * Compute the set of parent relids of an appendrel child rel.
971  *
972  * Since appendrels can be nested, a child could have multiple levels of
973  * appendrel ancestors. This function computes a Relids set of all the
974  * parent relation IDs.
975  */
976 Relids
978 {
979  Relids result = NULL;
980 
981  do
982  {
983  AppendRelInfo *appinfo = find_childrel_appendrelinfo(root, rel);
984  Index prelid = appinfo->parent_relid;
985 
986  result = bms_add_member(result, prelid);
987 
988  /* traverse up to the parent rel, loop if it's also a child rel */
989  rel = find_base_rel(root, prelid);
990  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
991 
993 
994  return result;
995 }
996 
997 
998 /*
999  * get_baserel_parampathinfo
1000  * Get the ParamPathInfo for a parameterized path for a base relation,
1001  * constructing one if we don't have one already.
1002  *
1003  * This centralizes estimating the rowcounts for parameterized paths.
1004  * We need to cache those to be sure we use the same rowcount for all paths
1005  * of the same parameterization for a given rel. This is also a convenient
1006  * place to determine which movable join clauses the parameterized path will
1007  * be responsible for evaluating.
1008  */
1009 ParamPathInfo *
1011  Relids required_outer)
1012 {
1013  ParamPathInfo *ppi;
1014  Relids joinrelids;
1015  List *pclauses;
1016  double rows;
1017  ListCell *lc;
1018 
1019  /* Unparameterized paths have no ParamPathInfo */
1020  if (bms_is_empty(required_outer))
1021  return NULL;
1022 
1023  Assert(!bms_overlap(baserel->relids, required_outer));
1024 
1025  /* If we already have a PPI for this parameterization, just return it */
1026  foreach(lc, baserel->ppilist)
1027  {
1028  ppi = (ParamPathInfo *) lfirst(lc);
1029  if (bms_equal(ppi->ppi_req_outer, required_outer))
1030  return ppi;
1031  }
1032 
1033  /*
1034  * Identify all joinclauses that are movable to this base rel given this
1035  * parameterization.
1036  */
1037  joinrelids = bms_union(baserel->relids, required_outer);
1038  pclauses = NIL;
1039  foreach(lc, baserel->joininfo)
1040  {
1041  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1042 
1043  if (join_clause_is_movable_into(rinfo,
1044  baserel->relids,
1045  joinrelids))
1046  pclauses = lappend(pclauses, rinfo);
1047  }
1048 
1049  /*
1050  * Add in joinclauses generated by EquivalenceClasses, too. (These
1051  * necessarily satisfy join_clause_is_movable_into.)
1052  */
1053  pclauses = list_concat(pclauses,
1055  joinrelids,
1056  required_outer,
1057  baserel));
1058 
1059  /* Estimate the number of rows returned by the parameterized scan */
1060  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1061 
1062  /* And now we can build the ParamPathInfo */
1063  ppi = makeNode(ParamPathInfo);
1064  ppi->ppi_req_outer = required_outer;
1065  ppi->ppi_rows = rows;
1066  ppi->ppi_clauses = pclauses;
1067  baserel->ppilist = lappend(baserel->ppilist, ppi);
1068 
1069  return ppi;
1070 }
1071 
1072 /*
1073  * get_joinrel_parampathinfo
1074  * Get the ParamPathInfo for a parameterized path for a join relation,
1075  * constructing one if we don't have one already.
1076  *
1077  * This centralizes estimating the rowcounts for parameterized paths.
1078  * We need to cache those to be sure we use the same rowcount for all paths
1079  * of the same parameterization for a given rel. This is also a convenient
1080  * place to determine which movable join clauses the parameterized path will
1081  * be responsible for evaluating.
1082  *
1083  * outer_path and inner_path are a pair of input paths that can be used to
1084  * construct the join, and restrict_clauses is the list of regular join
1085  * clauses (including clauses derived from EquivalenceClasses) that must be
1086  * applied at the join node when using these inputs.
1087  *
1088  * Unlike the situation for base rels, the set of movable join clauses to be
1089  * enforced at a join varies with the selected pair of input paths, so we
1090  * must calculate that and pass it back, even if we already have a matching
1091  * ParamPathInfo. We handle this by adding any clauses moved down to this
1092  * join to *restrict_clauses, which is an in/out parameter. (The addition
1093  * is done in such a way as to not modify the passed-in List structure.)
1094  *
1095  * Note: when considering a nestloop join, the caller must have removed from
1096  * restrict_clauses any movable clauses that are themselves scheduled to be
1097  * pushed into the right-hand path. We do not do that here since it's
1098  * unnecessary for other join types.
1099  */
1100 ParamPathInfo *
1102  Path *outer_path,
1103  Path *inner_path,
1104  SpecialJoinInfo *sjinfo,
1105  Relids required_outer,
1106  List **restrict_clauses)
1107 {
1108  ParamPathInfo *ppi;
1109  Relids join_and_req;
1110  Relids outer_and_req;
1111  Relids inner_and_req;
1112  List *pclauses;
1113  List *eclauses;
1114  List *dropped_ecs;
1115  double rows;
1116  ListCell *lc;
1117 
1118  /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1119  if (bms_is_empty(required_outer))
1120  return NULL;
1121 
1122  Assert(!bms_overlap(joinrel->relids, required_outer));
1123 
1124  /*
1125  * Identify all joinclauses that are movable to this join rel given this
1126  * parameterization. These are the clauses that are movable into this
1127  * join, but not movable into either input path. Treat an unparameterized
1128  * input path as not accepting parameterized clauses (because it won't,
1129  * per the shortcut exit above), even though the joinclause movement rules
1130  * might allow the same clauses to be moved into a parameterized path for
1131  * that rel.
1132  */
1133  join_and_req = bms_union(joinrel->relids, required_outer);
1134  if (outer_path->param_info)
1135  outer_and_req = bms_union(outer_path->parent->relids,
1136  PATH_REQ_OUTER(outer_path));
1137  else
1138  outer_and_req = NULL; /* outer path does not accept parameters */
1139  if (inner_path->param_info)
1140  inner_and_req = bms_union(inner_path->parent->relids,
1141  PATH_REQ_OUTER(inner_path));
1142  else
1143  inner_and_req = NULL; /* inner path does not accept parameters */
1144 
1145  pclauses = NIL;
1146  foreach(lc, joinrel->joininfo)
1147  {
1148  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1149 
1150  if (join_clause_is_movable_into(rinfo,
1151  joinrel->relids,
1152  join_and_req) &&
1154  outer_path->parent->relids,
1155  outer_and_req) &&
1157  inner_path->parent->relids,
1158  inner_and_req))
1159  pclauses = lappend(pclauses, rinfo);
1160  }
1161 
1162  /* Consider joinclauses generated by EquivalenceClasses, too */
1163  eclauses = generate_join_implied_equalities(root,
1164  join_and_req,
1165  required_outer,
1166  joinrel);
1167  /* We only want ones that aren't movable to lower levels */
1168  dropped_ecs = NIL;
1169  foreach(lc, eclauses)
1170  {
1171  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1172 
1173  /*
1174  * In principle, join_clause_is_movable_into() should accept anything
1175  * returned by generate_join_implied_equalities(); but because its
1176  * analysis is only approximate, sometimes it doesn't. So we
1177  * currently cannot use this Assert; instead just assume it's okay to
1178  * apply the joinclause at this level.
1179  */
1180 #ifdef NOT_USED
1182  joinrel->relids,
1183  join_and_req));
1184 #endif
1185  if (join_clause_is_movable_into(rinfo,
1186  outer_path->parent->relids,
1187  outer_and_req))
1188  continue; /* drop if movable into LHS */
1189  if (join_clause_is_movable_into(rinfo,
1190  inner_path->parent->relids,
1191  inner_and_req))
1192  {
1193  /* drop if movable into RHS, but remember EC for use below */
1194  Assert(rinfo->left_ec == rinfo->right_ec);
1195  dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1196  continue;
1197  }
1198  pclauses = lappend(pclauses, rinfo);
1199  }
1200 
1201  /*
1202  * EquivalenceClasses are harder to deal with than we could wish, because
1203  * of the fact that a given EC can generate different clauses depending on
1204  * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1205  * LHS and RHS of the current join and Z is in required_outer, and further
1206  * suppose that the inner_path is parameterized by both X and Z. The code
1207  * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1208  * and in the latter case will have discarded it as being movable into the
1209  * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1210  * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1211  * not have produced both, and we can't readily tell from here which one
1212  * it did pick. If we add no clause to this join, we'll end up with
1213  * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1214  * constrained to be equal to the other members of the EC. (When we come
1215  * to join Z to this X/Y path, we will certainly drop whichever EC clause
1216  * is generated at that join, so this omission won't get fixed later.)
1217  *
1218  * To handle this, for each EC we discarded such a clause from, try to
1219  * generate a clause connecting the required_outer rels to the join's LHS
1220  * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1221  * the clause can't be moved to the LHS, add it to the current join's
1222  * restriction clauses. (If an EC cannot generate such a clause then it
1223  * has nothing that needs to be enforced here, while if the clause can be
1224  * moved into the LHS then it should have been enforced within that path.)
1225  *
1226  * Note that we don't need similar processing for ECs whose clause was
1227  * considered to be movable into the LHS, because the LHS can't refer to
1228  * the RHS so there is no comparable ambiguity about what it might
1229  * actually be enforcing internally.
1230  */
1231  if (dropped_ecs)
1232  {
1233  Relids real_outer_and_req;
1234 
1235  real_outer_and_req = bms_union(outer_path->parent->relids,
1236  required_outer);
1237  eclauses =
1239  dropped_ecs,
1240  real_outer_and_req,
1241  required_outer,
1242  outer_path->parent);
1243  foreach(lc, eclauses)
1244  {
1245  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1246 
1247  /* As above, can't quite assert this here */
1248 #ifdef NOT_USED
1250  outer_path->parent->relids,
1251  real_outer_and_req));
1252 #endif
1253  if (!join_clause_is_movable_into(rinfo,
1254  outer_path->parent->relids,
1255  outer_and_req))
1256  pclauses = lappend(pclauses, rinfo);
1257  }
1258  }
1259 
1260  /*
1261  * Now, attach the identified moved-down clauses to the caller's
1262  * restrict_clauses list. By using list_concat in this order, we leave
1263  * the original list structure of restrict_clauses undamaged.
1264  */
1265  *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1266 
1267  /* If we already have a PPI for this parameterization, just return it */
1268  foreach(lc, joinrel->ppilist)
1269  {
1270  ppi = (ParamPathInfo *) lfirst(lc);
1271  if (bms_equal(ppi->ppi_req_outer, required_outer))
1272  return ppi;
1273  }
1274 
1275  /* Estimate the number of rows returned by the parameterized join */
1276  rows = get_parameterized_joinrel_size(root, joinrel,
1277  outer_path,
1278  inner_path,
1279  sjinfo,
1280  *restrict_clauses);
1281 
1282  /*
1283  * And now we can build the ParamPathInfo. No point in saving the
1284  * input-pair-dependent clause list, though.
1285  *
1286  * Note: in GEQO mode, we'll be called in a temporary memory context, but
1287  * the joinrel structure is there too, so no problem.
1288  */
1289  ppi = makeNode(ParamPathInfo);
1290  ppi->ppi_req_outer = required_outer;
1291  ppi->ppi_rows = rows;
1292  ppi->ppi_clauses = NIL;
1293  joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1294 
1295  return ppi;
1296 }
1297 
1298 /*
1299  * get_appendrel_parampathinfo
1300  * Get the ParamPathInfo for a parameterized path for an append relation.
1301  *
1302  * For an append relation, the rowcount estimate will just be the sum of
1303  * the estimates for its children. However, we still need a ParamPathInfo
1304  * to flag the fact that the path requires parameters. So this just creates
1305  * a suitable struct with zero ppi_rows (and no ppi_clauses either, since
1306  * the Append node isn't responsible for checking quals).
1307  */
1308 ParamPathInfo *
1309 get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
1310 {
1311  ParamPathInfo *ppi;
1312  ListCell *lc;
1313 
1314  /* Unparameterized paths have no ParamPathInfo */
1315  if (bms_is_empty(required_outer))
1316  return NULL;
1317 
1318  Assert(!bms_overlap(appendrel->relids, required_outer));
1319 
1320  /* If we already have a PPI for this parameterization, just return it */
1321  foreach(lc, appendrel->ppilist)
1322  {
1323  ppi = (ParamPathInfo *) lfirst(lc);
1324  if (bms_equal(ppi->ppi_req_outer, required_outer))
1325  return ppi;
1326  }
1327 
1328  /* Else build the ParamPathInfo */
1329  ppi = makeNode(ParamPathInfo);
1330  ppi->ppi_req_outer = required_outer;
1331  ppi->ppi_rows = 0;
1332  ppi->ppi_clauses = NIL;
1333  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1334 
1335  return ppi;
1336 }
bool has_eclass_joins
Definition: relation.h:551
struct Path * cheapest_unique_path
Definition: relation.h:509
RelOptInfo * find_childrel_top_parent(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:951
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:653
#define NIL
Definition: pg_list.h:69
int join_cur_level
Definition: relation.h:223
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
Query * parse
Definition: relation.h:152
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:110
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1010
RelOptKind reloptkind
Definition: relation.h:487
Relids * attr_needed
Definition: relation.h:523
Relids required_relids
Definition: relation.h:1655
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
List * colnames
Definition: primnodes.h:42
MemoryContext hcxt
Definition: hsearch.h:78
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:284
ParamPathInfo * get_joinrel_parampathinfo(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, Relids required_outer, List **restrict_clauses)
Definition: relnode.c:1101
Oid GetUserId(void)
Definition: miscinit.c:283
static List * subbuild_joinrel_restrictlist(RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
Definition: relnode.c:757
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:3851
struct Path * cheapest_startup_path
Definition: relation.h:507
Oid userid
Definition: relation.h:537
Relids join_relids
Definition: relnode.c:33
List * securityQuals
Definition: parsenodes.h:970
double tuples
Definition: relation.h:529
List * baserestrictinfo
Definition: relation.h:544
RelOptKind
Definition: relation.h:474
bool consider_param_startup
Definition: relation.h:497
ParamPathInfo * param_info
Definition: relation.h:897
Size entrysize
Definition: hsearch.h:73
Definition: nodes.h:508
List * partial_pathlist
Definition: relation.h:506
#define MemSet(start, val, len)
Definition: c.h:852
AttrNumber varattno
Definition: primnodes.h:146
List * join_rel_list
Definition: relation.h:212
List * list_concat(List *list1, List *list2)
Definition: list.c:321
EquivalenceClass * right_ec
Definition: relation.h:1686
List * cheapest_parameterized_paths
Definition: relation.h:510
Index baserestrict_min_security
Definition: relation.h:547
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
bool useridiscurrent
Definition: relation.h:538
UpperRelationKind
Definition: relation.h:71
Definition: primnodes.h:141
#define OidIsValid(objectId)
Definition: c.h:533
void add_placeholders_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: placeholder.c:411
Cost startup
Definition: relation.h:45
double allvisfrac
Definition: relation.h:530
signed int int32
Definition: c.h:253
struct RelOptInfo ** simple_rel_array
Definition: relation.h:176
AppendRelInfo * find_childrel_appendrelinfo(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:921
PlannerInfo * subroot
Definition: relation.h:531
bool consider_startup
Definition: relation.h:496
Definition: dynahash.c:193
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:1071
Relids lateral_relids
Definition: relation.h:515
Cost per_tuple
Definition: relation.h:46
double tuple_fraction
Definition: relation.h:286
struct JoinHashEntry JoinHashEntry
uint32 bitmap_hash(const void *key, Size keysize)
Definition: hashfn.c:76
List * rtable
Definition: parsenodes.h:128
#define ERROR
Definition: elog.h:43
static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel)
Definition: relnode.c:623
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:975
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1033
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:580
RelOptInfo * parent
Definition: relation.h:894
RelOptInfo * join_rel
Definition: relnode.c:34
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:870
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:575
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:3882
struct Path * cheapest_total_path
Definition: relation.h:508
static List * build_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:709
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:178
List * joininfo
Definition: relation.h:549
struct FdwRoutine * fdwroutine
Definition: relation.h:540
Relids relids
Definition: relation.h:490
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: hashfn.c:86
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:2354
int simple_rel_array_size
Definition: relation.h:177
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:514
List * ppilist
Definition: relation.h:505
Index relid
Definition: relation.h:518
Bitmapset * Relids
Definition: relation.h:28
List * lappend(List *list, void *datum)
Definition: list.c:128
RangeTblEntry ** simple_rte_array
Definition: relation.h:185
Relids lateral_referencers
Definition: relation.h:526
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:633
Index varno
Definition: primnodes.h:144
Oid serverid
Definition: relation.h:536
List * exprs
Definition: relation.h:824
Relids direct_lateral_relids
Definition: relation.h:514
void * palloc0(Size size)
Definition: mcxt.c:920
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1050
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
int rel_parallel_workers
Definition: relation.h:533
List * append_rel_list
Definition: relation.h:249
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:3801
Size keysize
Definition: hsearch.h:72
RelOptInfo * build_join_rel(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List **restrictlist_ptr)
Definition: relnode.c:346
unsigned int Index
Definition: c.h:361
HashCompareFunc match
Definition: hsearch.h:75
RTEKind rtekind
Definition: relation.h:520
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:977
List * indexlist
Definition: relation.h:527
double rows
Definition: relation.h:493
#define InvalidOid
Definition: postgres_ext.h:36
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:242
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptKind reloptkind)
Definition: relnode.c:88
void * fdw_private
Definition: relation.h:541
#define Max(x, y)
Definition: c.h:795
#define makeNode(_type_)
Definition: nodes.h:556
BlockNumber pages
Definition: relation.h:528
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
RelOptInfo * build_empty_join_rel(PlannerInfo *root)
Definition: relnode.c:836
List ** join_rel_level
Definition: relation.h:222
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:59
List * lateral_vars
Definition: relation.h:525
#define HASH_COMPARE
Definition: hsearch.h:90
#define PATH_REQ_OUTER(path)
Definition: relation.h:914
List * ppi_clauses
Definition: relation.h:855
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:217
static int list_length(const List *l)
Definition: pg_list.h:89
struct HTAB * join_rel_hash
Definition: relation.h:213
Index qual_security_level
Definition: relation.h:289
bool consider_parallel
Definition: relation.h:498
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
#define nodeTag(nodeptr)
Definition: nodes.h:513
double ppi_rows
Definition: relation.h:854
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:787
RTEKind rtekind
Definition: parsenodes.h:882
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:442
int width
Definition: relation.h:827
AttrNumber max_attr
Definition: relation.h:522
EquivalenceClass * left_ec
Definition: relation.h:1685
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:791
List * pathlist
Definition: relation.h:504
Relids ppi_req_outer
Definition: relation.h:853
#define elog
Definition: elog.h:219
Index child_relid
Definition: relation.h:1866
ParamPathInfo * get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
Definition: relnode.c:1309
Alias * eref
Definition: parsenodes.h:961
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:219
Index parent_relid
Definition: relation.h:1865
int32 * attr_widths
Definition: relation.h:524
Definition: regcomp.c:226
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:501
QualCost baserestrictcost
Definition: relation.h:546
List * subplan_params
Definition: relation.h:532
Definition: relation.h:888
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:739
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition: plancat.c:96
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:130
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:464
HashValueFunc hash
Definition: hsearch.h:74
#define HASH_FUNCTION
Definition: hsearch.h:89
List * upper_rels[UPPERREL_FINAL+1]
Definition: relation.h:267
AttrNumber min_attr
Definition: relation.h:521