PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
relnode.c File Reference
#include "postgres.h"
#include <limits.h>
#include "miscadmin.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h"
#include "utils/hsearch.h"
Include dependency graph for relnode.c:

Go to the source code of this file.

Data Structures

struct  JoinHashEntry
 

Typedefs

typedef struct JoinHashEntry JoinHashEntry
 

Functions

static void build_joinrel_tlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel)
 
static Listbuild_joinrel_restrictlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static void build_joinrel_joinlist (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static Listsubbuild_joinrel_restrictlist (RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
 
static Listsubbuild_joinrel_joinlist (RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
 
static void set_foreign_rel_properties (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static void add_join_rel (PlannerInfo *root, RelOptInfo *joinrel)
 
void setup_simple_rel_arrays (PlannerInfo *root)
 
RelOptInfobuild_simple_rel (PlannerInfo *root, int relid, RelOptInfo *parent)
 
RelOptInfofind_base_rel (PlannerInfo *root, int relid)
 
static void build_join_rel_hash (PlannerInfo *root)
 
RelOptInfofind_join_rel (PlannerInfo *root, Relids relids)
 
RelOptInfobuild_join_rel (PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List **restrictlist_ptr)
 
Relids min_join_parameterization (PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
RelOptInfobuild_empty_join_rel (PlannerInfo *root)
 
RelOptInfofetch_upper_rel (PlannerInfo *root, UpperRelationKind kind, Relids relids)
 
AppendRelInfofind_childrel_appendrelinfo (PlannerInfo *root, RelOptInfo *rel)
 
Relids find_childrel_parents (PlannerInfo *root, RelOptInfo *rel)
 
ParamPathInfoget_baserel_parampathinfo (PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
 
ParamPathInfoget_joinrel_parampathinfo (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, Relids required_outer, List **restrict_clauses)
 
ParamPathInfoget_appendrel_parampathinfo (RelOptInfo *appendrel, Relids required_outer)
 
ParamPathInfofind_param_path_info (RelOptInfo *rel, Relids required_outer)
 

Typedef Documentation

Function Documentation

static void add_join_rel ( PlannerInfo root,
RelOptInfo joinrel 
)
static

Definition at line 437 of file relnode.c.

References Assert, HASH_ENTER, hash_search(), JoinHashEntry::join_rel, PlannerInfo::join_rel_hash, PlannerInfo::join_rel_list, lappend(), and RelOptInfo::relids.

Referenced by build_join_rel().

438 {
439  /* GEQO requires us to append the new joinrel to the end of the list! */
440  root->join_rel_list = lappend(root->join_rel_list, joinrel);
441 
442  /* store it into the auxiliary hashtable if there is one. */
443  if (root->join_rel_hash)
444  {
445  JoinHashEntry *hentry;
446  bool found;
447 
448  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
449  &(joinrel->relids),
450  HASH_ENTER,
451  &found);
452  Assert(!found);
453  hentry->join_rel = joinrel;
454  }
455 }
List * join_rel_list
Definition: relation.h:215
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
RelOptInfo * join_rel
Definition: relnode.c:34
Relids relids
Definition: relation.h:571
List * lappend(List *list, void *datum)
Definition: list.c:128
#define Assert(condition)
Definition: c.h:664
struct HTAB * join_rel_hash
Definition: relation.h:216
RelOptInfo* build_empty_join_rel ( PlannerInfo root)

Definition at line 918 of file relnode.c.

References Assert, create_empty_pathtarget(), PlannerInfo::join_rel_list, lappend(), makeNode, NIL, RelOptInfo::relids, RELOPT_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, RelOptInfo::rows, RTE_JOIN, and RelOptInfo::rtekind.

Referenced by query_planner().

919 {
920  RelOptInfo *joinrel;
921 
922  /* The dummy join relation should be the only one ... */
923  Assert(root->join_rel_list == NIL);
924 
925  joinrel = makeNode(RelOptInfo);
926  joinrel->reloptkind = RELOPT_JOINREL;
927  joinrel->relids = NULL; /* empty set */
928  joinrel->rows = 1; /* we produce one row for such cases */
929  joinrel->rtekind = RTE_JOIN;
930  joinrel->reltarget = create_empty_pathtarget();
931 
932  root->join_rel_list = lappend(root->join_rel_list, joinrel);
933 
934  return joinrel;
935 }
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:653
#define NIL
Definition: pg_list.h:69
RelOptKind reloptkind
Definition: relation.h:568
List * join_rel_list
Definition: relation.h:215
Relids relids
Definition: relation.h:571
List * lappend(List *list, void *datum)
Definition: list.c:128
RTEKind rtekind
Definition: relation.h:601
double rows
Definition: relation.h:574
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:664
struct PathTarget * reltarget
Definition: relation.h:582
RelOptInfo* build_join_rel ( PlannerInfo root,
Relids  joinrelids,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
SpecialJoinInfo sjinfo,
List **  restrictlist_ptr 
)

Definition at line 474 of file relnode.c.

References add_join_rel(), add_placeholders_to_joinrel(), RelOptInfo::allvisfrac, Assert, RelOptInfo::attr_needed, RelOptInfo::attr_widths, RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_del_members(), bms_is_empty(), bms_num_members(), bms_union(), RelOptInfo::boundinfo, build_joinrel_joinlist(), build_joinrel_restrictlist(), build_joinrel_tlist(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::cheapest_unique_path, RelOptInfo::consider_parallel, RelOptInfo::consider_param_startup, RelOptInfo::consider_startup, create_empty_pathtarget(), RelOptInfo::direct_lateral_relids, PathTarget::exprs, RelOptInfo::fdw_private, RelOptInfo::fdwroutine, find_join_rel(), RelOptInfo::has_eclass_joins, has_relevant_eclass_joinclause(), RelOptInfo::indexlist, InvalidOid, is_parallel_safe(), PlannerInfo::join_cur_level, PlannerInfo::join_rel_level, RelOptInfo::joininfo, lappend(), RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, min_join_parameterization(), NIL, RelOptInfo::non_unique_for_rels, RelOptInfo::nparts, RelOptInfo::pages, RelOptInfo::part_rels, RelOptInfo::part_scheme, RelOptInfo::partexprs, RelOptInfo::partial_pathlist, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::ppilist, RelOptInfo::rel_parallel_workers, RelOptInfo::relid, RelOptInfo::relids, RELOPT_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, RelOptInfo::rows, RTE_JOIN, RelOptInfo::rtekind, RelOptInfo::serverid, set_foreign_rel_properties(), set_joinrel_size_estimates(), QualCost::startup, RelOptInfo::statlist, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, PlannerInfo::tuple_fraction, RelOptInfo::tuples, RelOptInfo::unique_for_rels, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by make_join_rel().

480 {
481  RelOptInfo *joinrel;
482  List *restrictlist;
483 
484  /*
485  * See if we already have a joinrel for this set of base rels.
486  */
487  joinrel = find_join_rel(root, joinrelids);
488 
489  if (joinrel)
490  {
491  /*
492  * Yes, so we only need to figure the restrictlist for this particular
493  * pair of component relations.
494  */
495  if (restrictlist_ptr)
496  *restrictlist_ptr = build_joinrel_restrictlist(root,
497  joinrel,
498  outer_rel,
499  inner_rel);
500  return joinrel;
501  }
502 
503  /*
504  * Nope, so make one.
505  */
506  joinrel = makeNode(RelOptInfo);
507  joinrel->reloptkind = RELOPT_JOINREL;
508  joinrel->relids = bms_copy(joinrelids);
509  joinrel->rows = 0;
510  /* cheap startup cost is interesting iff not all tuples to be retrieved */
511  joinrel->consider_startup = (root->tuple_fraction > 0);
512  joinrel->consider_param_startup = false;
513  joinrel->consider_parallel = false;
514  joinrel->reltarget = create_empty_pathtarget();
515  joinrel->pathlist = NIL;
516  joinrel->ppilist = NIL;
517  joinrel->partial_pathlist = NIL;
518  joinrel->cheapest_startup_path = NULL;
519  joinrel->cheapest_total_path = NULL;
520  joinrel->cheapest_unique_path = NULL;
522  /* init direct_lateral_relids from children; we'll finish it up below */
523  joinrel->direct_lateral_relids =
524  bms_union(outer_rel->direct_lateral_relids,
525  inner_rel->direct_lateral_relids);
526  joinrel->lateral_relids = min_join_parameterization(root, joinrel->relids,
527  outer_rel, inner_rel);
528  joinrel->relid = 0; /* indicates not a baserel */
529  joinrel->rtekind = RTE_JOIN;
530  joinrel->min_attr = 0;
531  joinrel->max_attr = 0;
532  joinrel->attr_needed = NULL;
533  joinrel->attr_widths = NULL;
534  joinrel->lateral_vars = NIL;
535  joinrel->lateral_referencers = NULL;
536  joinrel->indexlist = NIL;
537  joinrel->statlist = NIL;
538  joinrel->pages = 0;
539  joinrel->tuples = 0;
540  joinrel->allvisfrac = 0;
541  joinrel->subroot = NULL;
542  joinrel->subplan_params = NIL;
543  joinrel->rel_parallel_workers = -1;
544  joinrel->serverid = InvalidOid;
545  joinrel->userid = InvalidOid;
546  joinrel->useridiscurrent = false;
547  joinrel->fdwroutine = NULL;
548  joinrel->fdw_private = NULL;
549  joinrel->unique_for_rels = NIL;
550  joinrel->non_unique_for_rels = NIL;
551  joinrel->baserestrictinfo = NIL;
552  joinrel->baserestrictcost.startup = 0;
553  joinrel->baserestrictcost.per_tuple = 0;
554  joinrel->baserestrict_min_security = UINT_MAX;
555  joinrel->joininfo = NIL;
556  joinrel->has_eclass_joins = false;
557  joinrel->top_parent_relids = NULL;
558  joinrel->part_scheme = NULL;
559  joinrel->nparts = 0;
560  joinrel->boundinfo = NULL;
561  joinrel->part_rels = NULL;
562  joinrel->partexprs = NULL;
563 
564  /* Compute information relevant to the foreign relations. */
565  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
566 
567  /*
568  * Create a new tlist containing just the vars that need to be output from
569  * this join (ie, are needed for higher joinclauses or final output).
570  *
571  * NOTE: the tlist order for a join rel will depend on which pair of outer
572  * and inner rels we first try to build it from. But the contents should
573  * be the same regardless.
574  */
575  build_joinrel_tlist(root, joinrel, outer_rel);
576  build_joinrel_tlist(root, joinrel, inner_rel);
577  add_placeholders_to_joinrel(root, joinrel, outer_rel, inner_rel);
578 
579  /*
580  * add_placeholders_to_joinrel also took care of adding the ph_lateral
581  * sets of any PlaceHolderVars computed here to direct_lateral_relids, so
582  * now we can finish computing that. This is much like the computation of
583  * the transitively-closed lateral_relids in min_join_parameterization,
584  * except that here we *do* have to consider the added PHVs.
585  */
586  joinrel->direct_lateral_relids =
587  bms_del_members(joinrel->direct_lateral_relids, joinrel->relids);
588  if (bms_is_empty(joinrel->direct_lateral_relids))
589  joinrel->direct_lateral_relids = NULL;
590 
591  /*
592  * Construct restrict and join clause lists for the new joinrel. (The
593  * caller might or might not need the restrictlist, but I need it anyway
594  * for set_joinrel_size_estimates().)
595  */
596  restrictlist = build_joinrel_restrictlist(root, joinrel,
597  outer_rel, inner_rel);
598  if (restrictlist_ptr)
599  *restrictlist_ptr = restrictlist;
600  build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
601 
602  /*
603  * This is also the right place to check whether the joinrel has any
604  * pending EquivalenceClass joins.
605  */
606  joinrel->has_eclass_joins = has_relevant_eclass_joinclause(root, joinrel);
607 
608  /*
609  * Set estimates of the joinrel's size.
610  */
611  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
612  sjinfo, restrictlist);
613 
614  /*
615  * Set the consider_parallel flag if this joinrel could potentially be
616  * scanned within a parallel worker. If this flag is false for either
617  * inner_rel or outer_rel, then it must be false for the joinrel also.
618  * Even if both are true, there might be parallel-restricted expressions
619  * in the targetlist or quals.
620  *
621  * Note that if there are more than two rels in this relation, they could
622  * be divided between inner_rel and outer_rel in any arbitrary way. We
623  * assume this doesn't matter, because we should hit all the same baserels
624  * and joinclauses while building up to this joinrel no matter which we
625  * take; therefore, we should make the same decision here however we get
626  * here.
627  */
628  if (inner_rel->consider_parallel && outer_rel->consider_parallel &&
629  is_parallel_safe(root, (Node *) restrictlist) &&
630  is_parallel_safe(root, (Node *) joinrel->reltarget->exprs))
631  joinrel->consider_parallel = true;
632 
633  /* Add the joinrel to the PlannerInfo. */
634  add_join_rel(root, joinrel);
635 
636  /*
637  * Also, if dynamic-programming join search is active, add the new joinrel
638  * to the appropriate sublist. Note: you might think the Assert on number
639  * of members should be for equality, but some of the level 1 rels might
640  * have been joinrels already, so we can only assert <=.
641  */
642  if (root->join_rel_level)
643  {
644  Assert(root->join_cur_level > 0);
645  Assert(root->join_cur_level <= bms_num_members(joinrel->relids));
646  root->join_rel_level[root->join_cur_level] =
647  lappend(root->join_rel_level[root->join_cur_level], joinrel);
648  }
649 
650  return joinrel;
651 }
bool has_eclass_joins
Definition: relation.h:637
struct Path * cheapest_unique_path
Definition: relation.h:590
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:653
#define NIL
Definition: pg_list.h:69
int join_cur_level
Definition: relation.h:226
List * unique_for_rels
Definition: relation.h:626
List * statlist
Definition: relation.h:609
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
RelOptKind reloptkind
Definition: relation.h:568
Relids * attr_needed
Definition: relation.h:604
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:336
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:4117
struct Path * cheapest_startup_path
Definition: relation.h:588
Oid userid
Definition: relation.h:619
double tuples
Definition: relation.h:611
List * baserestrictinfo
Definition: relation.h:631
bool consider_param_startup
Definition: relation.h:578
Definition: nodes.h:509
List * partial_pathlist
Definition: relation.h:587
List * cheapest_parameterized_paths
Definition: relation.h:591
Index baserestrict_min_security
Definition: relation.h:633
bool useridiscurrent
Definition: relation.h:620
void add_placeholders_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: placeholder.c:411
Cost startup
Definition: relation.h:45
double allvisfrac
Definition: relation.h:612
PlannerInfo * subroot
Definition: relation.h:613
bool consider_startup
Definition: relation.h:577
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:1087
Relids lateral_relids
Definition: relation.h:596
Cost per_tuple
Definition: relation.h:46
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:399
double tuple_fraction
Definition: relation.h:294
List ** partexprs
Definition: relation.h:648
static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel)
Definition: relnode.c:705
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:662
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:605
struct Path * cheapest_total_path
Definition: relation.h:589
static List * build_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:791
List * joininfo
Definition: relation.h:635
struct FdwRoutine * fdwroutine
Definition: relation.h:622
int nparts
Definition: relation.h:644
Relids relids
Definition: relation.h:571
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:2359
List * non_unique_for_rels
Definition: relation.h:628
List * ppilist
Definition: relation.h:586
Index relid
Definition: relation.h:599
List * lappend(List *list, void *datum)
Definition: list.c:128
Relids lateral_referencers
Definition: relation.h:607
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
Oid serverid
Definition: relation.h:618
List * exprs
Definition: relation.h:938
Relids direct_lateral_relids
Definition: relation.h:595
int rel_parallel_workers
Definition: relation.h:615
struct PartitionBoundInfoData * boundinfo
Definition: relation.h:645
RTEKind rtekind
Definition: relation.h:601
List * indexlist
Definition: relation.h:608
double rows
Definition: relation.h:574
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: relation.h:623
#define makeNode(_type_)
Definition: nodes.h:557
BlockNumber pages
Definition: relation.h:610
#define Assert(condition)
Definition: c.h:664
List ** join_rel_level
Definition: relation.h:225
List * lateral_vars
Definition: relation.h:606
struct RelOptInfo ** part_rels
Definition: relation.h:646
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
bool consider_parallel
Definition: relation.h:579
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:817
AttrNumber max_attr
Definition: relation.h:603
PartitionScheme part_scheme
Definition: relation.h:643
List * pathlist
Definition: relation.h:585
int32 * attr_widths
Definition: relation.h:605
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:582
QualCost baserestrictcost
Definition: relation.h:632
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:437
List * subplan_params
Definition: relation.h:614
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:821
Relids top_parent_relids
Definition: relation.h:640
AttrNumber min_attr
Definition: relation.h:602
static void build_join_rel_hash ( PlannerInfo root)
static

Definition at line 294 of file relnode.c.

References Assert, bitmap_hash(), bitmap_match(), CurrentMemoryContext, HASHCTL::entrysize, HASHCTL::hash, HASH_COMPARE, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FUNCTION, hash_search(), HASHCTL::hcxt, JoinHashEntry::join_rel, PlannerInfo::join_rel_hash, PlannerInfo::join_rel_list, HASHCTL::keysize, lfirst, HASHCTL::match, MemSet, and RelOptInfo::relids.

Referenced by find_join_rel().

295 {
296  HTAB *hashtab;
297  HASHCTL hash_ctl;
298  ListCell *l;
299 
300  /* Create the hash table */
301  MemSet(&hash_ctl, 0, sizeof(hash_ctl));
302  hash_ctl.keysize = sizeof(Relids);
303  hash_ctl.entrysize = sizeof(JoinHashEntry);
304  hash_ctl.hash = bitmap_hash;
305  hash_ctl.match = bitmap_match;
306  hash_ctl.hcxt = CurrentMemoryContext;
307  hashtab = hash_create("JoinRelHashTable",
308  256L,
309  &hash_ctl,
311 
312  /* Insert all the already-existing joinrels */
313  foreach(l, root->join_rel_list)
314  {
315  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
316  JoinHashEntry *hentry;
317  bool found;
318 
319  hentry = (JoinHashEntry *) hash_search(hashtab,
320  &(rel->relids),
321  HASH_ENTER,
322  &found);
323  Assert(!found);
324  hentry->join_rel = rel;
325  }
326 
327  root->join_rel_hash = hashtab;
328 }
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:846
List * join_rel_list
Definition: relation.h:215
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
Definition: dynahash.c:208
struct JoinHashEntry JoinHashEntry
uint32 bitmap_hash(const void *key, Size keysize)
Definition: hashfn.c:76
RelOptInfo * join_rel
Definition: relnode.c:34
Relids relids
Definition: relation.h:571
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: hashfn.c:86
Bitmapset * Relids
Definition: relation.h:28
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size keysize
Definition: hsearch.h:72
HashCompareFunc match
Definition: hsearch.h:75
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
#define HASH_COMPARE
Definition: hsearch.h:90
struct HTAB * join_rel_hash
Definition: relation.h:216
HashValueFunc hash
Definition: hsearch.h:74
#define HASH_FUNCTION
Definition: hsearch.h:89
static void build_joinrel_joinlist ( RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel 
)
static

Definition at line 821 of file relnode.c.

References RelOptInfo::joininfo, NIL, and subbuild_joinrel_joinlist().

Referenced by build_join_rel().

824 {
825  List *result;
826 
827  /*
828  * Collect all the clauses that syntactically belong above this level,
829  * eliminating any duplicates (important since we will see many of the
830  * same clauses arriving from both input relations).
831  */
832  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
833  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
834 
835  joinrel->joininfo = result;
836 }
#define NIL
Definition: pg_list.h:69
List * joininfo
Definition: relation.h:635
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:873
Definition: pg_list.h:45
static List * build_joinrel_restrictlist ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel 
)
static

Definition at line 791 of file relnode.c.

References generate_join_implied_equalities(), RelOptInfo::joininfo, list_concat(), NIL, RelOptInfo::relids, and subbuild_joinrel_restrictlist().

Referenced by build_join_rel().

795 {
796  List *result;
797 
798  /*
799  * Collect all the clauses that syntactically belong at this level,
800  * eliminating any duplicates (important since we will see many of the
801  * same clauses arriving from both input relations).
802  */
803  result = subbuild_joinrel_restrictlist(joinrel, outer_rel->joininfo, NIL);
804  result = subbuild_joinrel_restrictlist(joinrel, inner_rel->joininfo, result);
805 
806  /*
807  * Add on any clauses derived from EquivalenceClasses. These cannot be
808  * redundant with the clauses in the joininfo lists, so don't bother
809  * checking.
810  */
811  result = list_concat(result,
813  joinrel->relids,
814  outer_rel->relids,
815  inner_rel));
816 
817  return result;
818 }
#define NIL
Definition: pg_list.h:69
static List * subbuild_joinrel_restrictlist(RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
Definition: relnode.c:839
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1033
List * joininfo
Definition: relation.h:635
Relids relids
Definition: relation.h:571
Definition: pg_list.h:45
static void build_joinrel_tlist ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo input_rel 
)
static

Definition at line 705 of file relnode.c.

References RelOptInfo::attr_needed, RelOptInfo::attr_widths, bms_nonempty_difference(), elog, ERROR, PathTarget::exprs, find_base_rel(), IsA, lappend(), lfirst, RelOptInfo::min_attr, nodeTag, RelOptInfo::relids, RelOptInfo::reltarget, Var::varattno, Var::varno, and PathTarget::width.

Referenced by build_join_rel().

707 {
708  Relids relids = joinrel->relids;
709  ListCell *vars;
710 
711  foreach(vars, input_rel->reltarget->exprs)
712  {
713  Var *var = (Var *) lfirst(vars);
714  RelOptInfo *baserel;
715  int ndx;
716 
717  /*
718  * Ignore PlaceHolderVars in the input tlists; we'll make our own
719  * decisions about whether to copy them.
720  */
721  if (IsA(var, PlaceHolderVar))
722  continue;
723 
724  /*
725  * Otherwise, anything in a baserel or joinrel targetlist ought to be
726  * a Var. (More general cases can only appear in appendrel child
727  * rels, which will never be seen here.)
728  */
729  if (!IsA(var, Var))
730  elog(ERROR, "unexpected node type in rel targetlist: %d",
731  (int) nodeTag(var));
732 
733  /* Get the Var's original base rel */
734  baserel = find_base_rel(root, var->varno);
735 
736  /* Is it still needed above this joinrel? */
737  ndx = var->varattno - baserel->min_attr;
738  if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
739  {
740  /* Yup, add it to the output */
741  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs, var);
742  /* Vars have cost zero, so no need to adjust reltarget->cost */
743  joinrel->reltarget->width += baserel->attr_widths[ndx];
744  }
745  }
746 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Relids * attr_needed
Definition: relation.h:604
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
#define ERROR
Definition: elog.h:43
Relids relids
Definition: relation.h:571
List * lappend(List *list, void *datum)
Definition: list.c:128
Index varno
Definition: primnodes.h:166
List * exprs
Definition: relation.h:938
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:514
int width
Definition: relation.h:941
#define elog
Definition: elog.h:219
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:271
int32 * attr_widths
Definition: relation.h:605
Definition: regcomp.c:224
struct PathTarget * reltarget
Definition: relation.h:582
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
AttrNumber min_attr
Definition: relation.h:602
RelOptInfo* build_simple_rel ( PlannerInfo root,
int  relid,
RelOptInfo parent 
)

Definition at line 91 of file relnode.c.

References RelOptInfo::allvisfrac, PlannerInfo::append_rel_list, Assert, RelOptInfo::attr_needed, RelOptInfo::attr_widths, RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_make_singleton(), RelOptInfo::boundinfo, build_simple_rel(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::cheapest_unique_path, RangeTblEntry::checkAsUser, AppendRelInfo::child_relid, Alias::colnames, RelOptInfo::consider_parallel, RelOptInfo::consider_param_startup, RelOptInfo::consider_startup, create_empty_pathtarget(), RelOptInfo::direct_lateral_relids, elog, RangeTblEntry::eref, ERROR, RelOptInfo::fdw_private, RelOptInfo::fdwroutine, get_relation_info(), RelOptInfo::has_eclass_joins, RelOptInfo::indexlist, RangeTblEntry::inh, InvalidOid, RelOptInfo::joininfo, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, lfirst, list_length(), makeNode, Max, RelOptInfo::max_attr, RelOptInfo::min_attr, NIL, RelOptInfo::non_unique_for_rels, RelOptInfo::nparts, RelOptInfo::pages, palloc(), palloc0(), AppendRelInfo::parent_relid, RelOptInfo::part_rels, RelOptInfo::part_scheme, RelOptInfo::partexprs, RelOptInfo::partial_pathlist, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::ppilist, PlannerInfo::qual_security_level, RelOptInfo::rel_parallel_workers, RelOptInfo::relid, RangeTblEntry::relid, RelOptInfo::relids, RELOPT_BASEREL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, RelOptInfo::rows, RTE_CTE, RTE_FUNCTION, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RelOptInfo::rtekind, RangeTblEntry::rtekind, RangeTblEntry::securityQuals, RelOptInfo::serverid, PlannerInfo::simple_rel_array, PlannerInfo::simple_rte_array, QualCost::startup, RelOptInfo::statlist, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, PlannerInfo::tuple_fraction, RelOptInfo::tuples, RelOptInfo::unique_for_rels, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by add_base_rels_to_query(), build_simple_rel(), plan_cluster_use_sort(), and recurse_set_operations().

92 {
93  RelOptInfo *rel;
94  RangeTblEntry *rte;
95 
96  /* Rel should not exist already */
97  Assert(relid > 0 && relid < root->simple_rel_array_size);
98  if (root->simple_rel_array[relid] != NULL)
99  elog(ERROR, "rel %d already exists", relid);
100 
101  /* Fetch RTE for relation */
102  rte = root->simple_rte_array[relid];
103  Assert(rte != NULL);
104 
105  rel = makeNode(RelOptInfo);
107  rel->relids = bms_make_singleton(relid);
108  rel->rows = 0;
109  /* cheap startup cost is interesting iff not all tuples to be retrieved */
110  rel->consider_startup = (root->tuple_fraction > 0);
111  rel->consider_param_startup = false; /* might get changed later */
112  rel->consider_parallel = false; /* might get changed later */
114  rel->pathlist = NIL;
115  rel->ppilist = NIL;
116  rel->partial_pathlist = NIL;
117  rel->cheapest_startup_path = NULL;
118  rel->cheapest_total_path = NULL;
119  rel->cheapest_unique_path = NULL;
121  rel->direct_lateral_relids = NULL;
122  rel->lateral_relids = NULL;
123  rel->relid = relid;
124  rel->rtekind = rte->rtekind;
125  /* min_attr, max_attr, attr_needed, attr_widths are set below */
126  rel->lateral_vars = NIL;
127  rel->lateral_referencers = NULL;
128  rel->indexlist = NIL;
129  rel->statlist = NIL;
130  rel->pages = 0;
131  rel->tuples = 0;
132  rel->allvisfrac = 0;
133  rel->subroot = NULL;
134  rel->subplan_params = NIL;
135  rel->rel_parallel_workers = -1; /* set up in get_relation_info */
136  rel->serverid = InvalidOid;
137  rel->userid = rte->checkAsUser;
138  rel->useridiscurrent = false;
139  rel->fdwroutine = NULL;
140  rel->fdw_private = NULL;
141  rel->unique_for_rels = NIL;
142  rel->non_unique_for_rels = NIL;
143  rel->baserestrictinfo = NIL;
144  rel->baserestrictcost.startup = 0;
145  rel->baserestrictcost.per_tuple = 0;
146  rel->baserestrict_min_security = UINT_MAX;
147  rel->joininfo = NIL;
148  rel->has_eclass_joins = false;
149  rel->part_scheme = NULL;
150  rel->nparts = 0;
151  rel->boundinfo = NULL;
152  rel->part_rels = NULL;
153  rel->partexprs = NULL;
154 
155  /*
156  * Pass top parent's relids down the inheritance hierarchy. If the parent
157  * has top_parent_relids set, it's a direct or an indirect child of the
158  * top parent indicated by top_parent_relids. By extension this child is
159  * also an indirect child of that parent.
160  */
161  if (parent)
162  {
163  if (parent->top_parent_relids)
164  rel->top_parent_relids = parent->top_parent_relids;
165  else
166  rel->top_parent_relids = bms_copy(parent->relids);
167  }
168  else
169  rel->top_parent_relids = NULL;
170 
171  /* Check type of rtable entry */
172  switch (rte->rtekind)
173  {
174  case RTE_RELATION:
175  /* Table --- retrieve statistics from the system catalogs */
176  get_relation_info(root, rte->relid, rte->inh, rel);
177  break;
178  case RTE_SUBQUERY:
179  case RTE_FUNCTION:
180  case RTE_TABLEFUNC:
181  case RTE_VALUES:
182  case RTE_CTE:
183  case RTE_NAMEDTUPLESTORE:
184 
185  /*
186  * Subquery, function, tablefunc, values list, CTE, or ENR --- set
187  * up attr range and arrays
188  *
189  * Note: 0 is included in range to support whole-row Vars
190  */
191  rel->min_attr = 0;
192  rel->max_attr = list_length(rte->eref->colnames);
193  rel->attr_needed = (Relids *)
194  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
195  rel->attr_widths = (int32 *)
196  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
197  break;
198  default:
199  elog(ERROR, "unrecognized RTE kind: %d",
200  (int) rte->rtekind);
201  break;
202  }
203 
204  /* Save the finished struct in the query's simple_rel_array */
205  root->simple_rel_array[relid] = rel;
206 
207  /*
208  * This is a convenient spot at which to note whether rels participating
209  * in the query have any securityQuals attached. If so, increase
210  * root->qual_security_level to ensure it's larger than the maximum
211  * security level needed for securityQuals.
212  */
213  if (rte->securityQuals)
215  list_length(rte->securityQuals));
216 
217  /*
218  * If this rel is an appendrel parent, recurse to build "other rel"
219  * RelOptInfos for its children. They are "other rels" because they are
220  * not in the main join tree, but we will need RelOptInfos to plan access
221  * to them.
222  */
223  if (rte->inh)
224  {
225  ListCell *l;
226  int nparts = rel->nparts;
227  int cnt_parts = 0;
228 
229  if (nparts > 0)
230  rel->part_rels = (RelOptInfo **)
231  palloc(sizeof(RelOptInfo *) * nparts);
232 
233  foreach(l, root->append_rel_list)
234  {
235  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
236  RelOptInfo *childrel;
237 
238  /* append_rel_list contains all append rels; ignore others */
239  if (appinfo->parent_relid != relid)
240  continue;
241 
242  childrel = build_simple_rel(root, appinfo->child_relid,
243  rel);
244 
245  /* Nothing more to do for an unpartitioned table. */
246  if (!rel->part_scheme)
247  continue;
248 
249  /*
250  * The order of partition OIDs in append_rel_list is the same as
251  * the order in the PartitionDesc, so the order of part_rels will
252  * also match the PartitionDesc. See expand_partitioned_rtentry.
253  */
254  Assert(cnt_parts < nparts);
255  rel->part_rels[cnt_parts] = childrel;
256  cnt_parts++;
257  }
258 
259  /* We should have seen all the child partitions. */
260  Assert(cnt_parts == nparts);
261  }
262 
263  return rel;
264 }
bool has_eclass_joins
Definition: relation.h:637
struct Path * cheapest_unique_path
Definition: relation.h:590
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:653
#define NIL
Definition: pg_list.h:69
List * unique_for_rels
Definition: relation.h:626
List * statlist
Definition: relation.h:609
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
RelOptKind reloptkind
Definition: relation.h:568
Relids * attr_needed
Definition: relation.h:604
List * colnames
Definition: primnodes.h:43
struct Path * cheapest_startup_path
Definition: relation.h:588
Oid userid
Definition: relation.h:619
List * securityQuals
Definition: parsenodes.h:1058
double tuples
Definition: relation.h:611
List * baserestrictinfo
Definition: relation.h:631
bool consider_param_startup
Definition: relation.h:578
List * partial_pathlist
Definition: relation.h:587
List * cheapest_parameterized_paths
Definition: relation.h:591
Index baserestrict_min_security
Definition: relation.h:633
bool useridiscurrent
Definition: relation.h:620
Cost startup
Definition: relation.h:45
double allvisfrac
Definition: relation.h:612
signed int int32
Definition: c.h:246
struct RelOptInfo ** simple_rel_array
Definition: relation.h:179
PlannerInfo * subroot
Definition: relation.h:613
bool consider_startup
Definition: relation.h:577
Relids lateral_relids
Definition: relation.h:596
Cost per_tuple
Definition: relation.h:46
double tuple_fraction
Definition: relation.h:294
List ** partexprs
Definition: relation.h:648
#define ERROR
Definition: elog.h:43
struct Path * cheapest_total_path
Definition: relation.h:589
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:179
List * joininfo
Definition: relation.h:635
struct FdwRoutine * fdwroutine
Definition: relation.h:622
int nparts
Definition: relation.h:644
Relids relids
Definition: relation.h:571
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:91
List * non_unique_for_rels
Definition: relation.h:628
List * ppilist
Definition: relation.h:586
Index relid
Definition: relation.h:599
Bitmapset * Relids
Definition: relation.h:28
RangeTblEntry ** simple_rte_array
Definition: relation.h:188
Relids lateral_referencers
Definition: relation.h:607
Oid serverid
Definition: relation.h:618
Relids direct_lateral_relids
Definition: relation.h:595
void * palloc0(Size size)
Definition: mcxt.c:877
int rel_parallel_workers
Definition: relation.h:615
List * append_rel_list
Definition: relation.h:252
struct PartitionBoundInfoData * boundinfo
Definition: relation.h:645
RTEKind rtekind
Definition: relation.h:601
List * indexlist
Definition: relation.h:608
double rows
Definition: relation.h:574
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: relation.h:623
#define Max(x, y)
Definition: c.h:789
#define makeNode(_type_)
Definition: nodes.h:557
BlockNumber pages
Definition: relation.h:610
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
List * lateral_vars
Definition: relation.h:606
struct RelOptInfo ** part_rels
Definition: relation.h:646
static int list_length(const List *l)
Definition: pg_list.h:89
Index qual_security_level
Definition: relation.h:297
bool consider_parallel
Definition: relation.h:579
RTEKind rtekind
Definition: parsenodes.h:945
AttrNumber max_attr
Definition: relation.h:603
void * palloc(Size size)
Definition: mcxt.c:848
PartitionScheme part_scheme
Definition: relation.h:643
List * pathlist
Definition: relation.h:585
#define elog
Definition: elog.h:219
Index child_relid
Definition: relation.h:2032
Alias * eref
Definition: parsenodes.h:1049
Index parent_relid
Definition: relation.h:2031
int32 * attr_widths
Definition: relation.h:605
struct PathTarget * reltarget
Definition: relation.h:582
QualCost baserestrictcost
Definition: relation.h:632
List * subplan_params
Definition: relation.h:614
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition: plancat.c:105
Relids top_parent_relids
Definition: relation.h:640
AttrNumber min_attr
Definition: relation.h:602
RelOptInfo* fetch_upper_rel ( PlannerInfo root,
UpperRelationKind  kind,
Relids  relids 
)

Definition at line 952 of file relnode.c.

References bms_copy(), bms_equal(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::cheapest_unique_path, RelOptInfo::consider_parallel, RelOptInfo::consider_param_startup, RelOptInfo::consider_startup, create_empty_pathtarget(), lappend(), lfirst, makeNode, NIL, RelOptInfo::pathlist, RelOptInfo::relids, RELOPT_UPPER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, PlannerInfo::tuple_fraction, and PlannerInfo::upper_rels.

Referenced by add_rtes_to_flat_rtable(), create_distinct_paths(), create_grouping_paths(), create_ordered_paths(), create_window_paths(), generate_nonunion_path(), generate_recursion_path(), generate_union_path(), grouping_planner(), inheritance_planner(), make_subplan(), make_union_unique(), plan_set_operations(), preprocess_minmax_aggregates(), recurse_set_operations(), set_subquery_pathlist(), set_subquery_size_estimates(), SS_process_ctes(), standard_planner(), and subquery_planner().

953 {
954  RelOptInfo *upperrel;
955  ListCell *lc;
956 
957  /*
958  * For the moment, our indexing data structure is just a List for each
959  * relation kind. If we ever get so many of one kind that this stops
960  * working well, we can improve it. No code outside this function should
961  * assume anything about how to find a particular upperrel.
962  */
963 
964  /* If we already made this upperrel for the query, return it */
965  foreach(lc, root->upper_rels[kind])
966  {
967  upperrel = (RelOptInfo *) lfirst(lc);
968 
969  if (bms_equal(upperrel->relids, relids))
970  return upperrel;
971  }
972 
973  upperrel = makeNode(RelOptInfo);
974  upperrel->reloptkind = RELOPT_UPPER_REL;
975  upperrel->relids = bms_copy(relids);
976 
977  /* cheap startup cost is interesting iff not all tuples to be retrieved */
978  upperrel->consider_startup = (root->tuple_fraction > 0);
979  upperrel->consider_param_startup = false;
980  upperrel->consider_parallel = false; /* might get changed later */
981  upperrel->reltarget = create_empty_pathtarget();
982  upperrel->pathlist = NIL;
983  upperrel->cheapest_startup_path = NULL;
984  upperrel->cheapest_total_path = NULL;
985  upperrel->cheapest_unique_path = NULL;
986  upperrel->cheapest_parameterized_paths = NIL;
987 
988  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
989 
990  return upperrel;
991 }
struct Path * cheapest_unique_path
Definition: relation.h:590
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:653
#define NIL
Definition: pg_list.h:69
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
RelOptKind reloptkind
Definition: relation.h:568
struct Path * cheapest_startup_path
Definition: relation.h:588
bool consider_param_startup
Definition: relation.h:578
List * cheapest_parameterized_paths
Definition: relation.h:591
bool consider_startup
Definition: relation.h:577
double tuple_fraction
Definition: relation.h:294
struct Path * cheapest_total_path
Definition: relation.h:589
Relids relids
Definition: relation.h:571
List * lappend(List *list, void *datum)
Definition: list.c:128
#define makeNode(_type_)
Definition: nodes.h:557
#define lfirst(lc)
Definition: pg_list.h:106
bool consider_parallel
Definition: relation.h:579
List * pathlist
Definition: relation.h:585
struct PathTarget * reltarget
Definition: relation.h:582
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:131
List * upper_rels[UPPERREL_FINAL+1]
Definition: relation.h:275
RelOptInfo* find_base_rel ( PlannerInfo root,
int  relid 
)

Definition at line 271 of file relnode.c.

References Assert, elog, ERROR, and PlannerInfo::simple_rel_array.

Referenced by add_join_clause_to_rels(), add_placeholders_to_base_rels(), add_vars_to_targetlist(), build_joinrel_tlist(), clause_selectivity(), create_lateral_join_info(), distribute_restrictinfo_to_rels(), examine_simple_variable(), examine_variable(), finalize_plan(), find_childrel_parents(), find_join_input_rel(), find_single_rel_for_clauses(), get_foreign_key_join_selectivity(), join_is_removable(), make_rel_from_joinlist(), reduce_unique_semijoins(), remove_join_clause_from_rels(), remove_rel_from_query(), set_append_rel_size(), set_base_rel_consider_startup(), set_subquery_size_estimates(), and set_subqueryscan_references().

272 {
273  RelOptInfo *rel;
274 
275  Assert(relid > 0);
276 
277  if (relid < root->simple_rel_array_size)
278  {
279  rel = root->simple_rel_array[relid];
280  if (rel)
281  return rel;
282  }
283 
284  elog(ERROR, "no relation entry for relid %d", relid);
285 
286  return NULL; /* keep compiler quiet */
287 }
struct RelOptInfo ** simple_rel_array
Definition: relation.h:179
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:664
#define elog
Definition: elog.h:219
AppendRelInfo* find_childrel_appendrelinfo ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1003 of file relnode.c.

References PlannerInfo::append_rel_list, Assert, AppendRelInfo::child_relid, elog, ERROR, lfirst, RelOptInfo::relid, RELOPT_OTHER_MEMBER_REL, and RelOptInfo::reloptkind.

Referenced by find_childrel_parents().

1004 {
1005  Index relid = rel->relid;
1006  ListCell *lc;
1007 
1008  /* Should only be called on child rels */
1010 
1011  foreach(lc, root->append_rel_list)
1012  {
1013  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
1014 
1015  if (appinfo->child_relid == relid)
1016  return appinfo;
1017  }
1018  /* should have found the entry ... */
1019  elog(ERROR, "child rel %d not found in append_rel_list", relid);
1020  return NULL; /* not reached */
1021 }
RelOptKind reloptkind
Definition: relation.h:568
#define ERROR
Definition: elog.h:43
Index relid
Definition: relation.h:599
List * append_rel_list
Definition: relation.h:252
unsigned int Index
Definition: c.h:359
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
#define elog
Definition: elog.h:219
Index child_relid
Definition: relation.h:2032
Relids find_childrel_parents ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1033 of file relnode.c.

References Assert, bms_add_member(), find_base_rel(), find_childrel_appendrelinfo(), AppendRelInfo::parent_relid, RELOPT_BASEREL, RELOPT_OTHER_MEMBER_REL, and RelOptInfo::reloptkind.

Referenced by check_index_predicates(), and generate_implied_equalities_for_column().

1034 {
1035  Relids result = NULL;
1036 
1038 
1039  do
1040  {
1041  AppendRelInfo *appinfo = find_childrel_appendrelinfo(root, rel);
1042  Index prelid = appinfo->parent_relid;
1043 
1044  result = bms_add_member(result, prelid);
1045 
1046  /* traverse up to the parent rel, loop if it's also a child rel */
1047  rel = find_base_rel(root, prelid);
1048  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1049 
1050  Assert(rel->reloptkind == RELOPT_BASEREL);
1051 
1052  return result;
1053 }
RelOptKind reloptkind
Definition: relation.h:568
AppendRelInfo * find_childrel_appendrelinfo(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1003
unsigned int Index
Definition: c.h:359
#define Assert(condition)
Definition: c.h:664
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:271
Index parent_relid
Definition: relation.h:2031
RelOptInfo* find_join_rel ( PlannerInfo root,
Relids  relids 
)

Definition at line 336 of file relnode.c.

References bms_equal(), build_join_rel_hash(), HASH_FIND, hash_search(), JoinHashEntry::join_rel, PlannerInfo::join_rel_hash, PlannerInfo::join_rel_list, lfirst, list_length(), and RelOptInfo::relids.

Referenced by build_join_rel(), examine_variable(), and find_join_input_rel().

337 {
338  /*
339  * Switch to using hash lookup when list grows "too long". The threshold
340  * is arbitrary and is known only here.
341  */
342  if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
343  build_join_rel_hash(root);
344 
345  /*
346  * Use either hashtable lookup or linear search, as appropriate.
347  *
348  * Note: the seemingly redundant hashkey variable is used to avoid taking
349  * the address of relids; unless the compiler is exceedingly smart, doing
350  * so would force relids out of a register and thus probably slow down the
351  * list-search case.
352  */
353  if (root->join_rel_hash)
354  {
355  Relids hashkey = relids;
356  JoinHashEntry *hentry;
357 
358  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
359  &hashkey,
360  HASH_FIND,
361  NULL);
362  if (hentry)
363  return hentry->join_rel;
364  }
365  else
366  {
367  ListCell *l;
368 
369  foreach(l, root->join_rel_list)
370  {
371  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
372 
373  if (bms_equal(rel->relids, relids))
374  return rel;
375  }
376  }
377 
378  return NULL;
379 }
List * join_rel_list
Definition: relation.h:215
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:902
RelOptInfo * join_rel
Definition: relnode.c:34
Relids relids
Definition: relation.h:571
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:294
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
struct HTAB * join_rel_hash
Definition: relation.h:216
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:131
ParamPathInfo* find_param_path_info ( RelOptInfo rel,
Relids  required_outer 
)

Definition at line 1388 of file relnode.c.

References bms_equal(), lfirst, ParamPathInfo::ppi_req_outer, and RelOptInfo::ppilist.

Referenced by get_appendrel_parampathinfo(), get_baserel_parampathinfo(), and get_joinrel_parampathinfo().

1389 {
1390  ListCell *lc;
1391 
1392  foreach(lc, rel->ppilist)
1393  {
1394  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1395 
1396  if (bms_equal(ppi->ppi_req_outer, required_outer))
1397  return ppi;
1398  }
1399 
1400  return NULL;
1401 }
List * ppilist
Definition: relation.h:586
#define lfirst(lc)
Definition: pg_list.h:106
Relids ppi_req_outer
Definition: relation.h:967
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:131
ParamPathInfo* get_appendrel_parampathinfo ( RelOptInfo appendrel,
Relids  required_outer 
)

Definition at line 1359 of file relnode.c.

References Assert, bms_is_empty(), bms_overlap(), find_param_path_info(), lappend(), makeNode, NIL, ParamPathInfo::ppi_clauses, ParamPathInfo::ppi_req_outer, ParamPathInfo::ppi_rows, RelOptInfo::ppilist, and RelOptInfo::relids.

Referenced by create_append_path(), and create_merge_append_path().

1360 {
1361  ParamPathInfo *ppi;
1362 
1363  /* Unparameterized paths have no ParamPathInfo */
1364  if (bms_is_empty(required_outer))
1365  return NULL;
1366 
1367  Assert(!bms_overlap(appendrel->relids, required_outer));
1368 
1369  /* If we already have a PPI for this parameterization, just return it */
1370  if ((ppi = find_param_path_info(appendrel, required_outer)))
1371  return ppi;
1372 
1373  /* Else build the ParamPathInfo */
1374  ppi = makeNode(ParamPathInfo);
1375  ppi->ppi_req_outer = required_outer;
1376  ppi->ppi_rows = 0;
1377  ppi->ppi_clauses = NIL;
1378  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1379 
1380  return ppi;
1381 }
#define NIL
Definition: pg_list.h:69
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1388
Relids relids
Definition: relation.h:571
List * ppilist
Definition: relation.h:586
List * lappend(List *list, void *datum)
Definition: list.c:128
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:664
List * ppi_clauses
Definition: relation.h:969
double ppi_rows
Definition: relation.h:968
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
Relids ppi_req_outer
Definition: relation.h:967
ParamPathInfo* get_baserel_parampathinfo ( PlannerInfo root,
RelOptInfo baserel,
Relids  required_outer 
)

Definition at line 1068 of file relnode.c.

References Assert, bms_is_empty(), bms_overlap(), bms_union(), find_param_path_info(), generate_join_implied_equalities(), get_parameterized_baserel_size(), join_clause_is_movable_into(), RelOptInfo::joininfo, lappend(), lfirst, list_concat(), makeNode, NIL, ParamPathInfo::ppi_clauses, ParamPathInfo::ppi_req_outer, ParamPathInfo::ppi_rows, RelOptInfo::ppilist, and RelOptInfo::relids.

Referenced by bitmap_and_cost_est(), bitmap_scan_cost_est(), create_bitmap_heap_path(), create_ctescan_path(), create_foreignscan_path(), create_functionscan_path(), create_gather_merge_path(), create_gather_path(), create_index_path(), create_namedtuplestorescan_path(), create_samplescan_path(), create_seqscan_path(), create_subqueryscan_path(), create_tablefuncscan_path(), create_tidscan_path(), create_valuesscan_path(), create_worktablescan_path(), postgresGetForeignPaths(), and reparameterize_path().

1070 {
1071  ParamPathInfo *ppi;
1072  Relids joinrelids;
1073  List *pclauses;
1074  double rows;
1075  ListCell *lc;
1076 
1077  /* Unparameterized paths have no ParamPathInfo */
1078  if (bms_is_empty(required_outer))
1079  return NULL;
1080 
1081  Assert(!bms_overlap(baserel->relids, required_outer));
1082 
1083  /* If we already have a PPI for this parameterization, just return it */
1084  if ((ppi = find_param_path_info(baserel, required_outer)))
1085  return ppi;
1086 
1087  /*
1088  * Identify all joinclauses that are movable to this base rel given this
1089  * parameterization.
1090  */
1091  joinrelids = bms_union(baserel->relids, required_outer);
1092  pclauses = NIL;
1093  foreach(lc, baserel->joininfo)
1094  {
1095  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1096 
1097  if (join_clause_is_movable_into(rinfo,
1098  baserel->relids,
1099  joinrelids))
1100  pclauses = lappend(pclauses, rinfo);
1101  }
1102 
1103  /*
1104  * Add in joinclauses generated by EquivalenceClasses, too. (These
1105  * necessarily satisfy join_clause_is_movable_into.)
1106  */
1107  pclauses = list_concat(pclauses,
1109  joinrelids,
1110  required_outer,
1111  baserel));
1112 
1113  /* Estimate the number of rows returned by the parameterized scan */
1114  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1115 
1116  /* And now we can build the ParamPathInfo */
1117  ppi = makeNode(ParamPathInfo);
1118  ppi->ppi_req_outer = required_outer;
1119  ppi->ppi_rows = rows;
1120  ppi->ppi_clauses = pclauses;
1121  baserel->ppilist = lappend(baserel->ppilist, ppi);
1122 
1123  return ppi;
1124 }
#define NIL
Definition: pg_list.h:69
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1388
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1033
List * joininfo
Definition: relation.h:635
Relids relids
Definition: relation.h:571
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:510
List * ppilist
Definition: relation.h:586
List * lappend(List *list, void *datum)
Definition: list.c:128
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:4067
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
List * ppi_clauses
Definition: relation.h:969
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
double ppi_rows
Definition: relation.h:968
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
Relids ppi_req_outer
Definition: relation.h:967
Definition: pg_list.h:45
ParamPathInfo* get_joinrel_parampathinfo ( PlannerInfo root,
RelOptInfo joinrel,
Path outer_path,
Path inner_path,
SpecialJoinInfo sjinfo,
Relids  required_outer,
List **  restrict_clauses 
)

Definition at line 1155 of file relnode.c.

References Assert, bms_is_empty(), bms_overlap(), bms_union(), find_param_path_info(), generate_join_implied_equalities(), generate_join_implied_equalities_for_ecs(), get_parameterized_joinrel_size(), join_clause_is_movable_into(), RelOptInfo::joininfo, lappend(), RestrictInfo::left_ec, lfirst, list_concat(), makeNode, NIL, Path::param_info, Path::parent, PATH_REQ_OUTER, ParamPathInfo::ppi_clauses, ParamPathInfo::ppi_req_outer, ParamPathInfo::ppi_rows, RelOptInfo::ppilist, RelOptInfo::relids, and RestrictInfo::right_ec.

Referenced by create_hashjoin_path(), create_mergejoin_path(), and create_nestloop_path().

1161 {
1162  ParamPathInfo *ppi;
1163  Relids join_and_req;
1164  Relids outer_and_req;
1165  Relids inner_and_req;
1166  List *pclauses;
1167  List *eclauses;
1168  List *dropped_ecs;
1169  double rows;
1170  ListCell *lc;
1171 
1172  /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1173  if (bms_is_empty(required_outer))
1174  return NULL;
1175 
1176  Assert(!bms_overlap(joinrel->relids, required_outer));
1177 
1178  /*
1179  * Identify all joinclauses that are movable to this join rel given this
1180  * parameterization. These are the clauses that are movable into this
1181  * join, but not movable into either input path. Treat an unparameterized
1182  * input path as not accepting parameterized clauses (because it won't,
1183  * per the shortcut exit above), even though the joinclause movement rules
1184  * might allow the same clauses to be moved into a parameterized path for
1185  * that rel.
1186  */
1187  join_and_req = bms_union(joinrel->relids, required_outer);
1188  if (outer_path->param_info)
1189  outer_and_req = bms_union(outer_path->parent->relids,
1190  PATH_REQ_OUTER(outer_path));
1191  else
1192  outer_and_req = NULL; /* outer path does not accept parameters */
1193  if (inner_path->param_info)
1194  inner_and_req = bms_union(inner_path->parent->relids,
1195  PATH_REQ_OUTER(inner_path));
1196  else
1197  inner_and_req = NULL; /* inner path does not accept parameters */
1198 
1199  pclauses = NIL;
1200  foreach(lc, joinrel->joininfo)
1201  {
1202  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1203 
1204  if (join_clause_is_movable_into(rinfo,
1205  joinrel->relids,
1206  join_and_req) &&
1208  outer_path->parent->relids,
1209  outer_and_req) &&
1211  inner_path->parent->relids,
1212  inner_and_req))
1213  pclauses = lappend(pclauses, rinfo);
1214  }
1215 
1216  /* Consider joinclauses generated by EquivalenceClasses, too */
1217  eclauses = generate_join_implied_equalities(root,
1218  join_and_req,
1219  required_outer,
1220  joinrel);
1221  /* We only want ones that aren't movable to lower levels */
1222  dropped_ecs = NIL;
1223  foreach(lc, eclauses)
1224  {
1225  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1226 
1227  /*
1228  * In principle, join_clause_is_movable_into() should accept anything
1229  * returned by generate_join_implied_equalities(); but because its
1230  * analysis is only approximate, sometimes it doesn't. So we
1231  * currently cannot use this Assert; instead just assume it's okay to
1232  * apply the joinclause at this level.
1233  */
1234 #ifdef NOT_USED
1236  joinrel->relids,
1237  join_and_req));
1238 #endif
1239  if (join_clause_is_movable_into(rinfo,
1240  outer_path->parent->relids,
1241  outer_and_req))
1242  continue; /* drop if movable into LHS */
1243  if (join_clause_is_movable_into(rinfo,
1244  inner_path->parent->relids,
1245  inner_and_req))
1246  {
1247  /* drop if movable into RHS, but remember EC for use below */
1248  Assert(rinfo->left_ec == rinfo->right_ec);
1249  dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1250  continue;
1251  }
1252  pclauses = lappend(pclauses, rinfo);
1253  }
1254 
1255  /*
1256  * EquivalenceClasses are harder to deal with than we could wish, because
1257  * of the fact that a given EC can generate different clauses depending on
1258  * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1259  * LHS and RHS of the current join and Z is in required_outer, and further
1260  * suppose that the inner_path is parameterized by both X and Z. The code
1261  * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1262  * and in the latter case will have discarded it as being movable into the
1263  * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1264  * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1265  * not have produced both, and we can't readily tell from here which one
1266  * it did pick. If we add no clause to this join, we'll end up with
1267  * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1268  * constrained to be equal to the other members of the EC. (When we come
1269  * to join Z to this X/Y path, we will certainly drop whichever EC clause
1270  * is generated at that join, so this omission won't get fixed later.)
1271  *
1272  * To handle this, for each EC we discarded such a clause from, try to
1273  * generate a clause connecting the required_outer rels to the join's LHS
1274  * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1275  * the clause can't be moved to the LHS, add it to the current join's
1276  * restriction clauses. (If an EC cannot generate such a clause then it
1277  * has nothing that needs to be enforced here, while if the clause can be
1278  * moved into the LHS then it should have been enforced within that path.)
1279  *
1280  * Note that we don't need similar processing for ECs whose clause was
1281  * considered to be movable into the LHS, because the LHS can't refer to
1282  * the RHS so there is no comparable ambiguity about what it might
1283  * actually be enforcing internally.
1284  */
1285  if (dropped_ecs)
1286  {
1287  Relids real_outer_and_req;
1288 
1289  real_outer_and_req = bms_union(outer_path->parent->relids,
1290  required_outer);
1291  eclauses =
1293  dropped_ecs,
1294  real_outer_and_req,
1295  required_outer,
1296  outer_path->parent);
1297  foreach(lc, eclauses)
1298  {
1299  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1300 
1301  /* As above, can't quite assert this here */
1302 #ifdef NOT_USED
1304  outer_path->parent->relids,
1305  real_outer_and_req));
1306 #endif
1307  if (!join_clause_is_movable_into(rinfo,
1308  outer_path->parent->relids,
1309  outer_and_req))
1310  pclauses = lappend(pclauses, rinfo);
1311  }
1312  }
1313 
1314  /*
1315  * Now, attach the identified moved-down clauses to the caller's
1316  * restrict_clauses list. By using list_concat in this order, we leave
1317  * the original list structure of restrict_clauses undamaged.
1318  */
1319  *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1320 
1321  /* If we already have a PPI for this parameterization, just return it */
1322  if ((ppi = find_param_path_info(joinrel, required_outer)))
1323  return ppi;
1324 
1325  /* Estimate the number of rows returned by the parameterized join */
1326  rows = get_parameterized_joinrel_size(root, joinrel,
1327  outer_path,
1328  inner_path,
1329  sjinfo,
1330  *restrict_clauses);
1331 
1332  /*
1333  * And now we can build the ParamPathInfo. No point in saving the
1334  * input-pair-dependent clause list, though.
1335  *
1336  * Note: in GEQO mode, we'll be called in a temporary memory context, but
1337  * the joinrel structure is there too, so no problem.
1338  */
1339  ppi = makeNode(ParamPathInfo);
1340  ppi->ppi_req_outer = required_outer;
1341  ppi->ppi_rows = rows;
1342  ppi->ppi_clauses = NIL;
1343  joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1344 
1345  return ppi;
1346 }
#define NIL
Definition: pg_list.h:69
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1388
ParamPathInfo * param_info
Definition: relation.h:1011
List * list_concat(List *list1, List *list2)
Definition: list.c:321
EquivalenceClass * right_ec
Definition: relation.h:1850
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1033
RelOptInfo * parent
Definition: relation.h:1008
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:4148
List * joininfo
Definition: relation.h:635
Relids relids
Definition: relation.h:571
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:510
List * ppilist
Definition: relation.h:586
List * lappend(List *list, void *datum)
Definition: list.c:128
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1050
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
#define PATH_REQ_OUTER(path)
Definition: relation.h:1027
List * ppi_clauses
Definition: relation.h:969
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
double ppi_rows
Definition: relation.h:968
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
EquivalenceClass * left_ec
Definition: relation.h:1849
Relids ppi_req_outer
Definition: relation.h:967
Definition: pg_list.h:45
Relids min_join_parameterization ( PlannerInfo root,
Relids  joinrelids,
RelOptInfo outer_rel,
RelOptInfo inner_rel 
)

