PostgreSQL Source Code  git master
relnode.c File Reference
#include "postgres.h"
#include <limits.h>
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/inherit.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 "parser/parse_relation.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.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)
 
static void build_joinrel_partition_info (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, JoinType jointype)
 
static bool have_partkey_equi_join (RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
 
static int match_expr_to_partition_keys (Expr *expr, RelOptInfo *rel, bool strict_op)
 
static void set_joinrel_partition_key_exprs (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
 
static void build_child_join_reltarget (PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
 
void setup_simple_rel_arrays (PlannerInfo *root)
 
void expand_planner_arrays (PlannerInfo *root, int add_size)
 
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)
 
RelOptInfobuild_child_join_rel (PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo, JoinType jointype)
 
Relids min_join_parameterization (PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
RelOptInfofetch_upper_rel (PlannerInfo *root, UpperRelationKind kind, Relids relids)
 
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

◆ JoinHashEntry

typedef struct JoinHashEntry JoinHashEntry

Function Documentation

◆ add_join_rel()

static void add_join_rel ( PlannerInfo root,
RelOptInfo joinrel 
)
static

Definition at line 544 of file relnode.c.

545 {
546  /* GEQO requires us to append the new joinrel to the end of the list! */
547  root->join_rel_list = lappend(root->join_rel_list, joinrel);
548 
549  /* store it into the auxiliary hashtable if there is one. */
550  if (root->join_rel_hash)
551  {
552  JoinHashEntry *hentry;
553  bool found;
554 
555  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
556  &(joinrel->relids),
557  HASH_ENTER,
558  &found);
559  Assert(!found);
560  hentry->join_rel = joinrel;
561  }
562 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
@ HASH_ENTER
Definition: hsearch.h:114
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
RelOptInfo * join_rel
Definition: relnode.c:39
List * join_rel_list
Definition: pathnodes.h:276
Relids relids
Definition: pathnodes.h:824

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

Referenced by build_child_join_rel(), and build_join_rel().

◆ build_child_join_rel()

RelOptInfo* build_child_join_rel ( PlannerInfo root,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
RelOptInfo parent_joinrel,
List restrictlist,
SpecialJoinInfo sjinfo,
JoinType  jointype 
)

Definition at line 793 of file relnode.c.

797 {
798  RelOptInfo *joinrel = makeNode(RelOptInfo);
799  AppendRelInfo **appinfos;
800  int nappinfos;
801 
802  /* Only joins between "other" relations land here. */
803  Assert(IS_OTHER_REL(outer_rel) && IS_OTHER_REL(inner_rel));
804 
805  /* The parent joinrel should have consider_partitionwise_join set. */
806  Assert(parent_joinrel->consider_partitionwise_join);
807 
808  joinrel->reloptkind = RELOPT_OTHER_JOINREL;
809  joinrel->relids = bms_union(outer_rel->relids, inner_rel->relids);
810  joinrel->rows = 0;
811  /* cheap startup cost is interesting iff not all tuples to be retrieved */
812  joinrel->consider_startup = (root->tuple_fraction > 0);
813  joinrel->consider_param_startup = false;
814  joinrel->consider_parallel = false;
815  joinrel->reltarget = create_empty_pathtarget();
816  joinrel->pathlist = NIL;
817  joinrel->ppilist = NIL;
818  joinrel->partial_pathlist = NIL;
819  joinrel->cheapest_startup_path = NULL;
820  joinrel->cheapest_total_path = NULL;
821  joinrel->cheapest_unique_path = NULL;
823  joinrel->direct_lateral_relids = NULL;
824  joinrel->lateral_relids = NULL;
825  joinrel->relid = 0; /* indicates not a baserel */
826  joinrel->rtekind = RTE_JOIN;
827  joinrel->min_attr = 0;
828  joinrel->max_attr = 0;
829  joinrel->attr_needed = NULL;
830  joinrel->attr_widths = NULL;
831  joinrel->lateral_vars = NIL;
832  joinrel->lateral_referencers = NULL;
833  joinrel->indexlist = NIL;
834  joinrel->pages = 0;
835  joinrel->tuples = 0;
836  joinrel->allvisfrac = 0;
837  joinrel->eclass_indexes = NULL;
838  joinrel->subroot = NULL;
839  joinrel->subplan_params = NIL;
840  joinrel->amflags = 0;
841  joinrel->serverid = InvalidOid;
842  joinrel->userid = InvalidOid;
843  joinrel->useridiscurrent = false;
844  joinrel->fdwroutine = NULL;
845  joinrel->fdw_private = NULL;
846  joinrel->baserestrictinfo = NIL;
847  joinrel->baserestrictcost.startup = 0;
848  joinrel->baserestrictcost.per_tuple = 0;
849  joinrel->joininfo = NIL;
850  joinrel->has_eclass_joins = false;
851  joinrel->consider_partitionwise_join = false; /* might get changed later */
852  joinrel->parent = parent_joinrel;
853  joinrel->top_parent = parent_joinrel->top_parent ? parent_joinrel->top_parent : parent_joinrel;
854  joinrel->top_parent_relids = joinrel->top_parent->relids;
855  joinrel->part_scheme = NULL;
856  joinrel->nparts = -1;
857  joinrel->boundinfo = NULL;
858  joinrel->partbounds_merged = false;
859  joinrel->partition_qual = NIL;
860  joinrel->part_rels = NULL;
861  joinrel->live_parts = NULL;
862  joinrel->all_partrels = NULL;
863  joinrel->partexprs = NULL;
864  joinrel->nullable_partexprs = NULL;
865 
866  /* Compute information relevant to foreign relations. */
867  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
868 
869  /* Compute information needed for mapping Vars to the child rel */
870  appinfos = find_appinfos_by_relids(root, joinrel->relids, &nappinfos);
871 
872  /* Set up reltarget struct */
873  build_child_join_reltarget(root, parent_joinrel, joinrel,
874  nappinfos, appinfos);
875 
876  /* Construct joininfo list. */
877  joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
878  (Node *) parent_joinrel->joininfo,
879  nappinfos,
880  appinfos);
881 
882  /*
883  * Lateral relids referred in child join will be same as that referred in
884  * the parent relation.
885  */
886  joinrel->direct_lateral_relids = (Relids) bms_copy(parent_joinrel->direct_lateral_relids);
887  joinrel->lateral_relids = (Relids) bms_copy(parent_joinrel->lateral_relids);
888 
889  /*
890  * If the parent joinrel has pending equivalence classes, so does the
891  * child.
892  */
893  joinrel->has_eclass_joins = parent_joinrel->has_eclass_joins;
894 
895  /* Is the join between partitions itself partitioned? */
896  build_joinrel_partition_info(joinrel, outer_rel, inner_rel, restrictlist,
897  jointype);
898 
899  /* Child joinrel is parallel safe if parent is parallel safe. */
900  joinrel->consider_parallel = parent_joinrel->consider_parallel;
901 
902  /* Set estimates of the child-joinrel's size. */
903  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
904  sjinfo, restrictlist);
905 
906  /* We build the join only once. */
907  Assert(!find_join_rel(root, joinrel->relids));
908 
909  /* Add the relation to the PlannerInfo. */
910  add_join_rel(root, joinrel);
911 
912  /*
913  * We might need EquivalenceClass members corresponding to the child join,
914  * so that we can represent sort pathkeys for it. As with children of
915  * baserels, we shouldn't need this unless there are relevant eclass joins
916  * (implying that a merge join might be possible) or pathkeys to sort by.
917  */
918  if (joinrel->has_eclass_joins || has_useful_pathkeys(root, parent_joinrel))
920  nappinfos, appinfos,
921  parent_joinrel, joinrel);
922 
923  pfree(appinfos);
924 
925  return joinrel;
926 }
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:697
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:195
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:226
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:5067
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
Definition: equivclass.c:2679
void pfree(void *pointer)
Definition: mcxt.c:1306
#define makeNode(_type_)
Definition: nodes.h:165
@ RTE_JOIN
Definition: parsenodes.h:984
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1963
Bitmapset * Relids
Definition: pathnodes.h:30
@ RELOPT_OTHER_JOINREL
Definition: pathnodes.h:782
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:807
#define NIL
Definition: pg_list.h:66
#define InvalidOid
Definition: postgres_ext.h:36
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:443
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:2051
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:506
static void build_joinrel_partition_info(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, JoinType jointype)
Definition: relnode.c:1668
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:544
Definition: pg_list.h:52
Definition: nodes.h:118
Selectivity tuple_fraction
Definition: pathnodes.h:443
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
List * baserestrictinfo
Definition: pathnodes.h:933
bool consider_param_startup
Definition: pathnodes.h:838
List * subplan_params
Definition: pathnodes.h:902
List * ppilist
Definition: pathnodes.h:852
bool useridiscurrent
Definition: pathnodes.h:916
uint32 amflags
Definition: pathnodes.h:906
List * joininfo
Definition: pathnodes.h:939
List * partition_qual
Definition: pathnodes.h:975
struct PathTarget * reltarget
Definition: pathnodes.h:846
Index relid
Definition: pathnodes.h:871
List * lateral_vars
Definition: pathnodes.h:885
int nparts
Definition: pathnodes.h:969
Cardinality tuples
Definition: pathnodes.h:894
bool consider_parallel
Definition: pathnodes.h:840
Relids top_parent_relids
Definition: pathnodes.h:957
bool partbounds_merged
Definition: pathnodes.h:973
BlockNumber pages
Definition: pathnodes.h:893
Relids lateral_relids
Definition: pathnodes.h:866
List * cheapest_parameterized_paths
Definition: pathnodes.h:857
List * pathlist
Definition: pathnodes.h:851
RelOptKind reloptkind
Definition: pathnodes.h:818
List * indexlist
Definition: pathnodes.h:889
struct Path * cheapest_unique_path
Definition: pathnodes.h:856
Relids lateral_referencers
Definition: pathnodes.h:887
struct Path * cheapest_startup_path
Definition: pathnodes.h:854
QualCost baserestrictcost
Definition: pathnodes.h:935
struct Path * cheapest_total_path
Definition: pathnodes.h:855
Oid userid
Definition: pathnodes.h:914
Bitmapset * eclass_indexes
Definition: pathnodes.h:900
Relids all_partrels
Definition: pathnodes.h:989
Relids direct_lateral_relids
Definition: pathnodes.h:864
bool has_eclass_joins
Definition: pathnodes.h:941
Oid serverid
Definition: pathnodes.h:912
bool consider_startup
Definition: pathnodes.h:836
Bitmapset * live_parts
Definition: pathnodes.h:987
bool consider_partitionwise_join
Definition: pathnodes.h:947
List * partial_pathlist
Definition: pathnodes.h:853
PlannerInfo * subroot
Definition: pathnodes.h:901
AttrNumber max_attr
Definition: pathnodes.h:879
double allvisfrac
Definition: pathnodes.h:895
Cardinality rows
Definition: pathnodes.h:830
AttrNumber min_attr
Definition: pathnodes.h:877
RTEKind rtekind
Definition: pathnodes.h:875
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:681

