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 "rewrite/rewriteManip.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, SpecialJoinInfo *sjinfo, bool can_null)
 
static Listbuild_joinrel_restrictlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
 
static void build_joinrel_joinlist (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static Listsubbuild_joinrel_restrictlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, Relids both_input_relids, 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 (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
 
static bool have_partkey_equi_join (PlannerInfo *root, 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)
 
RelOptInfofind_base_rel_ignore_join (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)
 
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)
 
Bitmapsetget_param_path_clause_serials (Path *path)
 

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

607 {
608  /* GEQO requires us to append the new joinrel to the end of the list! */
609  root->join_rel_list = lappend(root->join_rel_list, joinrel);
610 
611  /* store it into the auxiliary hashtable if there is one. */
612  if (root->join_rel_hash)
613  {
614  JoinHashEntry *hentry;
615  bool found;
616 
617  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
618  &(joinrel->relids),
619  HASH_ENTER,
620  &found);
621  Assert(!found);
622  hentry->join_rel = joinrel;
623  }
624 }
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:40
List * join_rel_list
Definition: pathnodes.h:280
Relids relids
Definition: pathnodes.h:862

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 
)

Definition at line 857 of file relnode.c.

860 {
861  RelOptInfo *joinrel = makeNode(RelOptInfo);
862  AppendRelInfo **appinfos;
863  int nappinfos;
864 
865  /* Only joins between "other" relations land here. */
866  Assert(IS_OTHER_REL(outer_rel) && IS_OTHER_REL(inner_rel));
867 
868  /* The parent joinrel should have consider_partitionwise_join set. */
869  Assert(parent_joinrel->consider_partitionwise_join);
870 
871  joinrel->reloptkind = RELOPT_OTHER_JOINREL;
872  joinrel->relids = bms_union(outer_rel->relids, inner_rel->relids);
873  if (sjinfo->ojrelid != 0)
874  joinrel->relids = bms_add_member(joinrel->relids, sjinfo->ojrelid);
875  joinrel->rows = 0;
876  /* cheap startup cost is interesting iff not all tuples to be retrieved */
877  joinrel->consider_startup = (root->tuple_fraction > 0);
878  joinrel->consider_param_startup = false;
879  joinrel->consider_parallel = false;
880  joinrel->reltarget = create_empty_pathtarget();
881  joinrel->pathlist = NIL;
882  joinrel->ppilist = NIL;
883  joinrel->partial_pathlist = NIL;
884  joinrel->cheapest_startup_path = NULL;
885  joinrel->cheapest_total_path = NULL;
886  joinrel->cheapest_unique_path = NULL;
888  joinrel->direct_lateral_relids = NULL;
889  joinrel->lateral_relids = NULL;
890  joinrel->relid = 0; /* indicates not a baserel */
891  joinrel->rtekind = RTE_JOIN;
892  joinrel->min_attr = 0;
893  joinrel->max_attr = 0;
894  joinrel->attr_needed = NULL;
895  joinrel->attr_widths = NULL;
896  joinrel->nulling_relids = NULL;
897  joinrel->lateral_vars = NIL;
898  joinrel->lateral_referencers = NULL;
899  joinrel->indexlist = NIL;
900  joinrel->pages = 0;
901  joinrel->tuples = 0;
902  joinrel->allvisfrac = 0;
903  joinrel->eclass_indexes = NULL;
904  joinrel->subroot = NULL;
905  joinrel->subplan_params = NIL;
906  joinrel->amflags = 0;
907  joinrel->serverid = InvalidOid;
908  joinrel->userid = InvalidOid;
909  joinrel->useridiscurrent = false;
910  joinrel->fdwroutine = NULL;
911  joinrel->fdw_private = NULL;
912  joinrel->baserestrictinfo = NIL;
913  joinrel->baserestrictcost.startup = 0;
914  joinrel->baserestrictcost.per_tuple = 0;
915  joinrel->joininfo = NIL;
916  joinrel->has_eclass_joins = false;
917  joinrel->consider_partitionwise_join = false; /* might get changed later */
918  joinrel->parent = parent_joinrel;
919  joinrel->top_parent = parent_joinrel->top_parent ? parent_joinrel->top_parent : parent_joinrel;
920  joinrel->top_parent_relids = joinrel->top_parent->relids;
921  joinrel->part_scheme = NULL;
922  joinrel->nparts = -1;
923  joinrel->boundinfo = NULL;
924  joinrel->partbounds_merged = false;
925  joinrel->partition_qual = NIL;
926  joinrel->part_rels = NULL;
927  joinrel->live_parts = NULL;
928  joinrel->all_partrels = NULL;
929  joinrel->partexprs = NULL;
930  joinrel->nullable_partexprs = NULL;
931 
932  /* Compute information relevant to foreign relations. */
933  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
934 
935  /* Compute information needed for mapping Vars to the child rel */
936  appinfos = find_appinfos_by_relids(root, joinrel->relids, &nappinfos);
937 
938  /* Set up reltarget struct */
939  build_child_join_reltarget(root, parent_joinrel, joinrel,
940  nappinfos, appinfos);
941 
942  /* Construct joininfo list. */
943  joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
944  (Node *) parent_joinrel->joininfo,
945  nappinfos,
946  appinfos);
947 
948  /*
949  * Lateral relids referred in child join will be same as that referred in
950  * the parent relation.
951  */
952  joinrel->direct_lateral_relids = (Relids) bms_copy(parent_joinrel->direct_lateral_relids);
953  joinrel->lateral_relids = (Relids) bms_copy(parent_joinrel->lateral_relids);
954 
955  /*
956  * If the parent joinrel has pending equivalence classes, so does the
957  * child.
958  */
959  joinrel->has_eclass_joins = parent_joinrel->has_eclass_joins;
960 
961  /* Is the join between partitions itself partitioned? */
962  build_joinrel_partition_info(root, joinrel, outer_rel, inner_rel, sjinfo,
963  restrictlist);
964 
965  /* Child joinrel is parallel safe if parent is parallel safe. */
966  joinrel->consider_parallel = parent_joinrel->consider_parallel;
967 
968  /* Set estimates of the child-joinrel's size. */
969  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
970  sjinfo, restrictlist);
971 
972  /* We build the join only once. */
973  Assert(!find_join_rel(root, joinrel->relids));
974 
975  /* Add the relation to the PlannerInfo. */
976  add_join_rel(root, joinrel);
977 
978  /*
979  * We might need EquivalenceClass members corresponding to the child join,
980  * so that we can represent sort pathkeys for it. As with children of
981  * baserels, we shouldn't need this unless there are relevant eclass joins
982  * (implying that a merge join might be possible) or pathkeys to sort by.
983  */
984  if (joinrel->has_eclass_joins || has_useful_pathkeys(root, parent_joinrel))
986  nappinfos, appinfos,
987  parent_joinrel, joinrel);
988 
989  pfree(appinfos);
990 
991  return joinrel;
992 }
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:732
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:195
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:755
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:5076
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
Definition: equivclass.c:2727
void pfree(void *pointer)
Definition: mcxt.c:1436
#define makeNode(_type_)
Definition: nodes.h:176
@ RTE_JOIN
Definition: parsenodes.h:1016
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1983
Bitmapset * Relids
Definition: pathnodes.h:30
@ RELOPT_OTHER_JOINREL
Definition: pathnodes.h:821
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:845
#define NIL
Definition: pg_list.h:68
#define InvalidOid
Definition: postgres_ext.h:36
static void build_joinrel_partition_info(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: relnode.c:1946
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:506
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:2354
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:568
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:606
Definition: pg_list.h:54
Definition: nodes.h:129
Selectivity tuple_fraction
Definition: pathnodes.h:481
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
List * baserestrictinfo
Definition: pathnodes.h:970
bool consider_param_startup
Definition: pathnodes.h:876
List * subplan_params
Definition: pathnodes.h:939
List * ppilist
Definition: pathnodes.h:890
bool useridiscurrent
Definition: pathnodes.h:953
uint32 amflags
Definition: pathnodes.h:943
List * joininfo
Definition: pathnodes.h:976
List * partition_qual
Definition: pathnodes.h:1012
struct PathTarget * reltarget
Definition: pathnodes.h:884
Index relid
Definition: pathnodes.h:909
List * lateral_vars
Definition: pathnodes.h:925
Cardinality tuples
Definition: pathnodes.h:934
bool consider_parallel
Definition: pathnodes.h:878
Relids top_parent_relids
Definition: pathnodes.h:994
bool partbounds_merged
Definition: pathnodes.h:1010
BlockNumber pages
Definition: pathnodes.h:933
Relids lateral_relids
Definition: pathnodes.h:904
List * cheapest_parameterized_paths
Definition: pathnodes.h:895
List * pathlist
Definition: pathnodes.h:889
RelOptKind reloptkind
Definition: pathnodes.h:856
List * indexlist
Definition: pathnodes.h:929
struct Path * cheapest_unique_path
Definition: pathnodes.h:894
Relids lateral_referencers
Definition: pathnodes.h:927
struct Path * cheapest_startup_path
Definition: pathnodes.h:892
QualCost baserestrictcost
Definition: pathnodes.h:972
struct Path * cheapest_total_path
Definition: pathnodes.h:893
Oid userid
Definition: pathnodes.h:951
Bitmapset * eclass_indexes
Definition: pathnodes.h:937
Relids all_partrels
Definition: pathnodes.h:1026
Relids direct_lateral_relids
Definition: pathnodes.h:902
bool has_eclass_joins
Definition: pathnodes.h:978
Oid serverid
Definition: pathnodes.h:949
bool consider_startup
Definition: pathnodes.h:874
Bitmapset * live_parts
Definition: pathnodes.h:1024
bool consider_partitionwise_join
Definition: pathnodes.h:984
List * partial_pathlist
Definition: pathnodes.h:891
PlannerInfo * subroot
Definition: pathnodes.h:938
AttrNumber max_attr
Definition: pathnodes.h:917
Relids nulling_relids
Definition: pathnodes.h:923
double allvisfrac
Definition: pathnodes.h:935
Cardinality rows
Definition: pathnodes.h:868
AttrNumber min_attr
Definition: pathnodes.h:915
RTEKind rtekind
Definition: pathnodes.h:913
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_add_member(), 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::nulling_relids, SpecialJoinInfo::ojrelid, 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 2354 of file relnode.c.

2359 {
2360  /* Build the targetlist */
2361  childrel->reltarget->exprs = (List *)
2363  (Node *) parentrel->reltarget->exprs,
2364  nappinfos, appinfos);
2365 
2366  /* Set the cost and width fields */
2367  childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
2368  childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
2369  childrel->reltarget->width = parentrel->reltarget->width;
2370 }
List * exprs
Definition: pathnodes.h:1507
QualCost cost
Definition: pathnodes.h:1513

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