Definition at line 662 of file relnode.c.

References bms_del_members(), bms_is_empty(), bms_union(), and RelOptInfo::lateral_relids.

Referenced by build_join_rel(), and join_is_legal().

666 {
667  Relids result;
668 
669  /*
670  * Basically we just need the union of the inputs' lateral_relids, less
671  * whatever is already in the join.
672  *
673  * It's not immediately obvious that this is a valid way to compute the
674  * result, because it might seem that we're ignoring possible lateral refs
675  * of PlaceHolderVars that are due to be computed at the join but not in
676  * either input. However, because create_lateral_join_info() already
677  * charged all such PHV refs to each member baserel of the join, they'll
678  * be accounted for already in the inputs' lateral_relids. Likewise, we
679  * do not need to worry about doing transitive closure here, because that
680  * was already accounted for in the original baserel lateral_relids.
681  */
682  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
683  result = bms_del_members(result, joinrelids);
684 
685  /* Maintain invariant that result is exactly NULL if empty */
686  if (bms_is_empty(result))
687  result = NULL;
688 
689  return result;
690 }
Relids lateral_relids
Definition: relation.h:596
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:817
static void set_foreign_rel_properties ( RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel 
)
static

Definition at line 399 of file relnode.c.

References RelOptInfo::fdwroutine, GetUserId(), OidIsValid, RelOptInfo::serverid, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by build_join_rel().