References add_child_join_rel_equivalences(), add_join_rel(), adjust_appendrel_attrs(), RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, Assert(), RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_union(), build_child_join_reltarget(), build_joinrel_partition_info(), 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_partitionwise_join, RelOptInfo::consider_startup, create_empty_pathtarget(), RelOptInfo::direct_lateral_relids, RelOptInfo::eclass_indexes, find_appinfos_by_relids(), find_join_rel(), RelOptInfo::has_eclass_joins, has_useful_pathkeys(), RelOptInfo::indexlist, InvalidOid, IS_OTHER_REL, RelOptInfo::joininfo, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, RelOptInfo::live_parts, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, NIL, RelOptInfo::nparts, RelOptInfo::pages, RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, pfree(), RelOptInfo::ppilist, RelOptInfo::relid, RelOptInfo::relids, RELOPT_OTHER_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, RelOptInfo::rows, RTE_JOIN, RelOptInfo::rtekind, RelOptInfo::serverid, set_foreign_rel_properties(), set_joinrel_size_estimates(), QualCost::startup, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, PlannerInfo::tuple_fraction, RelOptInfo::tuples, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by try_partitionwise_join().

◆ build_child_join_reltarget()

static void build_child_join_reltarget ( PlannerInfo root,
RelOptInfo parentrel,
RelOptInfo childrel,
int  nappinfos,
AppendRelInfo **  appinfos 
)
static

Definition at line 2051 of file relnode.c.

2056 {
2057  /* Build the targetlist */
2058  childrel->reltarget->exprs = (List *)
2060  (Node *) parentrel->reltarget->exprs,
2061  nappinfos, appinfos);
2062 
2063  /* Set the cost and width fields */
2064  childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
2065  childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
2066  childrel->reltarget->width = parentrel->reltarget->width;
2067 }
List * exprs
Definition: pathnodes.h:1427
QualCost cost
Definition: pathnodes.h:1433

References adjust_appendrel_attrs(), PathTarget::cost, PathTarget::exprs, QualCost::per_tuple, RelOptInfo::reltarget, QualCost::startup, and PathTarget::width.

Referenced by build_child_join_rel().

◆ build_join_rel()

RelOptInfo* build_join_rel ( PlannerInfo root,
Relids  joinrelids,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
SpecialJoinInfo sjinfo,
List **  restrictlist_ptr 
)

Definition at line 581 of file relnode.c.

587 {
588  RelOptInfo *joinrel;
589  List *restrictlist;
590 
591  /* This function should be used only for join between parents. */
592  Assert(!IS_OTHER_REL(outer_rel) && !IS_OTHER_REL(inner_rel));
593 
594  /*
595  * See if we already have a joinrel for this set of base rels.
596  */
597  joinrel = find_join_rel(root, joinrelids);
598 
599  if (joinrel)
600  {
601  /*
602  * Yes, so we only need to figure the restrictlist for this particular
603  * pair of component relations.
604  */
605  if (restrictlist_ptr)
606  *restrictlist_ptr = build_joinrel_restrictlist(root,
607  joinrel,
608  outer_rel,
609  inner_rel);
610  return joinrel;
611  }
612 
613  /*
614  * Nope, so make one.
615  */
616  joinrel = makeNode(RelOptInfo);
617  joinrel->reloptkind = RELOPT_JOINREL;
618  joinrel->relids = bms_copy(joinrelids);
619  joinrel->rows = 0;
620  /* cheap startup cost is interesting iff not all tuples to be retrieved */
621  joinrel->consider_startup = (root->tuple_fraction > 0);
622  joinrel->consider_param_startup = false;
623  joinrel->consider_parallel = false;
624  joinrel->reltarget = create_empty_pathtarget();
625  joinrel->pathlist = NIL;
626  joinrel->ppilist = NIL;
627  joinrel->partial_pathlist = NIL;
628  joinrel->cheapest_startup_path = NULL;
629  joinrel->cheapest_total_path = NULL;
630  joinrel->cheapest_unique_path = NULL;
632  /* init direct_lateral_relids from children; we'll finish it up below */
633  joinrel->direct_lateral_relids =
634  bms_union(outer_rel->direct_lateral_relids,
635  inner_rel->direct_lateral_relids);
636  joinrel->lateral_relids = min_join_parameterization(root, joinrel->relids,
637  outer_rel, inner_rel);
638  joinrel->relid = 0; /* indicates not a baserel */
639  joinrel->rtekind = RTE_JOIN;
640  joinrel->min_attr = 0;
641  joinrel->max_attr = 0;
642  joinrel->attr_needed = NULL;
643  joinrel->attr_widths = NULL;
644  joinrel->lateral_vars = NIL;
645  joinrel->lateral_referencers = NULL;
646  joinrel->indexlist = NIL;
647  joinrel->statlist = NIL;
648  joinrel->pages = 0;
649  joinrel->tuples = 0;
650  joinrel->allvisfrac = 0;
651  joinrel->eclass_indexes = NULL;
652  joinrel->subroot = NULL;
653  joinrel->subplan_params = NIL;
654  joinrel->rel_parallel_workers = -1;
655  joinrel->amflags = 0;
656  joinrel->serverid = InvalidOid;
657  joinrel->userid = InvalidOid;
658  joinrel->useridiscurrent = false;
659  joinrel->fdwroutine = NULL;
660  joinrel->fdw_private = NULL;
661  joinrel->unique_for_rels = NIL;
662  joinrel->non_unique_for_rels = NIL;
663  joinrel->baserestrictinfo = NIL;
664  joinrel->baserestrictcost.startup = 0;
665  joinrel->baserestrictcost.per_tuple = 0;
666  joinrel->baserestrict_min_security = UINT_MAX;
667  joinrel->joininfo = NIL;
668  joinrel->has_eclass_joins = false;
669  joinrel->consider_partitionwise_join = false; /* might get changed later */
670  joinrel->parent = NULL;
671  joinrel->top_parent = NULL;
672  joinrel->top_parent_relids = NULL;
673  joinrel->part_scheme = NULL;
674  joinrel->nparts = -1;
675  joinrel->boundinfo = NULL;
676  joinrel->partbounds_merged = false;
677  joinrel->partition_qual = NIL;
678  joinrel->part_rels = NULL;
679  joinrel->live_parts = NULL;
680  joinrel->all_partrels = NULL;
681  joinrel->partexprs = NULL;
682  joinrel->nullable_partexprs = NULL;
683 
684  /* Compute information relevant to the foreign relations. */
685  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
686 
687  /*
688  * Fill the joinrel's tlist with just the Vars and PHVs that need to be
689  * output from this join (ie, are needed for higher joinclauses or final
690  * output).
691  *
692  * NOTE: the tlist order for a join rel will depend on which pair of outer
693  * and inner rels we first try to build it from. But the contents should
694  * be the same regardless.
695  */
696  build_joinrel_tlist(root, joinrel, outer_rel);
697  build_joinrel_tlist(root, joinrel, inner_rel);
698  add_placeholders_to_joinrel(root, joinrel, outer_rel, inner_rel);
699 
700  /*
701  * add_placeholders_to_joinrel also took care of adding the ph_lateral
702  * sets of any PlaceHolderVars computed here to direct_lateral_relids, so
703  * now we can finish computing that. This is much like the computation of
704  * the transitively-closed lateral_relids in min_join_parameterization,
705  * except that here we *do* have to consider the added PHVs.
706  */
707  joinrel->direct_lateral_relids =
708  bms_del_members(joinrel->direct_lateral_relids, joinrel->relids);
709  if (bms_is_empty(joinrel->direct_lateral_relids))
710  joinrel->direct_lateral_relids = NULL;
711 
712  /*
713  * Construct restrict and join clause lists for the new joinrel. (The
714  * caller might or might not need the restrictlist, but I need it anyway
715  * for set_joinrel_size_estimates().)
716  */
717  restrictlist = build_joinrel_restrictlist(root, joinrel,
718  outer_rel, inner_rel);
719  if (restrictlist_ptr)
720  *restrictlist_ptr = restrictlist;
721  build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
722 
723  /*
724  * This is also the right place to check whether the joinrel has any
725  * pending EquivalenceClass joins.
726  */
727  joinrel->has_eclass_joins = has_relevant_eclass_joinclause(root, joinrel);
728 
729  /* Store the partition information. */
730  build_joinrel_partition_info(joinrel, outer_rel, inner_rel, restrictlist,
731  sjinfo->jointype);
732 
733  /*
734  * Set estimates of the joinrel's size.
735  */
736  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
737  sjinfo, restrictlist);
738 
739  /*
740  * Set the consider_parallel flag if this joinrel could potentially be
741  * scanned within a parallel worker. If this flag is false for either
742  * inner_rel or outer_rel, then it must be false for the joinrel also.
743  * Even if both are true, there might be parallel-restricted expressions
744  * in the targetlist or quals.
745  *
746  * Note that if there are more than two rels in this relation, they could
747  * be divided between inner_rel and outer_rel in any arbitrary way. We
748  * assume this doesn't matter, because we should hit all the same baserels
749  * and joinclauses while building up to this joinrel no matter which we
750  * take; therefore, we should make the same decision here however we get
751  * here.
752  */
753  if (inner_rel->consider_parallel && outer_rel->consider_parallel &&
754  is_parallel_safe(root, (Node *) restrictlist) &&
755  is_parallel_safe(root, (Node *) joinrel->reltarget->exprs))
756  joinrel->consider_parallel = true;
757 
758  /* Add the joinrel to the PlannerInfo. */
759  add_join_rel(root, joinrel);
760 
761  /*
762  * Also, if dynamic-programming join search is active, add the new joinrel
763  * to the appropriate sublist. Note: you might think the Assert on number
764  * of members should be for equality, but some of the level 1 rels might
765  * have been joinrels already, so we can only assert <=.
766  */
767  if (root->join_rel_level)
768  {
769  Assert(root->join_cur_level > 0);
770  Assert(root->join_cur_level <= bms_num_members(joinrel->relids));
771  root->join_rel_level[root->join_cur_level] =
772  lappend(root->join_rel_level[root->join_cur_level], joinrel);
773  }
774 
775  return joinrel;
776 }
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:649
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:932
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:704
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:634
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:3030
@ RELOPT_JOINREL
Definition: pathnodes.h:780
void add_placeholders_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: placeholder.c:437
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:937
static List * build_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1096
static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel)
Definition: relnode.c:981
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1126
int join_cur_level
Definition: pathnodes.h:292
List * statlist
Definition: pathnodes.h:891
List * unique_for_rels
Definition: pathnodes.h:925
List * non_unique_for_rels
Definition: pathnodes.h:927
int rel_parallel_workers
Definition: pathnodes.h:904
Index baserestrict_min_security
Definition: pathnodes.h:937
JoinType jointype
Definition: pathnodes.h:2707