649 {
650  RelOptInfo *joinrel;
651  List *restrictlist;
652 
653  /* This function should be used only for join between parents. */
654  Assert(!IS_OTHER_REL(outer_rel) && !IS_OTHER_REL(inner_rel));
655 
656  /*
657  * See if we already have a joinrel for this set of base rels.
658  */
659  joinrel = find_join_rel(root, joinrelids);
660 
661  if (joinrel)
662  {
663  /*
664  * Yes, so we only need to figure the restrictlist for this particular
665  * pair of component relations.
666  */
667  if (restrictlist_ptr)
668  *restrictlist_ptr = build_joinrel_restrictlist(root,
669  joinrel,
670  outer_rel,
671  inner_rel,
672  sjinfo);
673  return joinrel;
674  }
675 
676  /*
677  * Nope, so make one.
678  */
679  joinrel = makeNode(RelOptInfo);
680  joinrel->reloptkind = RELOPT_JOINREL;
681  joinrel->relids = bms_copy(joinrelids);
682  joinrel->rows = 0;
683  /* cheap startup cost is interesting iff not all tuples to be retrieved */
684  joinrel->consider_startup = (root->tuple_fraction > 0);
685  joinrel->consider_param_startup = false;
686  joinrel->consider_parallel = false;
687  joinrel->reltarget = create_empty_pathtarget();
688  joinrel->pathlist = NIL;
689  joinrel->ppilist = NIL;
690  joinrel->partial_pathlist = NIL;
691  joinrel->cheapest_startup_path = NULL;
692  joinrel->cheapest_total_path = NULL;
693  joinrel->cheapest_unique_path = NULL;
695  /* init direct_lateral_relids from children; we'll finish it up below */
696  joinrel->direct_lateral_relids =
697  bms_union(outer_rel->direct_lateral_relids,
698  inner_rel->direct_lateral_relids);
699  joinrel->lateral_relids = min_join_parameterization(root, joinrel->relids,
700  outer_rel, inner_rel);
701  joinrel->relid = 0; /* indicates not a baserel */
702  joinrel->rtekind = RTE_JOIN;
703  joinrel->min_attr = 0;
704  joinrel->max_attr = 0;
705  joinrel->attr_needed = NULL;
706  joinrel->attr_widths = NULL;
707  joinrel->nulling_relids = NULL;
708  joinrel->lateral_vars = NIL;
709  joinrel->lateral_referencers = NULL;
710  joinrel->indexlist = NIL;
711  joinrel->statlist = NIL;
712  joinrel->pages = 0;
713  joinrel->tuples = 0;
714  joinrel->allvisfrac = 0;
715  joinrel->eclass_indexes = NULL;
716  joinrel->subroot = NULL;
717  joinrel->subplan_params = NIL;
718  joinrel->rel_parallel_workers = -1;
719  joinrel->amflags = 0;
720  joinrel->serverid = InvalidOid;
721  joinrel->userid = InvalidOid;
722  joinrel->useridiscurrent = false;
723  joinrel->fdwroutine = NULL;
724  joinrel->fdw_private = NULL;
725  joinrel->unique_for_rels = NIL;
726  joinrel->non_unique_for_rels = NIL;
727  joinrel->baserestrictinfo = NIL;
728  joinrel->baserestrictcost.startup = 0;
729  joinrel->baserestrictcost.per_tuple = 0;
730  joinrel->baserestrict_min_security = UINT_MAX;
731  joinrel->joininfo = NIL;
732  joinrel->has_eclass_joins = false;
733  joinrel->consider_partitionwise_join = false; /* might get changed later */
734  joinrel->parent = NULL;
735  joinrel->top_parent = NULL;
736  joinrel->top_parent_relids = NULL;
737  joinrel->part_scheme = NULL;
738  joinrel->nparts = -1;
739  joinrel->boundinfo = NULL;
740  joinrel->partbounds_merged = false;
741  joinrel->partition_qual = NIL;
742  joinrel->part_rels = NULL;
743  joinrel->live_parts = NULL;
744  joinrel->all_partrels = NULL;
745  joinrel->partexprs = NULL;
746  joinrel->nullable_partexprs = NULL;
747 
748  /* Compute information relevant to the foreign relations. */
749  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
750 
751  /*
752  * Fill the joinrel's tlist with just the Vars and PHVs that need to be
753  * output from this join (ie, are needed for higher joinclauses or final
754  * output).
755  *
756  * NOTE: the tlist order for a join rel will depend on which pair of outer
757  * and inner rels we first try to build it from. But the contents should
758  * be the same regardless.
759  */
760  build_joinrel_tlist(root, joinrel, outer_rel, sjinfo,
761  (sjinfo->jointype == JOIN_FULL));
762  build_joinrel_tlist(root, joinrel, inner_rel, sjinfo,
763  (sjinfo->jointype != JOIN_INNER));
764  add_placeholders_to_joinrel(root, joinrel, outer_rel, inner_rel, sjinfo);
765 
766  /*
767  * add_placeholders_to_joinrel also took care of adding the ph_lateral
768  * sets of any PlaceHolderVars computed here to direct_lateral_relids, so
769  * now we can finish computing that. This is much like the computation of
770  * the transitively-closed lateral_relids in min_join_parameterization,
771  * except that here we *do* have to consider the added PHVs.
772  */
773  joinrel->direct_lateral_relids =
774  bms_del_members(joinrel->direct_lateral_relids, joinrel->relids);
775 
776  /*
777  * Construct restrict and join clause lists for the new joinrel. (The
778  * caller might or might not need the restrictlist, but I need it anyway
779  * for set_joinrel_size_estimates().)
780  */
781  restrictlist = build_joinrel_restrictlist(root, joinrel,
782  outer_rel, inner_rel,
783  sjinfo);
784  if (restrictlist_ptr)
785  *restrictlist_ptr = restrictlist;
786  build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
787 
788  /*
789  * This is also the right place to check whether the joinrel has any
790  * pending EquivalenceClass joins.
791  */
792  joinrel->has_eclass_joins = has_relevant_eclass_joinclause(root, joinrel);
793 
794  /* Store the partition information. */
795  build_joinrel_partition_info(root, joinrel, outer_rel, inner_rel, sjinfo,
796  restrictlist);
797 
798  /*
799  * Set estimates of the joinrel's size.
800  */
801  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
802  sjinfo, restrictlist);
803 
804  /*
805  * Set the consider_parallel flag if this joinrel could potentially be
806  * scanned within a parallel worker. If this flag is false for either
807  * inner_rel or outer_rel, then it must be false for the joinrel also.
808  * Even if both are true, there might be parallel-restricted expressions
809  * in the targetlist or quals.
810  *
811  * Note that if there are more than two rels in this relation, they could
812  * be divided between inner_rel and outer_rel in any arbitrary way. We
813  * assume this doesn't matter, because we should hit all the same baserels
814  * and joinclauses while building up to this joinrel no matter which we
815  * take; therefore, we should make the same decision here however we get
816  * here.
817  */
818  if (inner_rel->consider_parallel && outer_rel->consider_parallel &&
819  is_parallel_safe(root, (Node *) restrictlist) &&
820  is_parallel_safe(root, (Node *) joinrel->reltarget->exprs))
821  joinrel->consider_parallel = true;
822 
823  /* Add the joinrel to the PlannerInfo. */
824  add_join_rel(root, joinrel);
825 
826  /*
827  * Also, if dynamic-programming join search is active, add the new joinrel
828  * to the appropriate sublist. Note: you might think the Assert on number
829  * of members should be for equality, but some of the level 1 rels might
830  * have been joinrels already, so we can only assert <=.
831  */
832  if (root->join_rel_level)
833  {
834  Assert(root->join_cur_level > 0);
835  Assert(root->join_cur_level <= bms_num_members(joinrel->relids));
836  root->join_rel_level[root->join_cur_level] =
837  lappend(root->join_rel_level[root->join_cur_level], joinrel);
838  }
839 
840  return joinrel;
841 }
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:665
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:960
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:634
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:3076
@ JOIN_FULL
Definition: nodes.h:306
@ JOIN_INNER
Definition: nodes.h:304
@ RELOPT_JOINREL
Definition: pathnodes.h:819
void add_placeholders_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: placeholder.c:373
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1003
static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, SpecialJoinInfo *sjinfo, bool can_null)
Definition: relnode.c:1071
static List * build_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: relnode.c:1231
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1268
int join_cur_level
Definition: pathnodes.h:296
List * statlist
Definition: pathnodes.h:931
List * unique_for_rels
Definition: pathnodes.h:962
List * non_unique_for_rels
Definition: pathnodes.h:964
int rel_parallel_workers
Definition: pathnodes.h:941
Index baserestrict_min_security
Definition: pathnodes.h:974
JoinType jointype
Definition: pathnodes.h:2833

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_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, JOIN_FULL, JOIN_INNER, 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::nulling_relids, 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 465 of file relnode.c.