401 {
402  if (OidIsValid(outer_rel->serverid) &&
403  inner_rel->serverid == outer_rel->serverid)
404  {
405  if (inner_rel->userid == outer_rel->userid)
406  {
407  joinrel->serverid = outer_rel->serverid;
408  joinrel->userid = outer_rel->userid;
409  joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
410  joinrel->fdwroutine = outer_rel->fdwroutine;
411  }
412  else if (!OidIsValid(inner_rel->userid) &&
413  outer_rel->userid == GetUserId())
414  {
415  joinrel->serverid = outer_rel->serverid;
416  joinrel->userid = outer_rel->userid;
417  joinrel->useridiscurrent = true;
418  joinrel->fdwroutine = outer_rel->fdwroutine;
419  }
420  else if (!OidIsValid(outer_rel->userid) &&
421  inner_rel->userid == GetUserId())
422  {
423  joinrel->serverid = outer_rel->serverid;
424  joinrel->userid = inner_rel->userid;
425  joinrel->useridiscurrent = true;
426  joinrel->fdwroutine = outer_rel->fdwroutine;
427  }
428  }
429 }
Oid GetUserId(void)
Definition: miscinit.c:284
Oid userid
Definition: relation.h:619
bool useridiscurrent
Definition: relation.h:620
#define OidIsValid(objectId)
Definition: c.h:532
struct FdwRoutine * fdwroutine
Definition: relation.h:622
Oid serverid
Definition: relation.h:618
void setup_simple_rel_arrays ( PlannerInfo root)