References add_join_rel(), add_placeholders_to_joinrel(), RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, Assert(), RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_del_members(), bms_is_empty(), bms_num_members(), bms_union(), build_joinrel_joinlist(), build_joinrel_partition_info(), 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_partitionwise_join, RelOptInfo::consider_startup, create_empty_pathtarget(), RelOptInfo::direct_lateral_relids, RelOptInfo::eclass_indexes, PathTarget::exprs, find_join_rel(), RelOptInfo::has_eclass_joins, has_relevant_eclass_joinclause(), RelOptInfo::indexlist, InvalidOid, IS_OTHER_REL, is_parallel_safe(), PlannerInfo::join_cur_level, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lappend(), RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, RelOptInfo::live_parts, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, min_join_parameterization(), NIL, RelOptInfo::non_unique_for_rels, RelOptInfo::nparts, RelOptInfo::pages, RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, 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().

◆ build_join_rel_hash()

static void build_join_rel_hash ( PlannerInfo root)
static

Definition at line 402 of file relnode.c.

403 {
404  HTAB *hashtab;
405  HASHCTL hash_ctl;
406  ListCell *l;
407 
408  /* Create the hash table */
409  hash_ctl.keysize = sizeof(Relids);
410  hash_ctl.entrysize = sizeof(JoinHashEntry);
411  hash_ctl.hash = bitmap_hash;
412  hash_ctl.match = bitmap_match;
413  hash_ctl.hcxt = CurrentMemoryContext;
414  hashtab = hash_create("JoinRelHashTable",
415  256L,
416  &hash_ctl,
418 
419  /* Insert all the already-existing joinrels */
420  foreach(l, root->join_rel_list)
421  {
422  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
423  JoinHashEntry *hentry;
424  bool found;
425 
426  hentry = (JoinHashEntry *) hash_search(hashtab,
427  &(rel->relids),
428  HASH_ENTER,
429  &found);
430  Assert(!found);
431  hentry->join_rel = rel;
432  }
433 
434  root->join_rel_hash = hashtab;
435 }
uint32 bitmap_hash(const void *key, Size keysize)
Definition: bitmapset.c:1181
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: bitmapset.c:1191
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
#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
MemoryContext CurrentMemoryContext
Definition: mcxt.c:124
#define lfirst(lc)
Definition: pg_list.h:170
struct JoinHashEntry JoinHashEntry
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

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_list, HASHCTL::keysize, lfirst, HASHCTL::match, and RelOptInfo::relids.

Referenced by find_join_rel().

◆ build_joinrel_joinlist()

static void build_joinrel_joinlist ( RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel 
)
static

Definition at line 1126 of file relnode.c.

1129 {
1130  List *result;
1131 
1132  /*
1133  * Collect all the clauses that syntactically belong above this level,
1134  * eliminating any duplicates (important since we will see many of the
1135  * same clauses arriving from both input relations).
1136  */
1137  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1138  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1139 
1140  joinrel->joininfo = result;
1141 }
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:1178

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

Referenced by build_join_rel().

◆ build_joinrel_partition_info()

static void build_joinrel_partition_info ( RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
List restrictlist,
JoinType  jointype 
)
static

Definition at line 1668 of file relnode.c.

1671 {
1672  PartitionScheme part_scheme;
1673 
1674  /* Nothing to do if partitionwise join technique is disabled. */
1676  {
1677  Assert(!IS_PARTITIONED_REL(joinrel));
1678  return;
1679  }
1680 
1681  /*
1682  * We can only consider this join as an input to further partitionwise
1683  * joins if (a) the input relations are partitioned and have
1684  * consider_partitionwise_join=true, (b) the partition schemes match, and
1685  * (c) we can identify an equi-join between the partition keys. Note that
1686  * if it were possible for have_partkey_equi_join to return different
1687  * answers for the same joinrel depending on which join ordering we try
1688  * first, this logic would break. That shouldn't happen, though, because
1689  * of the way the query planner deduces implied equalities and reorders
1690  * the joins. Please see optimizer/README for details.
1691  */
1692  if (outer_rel->part_scheme == NULL || inner_rel->part_scheme == NULL ||
1693  !outer_rel->consider_partitionwise_join ||
1694  !inner_rel->consider_partitionwise_join ||
1695  outer_rel->part_scheme != inner_rel->part_scheme ||
1696  !have_partkey_equi_join(joinrel, outer_rel, inner_rel,
1697  jointype, restrictlist))
1698  {
1699  Assert(!IS_PARTITIONED_REL(joinrel));
1700  return;
1701  }
1702 
1703  part_scheme = outer_rel->part_scheme;
1704 
1705  /*
1706  * This function will be called only once for each joinrel, hence it
1707  * should not have partitioning fields filled yet.
1708  */
1709  Assert(!joinrel->part_scheme && !joinrel->partexprs &&
1710  !joinrel->nullable_partexprs && !joinrel->part_rels &&
1711  !joinrel->boundinfo);
1712 
1713  /*
1714  * If the join relation is partitioned, it uses the same partitioning
1715  * scheme as the joining relations.
1716  *
1717  * Note: we calculate the partition bounds, number of partitions, and
1718  * child-join relations of the join relation in try_partitionwise_join().
1719  */
1720  joinrel->part_scheme = part_scheme;
1721  set_joinrel_partition_key_exprs(joinrel, outer_rel, inner_rel, jointype);
1722 
1723  /*
1724  * Set the consider_partitionwise_join flag.
1725  */
1726  Assert(outer_rel->consider_partitionwise_join);
1727  Assert(inner_rel->consider_partitionwise_join);
1728  joinrel->consider_partitionwise_join = true;
1729 }
bool enable_partitionwise_join
Definition: costsize.c:149
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1010
static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
Definition: relnode.c:1912
static bool have_partkey_equi_join(RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition: relnode.c:1739

References Assert(), RelOptInfo::consider_partitionwise_join, enable_partitionwise_join, have_partkey_equi_join(), IS_PARTITIONED_REL, and set_joinrel_partition_key_exprs().

Referenced by build_child_join_rel(), and build_join_rel().

◆ build_joinrel_restrictlist()

static List * build_joinrel_restrictlist ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel 
)
static

Definition at line 1096 of file relnode.c.

1100 {
1101  List *result;
1102 
1103  /*
1104  * Collect all the clauses that syntactically belong at this level,
1105  * eliminating any duplicates (important since we will see many of the
1106  * same clauses arriving from both input relations).
1107  */
1108  result = subbuild_joinrel_restrictlist(joinrel, outer_rel->joininfo, NIL);
1109  result = subbuild_joinrel_restrictlist(joinrel, inner_rel->joininfo, result);
1110 
1111  /*
1112  * Add on any clauses derived from EquivalenceClasses. These cannot be
1113  * redundant with the clauses in the joininfo lists, so don't bother
1114  * checking.
1115  */
1116  result = list_concat(result,
1118  joinrel->relids,
1119  outer_rel->relids,
1120  inner_rel));
1121 
1122  return result;
1123 }
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1394
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
static List * subbuild_joinrel_restrictlist(RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
Definition: relnode.c:1144

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

Referenced by build_join_rel().

◆ build_joinrel_tlist()

static void build_joinrel_tlist ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo input_rel 
)
static

Definition at line 981 of file relnode.c.