466 {
467  HTAB *hashtab;
468  HASHCTL hash_ctl;
469  ListCell *l;
470 
471  /* Create the hash table */
472  hash_ctl.keysize = sizeof(Relids);
473  hash_ctl.entrysize = sizeof(JoinHashEntry);
474  hash_ctl.hash = bitmap_hash;
475  hash_ctl.match = bitmap_match;
476  hash_ctl.hcxt = CurrentMemoryContext;
477  hashtab = hash_create("JoinRelHashTable",
478  256L,
479  &hash_ctl,
481 
482  /* Insert all the already-existing joinrels */
483  foreach(l, root->join_rel_list)
484  {
485  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
486  JoinHashEntry *hentry;
487  bool found;
488 
489  hentry = (JoinHashEntry *) hash_search(hashtab,
490  &(rel->relids),
491  HASH_ENTER,
492  &found);
493  Assert(!found);
494  hentry->join_rel = rel;
495  }
496 
497  root->join_rel_hash = hashtab;
498 }
uint32 bitmap_hash(const void *key, Size keysize)
Definition: bitmapset.c:1173
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: bitmapset.c:1183
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:135
#define lfirst(lc)
Definition: pg_list.h:172
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 1268 of file relnode.c.

1271 {
1272  List *result;
1273 
1274  /*
1275  * Collect all the clauses that syntactically belong above this level,
1276  * eliminating any duplicates (important since we will see many of the
1277  * same clauses arriving from both input relations).
1278  */
1279  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1280  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1281 
1282  joinrel->joininfo = result;
1283 }
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:1359

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

Referenced by build_join_rel().

◆ build_joinrel_partition_info()

static void build_joinrel_partition_info ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
SpecialJoinInfo sjinfo,
List restrictlist 
)
static

Definition at line 1946 of file relnode.c.

1950 {
1951  PartitionScheme part_scheme;
1952 
1953  /* Nothing to do if partitionwise join technique is disabled. */
1955  {
1956  Assert(!IS_PARTITIONED_REL(joinrel));
1957  return;
1958  }
1959 
1960  /*
1961  * We can only consider this join as an input to further partitionwise
1962  * joins if (a) the input relations are partitioned and have
1963  * consider_partitionwise_join=true, (b) the partition schemes match, and
1964  * (c) we can identify an equi-join between the partition keys. Note that
1965  * if it were possible for have_partkey_equi_join to return different
1966  * answers for the same joinrel depending on which join ordering we try
1967  * first, this logic would break. That shouldn't happen, though, because
1968  * of the way the query planner deduces implied equalities and reorders
1969  * the joins. Please see optimizer/README for details.
1970  */
1971  if (outer_rel->part_scheme == NULL || inner_rel->part_scheme == NULL ||
1972  !outer_rel->consider_partitionwise_join ||
1973  !inner_rel->consider_partitionwise_join ||
1974  outer_rel->part_scheme != inner_rel->part_scheme ||
1975  !have_partkey_equi_join(root, joinrel, outer_rel, inner_rel,
1976  sjinfo->jointype, restrictlist))
1977  {
1978  Assert(!IS_PARTITIONED_REL(joinrel));
1979  return;
1980  }
1981 
1982  part_scheme = outer_rel->part_scheme;
1983 
1984  /*
1985  * This function will be called only once for each joinrel, hence it
1986  * should not have partitioning fields filled yet.
1987  */
1988  Assert(!joinrel->part_scheme && !joinrel->partexprs &&
1989  !joinrel->nullable_partexprs && !joinrel->part_rels &&
1990  !joinrel->boundinfo);
1991 
1992  /*
1993  * If the join relation is partitioned, it uses the same partitioning
1994  * scheme as the joining relations.
1995  *
1996  * Note: we calculate the partition bounds, number of partitions, and
1997  * child-join relations of the join relation in try_partitionwise_join().
1998  */
1999  joinrel->part_scheme = part_scheme;
2000  set_joinrel_partition_key_exprs(joinrel, outer_rel, inner_rel,
2001  sjinfo->jointype);
2002 
2003  /*
2004  * Set the consider_partitionwise_join flag.
2005  */
2006  Assert(outer_rel->consider_partitionwise_join);
2007  Assert(inner_rel->consider_partitionwise_join);
2008  joinrel->consider_partitionwise_join = true;
2009 }
bool enable_partitionwise_join
Definition: costsize.c:149
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1047
static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
Definition: relnode.c:2210
static bool have_partkey_equi_join(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition: relnode.c:2019

References Assert(), RelOptInfo::consider_partitionwise_join, enable_partitionwise_join, have_partkey_equi_join(), IS_PARTITIONED_REL, SpecialJoinInfo::jointype, 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,
SpecialJoinInfo sjinfo 
)
static

Definition at line 1231 of file relnode.c.

1236 {
1237  List *result;
1238  Relids both_input_relids;
1239 
1240  both_input_relids = bms_union(outer_rel->relids, inner_rel->relids);
1241 
1242  /*
1243  * Collect all the clauses that syntactically belong at this level,
1244  * eliminating any duplicates (important since we will see many of the
1245  * same clauses arriving from both input relations).
1246  */
1247  result = subbuild_joinrel_restrictlist(root, joinrel, outer_rel,
1248  both_input_relids, NIL);
1249  result = subbuild_joinrel_restrictlist(root, joinrel, inner_rel,
1250  both_input_relids, result);
1251 
1252  /*
1253  * Add on any clauses derived from EquivalenceClasses. These cannot be
1254  * redundant with the clauses in the joininfo lists, so don't bother
1255  * checking.
1256  */
1257  result = list_concat(result,
1259  joinrel->relids,
1260  outer_rel->relids,
1261  inner_rel,
1262  sjinfo->ojrelid));
1263 
1264  return result;
1265 }
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, Index ojrelid)
Definition: equivclass.c:1377
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
static List * subbuild_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, Relids both_input_relids, List *new_restrictlist)
Definition: relnode.c:1286

References bms_union(), generate_join_implied_equalities(), list_concat(), NIL, SpecialJoinInfo::ojrelid, 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,
SpecialJoinInfo sjinfo,
bool  can_null 
)
static

Definition at line 1071 of file relnode.c.

