PostgreSQL Source Code  git master
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-2024, 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 "nodes/nodeFuncs.h"
21 #include "optimizer/appendinfo.h"
22 #include "optimizer/clauses.h"
23 #include "optimizer/cost.h"
24 #include "optimizer/inherit.h"
25 #include "optimizer/optimizer.h"
26 #include "optimizer/pathnode.h"
27 #include "optimizer/paths.h"
28 #include "optimizer/placeholder.h"
29 #include "optimizer/plancat.h"
30 #include "optimizer/restrictinfo.h"
31 #include "optimizer/tlist.h"
32 #include "parser/parse_relation.h"
33 #include "rewrite/rewriteManip.h"
34 #include "utils/hsearch.h"
35 #include "utils/lsyscache.h"
36 
37 
38 typedef struct JoinHashEntry
39 {
40  Relids join_relids; /* hash key --- MUST BE FIRST */
43 
44 static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel,
45  RelOptInfo *input_rel,
46  SpecialJoinInfo *sjinfo,
47  List *pushed_down_joins,
48  bool can_null);
50  RelOptInfo *joinrel,
51  RelOptInfo *outer_rel,
52  RelOptInfo *inner_rel,
53  SpecialJoinInfo *sjinfo);
54 static void build_joinrel_joinlist(RelOptInfo *joinrel,
55  RelOptInfo *outer_rel,
56  RelOptInfo *inner_rel);
58  RelOptInfo *joinrel,
59  RelOptInfo *input_rel,
60  Relids both_input_relids,
61  List *new_restrictlist);
63  List *joininfo_list,
64  List *new_joininfo);
65 static void set_foreign_rel_properties(RelOptInfo *joinrel,
66  RelOptInfo *outer_rel, RelOptInfo *inner_rel);
67 static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel);
69  RelOptInfo *joinrel,
70  RelOptInfo *outer_rel, RelOptInfo *inner_rel,
71  SpecialJoinInfo *sjinfo,
72  List *restrictlist);
73 static bool have_partkey_equi_join(PlannerInfo *root, RelOptInfo *joinrel,
74  RelOptInfo *rel1, RelOptInfo *rel2,
75  JoinType jointype, List *restrictlist);
76 static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel,
77  bool strict_op);
78 static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel,
79  RelOptInfo *outer_rel, RelOptInfo *inner_rel,
80  JoinType jointype);
81 static void build_child_join_reltarget(PlannerInfo *root,
82  RelOptInfo *parentrel,
83  RelOptInfo *childrel,
84  int nappinfos,
85  AppendRelInfo **appinfos);
86 
87 
88 /*
89  * setup_simple_rel_arrays
90  * Prepare the arrays we use for quickly accessing base relations
91  * and AppendRelInfos.
92  */
93 void
95 {
96  int size;
97  Index rti;
98  ListCell *lc;
99 
100  /* Arrays are accessed using RT indexes (1..N) */
101  size = list_length(root->parse->rtable) + 1;
102  root->simple_rel_array_size = size;
103 
104  /*
105  * simple_rel_array is initialized to all NULLs, since no RelOptInfos
106  * exist yet. It'll be filled by later calls to build_simple_rel().
107  */
108  root->simple_rel_array = (RelOptInfo **)
109  palloc0(size * sizeof(RelOptInfo *));
110 
111  /* simple_rte_array is an array equivalent of the rtable list */
112  root->simple_rte_array = (RangeTblEntry **)
113  palloc0(size * sizeof(RangeTblEntry *));
114  rti = 1;
115  foreach(lc, root->parse->rtable)
116  {
117  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
118 
119  root->simple_rte_array[rti++] = rte;
120  }
121 
122  /* append_rel_array is not needed if there are no AppendRelInfos */
123  if (root->append_rel_list == NIL)
124  {
125  root->append_rel_array = NULL;
126  return;
127  }
128 
129  root->append_rel_array = (AppendRelInfo **)
130  palloc0(size * sizeof(AppendRelInfo *));
131 
132  /*
133  * append_rel_array is filled with any already-existing AppendRelInfos,
134  * which currently could only come from UNION ALL flattening. We might
135  * add more later during inheritance expansion, but it's the
136  * responsibility of the expansion code to update the array properly.
137  */
138  foreach(lc, root->append_rel_list)
139  {
140  AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
141  int child_relid = appinfo->child_relid;
142 
143  /* Sanity check */
144  Assert(child_relid < size);
145 
146  if (root->append_rel_array[child_relid])
147  elog(ERROR, "child relation already exists");
148 
149  root->append_rel_array[child_relid] = appinfo;
150  }
151 }
152 
153 /*
154  * expand_planner_arrays
155  * Expand the PlannerInfo's per-RTE arrays by add_size members
156  * and initialize the newly added entries to NULLs
157  *
158  * Note: this causes the append_rel_array to become allocated even if
159  * it was not before. This is okay for current uses, because we only call
160  * this when adding child relations, which always have AppendRelInfos.
161  */
162 void
164 {
165  int new_size;
166 
167  Assert(add_size > 0);
168 
169  new_size = root->simple_rel_array_size + add_size;
170 
171  root->simple_rel_array =
172  repalloc0_array(root->simple_rel_array, RelOptInfo *, root->simple_rel_array_size, new_size);
173 
174  root->simple_rte_array =
175  repalloc0_array(root->simple_rte_array, RangeTblEntry *, root->simple_rel_array_size, new_size);
176 
177  if (root->append_rel_array)
178  root->append_rel_array =
179  repalloc0_array(root->append_rel_array, AppendRelInfo *, root->simple_rel_array_size, new_size);
180  else
181  root->append_rel_array =
182  palloc0_array(AppendRelInfo *, new_size);
183 
184  root->simple_rel_array_size = new_size;
185 }
186 
187 /*
188  * build_simple_rel
189  * Construct a new RelOptInfo for a base relation or 'other' relation.
190  */
191 RelOptInfo *
192 build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
193 {
194  RelOptInfo *rel;
195  RangeTblEntry *rte;
196 
197  /* Rel should not exist already */
198  Assert(relid > 0 && relid < root->simple_rel_array_size);
199  if (root->simple_rel_array[relid] != NULL)
200  elog(ERROR, "rel %d already exists", relid);
201 
202  /* Fetch RTE for relation */
203  rte = root->simple_rte_array[relid];
204  Assert(rte != NULL);
205 
206  rel = makeNode(RelOptInfo);
208  rel->relids = bms_make_singleton(relid);
209  rel->rows = 0;
210  /* cheap startup cost is interesting iff not all tuples to be retrieved */
211  rel->consider_startup = (root->tuple_fraction > 0);
212  rel->consider_param_startup = false; /* might get changed later */
213  rel->consider_parallel = false; /* might get changed later */
215  rel->pathlist = NIL;
216  rel->ppilist = NIL;
217  rel->partial_pathlist = NIL;
218  rel->cheapest_startup_path = NULL;
219  rel->cheapest_total_path = NULL;
220  rel->cheapest_unique_path = NULL;
222  rel->relid = relid;
223  rel->rtekind = rte->rtekind;
224  /* min_attr, max_attr, attr_needed, attr_widths are set below */
225  rel->notnullattnums = NULL;
226  rel->lateral_vars = NIL;
227  rel->indexlist = NIL;
228  rel->statlist = NIL;
229  rel->pages = 0;
230  rel->tuples = 0;
231  rel->allvisfrac = 0;
232  rel->eclass_indexes = NULL;
233  rel->subroot = NULL;
234  rel->subplan_params = NIL;
235  rel->rel_parallel_workers = -1; /* set up in get_relation_info */
236  rel->amflags = 0;
237  rel->serverid = InvalidOid;
238  if (rte->rtekind == RTE_RELATION)
239  {
240  Assert(parent == NULL ||
241  parent->rtekind == RTE_RELATION ||
242  parent->rtekind == RTE_SUBQUERY);
243 
244  /*
245  * For any RELATION rte, we need a userid with which to check
246  * permission access. Baserels simply use their own
247  * RTEPermissionInfo's checkAsUser.
248  *
249  * For otherrels normally there's no RTEPermissionInfo, so we use the
250  * parent's, which normally has one. The exceptional case is that the
251  * parent is a subquery, in which case the otherrel will have its own.
252  */
253  if (rel->reloptkind == RELOPT_BASEREL ||
255  parent->rtekind == RTE_SUBQUERY))
256  {
257  RTEPermissionInfo *perminfo;
258 
259  perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
260  rel->userid = perminfo->checkAsUser;
261  }
262  else
263  rel->userid = parent->userid;
264  }
265  else
266  rel->userid = InvalidOid;
267  rel->useridiscurrent = false;
268  rel->fdwroutine = NULL;
269  rel->fdw_private = NULL;
270  rel->unique_for_rels = NIL;
271  rel->non_unique_for_rels = NIL;
272  rel->baserestrictinfo = NIL;
273  rel->baserestrictcost.startup = 0;
274  rel->baserestrictcost.per_tuple = 0;
275  rel->baserestrict_min_security = UINT_MAX;
276  rel->joininfo = NIL;
277  rel->has_eclass_joins = false;
278  rel->consider_partitionwise_join = false; /* might get changed later */
279  rel->part_scheme = NULL;
280  rel->nparts = -1;
281  rel->boundinfo = NULL;
282  rel->partbounds_merged = false;
283  rel->partition_qual = NIL;
284  rel->part_rels = NULL;
285  rel->live_parts = NULL;
286  rel->all_partrels = NULL;
287  rel->partexprs = NULL;
288  rel->nullable_partexprs = NULL;
289 
290  /*
291  * Pass assorted information down the inheritance hierarchy.
292  */
293  if (parent)
294  {
295  /* We keep back-links to immediate parent and topmost parent. */
296  rel->parent = parent;
297  rel->top_parent = parent->top_parent ? parent->top_parent : parent;
298  rel->top_parent_relids = rel->top_parent->relids;
299 
300  /*
301  * A child rel is below the same outer joins as its parent. (We
302  * presume this info was already calculated for the parent.)
303  */
304  rel->nulling_relids = parent->nulling_relids;
305 
306  /*
307  * Also propagate lateral-reference information from appendrel parent
308  * rels to their child rels. We intentionally give each child rel the
309  * same minimum parameterization, even though it's quite possible that
310  * some don't reference all the lateral rels. This is because any
311  * append path for the parent will have to have the same
312  * parameterization for every child anyway, and there's no value in
313  * forcing extra reparameterize_path() calls. Similarly, a lateral
314  * reference to the parent prevents use of otherwise-movable join rels
315  * for each child.
316  *
317  * It's possible for child rels to have their own children, in which
318  * case the topmost parent's lateral info propagates all the way down.
319  */
321  rel->lateral_relids = parent->lateral_relids;
323  }
324  else
325  {
326  rel->parent = NULL;
327  rel->top_parent = NULL;
328  rel->top_parent_relids = NULL;
329  rel->nulling_relids = NULL;
330  rel->direct_lateral_relids = NULL;
331  rel->lateral_relids = NULL;
332  rel->lateral_referencers = NULL;
333  }
334 
335  /* Check type of rtable entry */
336  switch (rte->rtekind)
337  {
338  case RTE_RELATION:
339  /* Table --- retrieve statistics from the system catalogs */
340  get_relation_info(root, rte->relid, rte->inh, rel);
341  break;
342  case RTE_SUBQUERY:
343  case RTE_FUNCTION:
344  case RTE_TABLEFUNC:
345  case RTE_VALUES:
346  case RTE_CTE:
347  case RTE_NAMEDTUPLESTORE:
348 
349  /*
350  * Subquery, function, tablefunc, values list, CTE, or ENR --- set
351  * up attr range and arrays
352  *
353  * Note: 0 is included in range to support whole-row Vars
354  */
355  rel->min_attr = 0;
356  rel->max_attr = list_length(rte->eref->colnames);
357  rel->attr_needed = (Relids *)
358  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
359  rel->attr_widths = (int32 *)
360  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
361  break;
362  case RTE_RESULT:
363  /* RTE_RESULT has no columns, nor could it have whole-row Var */
364  rel->min_attr = 0;
365  rel->max_attr = -1;
366  rel->attr_needed = NULL;
367  rel->attr_widths = NULL;
368  break;
369  default:
370  elog(ERROR, "unrecognized RTE kind: %d",
371  (int) rte->rtekind);
372  break;
373  }
374 
375  /*
376  * Copy the parent's quals to the child, with appropriate substitution of
377  * variables. If any constant false or NULL clauses turn up, we can mark
378  * the child as dummy right away. (We must do this immediately so that
379  * pruning works correctly when recursing in expand_partitioned_rtentry.)
380  */
381  if (parent)
382  {
383  AppendRelInfo *appinfo = root->append_rel_array[relid];
384 
385  Assert(appinfo != NULL);
386  if (!apply_child_basequals(root, parent, rel, rte, appinfo))
387  {
388  /*
389  * Some restriction clause reduced to constant FALSE or NULL after
390  * substitution, so this child need not be scanned.
391  */
392  mark_dummy_rel(rel);
393  }
394  }
395 
396  /* Save the finished struct in the query's simple_rel_array */
397  root->simple_rel_array[relid] = rel;
398 
399  return rel;
400 }
401 
402 /*
403  * find_base_rel
404  * Find a base or otherrel relation entry, which must already exist.
405  */
406 RelOptInfo *
407 find_base_rel(PlannerInfo *root, int relid)
408 {
409  RelOptInfo *rel;
410 
411  /* use an unsigned comparison to prevent negative array element access */
412  if ((uint32) relid < (uint32) root->simple_rel_array_size)
413  {
414  rel = root->simple_rel_array[relid];
415  if (rel)
416  return rel;
417  }
418 
419  elog(ERROR, "no relation entry for relid %d", relid);
420 
421  return NULL; /* keep compiler quiet */
422 }
423 
424 /*
425  * find_base_rel_noerr
426  * Find a base or otherrel relation entry, returning NULL if there's none
427  */
428 RelOptInfo *
430 {
431  /* use an unsigned comparison to prevent negative array element access */
432  if ((uint32) relid < (uint32) root->simple_rel_array_size)
433  return root->simple_rel_array[relid];
434  return NULL;
435 }
436 
437 /*
438  * find_base_rel_ignore_join
439  * Find a base or otherrel relation entry, which must already exist.
440  *
441  * Unlike find_base_rel, if relid references an outer join then this
442  * will return NULL rather than raising an error. This is convenient
443  * for callers that must deal with relid sets including both base and
444  * outer joins.
445  */
446 RelOptInfo *
448 {
449  /* use an unsigned comparison to prevent negative array element access */
450  if ((uint32) relid < (uint32) root->simple_rel_array_size)
451  {
452  RelOptInfo *rel;
453  RangeTblEntry *rte;
454 
455  rel = root->simple_rel_array[relid];
456  if (rel)
457  return rel;
458 
459  /*
460  * We could just return NULL here, but for debugging purposes it seems
461  * best to actually verify that the relid is an outer join and not
462  * something weird.
463  */
464  rte = root->simple_rte_array[relid];
465  if (rte && rte->rtekind == RTE_JOIN && rte->jointype != JOIN_INNER)
466  return NULL;
467  }
468 
469  elog(ERROR, "no relation entry for relid %d", relid);
470 
471  return NULL; /* keep compiler quiet */
472 }
473 
474 /*
475  * build_join_rel_hash
476  * Construct the auxiliary hash table for join relations.
477  */
478 static void
480 {
481  HTAB *hashtab;
482  HASHCTL hash_ctl;
483  ListCell *l;
484 
485  /* Create the hash table */
486  hash_ctl.keysize = sizeof(Relids);
487  hash_ctl.entrysize = sizeof(JoinHashEntry);
488  hash_ctl.hash = bitmap_hash;
489  hash_ctl.match = bitmap_match;
490  hash_ctl.hcxt = CurrentMemoryContext;
491  hashtab = hash_create("JoinRelHashTable",
492  256L,
493  &hash_ctl,
495 
496  /* Insert all the already-existing joinrels */
497  foreach(l, root->join_rel_list)
498  {
499  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
500  JoinHashEntry *hentry;
501  bool found;
502 
503  hentry = (JoinHashEntry *) hash_search(hashtab,
504  &(rel->relids),
505  HASH_ENTER,
506  &found);
507  Assert(!found);
508  hentry->join_rel = rel;
509  }
510 
511  root->join_rel_hash = hashtab;
512 }
513 
514 /*
515  * find_join_rel
516  * Returns relation entry corresponding to 'relids' (a set of RT indexes),
517  * or NULL if none exists. This is for join relations.
518  */
519 RelOptInfo *
521 {
522  /*
523  * Switch to using hash lookup when list grows "too long". The threshold
524  * is arbitrary and is known only here.
525  */
526  if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
527  build_join_rel_hash(root);
528 
529  /*
530  * Use either hashtable lookup or linear search, as appropriate.
531  *
532  * Note: the seemingly redundant hashkey variable is used to avoid taking
533  * the address of relids; unless the compiler is exceedingly smart, doing
534  * so would force relids out of a register and thus probably slow down the
535  * list-search case.
536  */
537  if (root->join_rel_hash)
538  {
539  Relids hashkey = relids;
540  JoinHashEntry *hentry;
541 
542  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
543  &hashkey,
544  HASH_FIND,
545  NULL);
546  if (hentry)
547  return hentry->join_rel;
548  }
549  else
550  {
551  ListCell *l;
552 
553  foreach(l, root->join_rel_list)
554  {
555  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
556 
557  if (bms_equal(rel->relids, relids))
558  return rel;
559  }
560  }
561 
562  return NULL;
563 }
564 
565 /*
566  * set_foreign_rel_properties
567  * Set up foreign-join fields if outer and inner relation are foreign
568  * tables (or joins) belonging to the same server and assigned to the same
569  * user to check access permissions as.
570  *
571  * In addition to an exact match of userid, we allow the case where one side
572  * has zero userid (implying current user) and the other side has explicit
573  * userid that happens to equal the current user; but in that case, pushdown of
574  * the join is only valid for the current user. The useridiscurrent field
575  * records whether we had to make such an assumption for this join or any
576  * sub-join.
577  *
578  * Otherwise these fields are left invalid, so GetForeignJoinPaths will not be
579  * called for the join relation.
580  */
581 static void
583  RelOptInfo *inner_rel)
584 {
585  if (OidIsValid(outer_rel->serverid) &&
586  inner_rel->serverid == outer_rel->serverid)
587  {
588  if (inner_rel->userid == outer_rel->userid)
589  {
590  joinrel->serverid = outer_rel->serverid;
591  joinrel->userid = outer_rel->userid;
592  joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
593  joinrel->fdwroutine = outer_rel->fdwroutine;
594  }
595  else if (!OidIsValid(inner_rel->userid) &&
596  outer_rel->userid == GetUserId())
597  {
598  joinrel->serverid = outer_rel->serverid;
599  joinrel->userid = outer_rel->userid;
600  joinrel->useridiscurrent = true;
601  joinrel->fdwroutine = outer_rel->fdwroutine;
602  }
603  else if (!OidIsValid(outer_rel->userid) &&
604  inner_rel->userid == GetUserId())
605  {
606  joinrel->serverid = outer_rel->serverid;
607  joinrel->userid = inner_rel->userid;
608  joinrel->useridiscurrent = true;
609  joinrel->fdwroutine = outer_rel->fdwroutine;
610  }
611  }
612 }
613 
614 /*
615  * add_join_rel
616  * Add given join relation to the list of join relations in the given
617  * PlannerInfo. Also add it to the auxiliary hashtable if there is one.
618  */
619 static void
621 {
622  /* GEQO requires us to append the new joinrel to the end of the list! */
623  root->join_rel_list = lappend(root->join_rel_list, joinrel);
624 
625  /* store it into the auxiliary hashtable if there is one. */
626  if (root->join_rel_hash)
627  {
628  JoinHashEntry *hentry;
629  bool found;
630 
631  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
632  &(joinrel->relids),
633  HASH_ENTER,
634  &found);
635  Assert(!found);
636  hentry->join_rel = joinrel;
637  }
638 }
639 
640 /*
641  * build_join_rel
642  * Returns relation entry corresponding to the union of two given rels,
643  * creating a new relation entry if none already exists.
644  *
645  * 'joinrelids' is the Relids set that uniquely identifies the join
646  * 'outer_rel' and 'inner_rel' are relation nodes for the relations to be
647  * joined
648  * 'sjinfo': join context info
649  * 'pushed_down_joins': any pushed-down outer joins that are now completed
650  * 'restrictlist_ptr': result variable. If not NULL, *restrictlist_ptr
651  * receives the list of RestrictInfo nodes that apply to this
652  * particular pair of joinable relations.
653  *
654  * restrictlist_ptr makes the routine's API a little grotty, but it saves
655  * duplicated calculation of the restrictlist...
656  */
657 RelOptInfo *
659  Relids joinrelids,
660  RelOptInfo *outer_rel,
661  RelOptInfo *inner_rel,
662  SpecialJoinInfo *sjinfo,
663  List *pushed_down_joins,
664  List **restrictlist_ptr)
665 {
666  RelOptInfo *joinrel;
667  List *restrictlist;
668 
669  /* This function should be used only for join between parents. */
670  Assert(!IS_OTHER_REL(outer_rel) && !IS_OTHER_REL(inner_rel));
671 
672  /*
673  * See if we already have a joinrel for this set of base rels.
674  */
675  joinrel = find_join_rel(root, joinrelids);
676 
677  if (joinrel)
678  {
679  /*
680  * Yes, so we only need to figure the restrictlist for this particular
681  * pair of component relations.
682  */
683  if (restrictlist_ptr)
684  *restrictlist_ptr = build_joinrel_restrictlist(root,
685  joinrel,
686  outer_rel,
687  inner_rel,
688  sjinfo);
689  return joinrel;
690  }
691 
692  /*
693  * Nope, so make one.
694  */
695  joinrel = makeNode(RelOptInfo);
696  joinrel->reloptkind = RELOPT_JOINREL;
697  joinrel->relids = bms_copy(joinrelids);
698  joinrel->rows = 0;
699  /* cheap startup cost is interesting iff not all tuples to be retrieved */
700  joinrel->consider_startup = (root->tuple_fraction > 0);
701  joinrel->consider_param_startup = false;
702  joinrel->consider_parallel = false;
703  joinrel->reltarget = create_empty_pathtarget();
704  joinrel->pathlist = NIL;
705  joinrel->ppilist = NIL;
706  joinrel->partial_pathlist = NIL;
707  joinrel->cheapest_startup_path = NULL;
708  joinrel->cheapest_total_path = NULL;
709  joinrel->cheapest_unique_path = NULL;
711  /* init direct_lateral_relids from children; we'll finish it up below */
712  joinrel->direct_lateral_relids =
713  bms_union(outer_rel->direct_lateral_relids,
714  inner_rel->direct_lateral_relids);
715  joinrel->lateral_relids = min_join_parameterization(root, joinrel->relids,
716  outer_rel, inner_rel);
717  joinrel->relid = 0; /* indicates not a baserel */
718  joinrel->rtekind = RTE_JOIN;
719  joinrel->min_attr = 0;
720  joinrel->max_attr = 0;
721  joinrel->attr_needed = NULL;
722  joinrel->attr_widths = NULL;
723  joinrel->notnullattnums = NULL;
724  joinrel->nulling_relids = NULL;
725  joinrel->lateral_vars = NIL;
726  joinrel->lateral_referencers = NULL;
727  joinrel->indexlist = NIL;
728  joinrel->statlist = NIL;
729  joinrel->pages = 0;
730  joinrel->tuples = 0;
731  joinrel->allvisfrac = 0;
732  joinrel->eclass_indexes = NULL;
733  joinrel->subroot = NULL;
734  joinrel->subplan_params = NIL;
735  joinrel->rel_parallel_workers = -1;
736  joinrel->amflags = 0;
737  joinrel->serverid = InvalidOid;
738  joinrel->userid = InvalidOid;
739  joinrel->useridiscurrent = false;
740  joinrel->fdwroutine = NULL;
741  joinrel->fdw_private = NULL;
742  joinrel->unique_for_rels = NIL;
743  joinrel->non_unique_for_rels = NIL;
744  joinrel->baserestrictinfo = NIL;
745  joinrel->baserestrictcost.startup = 0;
746  joinrel->baserestrictcost.per_tuple = 0;
747  joinrel->baserestrict_min_security = UINT_MAX;
748  joinrel->joininfo = NIL;
749  joinrel->has_eclass_joins = false;
750  joinrel->consider_partitionwise_join = false; /* might get changed later */
751  joinrel->parent = NULL;
752  joinrel->top_parent = NULL;
753  joinrel->top_parent_relids = NULL;
754  joinrel->part_scheme = NULL;
755  joinrel->nparts = -1;
756  joinrel->boundinfo = NULL;
757  joinrel->partbounds_merged = false;
758  joinrel->partition_qual = NIL;
759  joinrel->part_rels = NULL;
760  joinrel->live_parts = NULL;
761  joinrel->all_partrels = NULL;
762  joinrel->partexprs = NULL;
763  joinrel->nullable_partexprs = NULL;
764 
765  /* Compute information relevant to the foreign relations. */
766  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
767 
768  /*
769  * Fill the joinrel's tlist with just the Vars and PHVs that need to be
770  * output from this join (ie, are needed for higher joinclauses or final
771  * output).
772  *
773  * NOTE: the tlist order for a join rel will depend on which pair of outer
774  * and inner rels we first try to build it from. But the contents should
775  * be the same regardless.
776  */
777  build_joinrel_tlist(root, joinrel, outer_rel, sjinfo, pushed_down_joins,
778  (sjinfo->jointype == JOIN_FULL));
779  build_joinrel_tlist(root, joinrel, inner_rel, sjinfo, pushed_down_joins,
780  (sjinfo->jointype != JOIN_INNER));
781  add_placeholders_to_joinrel(root, joinrel, outer_rel, inner_rel, sjinfo);
782 
783  /*
784  * add_placeholders_to_joinrel also took care of adding the ph_lateral
785  * sets of any PlaceHolderVars computed here to direct_lateral_relids, so
786  * now we can finish computing that. This is much like the computation of
787  * the transitively-closed lateral_relids in min_join_parameterization,
788  * except that here we *do* have to consider the added PHVs.
789  */
790  joinrel->direct_lateral_relids =
791  bms_del_members(joinrel->direct_lateral_relids, joinrel->relids);
792 
793  /*
794  * Construct restrict and join clause lists for the new joinrel. (The
795  * caller might or might not need the restrictlist, but I need it anyway
796  * for set_joinrel_size_estimates().)
797  */
798  restrictlist = build_joinrel_restrictlist(root, joinrel,
799  outer_rel, inner_rel,
800  sjinfo);
801  if (restrictlist_ptr)
802  *restrictlist_ptr = restrictlist;
803  build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
804 
805  /*
806  * This is also the right place to check whether the joinrel has any
807  * pending EquivalenceClass joins.
808  */
809  joinrel->has_eclass_joins = has_relevant_eclass_joinclause(root, joinrel);
810 
811  /* Store the partition information. */
812  build_joinrel_partition_info(root, joinrel, outer_rel, inner_rel, sjinfo,
813  restrictlist);
814 
815  /*
816  * Set estimates of the joinrel's size.
817  */
818  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
819  sjinfo, restrictlist);
820 
821  /*
822  * Set the consider_parallel flag if this joinrel could potentially be
823  * scanned within a parallel worker. If this flag is false for either
824  * inner_rel or outer_rel, then it must be false for the joinrel also.
825  * Even if both are true, there might be parallel-restricted expressions
826  * in the targetlist or quals.
827  *
828  * Note that if there are more than two rels in this relation, they could
829  * be divided between inner_rel and outer_rel in any arbitrary way. We
830  * assume this doesn't matter, because we should hit all the same baserels
831  * and joinclauses while building up to this joinrel no matter which we
832  * take; therefore, we should make the same decision here however we get
833  * here.
834  */
835  if (inner_rel->consider_parallel && outer_rel->consider_parallel &&
836  is_parallel_safe(root, (Node *) restrictlist) &&
837  is_parallel_safe(root, (Node *) joinrel->reltarget->exprs))
838  joinrel->consider_parallel = true;
839 
840  /* Add the joinrel to the PlannerInfo. */
841  add_join_rel(root, joinrel);
842 
843  /*
844  * Also, if dynamic-programming join search is active, add the new joinrel
845  * to the appropriate sublist. Note: you might think the Assert on number
846  * of members should be for equality, but some of the level 1 rels might
847  * have been joinrels already, so we can only assert <=.
848  */
849  if (root->join_rel_level)
850  {
851  Assert(root->join_cur_level > 0);
852  Assert(root->join_cur_level <= bms_num_members(joinrel->relids));
853  root->join_rel_level[root->join_cur_level] =
854  lappend(root->join_rel_level[root->join_cur_level], joinrel);
855  }
856 
857  return joinrel;
858 }
859 
860 /*
861  * build_child_join_rel
862  * Builds RelOptInfo representing join between given two child relations.
863  *
864  * 'outer_rel' and 'inner_rel' are the RelOptInfos of child relations being
865  * joined
866  * 'parent_joinrel' is the RelOptInfo representing the join between parent
867  * relations. Some of the members of new RelOptInfo are produced by
868  * translating corresponding members of this RelOptInfo
869  * 'restrictlist': list of RestrictInfo nodes that apply to this particular
870  * pair of joinable relations
871  * 'sjinfo': child join's join-type details
872  */
873 RelOptInfo *
875  RelOptInfo *inner_rel, RelOptInfo *parent_joinrel,
876  List *restrictlist, SpecialJoinInfo *sjinfo)
877 {
878  RelOptInfo *joinrel = makeNode(RelOptInfo);
879  AppendRelInfo **appinfos;
880  int nappinfos;
881 
882  /* Only joins between "other" relations land here. */
883  Assert(IS_OTHER_REL(outer_rel) && IS_OTHER_REL(inner_rel));
884 
885  /* The parent joinrel should have consider_partitionwise_join set. */
886  Assert(parent_joinrel->consider_partitionwise_join);
887 
888  /*
889  * Find the AppendRelInfo structures for the child baserels. We'll need
890  * these for computing the child join's relid set, and later for mapping
891  * Vars to the child rel.
892  */
893  appinfos = find_appinfos_by_relids(root,
894  bms_union(outer_rel->relids,
895  inner_rel->relids),
896  &nappinfos);
897 
898  joinrel->reloptkind = RELOPT_OTHER_JOINREL;
899  joinrel->relids = adjust_child_relids(parent_joinrel->relids,
900  nappinfos, appinfos);
901  joinrel->rows = 0;
902  /* cheap startup cost is interesting iff not all tuples to be retrieved */
903  joinrel->consider_startup = (root->tuple_fraction > 0);
904  joinrel->consider_param_startup = false;
905  joinrel->consider_parallel = false;
906  joinrel->reltarget = create_empty_pathtarget();
907  joinrel->pathlist = NIL;
908  joinrel->ppilist = NIL;
909  joinrel->partial_pathlist = NIL;
910  joinrel->cheapest_startup_path = NULL;
911  joinrel->cheapest_total_path = NULL;
912  joinrel->cheapest_unique_path = NULL;
914  joinrel->direct_lateral_relids = NULL;
915  joinrel->lateral_relids = NULL;
916  joinrel->relid = 0; /* indicates not a baserel */
917  joinrel->rtekind = RTE_JOIN;
918  joinrel->min_attr = 0;
919  joinrel->max_attr = 0;
920  joinrel->attr_needed = NULL;
921  joinrel->attr_widths = NULL;
922  joinrel->notnullattnums = NULL;
923  joinrel->nulling_relids = NULL;
924  joinrel->lateral_vars = NIL;
925  joinrel->lateral_referencers = NULL;
926  joinrel->indexlist = NIL;
927  joinrel->pages = 0;
928  joinrel->tuples = 0;
929  joinrel->allvisfrac = 0;
930  joinrel->eclass_indexes = NULL;
931  joinrel->subroot = NULL;
932  joinrel->subplan_params = NIL;
933  joinrel->amflags = 0;
934  joinrel->serverid = InvalidOid;
935  joinrel->userid = InvalidOid;
936  joinrel->useridiscurrent = false;
937  joinrel->fdwroutine = NULL;
938  joinrel->fdw_private = NULL;
939  joinrel->baserestrictinfo = NIL;
940  joinrel->baserestrictcost.startup = 0;
941  joinrel->baserestrictcost.per_tuple = 0;
942  joinrel->joininfo = NIL;
943  joinrel->has_eclass_joins = false;
944  joinrel->consider_partitionwise_join = false; /* might get changed later */
945  joinrel->parent = parent_joinrel;
946  joinrel->top_parent = parent_joinrel->top_parent ? parent_joinrel->top_parent : parent_joinrel;
947  joinrel->top_parent_relids = joinrel->top_parent->relids;
948  joinrel->part_scheme = NULL;
949  joinrel->nparts = -1;
950  joinrel->boundinfo = NULL;
951  joinrel->partbounds_merged = false;
952  joinrel->partition_qual = NIL;
953  joinrel->part_rels = NULL;
954  joinrel->live_parts = NULL;
955  joinrel->all_partrels = NULL;
956  joinrel->partexprs = NULL;
957  joinrel->nullable_partexprs = NULL;
958 
959  /* Compute information relevant to foreign relations. */
960  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
961 
962  /* Set up reltarget struct */
963  build_child_join_reltarget(root, parent_joinrel, joinrel,
964  nappinfos, appinfos);
965 
966  /* Construct joininfo list. */
967  joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
968  (Node *) parent_joinrel->joininfo,
969  nappinfos,
970  appinfos);
971 
972  /*
973  * Lateral relids referred in child join will be same as that referred in
974  * the parent relation.
975  */
976  joinrel->direct_lateral_relids = (Relids) bms_copy(parent_joinrel->direct_lateral_relids);
977  joinrel->lateral_relids = (Relids) bms_copy(parent_joinrel->lateral_relids);
978 
979  /*
980  * If the parent joinrel has pending equivalence classes, so does the
981  * child.
982  */
983  joinrel->has_eclass_joins = parent_joinrel->has_eclass_joins;
984 
985  /* Is the join between partitions itself partitioned? */
986  build_joinrel_partition_info(root, joinrel, outer_rel, inner_rel, sjinfo,
987  restrictlist);
988 
989  /* Child joinrel is parallel safe if parent is parallel safe. */
990  joinrel->consider_parallel = parent_joinrel->consider_parallel;
991 
992  /* Set estimates of the child-joinrel's size. */
993  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
994  sjinfo, restrictlist);
995 
996  /* We build the join only once. */
997  Assert(!find_join_rel(root, joinrel->relids));
998 
999  /* Add the relation to the PlannerInfo. */
1000  add_join_rel(root, joinrel);
1001 
1002  /*
1003  * We might need EquivalenceClass members corresponding to the child join,
1004  * so that we can represent sort pathkeys for it. As with children of
1005  * baserels, we shouldn't need this unless there are relevant eclass joins
1006  * (implying that a merge join might be possible) or pathkeys to sort by.
1007  */
1008  if (joinrel->has_eclass_joins || has_useful_pathkeys(root, parent_joinrel))
1010  nappinfos, appinfos,
1011  parent_joinrel, joinrel);
1012 
1013  pfree(appinfos);
1014 
1015  return joinrel;
1016 }
1017 
1018 /*
1019  * min_join_parameterization
1020  *
1021  * Determine the minimum possible parameterization of a joinrel, that is, the
1022  * set of other rels it contains LATERAL references to. We save this value in
1023  * the join's RelOptInfo. This function is split out of build_join_rel()
1024  * because join_is_legal() needs the value to check a prospective join.
1025  */
1026 Relids
1028  Relids joinrelids,
1029  RelOptInfo *outer_rel,
1030  RelOptInfo *inner_rel)
1031 {
1032  Relids result;
1033 
1034  /*
1035  * Basically we just need the union of the inputs' lateral_relids, less
1036  * whatever is already in the join.
1037  *
1038  * It's not immediately obvious that this is a valid way to compute the
1039  * result, because it might seem that we're ignoring possible lateral refs
1040  * of PlaceHolderVars that are due to be computed at the join but not in
1041  * either input. However, because create_lateral_join_info() already
1042  * charged all such PHV refs to each member baserel of the join, they'll
1043  * be accounted for already in the inputs' lateral_relids. Likewise, we
1044  * do not need to worry about doing transitive closure here, because that
1045  * was already accounted for in the original baserel lateral_relids.
1046  */
1047  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
1048  result = bms_del_members(result, joinrelids);
1049  return result;
1050 }
1051 
1052 /*
1053  * build_joinrel_tlist
1054  * Builds a join relation's target list from an input relation.
1055  * (This is invoked twice to handle the two input relations.)
1056  *
1057  * The join's targetlist includes all Vars of its member relations that
1058  * will still be needed above the join. This subroutine adds all such
1059  * Vars from the specified input rel's tlist to the join rel's tlist.
1060  * Likewise for any PlaceHolderVars emitted by the input rel.
1061  *
1062  * We also compute the expected width of the join's output, making use
1063  * of data that was cached at the baserel level by set_rel_width().
1064  *
1065  * Pass can_null as true if the join is an outer join that can null Vars
1066  * from this input relation. If so, we will (normally) add the join's relid
1067  * to the nulling bitmaps of Vars and PHVs bubbled up from the input.
1068  *
1069  * When forming an outer join's target list, special handling is needed in
1070  * case the outer join was commuted with another one per outer join identity 3
1071  * (see optimizer/README). We must take steps to ensure that the output Vars
1072  * have the same nulling bitmaps that they would if the two joins had been
1073  * done in syntactic order; else they won't match Vars appearing higher in
1074  * the query tree. An exception to the match-the-syntactic-order rule is
1075  * that when an outer join is pushed down into another one's RHS per identity
1076  * 3, we can't mark its Vars as nulled until the now-upper outer join is also
1077  * completed. So we need to do three things:
1078  *
1079  * First, we add the outer join's relid to the nulling bitmap only if the
1080  * outer join has been completely performed and the Var or PHV actually
1081  * comes from within the syntactically nullable side(s) of the outer join.
1082  * This takes care of the possibility that we have transformed
1083  * (A leftjoin B on (Pab)) leftjoin C on (Pbc)
1084  * to
1085  * A leftjoin (B leftjoin C on (Pbc)) on (Pab)
1086  * Here the pushed-down B/C join cannot mark C columns as nulled yet,
1087  * while the now-upper A/B join must not mark C columns as nulled by itself.
1088  *
1089  * Second, perform the same operation for each SpecialJoinInfo listed in
1090  * pushed_down_joins (which, in this example, would be the B/C join when
1091  * we are at the now-upper A/B join). This allows the now-upper join to
1092  * complete the marking of "C" Vars that now have fully valid values.
1093  *
1094  * Third, any relid in sjinfo->commute_above_r that is already part of
1095  * the joinrel is added to the nulling bitmaps of nullable Vars and PHVs.
1096  * This takes care of the reverse case where we implement
1097  * A leftjoin (B leftjoin C on (Pbc)) on (Pab)
1098  * as
1099  * (A leftjoin B on (Pab)) leftjoin C on (Pbc)
1100  * The C columns emitted by the B/C join need to be shown as nulled by both
1101  * the B/C and A/B joins, even though they've not physically traversed the
1102  * A/B join.
1103  */
1104 static void
1106  RelOptInfo *input_rel,
1107  SpecialJoinInfo *sjinfo,
1108  List *pushed_down_joins,
1109  bool can_null)
1110 {
1111  Relids relids = joinrel->relids;
1112  int64 tuple_width = joinrel->reltarget->width;
1113  ListCell *vars;
1114  ListCell *lc;
1115 
1116  foreach(vars, input_rel->reltarget->exprs)
1117  {
1118  Var *var = (Var *) lfirst(vars);
1119 
1120  /*
1121  * For a PlaceHolderVar, we have to look up the PlaceHolderInfo.
1122  */
1123  if (IsA(var, PlaceHolderVar))
1124  {
1125  PlaceHolderVar *phv = (PlaceHolderVar *) var;
1126  PlaceHolderInfo *phinfo = find_placeholder_info(root, phv);
1127 
1128  /* Is it still needed above this joinrel? */
1129  if (bms_nonempty_difference(phinfo->ph_needed, relids))
1130  {
1131  /*
1132  * Yup, add it to the output. If this join potentially nulls
1133  * this input, we have to update the PHV's phnullingrels,
1134  * which means making a copy.
1135  */
1136  if (can_null)
1137  {
1138  phv = copyObject(phv);
1139  /* See comments above to understand this logic */
1140  if (sjinfo->ojrelid != 0 &&
1141  bms_is_member(sjinfo->ojrelid, relids) &&
1142  (bms_is_subset(phv->phrels, sjinfo->syn_righthand) ||
1143  (sjinfo->jointype == JOIN_FULL &&
1144  bms_is_subset(phv->phrels, sjinfo->syn_lefthand))))
1146  sjinfo->ojrelid);
1147  foreach(lc, pushed_down_joins)
1148  {
1149  SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
1150 
1151  Assert(bms_is_member(othersj->ojrelid, relids));
1152  if (bms_is_subset(phv->phrels, othersj->syn_righthand))
1154  othersj->ojrelid);
1155  }
1156  phv->phnullingrels =
1157  bms_join(phv->phnullingrels,
1159  relids));
1160  }
1161 
1162  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1163  phv);
1164  /* Bubbling up the precomputed result has cost zero */
1165  tuple_width += phinfo->ph_width;
1166  }
1167  continue;
1168  }
1169 
1170  /*
1171  * Otherwise, anything in a baserel or joinrel targetlist ought to be
1172  * a Var. (More general cases can only appear in appendrel child
1173  * rels, which will never be seen here.)
1174  */
1175  if (!IsA(var, Var))
1176  elog(ERROR, "unexpected node type in rel targetlist: %d",
1177  (int) nodeTag(var));
1178 
1179  if (var->varno == ROWID_VAR)
1180  {
1181  /* UPDATE/DELETE/MERGE row identity vars are always needed */
1182  RowIdentityVarInfo *ridinfo = (RowIdentityVarInfo *)
1183  list_nth(root->row_identity_vars, var->varattno - 1);
1184 
1185  /* Update reltarget width estimate from RowIdentityVarInfo */
1186  tuple_width += ridinfo->rowidwidth;
1187  }
1188  else
1189  {
1190  RelOptInfo *baserel;
1191  int ndx;
1192 
1193  /* Get the Var's original base rel */
1194  baserel = find_base_rel(root, var->varno);
1195 
1196  /* Is it still needed above this joinrel? */
1197  ndx = var->varattno - baserel->min_attr;
1198  if (!bms_nonempty_difference(baserel->attr_needed[ndx], relids))
1199  continue; /* nope, skip it */
1200 
1201  /* Update reltarget width estimate from baserel's attr_widths */
1202  tuple_width += baserel->attr_widths[ndx];
1203  }
1204 
1205  /*
1206  * Add the Var to the output. If this join potentially nulls this
1207  * input, we have to update the Var's varnullingrels, which means
1208  * making a copy. But note that we don't ever add nullingrel bits to
1209  * row identity Vars (cf. comments in setrefs.c).
1210  */
1211  if (can_null && var->varno != ROWID_VAR)
1212  {
1213  var = copyObject(var);
1214  /* See comments above to understand this logic */
1215  if (sjinfo->ojrelid != 0 &&
1216  bms_is_member(sjinfo->ojrelid, relids) &&
1217  (bms_is_member(var->varno, sjinfo->syn_righthand) ||
1218  (sjinfo->jointype == JOIN_FULL &&
1219  bms_is_member(var->varno, sjinfo->syn_lefthand))))
1220  var->varnullingrels = bms_add_member(var->varnullingrels,
1221  sjinfo->ojrelid);
1222  foreach(lc, pushed_down_joins)
1223  {
1224  SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
1225 
1226  Assert(bms_is_member(othersj->ojrelid, relids));
1227  if (bms_is_member(var->varno, othersj->syn_righthand))
1228  var->varnullingrels = bms_add_member(var->varnullingrels,
1229  othersj->ojrelid);
1230  }
1231  var->varnullingrels =
1232  bms_join(var->varnullingrels,
1234  relids));
1235  }
1236 
1237  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1238  var);
1239 
1240  /* Vars have cost zero, so no need to adjust reltarget->cost */
1241  }
1242 
1243  joinrel->reltarget->width = clamp_width_est(tuple_width);
1244 }
1245 
1246 /*
1247  * build_joinrel_restrictlist
1248  * build_joinrel_joinlist
1249  * These routines build lists of restriction and join clauses for a
1250  * join relation from the joininfo lists of the relations it joins.
1251  *
1252  * These routines are separate because the restriction list must be
1253  * built afresh for each pair of input sub-relations we consider, whereas
1254  * the join list need only be computed once for any join RelOptInfo.
1255  * The join list is fully determined by the set of rels making up the
1256  * joinrel, so we should get the same results (up to ordering) from any
1257  * candidate pair of sub-relations. But the restriction list is whatever
1258  * is not handled in the sub-relations, so it depends on which
1259  * sub-relations are considered.
1260  *
1261  * If a join clause from an input relation refers to base+OJ rels still not
1262  * present in the joinrel, then it is still a join clause for the joinrel;
1263  * we put it into the joininfo list for the joinrel. Otherwise,
1264  * the clause is now a restrict clause for the joined relation, and we
1265  * return it to the caller of build_joinrel_restrictlist() to be stored in
1266  * join paths made from this pair of sub-relations. (It will not need to
1267  * be considered further up the join tree.)
1268  *
1269  * In many cases we will find the same RestrictInfos in both input
1270  * relations' joinlists, so be careful to eliminate duplicates.
1271  * Pointer equality should be a sufficient test for dups, since all
1272  * the various joinlist entries ultimately refer to RestrictInfos
1273  * pushed into them by distribute_restrictinfo_to_rels().
1274  *
1275  * 'joinrel' is a join relation node
1276  * 'outer_rel' and 'inner_rel' are a pair of relations that can be joined
1277  * to form joinrel.
1278  * 'sjinfo': join context info
1279  *
1280  * build_joinrel_restrictlist() returns a list of relevant restrictinfos,
1281  * whereas build_joinrel_joinlist() stores its results in the joinrel's
1282  * joininfo list. One or the other must accept each given clause!
1283  *
1284  * NB: Formerly, we made deep(!) copies of each input RestrictInfo to pass
1285  * up to the join relation. I believe this is no longer necessary, because
1286  * RestrictInfo nodes are no longer context-dependent. Instead, just include
1287  * the original nodes in the lists made for the join relation.
1288  */
1289 static List *
1291  RelOptInfo *joinrel,
1292  RelOptInfo *outer_rel,
1293  RelOptInfo *inner_rel,
1294  SpecialJoinInfo *sjinfo)
1295 {
1296  List *result;
1297  Relids both_input_relids;
1298 
1299  both_input_relids = bms_union(outer_rel->relids, inner_rel->relids);
1300 
1301  /*
1302  * Collect all the clauses that syntactically belong at this level,
1303  * eliminating any duplicates (important since we will see many of the
1304  * same clauses arriving from both input relations).
1305  */
1306  result = subbuild_joinrel_restrictlist(root, joinrel, outer_rel,
1307  both_input_relids, NIL);
1308  result = subbuild_joinrel_restrictlist(root, joinrel, inner_rel,
1309  both_input_relids, result);
1310 
1311  /*
1312  * Add on any clauses derived from EquivalenceClasses. These cannot be
1313  * redundant with the clauses in the joininfo lists, so don't bother
1314  * checking.
1315  */
1316  result = list_concat(result,
1318  joinrel->relids,
1319  outer_rel->relids,
1320  inner_rel,
1321  sjinfo));
1322 
1323  return result;
1324 }
1325 
1326 static void
1328  RelOptInfo *outer_rel,
1329  RelOptInfo *inner_rel)
1330 {
1331  List *result;
1332 
1333  /*
1334  * Collect all the clauses that syntactically belong above this level,
1335  * eliminating any duplicates (important since we will see many of the
1336  * same clauses arriving from both input relations).
1337  */
1338  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1339  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1340 
1341  joinrel->joininfo = result;
1342 }
1343 
1344 static List *
1346  RelOptInfo *joinrel,
1347  RelOptInfo *input_rel,
1348  Relids both_input_relids,
1349  List *new_restrictlist)
1350 {
1351  ListCell *l;
1352 
1353  foreach(l, input_rel->joininfo)
1354  {
1355  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1356 
1357  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1358  {
1359  /*
1360  * This clause should become a restriction clause for the joinrel,
1361  * since it refers to no outside rels. However, if it's a clone
1362  * clause then it might be too late to evaluate it, so we have to
1363  * check. (If it is too late, just ignore the clause, taking it
1364  * on faith that another clone was or will be selected.) Clone
1365  * clauses should always be outer-join clauses, so we compare
1366  * against both_input_relids.
1367  */
1368  if (rinfo->has_clone || rinfo->is_clone)
1369  {
1370  Assert(!RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids));
1371  if (!bms_is_subset(rinfo->required_relids, both_input_relids))
1372  continue;
1373  if (bms_overlap(rinfo->incompatible_relids, both_input_relids))
1374  continue;
1375  }
1376  else
1377  {
1378  /*
1379  * For non-clone clauses, we just Assert it's OK. These might
1380  * be either join or filter clauses; if it's a join clause
1381  * then it should not refer to the current join's output.
1382  * (There is little point in checking incompatible_relids,
1383  * because it'll be NULL.)
1384  */
1385  Assert(RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids) ||
1387  both_input_relids));
1388  }
1389 
1390  /*
1391  * OK, so add it to the list, being careful to eliminate
1392  * duplicates. (Since RestrictInfo nodes in different joinlists
1393  * will have been multiply-linked rather than copied, pointer
1394  * equality should be a sufficient test.)
1395  */
1396  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1397  }
1398  else
1399  {
1400  /*
1401  * This clause is still a join clause at this level, so we ignore
1402  * it in this routine.
1403  */
1404  }
1405  }
1406 
1407  return new_restrictlist;
1408 }
1409 
1410 static List *
1412  List *joininfo_list,
1413  List *new_joininfo)
1414 {
1415  ListCell *l;
1416 
1417  /* Expected to be called only for join between parent relations. */
1418  Assert(joinrel->reloptkind == RELOPT_JOINREL);
1419 
1420  foreach(l, joininfo_list)
1421  {
1422  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1423 
1424  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1425  {
1426  /*
1427  * This clause becomes a restriction clause for the joinrel, since
1428  * it refers to no outside rels. So we can ignore it in this
1429  * routine.
1430  */
1431  }
1432  else
1433  {
1434  /*
1435  * This clause is still a join clause at this level, so add it to
1436  * the new joininfo list, being careful to eliminate duplicates.
1437  * (Since RestrictInfo nodes in different joinlists will have been
1438  * multiply-linked rather than copied, pointer equality should be
1439  * a sufficient test.)
1440  */
1441  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1442  }
1443  }
1444 
1445  return new_joininfo;
1446 }
1447 
1448 
1449 /*
1450  * fetch_upper_rel
1451  * Build a RelOptInfo describing some post-scan/join query processing,
1452  * or return a pre-existing one if somebody already built it.
1453  *
1454  * An "upper" relation is identified by an UpperRelationKind and a Relids set.
1455  * The meaning of the Relids set is not specified here, and very likely will
1456  * vary for different relation kinds.
1457  *
1458  * Most of the fields in an upper-level RelOptInfo are not used and are not
1459  * set here (though makeNode should ensure they're zeroes). We basically only
1460  * care about fields that are of interest to add_path() and set_cheapest().
1461  */
1462 RelOptInfo *
1464 {
1465  RelOptInfo *upperrel;
1466  ListCell *lc;
1467 
1468  /*
1469  * For the moment, our indexing data structure is just a List for each
1470  * relation kind. If we ever get so many of one kind that this stops
1471  * working well, we can improve it. No code outside this function should
1472  * assume anything about how to find a particular upperrel.
1473  */
1474 
1475  /* If we already made this upperrel for the query, return it */
1476  foreach(lc, root->upper_rels[kind])
1477  {
1478  upperrel = (RelOptInfo *) lfirst(lc);
1479 
1480  if (bms_equal(upperrel->relids, relids))
1481  return upperrel;
1482  }
1483 
1484  upperrel = makeNode(RelOptInfo);
1485  upperrel->reloptkind = RELOPT_UPPER_REL;
1486  upperrel->relids = bms_copy(relids);
1487 
1488  /* cheap startup cost is interesting iff not all tuples to be retrieved */
1489  upperrel->consider_startup = (root->tuple_fraction > 0);
1490  upperrel->consider_param_startup = false;
1491  upperrel->consider_parallel = false; /* might get changed later */
1492  upperrel->reltarget = create_empty_pathtarget();
1493  upperrel->pathlist = NIL;
1494  upperrel->cheapest_startup_path = NULL;
1495  upperrel->cheapest_total_path = NULL;
1496  upperrel->cheapest_unique_path = NULL;
1497  upperrel->cheapest_parameterized_paths = NIL;
1498 
1499  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1500 
1501  return upperrel;
1502 }
1503 
1504 
1505 /*
1506  * find_childrel_parents
1507  * Compute the set of parent relids of an appendrel child rel.
1508  *
1509  * Since appendrels can be nested, a child could have multiple levels of
1510  * appendrel ancestors. This function computes a Relids set of all the
1511  * parent relation IDs.
1512  */
1513 Relids
1515 {
1516  Relids result = NULL;
1517 
1519  Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1520 
1521  do
1522  {
1523  AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1524  Index prelid = appinfo->parent_relid;
1525 
1526  result = bms_add_member(result, prelid);
1527 
1528  /* traverse up to the parent rel, loop if it's also a child rel */
1529  rel = find_base_rel(root, prelid);
1530  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1531 
1532  Assert(rel->reloptkind == RELOPT_BASEREL);
1533 
1534  return result;
1535 }
1536 
1537 
1538 /*
1539  * get_baserel_parampathinfo
1540  * Get the ParamPathInfo for a parameterized path for a base relation,
1541  * constructing one if we don't have one already.
1542  *
1543  * This centralizes estimating the rowcounts for parameterized paths.
1544  * We need to cache those to be sure we use the same rowcount for all paths
1545  * of the same parameterization for a given rel. This is also a convenient
1546  * place to determine which movable join clauses the parameterized path will
1547  * be responsible for evaluating.
1548  */
1549 ParamPathInfo *
1551  Relids required_outer)
1552 {
1553  ParamPathInfo *ppi;
1554  Relids joinrelids;
1555  List *pclauses;
1556  Bitmapset *pserials;
1557  double rows;
1558  ListCell *lc;
1559 
1560  /* If rel has LATERAL refs, every path for it should account for them */
1561  Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1562 
1563  /* Unparameterized paths have no ParamPathInfo */
1564  if (bms_is_empty(required_outer))
1565  return NULL;
1566 
1567  Assert(!bms_overlap(baserel->relids, required_outer));
1568 
1569  /* If we already have a PPI for this parameterization, just return it */
1570  if ((ppi = find_param_path_info(baserel, required_outer)))
1571  return ppi;
1572 
1573  /*
1574  * Identify all joinclauses that are movable to this base rel given this
1575  * parameterization.
1576  */
1577  joinrelids = bms_union(baserel->relids, required_outer);
1578  pclauses = NIL;
1579  foreach(lc, baserel->joininfo)
1580  {
1581  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1582 
1583  if (join_clause_is_movable_into(rinfo,
1584  baserel->relids,
1585  joinrelids))
1586  pclauses = lappend(pclauses, rinfo);
1587  }
1588 
1589  /*
1590  * Add in joinclauses generated by EquivalenceClasses, too. (These
1591  * necessarily satisfy join_clause_is_movable_into.)
1592  */
1593  pclauses = list_concat(pclauses,
1595  joinrelids,
1596  required_outer,
1597  baserel,
1598  NULL));
1599 
1600  /* Compute set of serial numbers of the enforced clauses */
1601  pserials = NULL;
1602  foreach(lc, pclauses)
1603  {
1604  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1605 
1606  pserials = bms_add_member(pserials, rinfo->rinfo_serial);
1607  }
1608 
1609  /* Estimate the number of rows returned by the parameterized scan */
1610  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1611 
1612  /* And now we can build the ParamPathInfo */
1613  ppi = makeNode(ParamPathInfo);
1614  ppi->ppi_req_outer = required_outer;
1615  ppi->ppi_rows = rows;
1616  ppi->ppi_clauses = pclauses;
1617  ppi->ppi_serials = pserials;
1618  baserel->ppilist = lappend(baserel->ppilist, ppi);
1619 
1620  return ppi;
1621 }
1622 
1623 /*
1624  * get_joinrel_parampathinfo
1625  * Get the ParamPathInfo for a parameterized path for a join relation,
1626  * constructing one if we don't have one already.
1627  *
1628  * This centralizes estimating the rowcounts for parameterized paths.
1629  * We need to cache those to be sure we use the same rowcount for all paths
1630  * of the same parameterization for a given rel. This is also a convenient
1631  * place to determine which movable join clauses the parameterized path will
1632  * be responsible for evaluating.
1633  *
1634  * outer_path and inner_path are a pair of input paths that can be used to
1635  * construct the join, and restrict_clauses is the list of regular join
1636  * clauses (including clauses derived from EquivalenceClasses) that must be
1637  * applied at the join node when using these inputs.
1638  *
1639  * Unlike the situation for base rels, the set of movable join clauses to be
1640  * enforced at a join varies with the selected pair of input paths, so we
1641  * must calculate that and pass it back, even if we already have a matching
1642  * ParamPathInfo. We handle this by adding any clauses moved down to this
1643  * join to *restrict_clauses, which is an in/out parameter. (The addition
1644  * is done in such a way as to not modify the passed-in List structure.)
1645  *
1646  * Note: when considering a nestloop join, the caller must have removed from
1647  * restrict_clauses any movable clauses that are themselves scheduled to be
1648  * pushed into the right-hand path. We do not do that here since it's
1649  * unnecessary for other join types.
1650  */
1651 ParamPathInfo *
1653  Path *outer_path,
1654  Path *inner_path,
1655  SpecialJoinInfo *sjinfo,
1656  Relids required_outer,
1657  List **restrict_clauses)
1658 {
1659  ParamPathInfo *ppi;
1660  Relids join_and_req;
1661  Relids outer_and_req;
1662  Relids inner_and_req;
1663  List *pclauses;
1664  List *eclauses;
1665  List *dropped_ecs;
1666  double rows;
1667  ListCell *lc;
1668 
1669  /* If rel has LATERAL refs, every path for it should account for them */
1670  Assert(bms_is_subset(joinrel->lateral_relids, required_outer));
1671 
1672  /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1673  if (bms_is_empty(required_outer))
1674  return NULL;
1675 
1676  Assert(!bms_overlap(joinrel->relids, required_outer));
1677 
1678  /*
1679  * Identify all joinclauses that are movable to this join rel given this
1680  * parameterization. These are the clauses that are movable into this
1681  * join, but not movable into either input path. Treat an unparameterized
1682  * input path as not accepting parameterized clauses (because it won't,
1683  * per the shortcut exit above), even though the joinclause movement rules
1684  * might allow the same clauses to be moved into a parameterized path for
1685  * that rel.
1686  */
1687  join_and_req = bms_union(joinrel->relids, required_outer);
1688  if (outer_path->param_info)
1689  outer_and_req = bms_union(outer_path->parent->relids,
1690  PATH_REQ_OUTER(outer_path));
1691  else
1692  outer_and_req = NULL; /* outer path does not accept parameters */
1693  if (inner_path->param_info)
1694  inner_and_req = bms_union(inner_path->parent->relids,
1695  PATH_REQ_OUTER(inner_path));
1696  else
1697  inner_and_req = NULL; /* inner path does not accept parameters */
1698 
1699  pclauses = NIL;
1700  foreach(lc, joinrel->joininfo)
1701  {
1702  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1703 
1704  if (join_clause_is_movable_into(rinfo,
1705  joinrel->relids,
1706  join_and_req) &&
1708  outer_path->parent->relids,
1709  outer_and_req) &&
1711  inner_path->parent->relids,
1712  inner_and_req))
1713  pclauses = lappend(pclauses, rinfo);
1714  }
1715 
1716  /* Consider joinclauses generated by EquivalenceClasses, too */
1717  eclauses = generate_join_implied_equalities(root,
1718  join_and_req,
1719  required_outer,
1720  joinrel,
1721  NULL);
1722  /* We only want ones that aren't movable to lower levels */
1723  dropped_ecs = NIL;
1724  foreach(lc, eclauses)
1725  {
1726  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1727 
1729  joinrel->relids,
1730  join_and_req));
1731  if (join_clause_is_movable_into(rinfo,
1732  outer_path->parent->relids,
1733  outer_and_req))
1734  continue; /* drop if movable into LHS */
1735  if (join_clause_is_movable_into(rinfo,
1736  inner_path->parent->relids,
1737  inner_and_req))
1738  {
1739  /* drop if movable into RHS, but remember EC for use below */
1740  Assert(rinfo->left_ec == rinfo->right_ec);
1741  dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1742  continue;
1743  }
1744  pclauses = lappend(pclauses, rinfo);
1745  }
1746 
1747  /*
1748  * EquivalenceClasses are harder to deal with than we could wish, because
1749  * of the fact that a given EC can generate different clauses depending on
1750  * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1751  * LHS and RHS of the current join and Z is in required_outer, and further
1752  * suppose that the inner_path is parameterized by both X and Z. The code
1753  * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1754  * and in the latter case will have discarded it as being movable into the
1755  * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1756  * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1757  * not have produced both, and we can't readily tell from here which one
1758  * it did pick. If we add no clause to this join, we'll end up with
1759  * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1760  * constrained to be equal to the other members of the EC. (When we come
1761  * to join Z to this X/Y path, we will certainly drop whichever EC clause
1762  * is generated at that join, so this omission won't get fixed later.)
1763  *
1764  * To handle this, for each EC we discarded such a clause from, try to
1765  * generate a clause connecting the required_outer rels to the join's LHS
1766  * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1767  * the clause can't be moved to the LHS, add it to the current join's
1768  * restriction clauses. (If an EC cannot generate such a clause then it
1769  * has nothing that needs to be enforced here, while if the clause can be
1770  * moved into the LHS then it should have been enforced within that path.)
1771  *
1772  * Note that we don't need similar processing for ECs whose clause was
1773  * considered to be movable into the LHS, because the LHS can't refer to
1774  * the RHS so there is no comparable ambiguity about what it might
1775  * actually be enforcing internally.
1776  */
1777  if (dropped_ecs)
1778  {
1779  Relids real_outer_and_req;
1780 
1781  real_outer_and_req = bms_union(outer_path->parent->relids,
1782  required_outer);
1783  eclauses =
1785  dropped_ecs,
1786  real_outer_and_req,
1787  required_outer,
1788  outer_path->parent);
1789  foreach(lc, eclauses)
1790  {
1791  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1792 
1794  outer_path->parent->relids,
1795  real_outer_and_req));
1796  if (!join_clause_is_movable_into(rinfo,
1797  outer_path->parent->relids,
1798  outer_and_req))
1799  pclauses = lappend(pclauses, rinfo);
1800  }
1801  }
1802 
1803  /*
1804  * Now, attach the identified moved-down clauses to the caller's
1805  * restrict_clauses list. By using list_concat in this order, we leave
1806  * the original list structure of restrict_clauses undamaged.
1807  */
1808  *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1809 
1810  /* If we already have a PPI for this parameterization, just return it */
1811  if ((ppi = find_param_path_info(joinrel, required_outer)))
1812  return ppi;
1813 
1814  /* Estimate the number of rows returned by the parameterized join */
1815  rows = get_parameterized_joinrel_size(root, joinrel,
1816  outer_path,
1817  inner_path,
1818  sjinfo,
1819  *restrict_clauses);
1820 
1821  /*
1822  * And now we can build the ParamPathInfo. No point in saving the
1823  * input-pair-dependent clause list, though.
1824  *
1825  * Note: in GEQO mode, we'll be called in a temporary memory context, but
1826  * the joinrel structure is there too, so no problem.
1827  */
1828  ppi = makeNode(ParamPathInfo);
1829  ppi->ppi_req_outer = required_outer;
1830  ppi->ppi_rows = rows;
1831  ppi->ppi_clauses = NIL;
1832  ppi->ppi_serials = NULL;
1833  joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1834 
1835  return ppi;
1836 }
1837 
1838 /*
1839  * get_appendrel_parampathinfo
1840  * Get the ParamPathInfo for a parameterized path for an append relation.
1841  *
1842  * For an append relation, the rowcount estimate will just be the sum of
1843  * the estimates for its children. However, we still need a ParamPathInfo
1844  * to flag the fact that the path requires parameters. So this just creates
1845  * a suitable struct with zero ppi_rows (and no ppi_clauses either, since
1846  * the Append node isn't responsible for checking quals).
1847  */
1848 ParamPathInfo *
1849 get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
1850 {
1851  ParamPathInfo *ppi;
1852 
1853  /* If rel has LATERAL refs, every path for it should account for them */
1854  Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1855 
1856  /* Unparameterized paths have no ParamPathInfo */
1857  if (bms_is_empty(required_outer))
1858  return NULL;
1859 
1860  Assert(!bms_overlap(appendrel->relids, required_outer));
1861 
1862  /* If we already have a PPI for this parameterization, just return it */
1863  if ((ppi = find_param_path_info(appendrel, required_outer)))
1864  return ppi;
1865 
1866  /* Else build the ParamPathInfo */
1867  ppi = makeNode(ParamPathInfo);
1868  ppi->ppi_req_outer = required_outer;
1869  ppi->ppi_rows = 0;
1870  ppi->ppi_clauses = NIL;
1871  ppi->ppi_serials = NULL;
1872  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1873 
1874  return ppi;
1875 }
1876 
1877 /*
1878  * Returns a ParamPathInfo for the parameterization given by required_outer, if
1879  * already available in the given rel. Returns NULL otherwise.
1880  */
1881 ParamPathInfo *
1883 {
1884  ListCell *lc;
1885 
1886  foreach(lc, rel->ppilist)
1887  {
1888  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1889 
1890  if (bms_equal(ppi->ppi_req_outer, required_outer))
1891  return ppi;
1892  }
1893 
1894  return NULL;
1895 }
1896 
1897 /*
1898  * get_param_path_clause_serials
1899  * Given a parameterized Path, return the set of pushed-down clauses
1900  * (identified by rinfo_serial numbers) enforced within the Path.
1901  */
1902 Bitmapset *
1904 {
1905  if (path->param_info == NULL)
1906  return NULL; /* not parameterized */
1907  if (IsA(path, NestPath) ||
1908  IsA(path, MergePath) ||
1909  IsA(path, HashPath))
1910  {
1911  /*
1912  * For a join path, combine clauses enforced within either input path
1913  * with those enforced as joinrestrictinfo in this path. Note that
1914  * joinrestrictinfo may include some non-pushed-down clauses, but for
1915  * current purposes it's okay if we include those in the result. (To
1916  * be more careful, we could check for clause_relids overlapping the
1917  * path parameterization, but it's not worth the cycles for now.)
1918  */
1919  JoinPath *jpath = (JoinPath *) path;
1920  Bitmapset *pserials;
1921  ListCell *lc;
1922 
1923  pserials = NULL;
1924  pserials = bms_add_members(pserials,
1926  pserials = bms_add_members(pserials,
1928  foreach(lc, jpath->joinrestrictinfo)
1929  {
1930  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1931 
1932  pserials = bms_add_member(pserials, rinfo->rinfo_serial);
1933  }
1934  return pserials;
1935  }
1936  else if (IsA(path, AppendPath))
1937  {
1938  /*
1939  * For an appendrel, take the intersection of the sets of clauses
1940  * enforced in each input path.
1941  */
1942  AppendPath *apath = (AppendPath *) path;
1943  Bitmapset *pserials;
1944  ListCell *lc;
1945 
1946  pserials = NULL;
1947  foreach(lc, apath->subpaths)
1948  {
1949  Path *subpath = (Path *) lfirst(lc);
1950  Bitmapset *subserials;
1951 
1952  subserials = get_param_path_clause_serials(subpath);
1953  if (lc == list_head(apath->subpaths))
1954  pserials = bms_copy(subserials);
1955  else
1956  pserials = bms_int_members(pserials, subserials);
1957  }
1958  return pserials;
1959  }
1960  else if (IsA(path, MergeAppendPath))
1961  {
1962  /* Same as AppendPath case */
1963  MergeAppendPath *apath = (MergeAppendPath *) path;
1964  Bitmapset *pserials;
1965  ListCell *lc;
1966 
1967  pserials = NULL;
1968  foreach(lc, apath->subpaths)
1969  {
1970  Path *subpath = (Path *) lfirst(lc);
1971  Bitmapset *subserials;
1972 
1973  subserials = get_param_path_clause_serials(subpath);
1974  if (lc == list_head(apath->subpaths))
1975  pserials = bms_copy(subserials);
1976  else
1977  pserials = bms_int_members(pserials, subserials);
1978  }
1979  return pserials;
1980  }
1981  else
1982  {
1983  /*
1984  * Otherwise, it's a baserel path and we can use the
1985  * previously-computed set of serial numbers.
1986  */
1987  return path->param_info->ppi_serials;
1988  }
1989 }
1990 
1991 /*
1992  * build_joinrel_partition_info
1993  * Checks if the two relations being joined can use partitionwise join
1994  * and if yes, initialize partitioning information of the resulting
1995  * partitioned join relation.
1996  */
1997 static void
1999  RelOptInfo *joinrel, RelOptInfo *outer_rel,
2000  RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo,
2001  List *restrictlist)
2002 {
2003  PartitionScheme part_scheme;
2004 
2005  /* Nothing to do if partitionwise join technique is disabled. */
2007  {
2008  Assert(!IS_PARTITIONED_REL(joinrel));
2009  return;
2010  }
2011 
2012  /*
2013  * We can only consider this join as an input to further partitionwise
2014  * joins if (a) the input relations are partitioned and have
2015  * consider_partitionwise_join=true, (b) the partition schemes match, and
2016  * (c) we can identify an equi-join between the partition keys. Note that
2017  * if it were possible for have_partkey_equi_join to return different
2018  * answers for the same joinrel depending on which join ordering we try
2019  * first, this logic would break. That shouldn't happen, though, because
2020  * of the way the query planner deduces implied equalities and reorders
2021  * the joins. Please see optimizer/README for details.
2022  */
2023  if (outer_rel->part_scheme == NULL || inner_rel->part_scheme == NULL ||
2024  !outer_rel->consider_partitionwise_join ||
2025  !inner_rel->consider_partitionwise_join ||
2026  outer_rel->part_scheme != inner_rel->part_scheme ||
2027  !have_partkey_equi_join(root, joinrel, outer_rel, inner_rel,
2028  sjinfo->jointype, restrictlist))
2029  {
2030  Assert(!IS_PARTITIONED_REL(joinrel));
2031  return;
2032  }
2033 
2034  part_scheme = outer_rel->part_scheme;
2035 
2036  /*
2037  * This function will be called only once for each joinrel, hence it
2038  * should not have partitioning fields filled yet.
2039  */
2040  Assert(!joinrel->part_scheme && !joinrel->partexprs &&
2041  !joinrel->nullable_partexprs && !joinrel->part_rels &&
2042  !joinrel->boundinfo);
2043 
2044  /*
2045  * If the join relation is partitioned, it uses the same partitioning
2046  * scheme as the joining relations.
2047  *
2048  * Note: we calculate the partition bounds, number of partitions, and
2049  * child-join relations of the join relation in try_partitionwise_join().
2050  */
2051  joinrel->part_scheme = part_scheme;
2052  set_joinrel_partition_key_exprs(joinrel, outer_rel, inner_rel,
2053  sjinfo->jointype);
2054 
2055  /*
2056  * Set the consider_partitionwise_join flag.
2057  */
2058  Assert(outer_rel->consider_partitionwise_join);
2059  Assert(inner_rel->consider_partitionwise_join);
2060  joinrel->consider_partitionwise_join = true;
2061 }
2062 
2063 /*
2064  * have_partkey_equi_join
2065  *
2066  * Returns true if there exist equi-join conditions involving pairs
2067  * of matching partition keys of the relations being joined for all
2068  * partition keys.
2069  */
2070 static bool
2072  RelOptInfo *rel1, RelOptInfo *rel2,
2073  JoinType jointype, List *restrictlist)
2074 {
2075  PartitionScheme part_scheme = rel1->part_scheme;
2076  ListCell *lc;
2077  int cnt_pks;
2078  bool pk_has_clause[PARTITION_MAX_KEYS];
2079  bool strict_op;
2080 
2081  /*
2082  * This function must only be called when the joined relations have same
2083  * partitioning scheme.
2084  */
2085  Assert(rel1->part_scheme == rel2->part_scheme);
2086  Assert(part_scheme);
2087 
2088  memset(pk_has_clause, 0, sizeof(pk_has_clause));
2089  foreach(lc, restrictlist)
2090  {
2091  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2092  OpExpr *opexpr;
2093  Expr *expr1;
2094  Expr *expr2;
2095  int ipk1;
2096  int ipk2;
2097 
2098  /* If processing an outer join, only use its own join clauses. */
2099  if (IS_OUTER_JOIN(jointype) &&
2100  RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
2101  continue;
2102 
2103  /* Skip clauses which can not be used for a join. */
2104  if (!rinfo->can_join)
2105  continue;
2106 
2107  /* Skip clauses which are not equality conditions. */
2108  if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
2109  continue;
2110 
2111  /* Should be OK to assume it's an OpExpr. */
2112  opexpr = castNode(OpExpr, rinfo->clause);
2113 
2114  /* Match the operands to the relation. */
2115  if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
2116  bms_is_subset(rinfo->right_relids, rel2->relids))
2117  {
2118  expr1 = linitial(opexpr->args);
2119  expr2 = lsecond(opexpr->args);
2120  }
2121  else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
2122  bms_is_subset(rinfo->right_relids, rel1->relids))
2123  {
2124  expr1 = lsecond(opexpr->args);
2125  expr2 = linitial(opexpr->args);
2126  }
2127  else
2128  continue;
2129 
2130  /*
2131  * Now we need to know whether the join operator is strict; see
2132  * comments in pathnodes.h.
2133  */
2134  strict_op = op_strict(opexpr->opno);
2135 
2136  /*
2137  * Vars appearing in the relation's partition keys will not have any
2138  * varnullingrels, but those in expr1 and expr2 will if we're above
2139  * outer joins that could null the respective rels. It's okay to
2140  * match anyway, if the join operator is strict.
2141  */
2142  if (strict_op)
2143  {
2144  if (bms_overlap(rel1->relids, root->outer_join_rels))
2145  expr1 = (Expr *) remove_nulling_relids((Node *) expr1,
2146  root->outer_join_rels,
2147  NULL);
2148  if (bms_overlap(rel2->relids, root->outer_join_rels))
2149  expr2 = (Expr *) remove_nulling_relids((Node *) expr2,
2150  root->outer_join_rels,
2151  NULL);
2152  }
2153 
2154  /*
2155  * Only clauses referencing the partition keys are useful for
2156  * partitionwise join.
2157  */
2158  ipk1 = match_expr_to_partition_keys(expr1, rel1, strict_op);
2159  if (ipk1 < 0)
2160  continue;
2161  ipk2 = match_expr_to_partition_keys(expr2, rel2, strict_op);
2162  if (ipk2 < 0)
2163  continue;
2164 
2165  /*
2166  * If the clause refers to keys at different ordinal positions, it can
2167  * not be used for partitionwise join.
2168  */
2169  if (ipk1 != ipk2)
2170  continue;
2171 
2172  /*
2173  * The clause allows partitionwise join only if it uses the same
2174  * operator family as that specified by the partition key.
2175  */
2176  if (rel1->part_scheme->strategy == PARTITION_STRATEGY_HASH)
2177  {
2178  if (!OidIsValid(rinfo->hashjoinoperator) ||
2179  !op_in_opfamily(rinfo->hashjoinoperator,
2180  part_scheme->partopfamily[ipk1]))
2181  continue;
2182  }
2183  else if (!list_member_oid(rinfo->mergeopfamilies,
2184  part_scheme->partopfamily[ipk1]))
2185  continue;
2186 
2187  /* Mark the partition key as having an equi-join clause. */
2188  pk_has_clause[ipk1] = true;
2189  }
2190 
2191  /* Check whether every partition key has an equi-join condition. */
2192  for (cnt_pks = 0; cnt_pks < part_scheme->partnatts; cnt_pks++)
2193  {
2194  if (!pk_has_clause[cnt_pks])
2195  return false;
2196  }
2197 
2198  return true;
2199 }
2200 
2201 /*
2202  * match_expr_to_partition_keys
2203  *
2204  * Tries to match an expression to one of the nullable or non-nullable
2205  * partition keys of "rel". Returns the matched key's ordinal position,
2206  * or -1 if the expression could not be matched to any of the keys.
2207  *
2208  * strict_op must be true if the expression will be compared with the
2209  * partition key using a strict operator. This allows us to consider
2210  * nullable as well as nonnullable partition keys.
2211  */
2212 static int
2213 match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
2214 {
2215  int cnt;
2216 
2217  /* This function should be called only for partitioned relations. */
2218  Assert(rel->part_scheme);
2219  Assert(rel->partexprs);
2220  Assert(rel->nullable_partexprs);
2221 
2222  /* Remove any relabel decorations. */
2223  while (IsA(expr, RelabelType))
2224  expr = (Expr *) (castNode(RelabelType, expr))->arg;
2225 
2226  for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
2227  {
2228  ListCell *lc;
2229 
2230  /* We can always match to the non-nullable partition keys. */
2231  foreach(lc, rel->partexprs[cnt])
2232  {
2233  if (equal(lfirst(lc), expr))
2234  return cnt;
2235  }
2236 
2237  if (!strict_op)
2238  continue;
2239 
2240  /*
2241  * If it's a strict join operator then a NULL partition key on one
2242  * side will not join to any partition key on the other side, and in
2243  * particular such a row can't join to a row from a different
2244  * partition on the other side. So, it's okay to search the nullable
2245  * partition keys as well.
2246  */
2247  foreach(lc, rel->nullable_partexprs[cnt])
2248  {
2249  if (equal(lfirst(lc), expr))
2250  return cnt;
2251  }
2252  }
2253 
2254  return -1;
2255 }
2256 
2257 /*
2258  * set_joinrel_partition_key_exprs
2259  * Initialize partition key expressions for a partitioned joinrel.
2260  */
2261 static void
2263  RelOptInfo *outer_rel, RelOptInfo *inner_rel,
2264  JoinType jointype)
2265 {
2266  PartitionScheme part_scheme = joinrel->part_scheme;
2267  int partnatts = part_scheme->partnatts;
2268 
2269  joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts);
2270  joinrel->nullable_partexprs =
2271  (List **) palloc0(sizeof(List *) * partnatts);
2272 
2273  /*
2274  * The joinrel's partition expressions are the same as those of the input
2275  * rels, but we must properly classify them as nullable or not in the
2276  * joinrel's output. (Also, we add some more partition expressions if
2277  * it's a FULL JOIN.)
2278  */
2279  for (int cnt = 0; cnt < partnatts; cnt++)
2280  {
2281  /* mark these const to enforce that we copy them properly */
2282  const List *outer_expr = outer_rel->partexprs[cnt];
2283  const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
2284  const List *inner_expr = inner_rel->partexprs[cnt];
2285  const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
2286  List *partexpr = NIL;
2287  List *nullable_partexpr = NIL;
2288  ListCell *lc;
2289 
2290  switch (jointype)
2291  {
2292  /*
2293  * A join relation resulting from an INNER join may be
2294  * regarded as partitioned by either of the inner and outer
2295  * relation keys. For example, A INNER JOIN B ON A.a = B.b
2296  * can be regarded as partitioned on either A.a or B.b. So we
2297  * add both keys to the joinrel's partexpr lists. However,
2298  * anything that was already nullable still has to be treated
2299  * as nullable.
2300  */
2301  case JOIN_INNER:
2302  partexpr = list_concat_copy(outer_expr, inner_expr);
2303  nullable_partexpr = list_concat_copy(outer_null_expr,
2304  inner_null_expr);
2305  break;
2306 
2307  /*
2308  * A join relation resulting from a SEMI or ANTI join may be
2309  * regarded as partitioned by the outer relation keys. The
2310  * inner relation's keys are no longer interesting; since they
2311  * aren't visible in the join output, nothing could join to
2312  * them.
2313  */
2314  case JOIN_SEMI:
2315  case JOIN_ANTI:
2316  partexpr = list_copy(outer_expr);
2317  nullable_partexpr = list_copy(outer_null_expr);
2318  break;
2319 
2320  /*
2321  * A join relation resulting from a LEFT OUTER JOIN likewise
2322  * may be regarded as partitioned on the (non-nullable) outer
2323  * relation keys. The inner (nullable) relation keys are okay
2324  * as partition keys for further joins as long as they involve
2325  * strict join operators.
2326  */
2327  case JOIN_LEFT:
2328  partexpr = list_copy(outer_expr);
2329  nullable_partexpr = list_concat_copy(inner_expr,
2330  outer_null_expr);
2331  nullable_partexpr = list_concat(nullable_partexpr,
2332  inner_null_expr);
2333  break;
2334 
2335  /*
2336  * For FULL OUTER JOINs, both relations are nullable, so the
2337  * resulting join relation may be regarded as partitioned on
2338  * either of inner and outer relation keys, but only for joins
2339  * that involve strict join operators.
2340  */
2341  case JOIN_FULL:
2342  nullable_partexpr = list_concat_copy(outer_expr,
2343  inner_expr);
2344  nullable_partexpr = list_concat(nullable_partexpr,
2345  outer_null_expr);
2346  nullable_partexpr = list_concat(nullable_partexpr,
2347  inner_null_expr);
2348 
2349  /*
2350  * Also add CoalesceExprs corresponding to each possible
2351  * full-join output variable (that is, left side coalesced to
2352  * right side), so that we can match equijoin expressions
2353  * using those variables. We really only need these for
2354  * columns merged by JOIN USING, and only with the pairs of
2355  * input items that correspond to the data structures that
2356  * parse analysis would build for such variables. But it's
2357  * hard to tell which those are, so just make all the pairs.
2358  * Extra items in the nullable_partexprs list won't cause big
2359  * problems. (It's possible that such items will get matched
2360  * to user-written COALESCEs, but it should still be valid to
2361  * partition on those, since they're going to be either the
2362  * partition column or NULL; it's the same argument as for
2363  * partitionwise nesting of any outer join.) We assume no
2364  * type coercions are needed to make the coalesce expressions,
2365  * since columns of different types won't have gotten
2366  * classified as the same PartitionScheme. Note that we
2367  * intentionally leave out the varnullingrels decoration that
2368  * would ordinarily appear on the Vars inside these
2369  * CoalesceExprs, because have_partkey_equi_join will strip
2370  * varnullingrels from the expressions it will compare to the
2371  * partexprs.
2372  */
2373  foreach(lc, list_concat_copy(outer_expr, outer_null_expr))
2374  {
2375  Node *larg = (Node *) lfirst(lc);
2376  ListCell *lc2;
2377 
2378  foreach(lc2, list_concat_copy(inner_expr, inner_null_expr))
2379  {
2380  Node *rarg = (Node *) lfirst(lc2);
2382 
2383  c->coalescetype = exprType(larg);
2384  c->coalescecollid = exprCollation(larg);
2385  c->args = list_make2(larg, rarg);
2386  c->location = -1;
2387  nullable_partexpr = lappend(nullable_partexpr, c);
2388  }
2389  }
2390  break;
2391 
2392  default:
2393  elog(ERROR, "unrecognized join type: %d", (int) jointype);
2394  }
2395 
2396  joinrel->partexprs[cnt] = partexpr;
2397  joinrel->nullable_partexprs[cnt] = nullable_partexpr;
2398  }
2399 }
2400 
2401 /*
2402  * build_child_join_reltarget
2403  * Set up a child-join relation's reltarget from a parent-join relation.
2404  */
2405 static void
2407  RelOptInfo *parentrel,
2408  RelOptInfo *childrel,
2409  int nappinfos,
2410  AppendRelInfo **appinfos)
2411 {
2412  /* Build the targetlist */
2413  childrel->reltarget->exprs = (List *)
2415  (Node *) parentrel->reltarget->exprs,
2416  nappinfos, appinfos);
2417 
2418  /* Set the cost and width fields */
2419  childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
2420  childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
2421  childrel->reltarget->width = parentrel->reltarget->width;
2422 }
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:733
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:196
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:554
uint32 bitmap_hash(const void *key, Size keysize)
Definition: bitmapset.c:1432
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:1230
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1161
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: bitmapset.c:1442
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:641
#define bms_is_empty(a)
Definition: bitmapset.h:118
unsigned int uint32
Definition: c.h:493
signed int int32
Definition: c.h:481
unsigned int Index
Definition: c.h:601
#define OidIsValid(objectId)
Definition: c.h:762
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:733
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:5299
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:5380
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:5348
bool enable_partitionwise_join
Definition: costsize.c:148
int32 clamp_width_est(int64 tuple_width)
Definition: costsize.c:231
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1492
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: equivclass.c:1392
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
Definition: equivclass.c:2754
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:3103
#define palloc0_array(type, count)
Definition: fe_memutils.h:65
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_COMPARE
Definition: hsearch.h:99
#define HASH_FUNCTION
Definition: hsearch.h:98
bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, RangeTblEntry *childRTE, AppendRelInfo *appinfo)
Definition: inherit.c:832
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1363
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:339
List * list_copy(const List *oldlist)
Definition: list.c:1573
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1356
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
bool op_strict(Oid opno)
Definition: lsyscache.c:1455
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:66
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
void pfree(void *pointer)
Definition: mcxt.c:1508
void * palloc0(Size size)
Definition: mcxt.c:1334
MemoryContext CurrentMemoryContext
Definition: mcxt.c:131
Oid GetUserId(void)
Definition: miscinit.c:514
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:788
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define copyObject(obj)
Definition: nodes.h:223
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:327
#define makeNode(_type_)
Definition: nodes.h:155
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
JoinType
Definition: nodes.h:278
@ JOIN_SEMI
Definition: nodes.h:297
@ JOIN_FULL
Definition: nodes.h:285
@ JOIN_INNER
Definition: nodes.h:283
@ JOIN_LEFT
Definition: nodes.h:284
@ JOIN_ANTI
Definition: nodes.h:298
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:109
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:870
@ RTE_JOIN
Definition: parsenodes.h:1013
@ RTE_CTE
Definition: parsenodes.h:1017
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1018
@ RTE_VALUES
Definition: parsenodes.h:1016
@ RTE_SUBQUERY
Definition: parsenodes.h:1012
@ RTE_RESULT
Definition: parsenodes.h:1019
@ RTE_FUNCTION
Definition: parsenodes.h:1014
@ RTE_TABLEFUNC
Definition: parsenodes.h:1015
@ RTE_RELATION
Definition: parsenodes.h:1011
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:2242
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2698
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1043
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1649
Bitmapset * Relids
Definition: pathnodes.h:30
UpperRelationKind
Definition: pathnodes.h:70
@ RELOPT_BASEREL
Definition: pathnodes.h:812
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:814
@ RELOPT_UPPER_REL
Definition: pathnodes.h:816
@ RELOPT_JOINREL
Definition: pathnodes.h:813
@ RELOPT_OTHER_JOINREL
Definition: pathnodes.h:815
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:839
void * arg
#define PARTITION_MAX_KEYS
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define list_make2(x1, x2)
Definition: pg_list.h:214
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition: placeholder.c:83
void add_placeholders_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: placeholder.c:373
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition: plancat.c:115
#define InvalidOid
Definition: postgres_ext.h:36
char * c
#define ROWID_VAR
Definition: primnodes.h:225
static void build_joinrel_partition_info(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: relnode.c:1998
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:94
static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
Definition: relnode.c:2262
static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, bool can_null)
Definition: relnode.c:1105
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1550
RelOptInfo * build_join_rel(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, List **restrictlist_ptr)
Definition: relnode.c:658
static bool have_partkey_equi_join(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition: relnode.c:2071
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1027
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:479
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:407
ParamPathInfo * get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
Definition: relnode.c:1849
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:192
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1514
void expand_planner_arrays(PlannerInfo *root, int add_size)
Definition: relnode.c:163
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:520
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:1652
RelOptInfo * find_base_rel_ignore_join(PlannerInfo *root, int relid)
Definition: relnode.c:447
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:2406
RelOptInfo * find_base_rel_noerr(PlannerInfo *root, int relid)
Definition: relnode.c:429
static List * build_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: relnode.c:1290
static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
Definition: relnode.c:2213
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:582
RelOptInfo * build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo)
Definition: relnode.c:874
struct JoinHashEntry JoinHashEntry
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1327
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1882
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1463
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:620
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:1411
static List * subbuild_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, Relids both_input_relids, List *new_restrictlist)
Definition: relnode.c:1345
Bitmapset * get_param_path_clause_serials(Path *path)
Definition: relnode.c:1903
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:670
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
static pg_noinline void Size size
Definition: slab.c:607
List * colnames
Definition: primnodes.h:51
List * subpaths
Definition: pathnodes.h:1912
Index child_relid
Definition: pathnodes.h:2944
Index parent_relid
Definition: pathnodes.h:2943
Size keysize
Definition: hsearch.h:75
HashValueFunc hash
Definition: hsearch.h:78
Size entrysize
Definition: hsearch.h:76
HashCompareFunc match
Definition: hsearch.h:80
MemoryContext hcxt
Definition: hsearch.h:86
Definition: dynahash.c:220
Relids join_relids
Definition: relnode.c:40
RelOptInfo * join_rel
Definition: relnode.c:41
Path * outerjoinpath
Definition: pathnodes.h:2054
Path * innerjoinpath
Definition: pathnodes.h:2055
List * joinrestrictinfo
Definition: pathnodes.h:2057
Definition: pg_list.h:54
Definition: nodes.h:129
Oid opno
Definition: primnodes.h:774
List * args
Definition: primnodes.h:792
Cardinality ppi_rows
Definition: pathnodes.h:1560
List * ppi_clauses
Definition: pathnodes.h:1561
Bitmapset * ppi_serials
Definition: pathnodes.h:1562
Relids ppi_req_outer
Definition: pathnodes.h:1559
List * exprs
Definition: pathnodes.h:1513
QualCost cost
Definition: pathnodes.h:1519
Relids ph_needed
Definition: pathnodes.h:3068
Relids phnullingrels
Definition: pathnodes.h:2768
List * join_rel_list
Definition: pathnodes.h:277
int simple_rel_array_size
Definition: pathnodes.h:229
Relids outer_join_rels
Definition: pathnodes.h:258
List * row_identity_vars
Definition: pathnodes.h:365
List * append_rel_list
Definition: pathnodes.h:362
Query * parse
Definition: pathnodes.h:199
Selectivity tuple_fraction
Definition: pathnodes.h:478
int join_cur_level
Definition: pathnodes.h:293
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
List * rtable
Definition: parsenodes.h:168
Alias * eref
Definition: parsenodes.h:1205
JoinType jointype
Definition: parsenodes.h:1132
RTEKind rtekind
Definition: parsenodes.h:1030
List * baserestrictinfo
Definition: pathnodes.h:966
bool consider_param_startup
Definition: pathnodes.h:870
List * subplan_params
Definition: pathnodes.h:935
List * ppilist
Definition: pathnodes.h:884
bool useridiscurrent
Definition: pathnodes.h:949
uint32 amflags
Definition: pathnodes.h:939
List * joininfo
Definition: pathnodes.h:972
Bitmapset * notnullattnums
Definition: pathnodes.h:917
List * partition_qual
Definition: pathnodes.h:1008
Relids relids
Definition: pathnodes.h:856
struct PathTarget * reltarget
Definition: pathnodes.h:878
Index relid
Definition: pathnodes.h:903
List * statlist
Definition: pathnodes.h:927
List * lateral_vars
Definition: pathnodes.h:921
List * unique_for_rels
Definition: pathnodes.h:958
Cardinality tuples
Definition: pathnodes.h:930
bool consider_parallel
Definition: pathnodes.h:872
Relids top_parent_relids
Definition: pathnodes.h:990
bool partbounds_merged
Definition: pathnodes.h:1006
BlockNumber pages
Definition: pathnodes.h:929
Relids lateral_relids
Definition: pathnodes.h:898
List * cheapest_parameterized_paths
Definition: pathnodes.h:889
List * pathlist
Definition: pathnodes.h:883
RelOptKind reloptkind
Definition: pathnodes.h:850
List * indexlist
Definition: pathnodes.h:925
struct Path * cheapest_unique_path
Definition: pathnodes.h:888
Relids lateral_referencers
Definition: pathnodes.h:923
struct Path * cheapest_startup_path
Definition: pathnodes.h:886
QualCost baserestrictcost
Definition: pathnodes.h:968
struct Path * cheapest_total_path
Definition: pathnodes.h:887
Oid userid
Definition: pathnodes.h:947
List * non_unique_for_rels
Definition: pathnodes.h:960
Bitmapset * eclass_indexes
Definition: pathnodes.h:933
Relids all_partrels
Definition: pathnodes.h:1022
Relids direct_lateral_relids
Definition: pathnodes.h:896
bool has_eclass_joins
Definition: pathnodes.h:974
Oid serverid
Definition: pathnodes.h:945
bool consider_startup
Definition: pathnodes.h:868
Bitmapset * live_parts
Definition: pathnodes.h:1020
int rel_parallel_workers
Definition: pathnodes.h:937
bool consider_partitionwise_join
Definition: pathnodes.h:980
List * partial_pathlist
Definition: pathnodes.h:885
PlannerInfo * subroot
Definition: pathnodes.h:934
AttrNumber max_attr
Definition: pathnodes.h:911
Relids nulling_relids
Definition: pathnodes.h:919
Index baserestrict_min_security
Definition: pathnodes.h:970
double allvisfrac
Definition: pathnodes.h:931
Cardinality rows
Definition: pathnodes.h:862
AttrNumber min_attr
Definition: pathnodes.h:909
RTEKind rtekind
Definition: pathnodes.h:907
Relids required_relids
Definition: pathnodes.h:2572
int rinfo_serial
Definition: pathnodes.h:2613
Relids incompatible_relids
Definition: pathnodes.h:2575
Expr * clause
Definition: pathnodes.h:2541
bool has_clone
Definition: pathnodes.h:2553
Relids commute_above_r
Definition: pathnodes.h:2875
Relids syn_lefthand
Definition: pathnodes.h:2870
JoinType jointype
Definition: pathnodes.h:2872
Relids syn_righthand
Definition: pathnodes.h:2871
Definition: primnodes.h:234
AttrNumber varattno
Definition: primnodes.h:246
int varno
Definition: primnodes.h:241
Definition: regcomp.c:281
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:681