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