1075 {
1076  Relids relids = joinrel->relids;
1077  ListCell *vars;
1078 
1079  foreach(vars, input_rel->reltarget->exprs)
1080  {
1081  Var *var = (Var *) lfirst(vars);
1082 
1083  /*
1084  * For a PlaceHolderVar, we have to look up the PlaceHolderInfo.
1085  */
1086  if (IsA(var, PlaceHolderVar))
1087  {
1088  PlaceHolderVar *phv = (PlaceHolderVar *) var;
1089  PlaceHolderInfo *phinfo = find_placeholder_info(root, phv);
1090 
1091  /* Is it still needed above this joinrel? */
1092  if (bms_nonempty_difference(phinfo->ph_needed, relids))
1093  {
1094  /*
1095  * Yup, add it to the output. If this join potentially nulls
1096  * this input, we have to update the PHV's phnullingrels,
1097  * which means making a copy.
1098  */
1099  if (can_null)
1100  {
1101  phv = copyObject(phv);
1102  /* See comments above to understand this logic */
1103  if (sjinfo->ojrelid != 0 &&
1104  (bms_is_subset(phv->phrels, sjinfo->syn_righthand) ||
1105  (sjinfo->jointype == JOIN_FULL &&
1106  bms_is_subset(phv->phrels, sjinfo->syn_lefthand))))
1108  sjinfo->ojrelid);
1109  phv->phnullingrels =
1110  bms_join(phv->phnullingrels,
1112  relids));
1113  }
1114 
1115  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1116  phv);
1117  /* Bubbling up the precomputed result has cost zero */
1118  joinrel->reltarget->width += phinfo->ph_width;
1119  }
1120  continue;
1121  }
1122 
1123  /*
1124  * Otherwise, anything in a baserel or joinrel targetlist ought to be
1125  * a Var. (More general cases can only appear in appendrel child
1126  * rels, which will never be seen here.)
1127  */
1128  if (!IsA(var, Var))
1129  elog(ERROR, "unexpected node type in rel targetlist: %d",
1130  (int) nodeTag(var));
1131 
1132  if (var->varno == ROWID_VAR)
1133  {
1134  /* UPDATE/DELETE/MERGE row identity vars are always needed */
1135  RowIdentityVarInfo *ridinfo = (RowIdentityVarInfo *)
1136  list_nth(root->row_identity_vars, var->varattno - 1);
1137 
1138  /* Update reltarget width estimate from RowIdentityVarInfo */
1139  joinrel->reltarget->width += ridinfo->rowidwidth;
1140  }
1141  else
1142  {
1143  RelOptInfo *baserel;
1144  int ndx;
1145 
1146  /* Get the Var's original base rel */
1147  baserel = find_base_rel(root, var->varno);
1148 
1149  /* Is it still needed above this joinrel? */
1150  ndx = var->varattno - baserel->min_attr;
1151  if (!bms_nonempty_difference(baserel->attr_needed[ndx], relids))
1152  continue; /* nope, skip it */
1153 
1154  /* Update reltarget width estimate from baserel's attr_widths */
1155  joinrel->reltarget->width += baserel->attr_widths[ndx];
1156  }
1157 
1158  /*
1159  * Add the Var to the output. If this join potentially nulls this
1160  * input, we have to update the Var's varnullingrels, which means
1161  * making a copy. But note that we don't ever add nullingrel bits to
1162  * row identity Vars (cf. comments in setrefs.c).
1163  */
1164  if (can_null && var->varno != ROWID_VAR)
1165  {
1166  var = copyObject(var);
1167  /* See comments above to understand this logic */
1168  if (sjinfo->ojrelid != 0 &&
1169  (bms_is_member(var->varno, sjinfo->syn_righthand) ||
1170  (sjinfo->jointype == JOIN_FULL &&
1171  bms_is_member(var->varno, sjinfo->syn_lefthand))))
1172  var->varnullingrels = bms_add_member(var->varnullingrels,
1173  sjinfo->ojrelid);
1174  var->varnullingrels =
1175  bms_join(var->varnullingrels,
1177  relids));
1178  }
1179 
1180  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1181  var);
1182 
1183  /* Vars have cost zero, so no need to adjust reltarget->cost */
1184  }
1185 }
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:987
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:332
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:444
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:260
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:564
#define ERROR
Definition: elog.h:39
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define copyObject(obj)
Definition: nodes.h:244
#define nodeTag(nodeptr)
Definition: nodes.h:133
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition: placeholder.c:83
#define ROWID_VAR
Definition: primnodes.h:217
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:404
Relids ph_needed
Definition: pathnodes.h:3028
Relids phnullingrels
Definition: pathnodes.h:2734
List * row_identity_vars
Definition: pathnodes.h:368
Relids commute_above_r
Definition: pathnodes.h:2836
Relids syn_lefthand
Definition: pathnodes.h:2831
Relids syn_righthand
Definition: pathnodes.h:2832
Definition: primnodes.h:226
AttrNumber varattno
Definition: primnodes.h:238
int varno
Definition: primnodes.h:233
Definition: regcomp.c:282

References bms_add_member(), bms_intersect(), bms_is_member(), bms_is_subset(), bms_join(), bms_nonempty_difference(), SpecialJoinInfo::commute_above_r, copyObject, elog(), ERROR, PathTarget::exprs, find_base_rel(), find_placeholder_info(), IsA, JOIN_FULL, SpecialJoinInfo::jointype, lappend(), lfirst, list_nth(), RelOptInfo::min_attr, nodeTag, SpecialJoinInfo::ojrelid, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_width, PlaceHolderVar::phnullingrels, RelOptInfo::relids, RelOptInfo::reltarget, PlannerInfo::row_identity_vars, ROWID_VAR, RowIdentityVarInfo::rowidwidth, SpecialJoinInfo::syn_lefthand, SpecialJoinInfo::syn_righthand, 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 190 of file relnode.c.

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

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::nulling_relids, 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, 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 161 of file relnode.c.

162 {
163  int new_size;
164 
165  Assert(add_size > 0);
166 
167  new_size = root->simple_rel_array_size + add_size;
168 
169  root->simple_rel_array =
170  repalloc0_array(root->simple_rel_array, RelOptInfo *, root->simple_rel_array_size, new_size);
171 
172  root->simple_rte_array =
173  repalloc0_array(root->simple_rte_array, RangeTblEntry *, root->simple_rel_array_size, new_size);
174 
175  if (root->append_rel_array)
176  root->append_rel_array =
177  repalloc0_array(root->append_rel_array, AppendRelInfo *, root->simple_rel_array_size, new_size);
178  else
179  root->append_rel_array =
180  palloc0_array(AppendRelInfo *, new_size);
181 
182  root->simple_rel_array_size = new_size;
183 }
#define palloc0_array(type, count)
Definition: fe_memutils.h:65
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:110
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 1411 of file relnode.c.