983 {
984  Relids relids = joinrel->relids;
985  ListCell *vars;
986 
987  foreach(vars, input_rel->reltarget->exprs)
988  {
989  Var *var = (Var *) lfirst(vars);
990 
991  /*
992  * For a PlaceHolderVar, we have to look up the PlaceHolderInfo.
993  */
994  if (IsA(var, PlaceHolderVar))
995  {
996  PlaceHolderVar *phv = (PlaceHolderVar *) var;
997  PlaceHolderInfo *phinfo = find_placeholder_info(root, phv);
998 
999  /* Is it still needed above this joinrel? */
1000  if (bms_nonempty_difference(phinfo->ph_needed, relids))
1001  {
1002  /* Yup, add it to the output */
1003  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1004  phv);
1005  /* Bubbling up the precomputed result has cost zero */
1006  joinrel->reltarget->width += phinfo->ph_width;
1007  }
1008  continue;
1009  }
1010 
1011  /*
1012  * Otherwise, anything in a baserel or joinrel targetlist ought to be
1013  * a Var. (More general cases can only appear in appendrel child
1014  * rels, which will never be seen here.)
1015  */
1016  if (!IsA(var, Var))
1017  elog(ERROR, "unexpected node type in rel targetlist: %d",
1018  (int) nodeTag(var));
1019 
1020  if (var->varno == ROWID_VAR)
1021  {
1022  /* UPDATE/DELETE/MERGE row identity vars are always needed */
1023  RowIdentityVarInfo *ridinfo = (RowIdentityVarInfo *)
1024  list_nth(root->row_identity_vars, var->varattno - 1);
1025 
1026  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1027  var);
1028  /* Vars have cost zero, so no need to adjust reltarget->cost */
1029  joinrel->reltarget->width += ridinfo->rowidwidth;
1030  }
1031  else
1032  {
1033  RelOptInfo *baserel;
1034  int ndx;
1035 
1036  /* Get the Var's original base rel */
1037  baserel = find_base_rel(root, var->varno);
1038 
1039  /* Is it still needed above this joinrel? */
1040  ndx = var->varattno - baserel->min_attr;
1041  if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
1042  {
1043  /* Yup, add it to the output */
1044  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1045  var);
1046  /* Vars have cost zero, so no need to adjust reltarget->cost */
1047  joinrel->reltarget->width += baserel->attr_widths[ndx];
1048  }
1049  }
1050  }
1051 }
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:548
#define ERROR
Definition: elog.h:35
#define IsA(nodeptr, _type_)
Definition: nodes.h:168
#define nodeTag(nodeptr)
Definition: nodes.h:122
static void * list_nth(const List *list, int n)
Definition: pg_list.h:297
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition: placeholder.c:67
#define ROWID_VAR
Definition: primnodes.h:196
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:379
Relids ph_needed
Definition: pathnodes.h:2882
List * row_identity_vars
Definition: pathnodes.h:358
Definition: primnodes.h:205
AttrNumber varattno
Definition: primnodes.h:217
int varno
Definition: primnodes.h:212
Definition: regcomp.c:282

References bms_nonempty_difference(), elog(), ERROR, PathTarget::exprs, find_base_rel(), find_placeholder_info(), IsA, lappend(), lfirst, list_nth(), RelOptInfo::min_attr, nodeTag, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_width, RelOptInfo::relids, RelOptInfo::reltarget, PlannerInfo::row_identity_vars, ROWID_VAR, RowIdentityVarInfo::rowidwidth, Var::varattno, Var::varno, and PathTarget::width.

Referenced by build_join_rel().

◆ build_simple_rel()

RelOptInfo* build_simple_rel ( PlannerInfo root,
int  relid,
RelOptInfo parent 
)

Definition at line 182 of file relnode.c.

183 {
184  RelOptInfo *rel;
185  RangeTblEntry *rte;
186 
187  /* Rel should not exist already */
188  Assert(relid > 0 && relid < root->simple_rel_array_size);
189  if (root->simple_rel_array[relid] != NULL)
190  elog(ERROR, "rel %d already exists", relid);
191 
192  /* Fetch RTE for relation */
193  rte = root->simple_rte_array[relid];
194  Assert(rte != NULL);
195 
196  rel = makeNode(RelOptInfo);
198  rel->relids = bms_make_singleton(relid);
199  rel->rows = 0;
200  /* cheap startup cost is interesting iff not all tuples to be retrieved */
201  rel->consider_startup = (root->tuple_fraction > 0);
202  rel->consider_param_startup = false; /* might get changed later */
203  rel->consider_parallel = false; /* might get changed later */
205  rel->pathlist = NIL;
206  rel->ppilist = NIL;
207  rel->partial_pathlist = NIL;
208  rel->cheapest_startup_path = NULL;
209  rel->cheapest_total_path = NULL;
210  rel->cheapest_unique_path = NULL;
212  rel->relid = relid;
213  rel->rtekind = rte->rtekind;
214  /* min_attr, max_attr, attr_needed, attr_widths are set below */
215  rel->lateral_vars = NIL;
216  rel->indexlist = NIL;
217  rel->statlist = NIL;
218  rel->pages = 0;
219  rel->tuples = 0;
220  rel->allvisfrac = 0;
221  rel->eclass_indexes = NULL;
222  rel->subroot = NULL;
223  rel->subplan_params = NIL;
224  rel->rel_parallel_workers = -1; /* set up in get_relation_info */
225  rel->amflags = 0;
226  rel->serverid = InvalidOid;
227  if (rte->rtekind == RTE_RELATION)
228  {
229  /*
230  * Get the userid from the relation's RTEPermissionInfo, though only
231  * the tables mentioned in query are assigned RTEPermissionInfos.
232  * Child relations (otherrels) simply use the parent's value.
233  */
234  if (parent == NULL)
235  {
236  RTEPermissionInfo *perminfo;
237 
238  perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
239  rel->userid = perminfo->checkAsUser;
240  }
241  else
242  rel->userid = parent->userid;
243  }
244  else
245  rel->userid = InvalidOid;
246  rel->useridiscurrent = false;
247  rel->fdwroutine = NULL;
248  rel->fdw_private = NULL;
249  rel->unique_for_rels = NIL;
250  rel->non_unique_for_rels = NIL;
251  rel->baserestrictinfo = NIL;
252  rel->baserestrictcost.startup = 0;
253  rel->baserestrictcost.per_tuple = 0;
254  rel->baserestrict_min_security = UINT_MAX;
255  rel->joininfo = NIL;
256  rel->has_eclass_joins = false;
257  rel->consider_partitionwise_join = false; /* might get changed later */
258  rel->part_scheme = NULL;
259  rel->nparts = -1;
260  rel->boundinfo = NULL;
261  rel->partbounds_merged = false;
262  rel->partition_qual = NIL;
263  rel->part_rels = NULL;
264  rel->live_parts = NULL;
265  rel->all_partrels = NULL;
266  rel->partexprs = NULL;
267  rel->nullable_partexprs = NULL;
268 
269  /*
270  * Pass assorted information down the inheritance hierarchy.
271  */
272  if (parent)
273  {
274  /* We keep back-links to immediate parent and topmost parent. */
275  rel->parent = parent;
276  rel->top_parent = parent->top_parent ? parent->top_parent : parent;
277  rel->top_parent_relids = rel->top_parent->relids;
278 
279  /*
280  * Also propagate lateral-reference information from appendrel parent
281  * rels to their child rels. We intentionally give each child rel the
282  * same minimum parameterization, even though it's quite possible that
283  * some don't reference all the lateral rels. This is because any
284  * append path for the parent will have to have the same
285  * parameterization for every child anyway, and there's no value in
286  * forcing extra reparameterize_path() calls. Similarly, a lateral
287  * reference to the parent prevents use of otherwise-movable join rels
288  * for each child.
289  *
290  * It's possible for child rels to have their own children, in which
291  * case the topmost parent's lateral info propagates all the way down.
292  */
294  rel->lateral_relids = parent->lateral_relids;
296  }
297  else
298  {
299  rel->parent = NULL;
300  rel->top_parent = NULL;
301  rel->top_parent_relids = NULL;
302  rel->direct_lateral_relids = NULL;
303  rel->lateral_relids = NULL;
304  rel->lateral_referencers = NULL;
305  }
306 
307  /* Check type of rtable entry */
308  switch (rte->rtekind)
309  {
310  case RTE_RELATION:
311  /* Table --- retrieve statistics from the system catalogs */
312  get_relation_info(root, rte->relid, rte->inh, rel);
313  break;
314  case RTE_SUBQUERY:
315  case RTE_FUNCTION:
316  case RTE_TABLEFUNC:
317  case RTE_VALUES:
318  case RTE_CTE:
319  case RTE_NAMEDTUPLESTORE:
320 
321  /*
322  * Subquery, function, tablefunc, values list, CTE, or ENR --- set
323  * up attr range and arrays
324  *
325  * Note: 0 is included in range to support whole-row Vars
326  */
327  rel->min_attr = 0;
328  rel->max_attr = list_length(rte->eref->colnames);
329  rel->attr_needed = (Relids *)
330  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
331  rel->attr_widths = (int32 *)
332  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
333  break;
334  case RTE_RESULT:
335  /* RTE_RESULT has no columns, nor could it have whole-row Var */
336  rel->min_attr = 0;
337  rel->max_attr = -1;
338  rel->attr_needed = NULL;
339  rel->attr_widths = NULL;
340  break;
341  default:
342  elog(ERROR, "unrecognized RTE kind: %d",
343  (int) rte->rtekind);
344  break;
345  }
346 
347  /*
348  * Copy the parent's quals to the child, with appropriate substitution of
349  * variables. If any constant false or NULL clauses turn up, we can mark
350  * the child as dummy right away. (We must do this immediately so that
351  * pruning works correctly when recursing in expand_partitioned_rtentry.)
352  */
353  if (parent)
354  {
355  AppendRelInfo *appinfo = root->append_rel_array[relid];
356 
357  Assert(appinfo != NULL);
358  if (!apply_child_basequals(root, parent, rel, rte, appinfo))
359  {
360  /*
361  * Some restriction clause reduced to constant FALSE or NULL after
362  * substitution, so this child need not be scanned.
363  */
364  mark_dummy_rel(rel);
365  }
366  }
367 
368  /* Save the finished struct in the query's simple_rel_array */
369  root->simple_rel_array[relid] = rel;
370 
371  return rel;
372 }
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
signed int int32
Definition: c.h:430
bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, RangeTblEntry *childRTE, AppendRelInfo *appinfo)
Definition: inherit.c:808
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1261
void * palloc0(Size size)
Definition: mcxt.c:1230
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
@ RTE_CTE
Definition: parsenodes.h:988
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:989
@ RTE_VALUES
Definition: parsenodes.h:987
@ RTE_SUBQUERY
Definition: parsenodes.h:983
@ RTE_RESULT
Definition: parsenodes.h:990
@ RTE_FUNCTION
Definition: parsenodes.h:985
@ RTE_TABLEFUNC
Definition: parsenodes.h:986
@ RTE_RELATION
Definition: parsenodes.h:982
@ RELOPT_BASEREL
Definition: pathnodes.h:779
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:781
static int list_length(const List *l)
Definition: pg_list.h:150
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition: plancat.c:115
List * colnames
Definition: primnodes.h:43
Query * parse
Definition: pathnodes.h:202
List * rteperminfos
Definition: parsenodes.h:156
Alias * eref
Definition: parsenodes.h:1152
RTEKind rtekind
Definition: parsenodes.h:1001

References RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, apply_child_basequals(), Assert(), RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_make_singleton(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::cheapest_unique_path, RTEPermissionInfo::checkAsUser, Alias::colnames, RelOptInfo::consider_parallel, RelOptInfo::consider_param_startup, RelOptInfo::consider_partitionwise_join, RelOptInfo::consider_startup, create_empty_pathtarget(), RelOptInfo::direct_lateral_relids, RelOptInfo::eclass_indexes, elog(), RangeTblEntry::eref, ERROR, get_relation_info(), getRTEPermissionInfo(), RelOptInfo::has_eclass_joins, RelOptInfo::indexlist, RangeTblEntry::inh, InvalidOid, RelOptInfo::joininfo, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, list_length(), RelOptInfo::live_parts, makeNode, mark_dummy_rel(), RelOptInfo::max_attr, RelOptInfo::min_attr, NIL, RelOptInfo::non_unique_for_rels, RelOptInfo::nparts, RelOptInfo::pages, palloc0(), PlannerInfo::parse, RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::ppilist, RelOptInfo::rel_parallel_workers, RangeTblEntry::relid, RelOptInfo::relid, RelOptInfo::relids, RELOPT_BASEREL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, RelOptInfo::rows, RTE_CTE, RTE_FUNCTION, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, RelOptInfo::rtekind, Query::rteperminfos, RelOptInfo::serverid, 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(), expand_appendrel_subquery(), expand_inherited_rtentry(), expand_partitioned_rtentry(), plan_cluster_use_sort(), plan_create_index_workers(), query_planner(), and recurse_set_operations().

◆ expand_planner_arrays()

void expand_planner_arrays ( PlannerInfo root,
int  add_size 
)

Definition at line 153 of file relnode.c.

154 {
155  int new_size;
156 
157  Assert(add_size > 0);
158 
159  new_size = root->simple_rel_array_size + add_size;
160 
161  root->simple_rel_array =
162  repalloc0_array(root->simple_rel_array, RelOptInfo *, root->simple_rel_array_size, new_size);
163 
164  root->simple_rte_array =
165  repalloc0_array(root->simple_rte_array, RangeTblEntry *, root->simple_rel_array_size, new_size);
166 
167  if (root->append_rel_array)
168  root->append_rel_array =
169  repalloc0_array(root->append_rel_array, AppendRelInfo *, root->simple_rel_array_size, new_size);
170  else
171  root->append_rel_array =
172  palloc0_array(AppendRelInfo *, new_size);
173 
174  root->simple_rel_array_size = new_size;
175 }
#define palloc0_array(type, count)
Definition: fe_memutils.h:65
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:107
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
int simple_rel_array_size
Definition: pathnodes.h:232

References add_size(), Assert(), palloc0_array, repalloc0_array, and PlannerInfo::simple_rel_array_size.

Referenced by expand_inherited_rtentry(), and expand_partitioned_rtentry().

◆ fetch_upper_rel()

RelOptInfo* fetch_upper_rel ( PlannerInfo root,
UpperRelationKind  kind,
Relids  relids 
)

Definition at line 1230 of file relnode.c.

1231 {
1232  RelOptInfo *upperrel;
1233  ListCell *lc;
1234 
1235  /*
1236  * For the moment, our indexing data structure is just a List for each
1237  * relation kind. If we ever get so many of one kind that this stops
1238  * working well, we can improve it. No code outside this function should
1239  * assume anything about how to find a particular upperrel.
1240  */
1241 
1242  /* If we already made this upperrel for the query, return it */
1243  foreach(lc, root->upper_rels[kind])
1244  {
1245  upperrel = (RelOptInfo *) lfirst(lc);
1246 
1247  if (bms_equal(upperrel->relids, relids))
1248  return upperrel;
1249  }
1250 
1251  upperrel = makeNode(RelOptInfo);
1252  upperrel->reloptkind = RELOPT_UPPER_REL;
1253  upperrel->relids = bms_copy(relids);
1254 
1255  /* cheap startup cost is interesting iff not all tuples to be retrieved */
1256  upperrel->consider_startup = (root->tuple_fraction > 0);
1257  upperrel->consider_param_startup = false;
1258  upperrel->consider_parallel = false; /* might get changed later */
1259  upperrel->reltarget = create_empty_pathtarget();
1260  upperrel->pathlist = NIL;
1261  upperrel->cheapest_startup_path = NULL;
1262  upperrel->cheapest_total_path = NULL;
1263  upperrel->cheapest_unique_path = NULL;
1264  upperrel->cheapest_parameterized_paths = NIL;
1265 
1266  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1267 
1268  return upperrel;
1269 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
@ RELOPT_UPPER_REL
Definition: pathnodes.h:783

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, and PlannerInfo::tuple_fraction.

Referenced by add_rtes_to_flat_rtable(), create_distinct_paths(), create_ordered_paths(), create_partial_distinct_paths(), create_partial_grouping_paths(), create_window_paths(), generate_nonunion_paths(), generate_recursion_path(), generate_union_paths(), grouping_planner(), make_grouping_rel(), make_subplan(), make_union_unique(), preprocess_minmax_aggregates(), recurse_set_operations(), set_subquery_pathlist(), set_subquery_size_estimates(), SS_process_ctes(), standard_planner(), and subquery_planner().

◆ find_base_rel()

◆ find_childrel_parents()

Relids find_childrel_parents ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1281 of file relnode.c.

1282 {
1283  Relids result = NULL;
1284 
1286  Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1287 
1288  do
1289  {
1290  AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1291  Index prelid = appinfo->parent_relid;
1292 
1293  result = bms_add_member(result, prelid);
1294 
1295  /* traverse up to the parent rel, loop if it's also a child rel */
1296  rel = find_base_rel(root, prelid);
1297  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1298 
1299  Assert(rel->reloptkind == RELOPT_BASEREL);
1300 
1301  return result;
1302 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:739
unsigned int Index
Definition: c.h:550
Index parent_relid
Definition: pathnodes.h:2757

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

Referenced by check_index_predicates(), and generate_implied_equalities_for_column().

◆ find_join_rel()

RelOptInfo* find_join_rel ( PlannerInfo root,
Relids  relids 
)

Definition at line 443 of file relnode.c.

444 {
445  /*
446  * Switch to using hash lookup when list grows "too long". The threshold
447  * is arbitrary and is known only here.
448  */
449  if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
450  build_join_rel_hash(root);
451 
452  /*
453  * Use either hashtable lookup or linear search, as appropriate.
454  *
455  * Note: the seemingly redundant hashkey variable is used to avoid taking
456  * the address of relids; unless the compiler is exceedingly smart, doing
457  * so would force relids out of a register and thus probably slow down the
458  * list-search case.
459  */
460  if (root->join_rel_hash)
461  {
462  Relids hashkey = relids;
463  JoinHashEntry *hentry;
464 
465  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
466  &hashkey,
467  HASH_FIND,
468  NULL);
469  if (hentry)
470  return hentry->join_rel;
471  }
472  else
473  {
474  ListCell *l;
475 
476  foreach(l, root->join_rel_list)
477  {
478  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
479 
480  if (bms_equal(rel->relids, relids))
481  return rel;
482  }
483  }
484 
485  return NULL;
486 }
@ HASH_FIND
Definition: hsearch.h:113
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:402

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

Referenced by build_child_join_rel(), build_join_rel(), examine_variable(), find_join_input_rel(), get_matching_part_pairs(), and postgresPlanDirectModify().

◆ find_param_path_info()

ParamPathInfo* find_param_path_info ( RelOptInfo rel,
Relids  required_outer 
)

Definition at line 1646 of file relnode.c.

1647 {
1648  ListCell *lc;
1649 
1650  foreach(lc, rel->ppilist)
1651  {
1652  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1653 
1654  if (bms_equal(ppi->ppi_req_outer, required_outer))
1655  return ppi;
1656  }
1657 
1658  return NULL;
1659 }
Relids ppi_req_outer
Definition: pathnodes.h:1467

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

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

◆ get_appendrel_parampathinfo()

ParamPathInfo* get_appendrel_parampathinfo ( RelOptInfo appendrel,
Relids  required_outer 
)

Definition at line 1614 of file relnode.c.

1615 {
1616  ParamPathInfo *ppi;
1617 
1618  /* If rel has LATERAL refs, every path for it should account for them */
1619  Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1620 
1621  /* Unparameterized paths have no ParamPathInfo */
1622  if (bms_is_empty(required_outer))
1623  return NULL;
1624 
1625  Assert(!bms_overlap(appendrel->relids, required_outer));
1626 
1627  /* If we already have a PPI for this parameterization, just return it */
1628  if ((ppi = find_param_path_info(appendrel, required_outer)))
1629  return ppi;
1630 
1631  /* Else build the ParamPathInfo */
1632  ppi = makeNode(ParamPathInfo);
1633  ppi->ppi_req_outer = required_outer;
1634  ppi->ppi_rows = 0;
1635  ppi->ppi_clauses = NIL;
1636  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1637 
1638  return ppi;
1639 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:316
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:495
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1646
Cardinality ppi_rows
Definition: pathnodes.h:1468
List * ppi_clauses
Definition: pathnodes.h:1469

References Assert(), bms_is_empty(), bms_is_subset(), bms_overlap(), find_param_path_info(), lappend(), RelOptInfo::lateral_relids, 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().

◆ get_baserel_parampathinfo()

ParamPathInfo* get_baserel_parampathinfo ( PlannerInfo root,
RelOptInfo baserel,
Relids  required_outer 
)

Definition at line 1317 of file relnode.c.

1319 {
1320  ParamPathInfo *ppi;
1321  Relids joinrelids;
1322  List *pclauses;
1323  double rows;
1324  ListCell *lc;
1325 
1326  /* If rel has LATERAL refs, every path for it should account for them */
1327  Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1328 
1329  /* Unparameterized paths have no ParamPathInfo */
1330  if (bms_is_empty(required_outer))
1331  return NULL;
1332 
1333  Assert(!bms_overlap(baserel->relids, required_outer));
1334 
1335  /* If we already have a PPI for this parameterization, just return it */
1336  if ((ppi = find_param_path_info(baserel, required_outer)))
1337  return ppi;
1338 
1339  /*
1340  * Identify all joinclauses that are movable to this base rel given this
1341  * parameterization.
1342  */
1343  joinrelids = bms_union(baserel->relids, required_outer);
1344  pclauses = NIL;
1345  foreach(lc, baserel->joininfo)
1346  {
1347  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1348 
1349  if (join_clause_is_movable_into(rinfo,
1350  baserel->relids,
1351  joinrelids))
1352  pclauses = lappend(pclauses, rinfo);
1353  }
1354 
1355  /*
1356  * Add in joinclauses generated by EquivalenceClasses, too. (These
1357  * necessarily satisfy join_clause_is_movable_into.)
1358  */
1359  pclauses = list_concat(pclauses,
1361  joinrelids,
1362  required_outer,
1363  baserel));
1364 
1365  /* Estimate the number of rows returned by the parameterized scan */
1366  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1367 
1368  /* And now we can build the ParamPathInfo */
1369  ppi = makeNode(ParamPathInfo);
1370  ppi->ppi_req_outer = required_outer;
1371  ppi->ppi_rows = rows;
1372  ppi->ppi_clauses = pclauses;
1373  baserel->ppilist = lappend(baserel->ppilist, ppi);
1374 
1375  return ppi;
1376 }
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:5018
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:600

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

Referenced by create_append_path(), create_bitmap_and_path(), create_bitmap_heap_path(), create_bitmap_or_path(), create_ctescan_path(), create_foreignscan_path(), create_functionscan_path(), create_gather_merge_path(), create_gather_path(), create_index_path(), create_namedtuplestorescan_path(), create_resultscan_path(), create_samplescan_path(), create_seqscan_path(), create_subqueryscan_path(), create_tablefuncscan_path(), create_tidrangescan_path(), create_tidscan_path(), create_valuesscan_path(), create_worktablescan_path(), postgresGetForeignPaths(), and reparameterize_path().

◆ get_joinrel_parampathinfo()

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 1407 of file relnode.c.

1413 {
1414  ParamPathInfo *ppi;
1415  Relids join_and_req;
1416  Relids outer_and_req;
1417  Relids inner_and_req;
1418  List *pclauses;
1419  List *eclauses;
1420  List *dropped_ecs;
1421  double rows;
1422  ListCell *lc;
1423 
1424  /* If rel has LATERAL refs, every path for it should account for them */
1425  Assert(bms_is_subset(joinrel->lateral_relids, required_outer));
1426 
1427  /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1428  if (bms_is_empty(required_outer))
1429  return NULL;
1430 
1431  Assert(!bms_overlap(joinrel->relids, required_outer));
1432 
1433  /*
1434  * Identify all joinclauses that are movable to this join rel given this
1435  * parameterization. These are the clauses that are movable into this
1436  * join, but not movable into either input path. Treat an unparameterized
1437  * input path as not accepting parameterized clauses (because it won't,
1438  * per the shortcut exit above), even though the joinclause movement rules
1439  * might allow the same clauses to be moved into a parameterized path for
1440  * that rel.
1441  */
1442  join_and_req = bms_union(joinrel->relids, required_outer);
1443  if (outer_path->param_info)
1444  outer_and_req = bms_union(outer_path->parent->relids,
1445  PATH_REQ_OUTER(outer_path));
1446  else
1447  outer_and_req = NULL; /* outer path does not accept parameters */
1448  if (inner_path->param_info)
1449  inner_and_req = bms_union(inner_path->parent->relids,
1450  PATH_REQ_OUTER(inner_path));
1451  else
1452  inner_and_req = NULL; /* inner path does not accept parameters */
1453 
1454  pclauses = NIL;
1455  foreach(lc, joinrel->joininfo)
1456  {
1457  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1458 
1459  if (join_clause_is_movable_into(rinfo,
1460  joinrel->relids,
1461  join_and_req) &&
1463  outer_path->parent->relids,
1464  outer_and_req) &&
1466  inner_path->parent->relids,
1467  inner_and_req))
1468  pclauses = lappend(pclauses, rinfo);
1469  }
1470 
1471  /* Consider joinclauses generated by EquivalenceClasses, too */
1472  eclauses = generate_join_implied_equalities(root,
1473  join_and_req,
1474  required_outer,
1475  joinrel);
1476  /* We only want ones that aren't movable to lower levels */
1477  dropped_ecs = NIL;
1478  foreach(lc, eclauses)
1479  {
1480  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1481 
1482  /*
1483  * In principle, join_clause_is_movable_into() should accept anything
1484  * returned by generate_join_implied_equalities(); but because its
1485  * analysis is only approximate, sometimes it doesn't. So we
1486  * currently cannot use this Assert; instead just assume it's okay to
1487  * apply the joinclause at this level.
1488  */
1489 #ifdef NOT_USED
1491  joinrel->relids,
1492  join_and_req));
1493 #endif
1494  if (join_clause_is_movable_into(rinfo,
1495  outer_path->parent->relids,
1496  outer_and_req))
1497  continue; /* drop if movable into LHS */
1498  if (join_clause_is_movable_into(rinfo,
1499  inner_path->parent->relids,
1500  inner_and_req))
1501  {
1502  /* drop if movable into RHS, but remember EC for use below */
1503  Assert(rinfo->left_ec == rinfo->right_ec);
1504  dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1505  continue;
1506  }
1507  pclauses = lappend(pclauses, rinfo);
1508  }
1509 
1510  /*
1511  * EquivalenceClasses are harder to deal with than we could wish, because
1512  * of the fact that a given EC can generate different clauses depending on
1513  * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1514  * LHS and RHS of the current join and Z is in required_outer, and further
1515  * suppose that the inner_path is parameterized by both X and Z. The code
1516  * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1517  * and in the latter case will have discarded it as being movable into the
1518  * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1519  * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1520  * not have produced both, and we can't readily tell from here which one
1521  * it did pick. If we add no clause to this join, we'll end up with
1522  * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1523  * constrained to be equal to the other members of the EC. (When we come
1524  * to join Z to this X/Y path, we will certainly drop whichever EC clause
1525  * is generated at that join, so this omission won't get fixed later.)
1526  *
1527  * To handle this, for each EC we discarded such a clause from, try to
1528  * generate a clause connecting the required_outer rels to the join's LHS
1529  * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1530  * the clause can't be moved to the LHS, add it to the current join's
1531  * restriction clauses. (If an EC cannot generate such a clause then it
1532  * has nothing that needs to be enforced here, while if the clause can be
1533  * moved into the LHS then it should have been enforced within that path.)
1534  *
1535  * Note that we don't need similar processing for ECs whose clause was
1536  * considered to be movable into the LHS, because the LHS can't refer to
1537  * the RHS so there is no comparable ambiguity about what it might
1538  * actually be enforcing internally.
1539  */
1540  if (dropped_ecs)
1541  {
1542  Relids real_outer_and_req;
1543 
1544  real_outer_and_req = bms_union(outer_path->parent->relids,
1545  required_outer);
1546  eclauses =
1548  dropped_ecs,
1549  real_outer_and_req,
1550  required_outer,
1551  outer_path->parent);
1552  foreach(lc, eclauses)
1553  {
1554  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1555 
1556  /* As above, can't quite assert this here */
1557 #ifdef NOT_USED
1559  outer_path->parent->relids,
1560  real_outer_and_req));
1561 #endif
1562  if (!join_clause_is_movable_into(rinfo,
1563  outer_path->parent->relids,
1564  outer_and_req))
1565  pclauses = lappend(pclauses, rinfo);
1566  }
1567  }
1568 
1569  /*
1570  * Now, attach the identified moved-down clauses to the caller's
1571  * restrict_clauses list. By using list_concat in this order, we leave
1572  * the original list structure of restrict_clauses undamaged.
1573  */
1574  *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1575 
1576  /* If we already have a PPI for this parameterization, just return it */
1577  if ((ppi = find_param_path_info(joinrel, required_outer)))
1578  return ppi;
1579 
1580  /* Estimate the number of rows returned by the parameterized join */
1581  rows = get_parameterized_joinrel_size(root, joinrel,
1582  outer_path,
1583  inner_path,
1584  sjinfo,
1585  *restrict_clauses);
1586 
1587  /*
1588  * And now we can build the ParamPathInfo. No point in saving the
1589  * input-pair-dependent clause list, though.
1590  *
1591  * Note: in GEQO mode, we'll be called in a temporary memory context, but
1592  * the joinrel structure is there too, so no problem.
1593  */
1594  ppi = makeNode(ParamPathInfo);
1595  ppi->ppi_req_outer = required_outer;
1596  ppi->ppi_rows = rows;
1597  ppi->ppi_clauses = NIL;
1598  joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1599 
1600  return ppi;
1601 }
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:5099
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1472
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1556