Definition at line 62 of file relnode.c.

References lfirst, list_length(), palloc0(), PlannerInfo::parse, Query::rtable, PlannerInfo::simple_rel_array, PlannerInfo::simple_rel_array_size, and PlannerInfo::simple_rte_array.

Referenced by plan_cluster_use_sort(), plan_set_operations(), and query_planner().

63 {
64  Index rti;
65  ListCell *lc;
66 
67  /* Arrays are accessed using RT indexes (1..N) */
68  root->simple_rel_array_size = list_length(root->parse->rtable) + 1;
69 
70  /* simple_rel_array is initialized to all NULLs */
71  root->simple_rel_array = (RelOptInfo **)
72  palloc0(root->simple_rel_array_size * sizeof(RelOptInfo *));
73 
74  /* simple_rte_array is an array equivalent of the rtable list */
75  root->simple_rte_array = (RangeTblEntry **)
76  palloc0(root->simple_rel_array_size * sizeof(RangeTblEntry *));
77  rti = 1;
78  foreach(lc, root->parse->rtable)
79  {
80  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
81 
82  root->simple_rte_array[rti++] = rte;
83  }
84 }
Query * parse
Definition: relation.h:155
struct RelOptInfo ** simple_rel_array
Definition: relation.h:179
List * rtable
Definition: parsenodes.h:135
int simple_rel_array_size
Definition: relation.h:180
RangeTblEntry ** simple_rte_array
Definition: relation.h:188
void * palloc0(Size size)
Definition: mcxt.c:877
unsigned int Index
Definition: c.h:359
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
static List * subbuild_joinrel_joinlist ( RelOptInfo joinrel,
List joininfo_list,
List new_joininfo 
)
static