1412 {
1413  RelOptInfo *upperrel;
1414  ListCell *lc;
1415 
1416  /*
1417  * For the moment, our indexing data structure is just a List for each
1418  * relation kind. If we ever get so many of one kind that this stops
1419  * working well, we can improve it. No code outside this function should
1420  * assume anything about how to find a particular upperrel.
1421  */
1422 
1423  /* If we already made this upperrel for the query, return it */
1424  foreach(lc, root->upper_rels[kind])
1425  {
1426  upperrel = (RelOptInfo *) lfirst(lc);
1427 
1428  if (bms_equal(upperrel->relids, relids))
1429  return upperrel;
1430  }
1431 
1432  upperrel = makeNode(RelOptInfo);
1433  upperrel->reloptkind = RELOPT_UPPER_REL;
1434  upperrel->relids = bms_copy(relids);
1435 
1436  /* cheap startup cost is interesting iff not all tuples to be retrieved */
1437  upperrel->consider_startup = (root->tuple_fraction > 0);
1438  upperrel->consider_param_startup = false;
1439  upperrel->consider_parallel = false; /* might get changed later */
1440  upperrel->reltarget = create_empty_pathtarget();
1441  upperrel->pathlist = NIL;
1442  upperrel->cheapest_startup_path = NULL;
1443  upperrel->cheapest_total_path = NULL;
1444  upperrel->cheapest_unique_path = NULL;
1445  upperrel->cheapest_parameterized_paths = NIL;
1446 
1447  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1448 
1449  return upperrel;
1450 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
@ RELOPT_UPPER_REL
Definition: pathnodes.h:822

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_base_rel_ignore_join()

RelOptInfo* find_base_rel_ignore_join ( PlannerInfo root,
int  relid 
)

Definition at line 432 of file relnode.c.

433 {
434  Assert(relid > 0);
435 
436  if (relid < root->simple_rel_array_size)
437  {
438  RelOptInfo *rel;
439  RangeTblEntry *rte;
440 
441  rel = root->simple_rel_array[relid];
442  if (rel)
443  return rel;
444 
445  /*
446  * We could just return NULL here, but for debugging purposes it seems
447  * best to actually verify that the relid is an outer join and not
448  * something weird.
449  */
450  rte = root->simple_rte_array[relid];
451  if (rte && rte->rtekind == RTE_JOIN && rte->jointype != JOIN_INNER)
452  return NULL;
453  }
454 
455  elog(ERROR, "no relation entry for relid %d", relid);
456 
457  return NULL; /* keep compiler quiet */
458 }
JoinType jointype
Definition: parsenodes.h:1127

References Assert(), elog(), ERROR, JOIN_INNER, RangeTblEntry::jointype, RTE_JOIN, and RangeTblEntry::rtekind.

Referenced by add_join_clause_to_rels(), create_lateral_join_info(), find_appinfos_by_relids(), and remove_join_clause_from_rels().

◆ find_childrel_parents()

Relids find_childrel_parents ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1462 of file relnode.c.

1463 {
1464  Relids result = NULL;
1465 
1467  Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1468 
1469  do
1470  {
1471  AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1472  Index prelid = appinfo->parent_relid;
1473 
1474  result = bms_add_member(result, prelid);
1475 
1476  /* traverse up to the parent rel, loop if it's also a child rel */
1477  rel = find_base_rel(root, prelid);
1478  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1479 
1480  Assert(rel->reloptkind == RELOPT_BASEREL);
1481 
1482  return result;
1483 }
unsigned int Index
Definition: c.h:598
Index parent_relid
Definition: pathnodes.h:2903

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

507 {
508  /*
509  * Switch to using hash lookup when list grows "too long". The threshold
510  * is arbitrary and is known only here.
511  */
512  if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
513  build_join_rel_hash(root);
514 
515  /*
516  * Use either hashtable lookup or linear search, as appropriate.
517  *
518  * Note: the seemingly redundant hashkey variable is used to avoid taking
519  * the address of relids; unless the compiler is exceedingly smart, doing
520  * so would force relids out of a register and thus probably slow down the
521  * list-search case.
522  */
523  if (root->join_rel_hash)
524  {
525  Relids hashkey = relids;
526  JoinHashEntry *hentry;
527 
528  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
529  &hashkey,
530  HASH_FIND,
531  NULL);
532  if (hentry)
533  return hentry->join_rel;
534  }
535  else
536  {
537  ListCell *l;
538 
539  foreach(l, root->join_rel_list)
540  {
541  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
542 
543  if (bms_equal(rel->relids, relids))
544  return rel;
545  }
546  }
547 
548  return NULL;
549 }
@ HASH_FIND
Definition: hsearch.h:113
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:465

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

1831 {
1832  ListCell *lc;
1833 
1834  foreach(lc, rel->ppilist)
1835  {
1836  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1837 
1838  if (bms_equal(ppi->ppi_req_outer, required_outer))
1839  return ppi;
1840  }
1841 
1842  return NULL;
1843 }
Relids ppi_req_outer
Definition: pathnodes.h:1553

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

1798 {
1799  ParamPathInfo *ppi;
1800 
1801  /* If rel has LATERAL refs, every path for it should account for them */
1802  Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1803 
1804  /* Unparameterized paths have no ParamPathInfo */
1805  if (bms_is_empty(required_outer))
1806  return NULL;
1807 
1808  Assert(!bms_overlap(appendrel->relids, required_outer));
1809 
1810  /* If we already have a PPI for this parameterization, just return it */
1811  if ((ppi = find_param_path_info(appendrel, required_outer)))
1812  return ppi;
1813 
1814  /* Else build the ParamPathInfo */
1815  ppi = makeNode(ParamPathInfo);
1816  ppi->ppi_req_outer = required_outer;
1817  ppi->ppi_rows = 0;
1818  ppi->ppi_clauses = NIL;
1819  ppi->ppi_serials = NULL;
1820  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1821 
1822  return ppi;
1823 }
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:511
#define bms_is_empty(a)
Definition: bitmapset.h:105
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1830
Cardinality ppi_rows
Definition: pathnodes.h:1554
List * ppi_clauses
Definition: pathnodes.h:1555
Bitmapset * ppi_serials
Definition: pathnodes.h:1556

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, ParamPathInfo::ppi_serials, 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 1498 of file relnode.c.

1500 {
1501  ParamPathInfo *ppi;
1502  Relids joinrelids;
1503  List *pclauses;
1504  Bitmapset *pserials;
1505  double rows;
1506  ListCell *lc;
1507 
1508  /* If rel has LATERAL refs, every path for it should account for them */
1509  Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1510 
1511  /* Unparameterized paths have no ParamPathInfo */
1512  if (bms_is_empty(required_outer))
1513  return NULL;
1514 
1515  Assert(!bms_overlap(baserel->relids, required_outer));
1516 
1517  /* If we already have a PPI for this parameterization, just return it */
1518  if ((ppi = find_param_path_info(baserel, required_outer)))
1519  return ppi;
1520 
1521  /*
1522  * Identify all joinclauses that are movable to this base rel given this
1523  * parameterization.
1524  */
1525  joinrelids = bms_union(baserel->relids, required_outer);
1526  pclauses = NIL;
1527  foreach(lc, baserel->joininfo)
1528  {
1529  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1530 
1531  if (join_clause_is_movable_into(rinfo,
1532  baserel->relids,
1533  joinrelids))
1534  pclauses = lappend(pclauses, rinfo);
1535  }
1536 
1537  /*
1538  * Add in joinclauses generated by EquivalenceClasses, too. (These
1539  * necessarily satisfy join_clause_is_movable_into.)
1540  */
1541  pclauses = list_concat(pclauses,
1543  joinrelids,
1544  required_outer,
1545  baserel,
1546  0));
1547 
1548  /* Compute set of serial numbers of the enforced clauses */
1549  pserials = NULL;
1550  foreach(lc, pclauses)
1551  {
1552  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1553 
1554  pserials = bms_add_member(pserials, rinfo->rinfo_serial);
1555  }
1556 
1557  /* Estimate the number of rows returned by the parameterized scan */
1558  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1559 
1560  /* And now we can build the ParamPathInfo */
1561  ppi = makeNode(ParamPathInfo);
1562  ppi->ppi_req_outer = required_outer;
1563  ppi->ppi_rows = rows;
1564  ppi->ppi_clauses = pclauses;
1565  ppi->ppi_serials = pserials;
1566  baserel->ppilist = lappend(baserel->ppilist, ppi);
1567 
1568  return ppi;
1569 }
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:5027
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:693
int rinfo_serial
Definition: pathnodes.h:2579

References Assert(), bms_add_member(), 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, ParamPathInfo::ppi_serials, RelOptInfo::ppilist, RelOptInfo::relids, and RestrictInfo::rinfo_serial.

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