References Assert(), bms_is_empty(), bms_is_subset(), 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(), RelOptInfo::lateral_relids, lfirst, list_concat(), makeNode, NIL, PATH_REQ_OUTER, ParamPathInfo::ppi_clauses, ParamPathInfo::ppi_req_outer, ParamPathInfo::ppi_rows, RelOptInfo::ppilist, and RelOptInfo::relids.

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

◆ have_partkey_equi_join()

static bool have_partkey_equi_join ( RelOptInfo joinrel,
RelOptInfo rel1,
RelOptInfo rel2,
JoinType  jointype,
List restrictlist 
)
static

Definition at line 1739 of file relnode.c.

1742 {
1743  PartitionScheme part_scheme = rel1->part_scheme;
1744  ListCell *lc;
1745  int cnt_pks;
1746  bool pk_has_clause[PARTITION_MAX_KEYS];
1747  bool strict_op;
1748 
1749  /*
1750  * This function must only be called when the joined relations have same
1751  * partitioning scheme.
1752  */
1753  Assert(rel1->part_scheme == rel2->part_scheme);
1754  Assert(part_scheme);
1755 
1756  memset(pk_has_clause, 0, sizeof(pk_has_clause));
1757  foreach(lc, restrictlist)
1758  {
1759  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1760  OpExpr *opexpr;
1761  Expr *expr1;
1762  Expr *expr2;
1763  int ipk1;
1764  int ipk2;
1765 
1766  /* If processing an outer join, only use its own join clauses. */
1767  if (IS_OUTER_JOIN(jointype) &&
1768  RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1769  continue;
1770 
1771  /* Skip clauses which can not be used for a join. */
1772  if (!rinfo->can_join)
1773  continue;
1774 
1775  /* Skip clauses which are not equality conditions. */
1776  if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
1777  continue;
1778 
1779  /* Should be OK to assume it's an OpExpr. */
1780  opexpr = castNode(OpExpr, rinfo->clause);
1781 
1782  /* Match the operands to the relation. */
1783  if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
1784  bms_is_subset(rinfo->right_relids, rel2->relids))
1785  {
1786  expr1 = linitial(opexpr->args);
1787  expr2 = lsecond(opexpr->args);
1788  }
1789  else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
1790  bms_is_subset(rinfo->right_relids, rel1->relids))
1791  {
1792  expr1 = lsecond(opexpr->args);
1793  expr2 = linitial(opexpr->args);
1794  }
1795  else
1796  continue;
1797 
1798  /*
1799  * Now we need to know whether the join operator is strict; see
1800  * comments in pathnodes.h.
1801  */
1802  strict_op = op_strict(opexpr->opno);
1803 
1804  /*
1805  * Only clauses referencing the partition keys are useful for
1806  * partitionwise join.
1807  */
1808  ipk1 = match_expr_to_partition_keys(expr1, rel1, strict_op);
1809  if (ipk1 < 0)
1810  continue;
1811  ipk2 = match_expr_to_partition_keys(expr2, rel2, strict_op);
1812  if (ipk2 < 0)
1813  continue;
1814 
1815  /*
1816  * If the clause refers to keys at different ordinal positions, it can
1817  * not be used for partitionwise join.
1818  */
1819  if (ipk1 != ipk2)
1820  continue;
1821 
1822  /*
1823  * The clause allows partitionwise join only if it uses the same
1824  * operator family as that specified by the partition key.
1825  */
1826  if (rel1->part_scheme->strategy == PARTITION_STRATEGY_HASH)
1827  {
1828  if (!OidIsValid(rinfo->hashjoinoperator) ||
1829  !op_in_opfamily(rinfo->hashjoinoperator,
1830  part_scheme->partopfamily[ipk1]))
1831  continue;
1832  }
1833  else if (!list_member_oid(rinfo->mergeopfamilies,
1834  part_scheme->partopfamily[ipk1]))
1835  continue;
1836 
1837  /* Mark the partition key as having an equi-join clause. */
1838  pk_has_clause[ipk1] = true;
1839  }
1840 
1841  /* Check whether every partition key has an equi-join condition. */
1842  for (cnt_pks = 0; cnt_pks < part_scheme->partnatts; cnt_pks++)
1843  {
1844  if (!pk_has_clause[cnt_pks])
1845  return false;
1846  }
1847 
1848  return true;
1849 }
#define OidIsValid(objectId)
Definition: c.h:711
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:721
bool op_strict(Oid opno)
Definition: lsyscache.c:1459
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:65
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:336
#define castNode(_type_, nodeptr)
Definition: nodes.h:186
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:837
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2569
#define PARTITION_MAX_KEYS
#define lfirst_node(type, lc)
Definition: pg_list.h:174
#define linitial(l)
Definition: pg_list.h:176
#define lsecond(l)
Definition: pg_list.h:181
static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
Definition: relnode.c:1863
Oid opno
Definition: primnodes.h:648
List * args
Definition: primnodes.h:666
Expr * clause
Definition: pathnodes.h:2435