Definition at line 873 of file relnode.c.

References bms_is_subset(), lfirst, list_append_unique_ptr(), RelOptInfo::relids, and RestrictInfo::required_relids.

Referenced by build_joinrel_joinlist().

876 {
877  ListCell *l;
878 
879  foreach(l, joininfo_list)
880  {
881  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
882 
883  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
884  {
885  /*
886  * This clause becomes a restriction clause for the joinrel, since
887  * it refers to no outside rels. So we can ignore it in this
888  * routine.
889  */
890  }
891  else
892  {
893  /*
894  * This clause is still a join clause at this level, so add it to
895  * the new joininfo list, being careful to eliminate duplicates.
896  * (Since RestrictInfo nodes in different joinlists will have been
897  * multiply-linked rather than copied, pointer equality should be
898  * a sufficient test.)
899  */
900  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
901  }
902  }
903 
904  return new_joininfo;
905 }
Relids required_relids
Definition: relation.h:1819
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:975
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids relids
Definition: relation.h:571
#define lfirst(lc)
Definition: pg_list.h:106
static List * subbuild_joinrel_restrictlist ( RelOptInfo joinrel,
List joininfo_list,
List new_restrictlist 
)
static

Definition at line 839 of file relnode.c.

References bms_is_subset(), lfirst, list_append_unique_ptr(), RelOptInfo::relids, and RestrictInfo::required_relids.

Referenced by build_joinrel_restrictlist().

842 {
843  ListCell *l;
844 
845  foreach(l, joininfo_list)
846  {
847  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
848 
849  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
850  {
851  /*
852  * This clause becomes a restriction clause for the joinrel, since
853  * it refers to no outside rels. Add it to the list, being
854  * careful to eliminate duplicates. (Since RestrictInfo nodes in
855  * different joinlists will have been multiply-linked rather than
856  * copied, pointer equality should be a sufficient test.)
857  */
858  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
859  }
860  else
861  {
862  /*
863  * This clause is still a join clause at this level, so we ignore
864  * it in this routine.
865  */
866  }
867  }
868 
869  return new_restrictlist;
870 }
Relids required_relids
Definition: relation.h:1819
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:975
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids relids
Definition: relation.h:571
#define lfirst(lc)
Definition: pg_list.h:106