1606 {
1607  ParamPathInfo *ppi;
1608  Relids join_and_req;
1609  Relids outer_and_req;
1610  Relids inner_and_req;
1611  List *pclauses;
1612  List *eclauses;
1613  List *dropped_ecs;
1614  double rows;
1615  ListCell *lc;
1616 
1617  /* If rel has LATERAL refs, every path for it should account for them */
1618  Assert(bms_is_subset(joinrel->lateral_relids, required_outer));
1619 
1620  /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1621  if (bms_is_empty(required_outer))
1622  return NULL;
1623 
1624  Assert(!bms_overlap(joinrel->relids, required_outer));
1625 
1626  /*
1627  * Identify all joinclauses that are movable to this join rel given this
1628  * parameterization. These are the clauses that are movable into this
1629  * join, but not movable into either input path. Treat an unparameterized
1630  * input path as not accepting parameterized clauses (because it won't,
1631  * per the shortcut exit above), even though the joinclause movement rules
1632  * might allow the same clauses to be moved into a parameterized path for
1633  * that rel.
1634  */
1635  join_and_req = bms_union(joinrel->relids, required_outer);
1636  if (outer_path->param_info)
1637  outer_and_req = bms_union(outer_path->parent->relids,
1638  PATH_REQ_OUTER(outer_path));
1639  else
1640  outer_and_req = NULL; /* outer path does not accept parameters */
1641  if (inner_path->param_info)
1642  inner_and_req = bms_union(inner_path->parent->relids,
1643  PATH_REQ_OUTER(inner_path));
1644  else
1645  inner_and_req = NULL; /* inner path does not accept parameters */
1646 
1647  pclauses = NIL;
1648  foreach(lc, joinrel->joininfo)
1649  {
1650  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1651 
1652  if (join_clause_is_movable_into(rinfo,
1653  joinrel->relids,
1654  join_and_req) &&
1656  outer_path->parent->relids,
1657  outer_and_req) &&
1659  inner_path->parent->relids,
1660  inner_and_req))
1661  pclauses = lappend(pclauses, rinfo);
1662  }
1663 
1664  /* Consider joinclauses generated by EquivalenceClasses, too */
1665  eclauses = generate_join_implied_equalities(root,
1666  join_and_req,
1667  required_outer,
1668  joinrel,
1669  0);
1670  /* We only want ones that aren't movable to lower levels */
1671  dropped_ecs = NIL;
1672  foreach(lc, eclauses)
1673  {
1674  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1675 
1677  joinrel->relids,
1678  join_and_req));
1679  if (join_clause_is_movable_into(rinfo,
1680  outer_path->parent->relids,
1681  outer_and_req))
1682  continue; /* drop if movable into LHS */
1683  if (join_clause_is_movable_into(rinfo,
1684  inner_path->parent->relids,
1685  inner_and_req))
1686  {
1687  /* drop if movable into RHS, but remember EC for use below */
1688  Assert(rinfo->left_ec == rinfo->right_ec);
1689  dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1690  continue;
1691  }
1692  pclauses = lappend(pclauses, rinfo);
1693  }
1694 
1695  /*
1696  * EquivalenceClasses are harder to deal with than we could wish, because
1697  * of the fact that a given EC can generate different clauses depending on
1698  * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1699  * LHS and RHS of the current join and Z is in required_outer, and further
1700  * suppose that the inner_path is parameterized by both X and Z. The code
1701  * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1702  * and in the latter case will have discarded it as being movable into the
1703  * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1704  * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1705  * not have produced both, and we can't readily tell from here which one
1706  * it did pick. If we add no clause to this join, we'll end up with
1707  * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1708  * constrained to be equal to the other members of the EC. (When we come
1709  * to join Z to this X/Y path, we will certainly drop whichever EC clause
1710  * is generated at that join, so this omission won't get fixed later.)
1711  *
1712  * To handle this, for each EC we discarded such a clause from, try to
1713  * generate a clause connecting the required_outer rels to the join's LHS
1714  * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1715  * the clause can't be moved to the LHS, add it to the current join's
1716  * restriction clauses. (If an EC cannot generate such a clause then it
1717  * has nothing that needs to be enforced here, while if the clause can be
1718  * moved into the LHS then it should have been enforced within that path.)
1719  *
1720  * Note that we don't need similar processing for ECs whose clause was
1721  * considered to be movable into the LHS, because the LHS can't refer to
1722  * the RHS so there is no comparable ambiguity about what it might
1723  * actually be enforcing internally.
1724  */
1725  if (dropped_ecs)
1726  {
1727  Relids real_outer_and_req;
1728 
1729  real_outer_and_req = bms_union(outer_path->parent->relids,
1730  required_outer);
1731  eclauses =
1733  dropped_ecs,
1734  real_outer_and_req,
1735  required_outer,
1736  outer_path->parent);
1737  foreach(lc, eclauses)
1738  {
1739  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1740 
1742  outer_path->parent->relids,
1743  real_outer_and_req));
1744  if (!join_clause_is_movable_into(rinfo,
1745  outer_path->parent->relids,
1746  outer_and_req))
1747  pclauses = lappend(pclauses, rinfo);
1748  }
1749  }
1750 
1751  /*
1752  * Now, attach the identified moved-down clauses to the caller's
1753  * restrict_clauses list. By using list_concat in this order, we leave
1754  * the original list structure of restrict_clauses undamaged.
1755  */
1756  *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1757 
1758  /* If we already have a PPI for this parameterization, just return it */
1759  if ((ppi = find_param_path_info(joinrel, required_outer)))
1760  return ppi;
1761 
1762  /* Estimate the number of rows returned by the parameterized join */
1763  rows = get_parameterized_joinrel_size(root, joinrel,
1764  outer_path,
1765  inner_path,
1766  sjinfo,
1767  *restrict_clauses);
1768 
1769  /*
1770  * And now we can build the ParamPathInfo. No point in saving the
1771  * input-pair-dependent clause list, though.
1772  *
1773  * Note: in GEQO mode, we'll be called in a temporary memory context, but
1774  * the joinrel structure is there too, so no problem.
1775  */
1776  ppi = makeNode(ParamPathInfo);
1777  ppi->ppi_req_outer = required_outer;
1778  ppi->ppi_rows = rows;
1779  ppi->ppi_clauses = NIL;
1780  ppi->ppi_serials = NULL;
1781  joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1782 
1783  return ppi;
1784 }
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:5108
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1475
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1643

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, ParamPathInfo::ppi_serials, RelOptInfo::ppilist, and RelOptInfo::relids.

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

◆ get_param_path_clause_serials()

Bitmapset* get_param_path_clause_serials ( Path path)

Definition at line 1851 of file relnode.c.