References OpExpr::args, Assert(), bms_is_subset(), castNode, RestrictInfo::clause, IS_OUTER_JOIN, lfirst_node, linitial, list_member_oid(), lsecond, match_expr_to_partition_keys(), OidIsValid, op_in_opfamily(), op_strict(), OpExpr::opno, PARTITION_MAX_KEYS, PARTITION_STRATEGY_HASH, PartitionSchemeData::partnatts, PartitionSchemeData::partopfamily, RelOptInfo::relids, and RINFO_IS_PUSHED_DOWN.

Referenced by build_joinrel_partition_info().

◆ match_expr_to_partition_keys()

static int match_expr_to_partition_keys ( Expr expr,
RelOptInfo rel,
bool  strict_op 
)
static

Definition at line 1863 of file relnode.c.

1864 {
1865  int cnt;
1866 
1867  /* This function should be called only for partitioned relations. */
1868  Assert(rel->part_scheme);
1869  Assert(rel->partexprs);
1870  Assert(rel->nullable_partexprs);
1871 
1872  /* Remove any relabel decorations. */
1873  while (IsA(expr, RelabelType))
1874  expr = (Expr *) (castNode(RelabelType, expr))->arg;
1875 
1876  for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
1877  {
1878  ListCell *lc;
1879 
1880  /* We can always match to the non-nullable partition keys. */
1881  foreach(lc, rel->partexprs[cnt])
1882  {
1883  if (equal(lfirst(lc), expr))
1884  return cnt;
1885  }
1886 
1887  if (!strict_op)
1888  continue;
1889 
1890  /*
1891  * If it's a strict join operator then a NULL partition key on one
1892  * side will not join to any partition key on the other side, and in
1893  * particular such a row can't join to a row from a different
1894  * partition on the other side. So, it's okay to search the nullable
1895  * partition keys as well.
1896  */
1897  foreach(lc, rel->nullable_partexprs[cnt])
1898  {
1899  if (equal(lfirst(lc), expr))
1900  return cnt;
1901  }
1902  }
1903 
1904  return -1;
1905 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:225
void * arg

References arg, Assert(), castNode, equal(), IsA, and lfirst.

Referenced by have_partkey_equi_join().

◆ min_join_parameterization()

Relids min_join_parameterization ( PlannerInfo root,
Relids  joinrelids,
RelOptInfo outer_rel,
RelOptInfo inner_rel 
)

Definition at line 937 of file relnode.c.

941 {
942  Relids result;
943 
944  /*
945  * Basically we just need the union of the inputs' lateral_relids, less
946  * whatever is already in the join.
947  *
948  * It's not immediately obvious that this is a valid way to compute the
949  * result, because it might seem that we're ignoring possible lateral refs
950  * of PlaceHolderVars that are due to be computed at the join but not in
951  * either input. However, because create_lateral_join_info() already
952  * charged all such PHV refs to each member baserel of the join, they'll
953  * be accounted for already in the inputs' lateral_relids. Likewise, we
954  * do not need to worry about doing transitive closure here, because that
955  * was already accounted for in the original baserel lateral_relids.
956  */
957  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
958  result = bms_del_members(result, joinrelids);
959 
960  /* Maintain invariant that result is exactly NULL if empty */
961  if (bms_is_empty(result))
962  result = NULL;
963 
964  return result;
965 }

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

Referenced by build_join_rel(), and join_is_legal().

◆ set_foreign_rel_properties()

static void set_foreign_rel_properties ( RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel 
)
static

Definition at line 506 of file relnode.c.

508 {
509  if (OidIsValid(outer_rel->serverid) &&
510  inner_rel->serverid == outer_rel->serverid)
511  {
512  if (inner_rel->userid == outer_rel->userid)
513  {
514  joinrel->serverid = outer_rel->serverid;
515  joinrel->userid = outer_rel->userid;
516  joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
517  joinrel->fdwroutine = outer_rel->fdwroutine;
518  }
519  else if (!OidIsValid(inner_rel->userid) &&
520  outer_rel->userid == GetUserId())
521  {
522  joinrel->serverid = outer_rel->serverid;
523  joinrel->userid = outer_rel->userid;
524  joinrel->useridiscurrent = true;
525  joinrel->fdwroutine = outer_rel->fdwroutine;
526  }
527  else if (!OidIsValid(outer_rel->userid) &&
528  inner_rel->userid == GetUserId())
529  {
530  joinrel->serverid = outer_rel->serverid;
531  joinrel->userid = inner_rel->userid;
532  joinrel->useridiscurrent = true;
533  joinrel->fdwroutine = outer_rel->fdwroutine;
534  }
535  }
536 }
Oid GetUserId(void)
Definition: miscinit.c:497

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

Referenced by build_child_join_rel(), and build_join_rel().

◆ set_joinrel_partition_key_exprs()

static void set_joinrel_partition_key_exprs ( RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
JoinType  jointype 
)
static

Definition at line 1912 of file relnode.c.

1915 {
1916  PartitionScheme part_scheme = joinrel->part_scheme;
1917  int partnatts = part_scheme->partnatts;
1918 
1919  joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts);
1920  joinrel->nullable_partexprs =
1921  (List **) palloc0(sizeof(List *) * partnatts);
1922 
1923  /*
1924  * The joinrel's partition expressions are the same as those of the input
1925  * rels, but we must properly classify them as nullable or not in the
1926  * joinrel's output. (Also, we add some more partition expressions if
1927  * it's a FULL JOIN.)
1928  */
1929  for (int cnt = 0; cnt < partnatts; cnt++)
1930  {
1931  /* mark these const to enforce that we copy them properly */
1932  const List *outer_expr = outer_rel->partexprs[cnt];
1933  const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
1934  const List *inner_expr = inner_rel->partexprs[cnt];
1935  const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
1936  List *partexpr = NIL;
1937  List *nullable_partexpr = NIL;
1938  ListCell *lc;
1939 
1940  switch (jointype)
1941  {
1942  /*
1943  * A join relation resulting from an INNER join may be
1944  * regarded as partitioned by either of the inner and outer
1945  * relation keys. For example, A INNER JOIN B ON A.a = B.b
1946  * can be regarded as partitioned on either A.a or B.b. So we
1947  * add both keys to the joinrel's partexpr lists. However,
1948  * anything that was already nullable still has to be treated
1949  * as nullable.
1950  */
1951  case JOIN_INNER:
1952  partexpr = list_concat_copy(outer_expr, inner_expr);
1953  nullable_partexpr = list_concat_copy(outer_null_expr,
1954  inner_null_expr);
1955  break;
1956 
1957  /*
1958  * A join relation resulting from a SEMI or ANTI join may be
1959  * regarded as partitioned by the outer relation keys. The
1960  * inner relation's keys are no longer interesting; since they
1961  * aren't visible in the join output, nothing could join to
1962  * them.
1963  */
1964  case JOIN_SEMI:
1965  case JOIN_ANTI:
1966  partexpr = list_copy(outer_expr);
1967  nullable_partexpr = list_copy(outer_null_expr);
1968  break;
1969 
1970  /*
1971  * A join relation resulting from a LEFT OUTER JOIN likewise
1972  * may be regarded as partitioned on the (non-nullable) outer
1973  * relation keys. The inner (nullable) relation keys are okay
1974  * as partition keys for further joins as long as they involve
1975  * strict join operators.
1976  */
1977  case JOIN_LEFT:
1978  partexpr = list_copy(outer_expr);
1979  nullable_partexpr = list_concat_copy(inner_expr,
1980  outer_null_expr);
1981  nullable_partexpr = list_concat(nullable_partexpr,
1982  inner_null_expr);
1983  break;
1984 
1985  /*
1986  * For FULL OUTER JOINs, both relations are nullable, so the
1987  * resulting join relation may be regarded as partitioned on
1988  * either of inner and outer relation keys, but only for joins
1989  * that involve strict join operators.
1990  */
1991  case JOIN_FULL:
1992  nullable_partexpr = list_concat_copy(outer_expr,
1993  inner_expr);
1994  nullable_partexpr = list_concat(nullable_partexpr,
1995  outer_null_expr);
1996  nullable_partexpr = list_concat(nullable_partexpr,
1997  inner_null_expr);
1998 
1999  /*
2000  * Also add CoalesceExprs corresponding to each possible
2001  * full-join output variable (that is, left side coalesced to
2002  * right side), so that we can match equijoin expressions
2003  * using those variables. We really only need these for
2004  * columns merged by JOIN USING, and only with the pairs of
2005  * input items that correspond to the data structures that
2006  * parse analysis would build for such variables. But it's
2007  * hard to tell which those are, so just make all the pairs.
2008  * Extra items in the nullable_partexprs list won't cause big
2009  * problems. (It's possible that such items will get matched
2010  * to user-written COALESCEs, but it should still be valid to
2011  * partition on those, since they're going to be either the
2012  * partition column or NULL; it's the same argument as for
2013  * partitionwise nesting of any outer join.) We assume no
2014  * type coercions are needed to make the coalesce expressions,
2015  * since columns of different types won't have gotten
2016  * classified as the same PartitionScheme.
2017  */
2018  foreach(lc, list_concat_copy(outer_expr, outer_null_expr))
2019  {
2020  Node *larg = (Node *) lfirst(lc);
2021  ListCell *lc2;
2022 
2023  foreach(lc2, list_concat_copy(inner_expr, inner_null_expr))
2024  {
2025  Node *rarg = (Node *) lfirst(lc2);
2027 
2028  c->coalescetype = exprType(larg);
2029  c->coalescecollid = exprCollation(larg);
2030  c->args = list_make2(larg, rarg);
2031  c->location = -1;
2032  nullable_partexpr = lappend(nullable_partexpr, c);
2033  }
2034  }
2035  break;
2036 
2037  default:
2038  elog(ERROR, "unrecognized join type: %d", (int) jointype);
2039  }
2040 
2041  joinrel->partexprs[cnt] = partexpr;
2042  joinrel->nullable_partexprs[cnt] = nullable_partexpr;
2043  }
2044 }
List * list_copy(const List *oldlist)
Definition: list.c:1572
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:597
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:764
@ JOIN_SEMI
Definition: nodes.h:307
@ JOIN_FULL
Definition: nodes.h:295
@ JOIN_INNER
Definition: nodes.h:293
@ JOIN_LEFT
Definition: nodes.h:294
@ JOIN_ANTI
Definition: nodes.h:308
#define list_make2(x1, x2)
Definition: pg_list.h:212
char * c