1852 {
1853  if (path->param_info == NULL)
1854  return NULL; /* not parameterized */
1855  if (IsA(path, NestPath) ||
1856  IsA(path, MergePath) ||
1857  IsA(path, HashPath))
1858  {
1859  /*
1860  * For a join path, combine clauses enforced within either input path
1861  * with those enforced as joinrestrictinfo in this path. Note that
1862  * joinrestrictinfo may include some non-pushed-down clauses, but for
1863  * current purposes it's okay if we include those in the result. (To
1864  * be more careful, we could check for clause_relids overlapping the
1865  * path parameterization, but it's not worth the cycles for now.)
1866  */
1867  JoinPath *jpath = (JoinPath *) path;
1868  Bitmapset *pserials;
1869  ListCell *lc;
1870 
1871  pserials = NULL;
1872  pserials = bms_add_members(pserials,
1874  pserials = bms_add_members(pserials,
1876  foreach(lc, jpath->joinrestrictinfo)
1877  {
1878  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1879 
1880  pserials = bms_add_member(pserials, rinfo->rinfo_serial);
1881  }
1882  return pserials;
1883  }
1884  else if (IsA(path, AppendPath))
1885  {
1886  /*
1887  * For an appendrel, take the intersection of the sets of clauses
1888  * enforced in each input path.
1889  */
1890  AppendPath *apath = (AppendPath *) path;
1891  Bitmapset *pserials;
1892  ListCell *lc;
1893 
1894  pserials = NULL;
1895  foreach(lc, apath->subpaths)
1896  {
1897  Path *subpath = (Path *) lfirst(lc);
1898  Bitmapset *subserials;
1899 
1900  subserials = get_param_path_clause_serials(subpath);
1901  if (lc == list_head(apath->subpaths))
1902  pserials = bms_copy(subserials);
1903  else
1904  pserials = bms_int_members(pserials, subserials);
1905  }
1906  return pserials;
1907  }
1908  else if (IsA(path, MergeAppendPath))
1909  {
1910  /* Same as AppendPath case */
1911  MergeAppendPath *apath = (MergeAppendPath *) path;
1912  Bitmapset *pserials;
1913  ListCell *lc;
1914 
1915  pserials = NULL;
1916  foreach(lc, apath->subpaths)
1917  {
1918  Path *subpath = (Path *) lfirst(lc);
1919  Bitmapset *subserials;
1920 
1921  subserials = get_param_path_clause_serials(subpath);
1922  if (lc == list_head(apath->subpaths))
1923  pserials = bms_copy(subserials);
1924  else
1925  pserials = bms_int_members(pserials, subserials);
1926  }
1927  return pserials;
1928  }
1929  else
1930  {
1931  /*
1932  * Otherwise, it's a baserel path and we can use the
1933  * previously-computed set of serial numbers.
1934  */
1935  return path->param_info->ppi_serials;
1936  }
1937 }
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:818
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:928
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
Bitmapset * get_param_path_clause_serials(Path *path)
Definition: relnode.c:1851
List * subpaths
Definition: pathnodes.h:1893
Path * outerjoinpath
Definition: pathnodes.h:2035
Path * innerjoinpath
Definition: pathnodes.h:2036
List * joinrestrictinfo
Definition: pathnodes.h:2038

References bms_add_member(), bms_add_members(), bms_copy(), bms_int_members(), JoinPath::innerjoinpath, IsA, JoinPath::joinrestrictinfo, lfirst, list_head(), JoinPath::outerjoinpath, RestrictInfo::rinfo_serial, subpath(), AppendPath::subpaths, and MergeAppendPath::subpaths.

Referenced by create_nestloop_path().

◆ have_partkey_equi_join()

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

Definition at line 2019 of file relnode.c.

2022 {
2023  PartitionScheme part_scheme = rel1->part_scheme;
2024  ListCell *lc;
2025  int cnt_pks;
2026  bool pk_has_clause[PARTITION_MAX_KEYS];
2027  bool strict_op;
2028 
2029  /*
2030  * This function must only be called when the joined relations have same
2031  * partitioning scheme.
2032  */
2033  Assert(rel1->part_scheme == rel2->part_scheme);
2034  Assert(part_scheme);
2035 
2036  memset(pk_has_clause, 0, sizeof(pk_has_clause));
2037  foreach(lc, restrictlist)
2038  {
2039  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2040  OpExpr *opexpr;
2041  Expr *expr1;
2042  Expr *expr2;
2043  int ipk1;
2044  int ipk2;
2045 
2046  /* If processing an outer join, only use its own join clauses. */
2047  if (IS_OUTER_JOIN(jointype) &&
2048  RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
2049  continue;
2050 
2051  /* Skip clauses which can not be used for a join. */
2052  if (!rinfo->can_join)
2053  continue;
2054 
2055  /* Skip clauses which are not equality conditions. */
2056  if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
2057  continue;
2058 
2059  /* Should be OK to assume it's an OpExpr. */
2060  opexpr = castNode(OpExpr, rinfo->clause);
2061 
2062  /* Match the operands to the relation. */
2063  if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
2064  bms_is_subset(rinfo->right_relids, rel2->relids))
2065  {
2066  expr1 = linitial(opexpr->args);
2067  expr2 = lsecond(opexpr->args);
2068  }
2069  else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
2070  bms_is_subset(rinfo->right_relids, rel1->relids))
2071  {
2072  expr1 = lsecond(opexpr->args);
2073  expr2 = linitial(opexpr->args);
2074  }
2075  else
2076  continue;
2077 
2078  /*
2079  * Now we need to know whether the join operator is strict; see
2080  * comments in pathnodes.h.
2081  */
2082  strict_op = op_strict(opexpr->opno);
2083 
2084  /*
2085  * Vars appearing in the relation's partition keys will not have any
2086  * varnullingrels, but those in expr1 and expr2 will if we're above
2087  * outer joins that could null the respective rels. It's okay to
2088  * match anyway, if the join operator is strict.
2089  */
2090  if (strict_op)
2091  {
2092  if (bms_overlap(rel1->relids, root->outer_join_rels))
2093  expr1 = (Expr *) remove_nulling_relids((Node *) expr1,
2094  root->outer_join_rels,
2095  NULL);
2096  if (bms_overlap(rel2->relids, root->outer_join_rels))
2097  expr2 = (Expr *) remove_nulling_relids((Node *) expr2,
2098  root->outer_join_rels,
2099  NULL);
2100  }
2101 
2102  /*
2103  * Only clauses referencing the partition keys are useful for
2104  * partitionwise join.
2105  */
2106  ipk1 = match_expr_to_partition_keys(expr1, rel1, strict_op);
2107  if (ipk1 < 0)
2108  continue;
2109  ipk2 = match_expr_to_partition_keys(expr2, rel2, strict_op);
2110  if (ipk2 < 0)
2111  continue;
2112 
2113  /*
2114  * If the clause refers to keys at different ordinal positions, it can
2115  * not be used for partitionwise join.
2116  */
2117  if (ipk1 != ipk2)
2118  continue;
2119 
2120  /*
2121  * The clause allows partitionwise join only if it uses the same
2122  * operator family as that specified by the partition key.
2123  */
2124  if (rel1->part_scheme->strategy == PARTITION_STRATEGY_HASH)
2125  {
2126  if (!OidIsValid(rinfo->hashjoinoperator) ||
2127  !op_in_opfamily(rinfo->hashjoinoperator,
2128  part_scheme->partopfamily[ipk1]))
2129  continue;
2130  }
2131  else if (!list_member_oid(rinfo->mergeopfamilies,
2132  part_scheme->partopfamily[ipk1]))
2133  continue;
2134 
2135  /* Mark the partition key as having an equi-join clause. */
2136  pk_has_clause[ipk1] = true;
2137  }
2138 
2139  /* Check whether every partition key has an equi-join condition. */
2140  for (cnt_pks = 0; cnt_pks < part_scheme->partnatts; cnt_pks++)
2141  {
2142  if (!pk_has_clause[cnt_pks])
2143  return false;
2144  }
2145 
2146  return true;
2147 }
#define OidIsValid(objectId)
Definition: c.h:759
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:347
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:869
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2664
#define PARTITION_MAX_KEYS
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
Definition: relnode.c:2161
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
Oid opno
Definition: primnodes.h:745
List * args
Definition: primnodes.h:763
Relids outer_join_rels
Definition: pathnodes.h:261
Expr * clause
Definition: pathnodes.h:2513

References OpExpr::args, Assert(), bms_is_subset(), bms_overlap(), 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, PlannerInfo::outer_join_rels, PARTITION_MAX_KEYS, PARTITION_STRATEGY_HASH, PartitionSchemeData::partnatts, PartitionSchemeData::partopfamily, RelOptInfo::relids, remove_nulling_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 2161 of file relnode.c.

2162 {
2163  int cnt;
2164 
2165  /* This function should be called only for partitioned relations. */
2166  Assert(rel->part_scheme);
2167  Assert(rel->partexprs);
2168  Assert(rel->nullable_partexprs);
2169 
2170  /* Remove any relabel decorations. */
2171  while (IsA(expr, RelabelType))
2172  expr = (Expr *) (castNode(RelabelType, expr))->arg;
2173 
2174  for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
2175  {
2176  ListCell *lc;
2177 
2178  /* We can always match to the non-nullable partition keys. */
2179  foreach(lc, rel->partexprs[cnt])
2180  {
2181  if (equal(lfirst(lc), expr))
2182  return cnt;
2183  }
2184 
2185  if (!strict_op)
2186  continue;
2187 
2188  /*
2189  * If it's a strict join operator then a NULL partition key on one
2190  * side will not join to any partition key on the other side, and in
2191  * particular such a row can't join to a row from a different
2192  * partition on the other side. So, it's okay to search the nullable
2193  * partition keys as well.
2194  */
2195  foreach(lc, rel->nullable_partexprs[cnt])
2196  {
2197  if (equal(lfirst(lc), expr))
2198  return cnt;
2199  }
2200  }
2201 
2202  return -1;
2203 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
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 1003 of file relnode.c.

1007 {
1008  Relids result;
1009 
1010  /*
1011  * Basically we just need the union of the inputs' lateral_relids, less
1012  * whatever is already in the join.
1013  *
1014  * It's not immediately obvious that this is a valid way to compute the
1015  * result, because it might seem that we're ignoring possible lateral refs
1016  * of PlaceHolderVars that are due to be computed at the join but not in
1017  * either input. However, because create_lateral_join_info() already
1018  * charged all such PHV refs to each member baserel of the join, they'll
1019  * be accounted for already in the inputs' lateral_relids. Likewise, we
1020  * do not need to worry about doing transitive closure here, because that
1021  * was already accounted for in the original baserel lateral_relids.
1022  */
1023  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
1024  result = bms_del_members(result, joinrelids);
1025  return result;
1026 }

References bms_del_members(), 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 568 of file relnode.c.

570 {
571  if (OidIsValid(outer_rel->serverid) &&
572  inner_rel->serverid == outer_rel->serverid)
573  {
574  if (inner_rel->userid == outer_rel->userid)
575  {
576  joinrel->serverid = outer_rel->serverid;
577  joinrel->userid = outer_rel->userid;
578  joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
579  joinrel->fdwroutine = outer_rel->fdwroutine;
580  }
581  else if (!OidIsValid(inner_rel->userid) &&
582  outer_rel->userid == GetUserId())
583  {
584  joinrel->serverid = outer_rel->serverid;
585  joinrel->userid = outer_rel->userid;
586  joinrel->useridiscurrent = true;
587  joinrel->fdwroutine = outer_rel->fdwroutine;
588  }
589  else if (!OidIsValid(outer_rel->userid) &&
590  inner_rel->userid == GetUserId())
591  {
592  joinrel->serverid = outer_rel->serverid;
593  joinrel->userid = inner_rel->userid;
594  joinrel->useridiscurrent = true;
595  joinrel->fdwroutine = outer_rel->fdwroutine;
596  }
597  }
598 }
Oid GetUserId(void)
Definition: miscinit.c:510

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

2213 {
2214  PartitionScheme part_scheme = joinrel->part_scheme;
2215  int partnatts = part_scheme->partnatts;
2216 
2217  joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts);
2218  joinrel->nullable_partexprs =
2219  (List **) palloc0(sizeof(List *) * partnatts);
2220 
2221  /*
2222  * The joinrel's partition expressions are the same as those of the input
2223  * rels, but we must properly classify them as nullable or not in the
2224  * joinrel's output. (Also, we add some more partition expressions if
2225  * it's a FULL JOIN.)
2226  */
2227  for (int cnt = 0; cnt < partnatts; cnt++)
2228  {
2229  /* mark these const to enforce that we copy them properly */
2230  const List *outer_expr = outer_rel->partexprs[cnt];
2231  const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
2232  const List *inner_expr = inner_rel->partexprs[cnt];
2233  const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
2234  List *partexpr = NIL;
2235  List *nullable_partexpr = NIL;
2236  ListCell *lc;
2237 
2238  switch (jointype)
2239  {
2240  /*
2241  * A join relation resulting from an INNER join may be
2242  * regarded as partitioned by either of the inner and outer
2243  * relation keys. For example, A INNER JOIN B ON A.a = B.b
2244  * can be regarded as partitioned on either A.a or B.b. So we
2245  * add both keys to the joinrel's partexpr lists. However,
2246  * anything that was already nullable still has to be treated
2247  * as nullable.
2248  */
2249  case JOIN_INNER:
2250  partexpr = list_concat_copy(outer_expr, inner_expr);
2251  nullable_partexpr = list_concat_copy(outer_null_expr,
2252  inner_null_expr);
2253  break;
2254 
2255  /*
2256  * A join relation resulting from a SEMI or ANTI join may be
2257  * regarded as partitioned by the outer relation keys. The
2258  * inner relation's keys are no longer interesting; since they
2259  * aren't visible in the join output, nothing could join to
2260  * them.
2261  */
2262  case JOIN_SEMI:
2263  case JOIN_ANTI:
2264  partexpr = list_copy(outer_expr);
2265  nullable_partexpr = list_copy(outer_null_expr);
2266  break;
2267 
2268  /*
2269  * A join relation resulting from a LEFT OUTER JOIN likewise
2270  * may be regarded as partitioned on the (non-nullable) outer
2271  * relation keys. The inner (nullable) relation keys are okay
2272  * as partition keys for further joins as long as they involve
2273  * strict join operators.
2274  */
2275  case JOIN_LEFT:
2276  partexpr = list_copy(outer_expr);
2277  nullable_partexpr = list_concat_copy(inner_expr,
2278  outer_null_expr);
2279  nullable_partexpr = list_concat(nullable_partexpr,
2280  inner_null_expr);
2281  break;
2282 
2283  /*
2284  * For FULL OUTER JOINs, both relations are nullable, so the
2285  * resulting join relation may be regarded as partitioned on
2286  * either of inner and outer relation keys, but only for joins
2287  * that involve strict join operators.
2288  */
2289  case JOIN_FULL:
2290  nullable_partexpr = list_concat_copy(outer_expr,
2291  inner_expr);
2292  nullable_partexpr = list_concat(nullable_partexpr,
2293  outer_null_expr);
2294  nullable_partexpr = list_concat(nullable_partexpr,
2295  inner_null_expr);
2296 
2297  /*
2298  * Also add CoalesceExprs corresponding to each possible
2299  * full-join output variable (that is, left side coalesced to
2300  * right side), so that we can match equijoin expressions
2301  * using those variables. We really only need these for
2302  * columns merged by JOIN USING, and only with the pairs of
2303  * input items that correspond to the data structures that
2304  * parse analysis would build for such variables. But it's
2305  * hard to tell which those are, so just make all the pairs.
2306  * Extra items in the nullable_partexprs list won't cause big
2307  * problems. (It's possible that such items will get matched
2308  * to user-written COALESCEs, but it should still be valid to
2309  * partition on those, since they're going to be either the
2310  * partition column or NULL; it's the same argument as for
2311  * partitionwise nesting of any outer join.) We assume no
2312  * type coercions are needed to make the coalesce expressions,
2313  * since columns of different types won't have gotten
2314  * classified as the same PartitionScheme. Note that we
2315  * intentionally leave out the varnullingrels decoration that
2316  * would ordinarily appear on the Vars inside these
2317  * CoalesceExprs, because have_partkey_equi_join will strip
2318  * varnullingrels from the expressions it will compare to the
2319  * partexprs.
2320  */
2321  foreach(lc, list_concat_copy(outer_expr, outer_null_expr))
2322  {
2323  Node *larg = (Node *) lfirst(lc);
2324  ListCell *lc2;
2325 
2326  foreach(lc2, list_concat_copy(inner_expr, inner_null_expr))
2327  {
2328  Node *rarg = (Node *) lfirst(lc2);
2330 
2331  c->coalescetype = exprType(larg);
2332  c->coalescecollid = exprCollation(larg);
2333  c->args = list_make2(larg, rarg);
2334  c->location = -1;
2335  nullable_partexpr = lappend(nullable_partexpr, c);
2336  }
2337  }
2338  break;
2339 
2340  default:
2341  elog(ERROR, "unrecognized join type: %d", (int) jointype);
2342  }
2343 
2344  joinrel->partexprs[cnt] = partexpr;
2345  joinrel->nullable_partexprs[cnt] = nullable_partexpr;
2346  }
2347 }
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:318
@ JOIN_LEFT
Definition: nodes.h:305
@ JOIN_ANTI
Definition: nodes.h:319
#define list_make2(x1, x2)
Definition: pg_list.h:214
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 92 of file relnode.c.

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

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

1362 {
1363  ListCell *l;
1364 
1365  /* Expected to be called only for join between parent relations. */
1366  Assert(joinrel->reloptkind == RELOPT_JOINREL);
1367 
1368  foreach(l, joininfo_list)
1369  {
1370  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1371 
1372  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1373  {
1374  /*
1375  * This clause becomes a restriction clause for the joinrel, since
1376  * it refers to no outside rels. So we can ignore it in this
1377  * routine.
1378  */
1379  }
1380  else
1381  {
1382  /*
1383  * This clause is still a join clause at this level, so add it to
1384  * the new joininfo list, being careful to eliminate duplicates.
1385  * (Since RestrictInfo nodes in different joinlists will have been
1386  * multiply-linked rather than copied, pointer equality should be
1387  * a sufficient test.)
1388  */
1389  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1390  }
1391  }
1392 
1393  return new_joininfo;
1394 }
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1355
Relids required_relids
Definition: pathnodes.h:2544

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 ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo input_rel,
Relids  both_input_relids,
List new_restrictlist 
)
static

Definition at line 1286 of file relnode.c.

1291 {
1292  ListCell *l;
1293 
1294  foreach(l, input_rel->joininfo)
1295  {
1296  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1297 
1298  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1299  {
1300  /*
1301  * This clause should become a restriction clause for the joinrel,
1302  * since it refers to no outside rels. However, if it's a clone
1303  * clause then it might be too late to evaluate it, so we have to
1304  * check. (If it is too late, just ignore the clause, taking it
1305  * on faith that another clone was or will be selected.) Clone
1306  * clauses should always be outer-join clauses, so we compare
1307  * against both_input_relids.
1308  */
1309  if (rinfo->has_clone || rinfo->is_clone)
1310  {
1311  Assert(!RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids));
1312  if (!bms_is_subset(rinfo->required_relids, both_input_relids))
1313  continue;
1314  if (!clause_is_computable_at(root, rinfo->clause_relids,
1315  both_input_relids))
1316  continue;
1317  }
1318  else
1319  {
1320  /*
1321  * For non-clone clauses, we just Assert it's OK. These might
1322  * be either join or filter clauses.
1323  */
1324 #ifdef USE_ASSERT_CHECKING
1325  if (RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1326  Assert(clause_is_computable_at(root, rinfo->clause_relids,
1327  joinrel->relids));
1328  else
1329  {
1331  both_input_relids));
1332  Assert(clause_is_computable_at(root, rinfo->clause_relids,
1333  both_input_relids));
1334  }
1335 #endif
1336  }
1337 
1338  /*
1339  * OK, so add it to the list, being careful to eliminate
1340  * duplicates. (Since RestrictInfo nodes in different joinlists
1341  * will have been multiply-linked rather than copied, pointer
1342  * equality should be a sufficient test.)
1343  */
1344  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1345  }
1346  else
1347  {
1348  /*
1349  * This clause is still a join clause at this level, so we ignore
1350  * it in this routine.
1351  */
1352  }
1353  }
1354 
1355  return new_restrictlist;
1356 }
bool clause_is_computable_at(PlannerInfo *root, Relids clause_relids, Relids eval_relids)
Definition: restrictinfo.c:543
bool has_clone
Definition: pathnodes.h:2525

References Assert(), bms_is_subset(), clause_is_computable_at(), RestrictInfo::has_clone, RestrictInfo::is_clone, RelOptInfo::joininfo, lfirst, list_append_unique_ptr(), RelOptInfo::relids, RestrictInfo::required_relids, and RINFO_IS_PUSHED_DOWN.

Referenced by build_joinrel_restrictlist().