References elog(), ERROR, exprCollation(), exprType(), JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, lappend(), lfirst, list_concat(), list_concat_copy(), list_copy(), list_make2, makeNode, NIL, palloc0(), and PartitionSchemeData::partnatts.

Referenced by build_joinrel_partition_info().

◆ setup_simple_rel_arrays()

void setup_simple_rel_arrays ( PlannerInfo root)

Definition at line 84 of file relnode.c.

85 {
86  int size;
87  Index rti;
88  ListCell *lc;
89 
90  /* Arrays are accessed using RT indexes (1..N) */
91  size = list_length(root->parse->rtable) + 1;
92  root->simple_rel_array_size = size;
93 
94  /*
95  * simple_rel_array is initialized to all NULLs, since no RelOptInfos
96  * exist yet. It'll be filled by later calls to build_simple_rel().
97  */
98  root->simple_rel_array = (RelOptInfo **)
99  palloc0(size * sizeof(RelOptInfo *));
100 
101  /* simple_rte_array is an array equivalent of the rtable list */
102  root->simple_rte_array = (RangeTblEntry **)
103  palloc0(size * sizeof(RangeTblEntry *));
104  rti = 1;
105  foreach(lc, root->parse->rtable)
106  {
107  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
108 
109  root->simple_rte_array[rti++] = rte;
110  }
111 
112  /* append_rel_array is not needed if there are no AppendRelInfos */
113  if (root->append_rel_list == NIL)
114  {
115  root->append_rel_array = NULL;
116  return;
117  }
118 
119  root->append_rel_array = (AppendRelInfo **)
120  palloc0(size * sizeof(AppendRelInfo *));
121 
122  /*
123  * append_rel_array is filled with any already-existing AppendRelInfos,
124  * which currently could only come from UNION ALL flattening. We might
125  * add more later during inheritance expansion, but it's the
126  * responsibility of the expansion code to update the array properly.
127  */
128  foreach(lc, root->append_rel_list)
129  {
130  AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
131  int child_relid = appinfo->child_relid;
132 
133  /* Sanity check */
134  Assert(child_relid < size);
135 
136  if (root->append_rel_array[child_relid])
137  elog(ERROR, "child relation already exists");
138 
139  root->append_rel_array[child_relid] = appinfo;
140  }
141 }
Index child_relid
Definition: pathnodes.h:2758
List * append_rel_list
Definition: pathnodes.h:355
List * rtable
Definition: parsenodes.h:155

References PlannerInfo::append_rel_list, Assert(), AppendRelInfo::child_relid, elog(), ERROR, lfirst, lfirst_node, list_length(), NIL, palloc0(), PlannerInfo::parse, Query::rtable, and PlannerInfo::simple_rel_array_size.

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

◆ subbuild_joinrel_joinlist()

static List * subbuild_joinrel_joinlist ( RelOptInfo joinrel,
List joininfo_list,
List new_joininfo 
)
static

Definition at line 1178 of file relnode.c.

1181 {
1182  ListCell *l;
1183 
1184  /* Expected to be called only for join between parent relations. */
1185  Assert(joinrel->reloptkind == RELOPT_JOINREL);
1186 
1187  foreach(l, joininfo_list)
1188  {
1189  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1190 
1191  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1192  {
1193  /*
1194  * This clause becomes a restriction clause for the joinrel, since
1195  * it refers to no outside rels. So we can ignore it in this
1196  * routine.
1197  */
1198  }
1199  else
1200  {
1201  /*
1202  * This clause is still a join clause at this level, so add it to
1203  * the new joininfo list, being careful to eliminate duplicates.
1204  * (Since RestrictInfo nodes in different joinlists will have been
1205  * multiply-linked rather than copied, pointer equality should be
1206  * a sufficient test.)
1207  */
1208  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1209  }
1210  }
1211 
1212  return new_joininfo;
1213 }
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1355
Relids required_relids
Definition: pathnodes.h:2462

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

Referenced by build_joinrel_joinlist().

◆ subbuild_joinrel_restrictlist()

static List * subbuild_joinrel_restrictlist ( RelOptInfo joinrel,
List joininfo_list,
List new_restrictlist 
)
static

Definition at line 1144 of file relnode.c.

1147 {
1148  ListCell *l;
1149 
1150  foreach(l, joininfo_list)
1151  {
1152  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1153 
1154  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1155  {
1156  /*
1157  * This clause becomes a restriction clause for the joinrel, since
1158  * it refers to no outside rels. Add it to the list, being
1159  * careful to eliminate duplicates. (Since RestrictInfo nodes in
1160  * different joinlists will have been multiply-linked rather than
1161  * copied, pointer equality should be a sufficient test.)
1162  */
1163  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1164  }
1165  else
1166  {
1167  /*
1168  * This clause is still a join clause at this level, so we ignore
1169  * it in this routine.
1170  */
1171  }
1172  }
1173 
1174  return new_restrictlist;
1175 }

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

Referenced by build_joinrel_restrictlist().