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/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h"
#include "parser/parse_relation.h"
#include "rewrite/rewriteManip.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, List *pushed_down_joins, 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_noerr (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 *pushed_down_joins, 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 627 of file relnode.c.

628 {
629  /* GEQO requires us to append the new joinrel to the end of the list! */
630  root->join_rel_list = lappend(root->join_rel_list, joinrel);
631 
632  /* store it into the auxiliary hashtable if there is one. */
633  if (root->join_rel_hash)
634  {
635  JoinHashEntry *hentry;
636  bool found;
637 
638  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
639  &(joinrel->relids),
640  HASH_ENTER,
641  &found);
642  Assert(!found);
643  hentry->join_rel = joinrel;
644  }
645 }
#define Assert(condition)
Definition: c.h:858
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
@ HASH_ENTER
Definition: hsearch.h:114
List * lappend(List *list, void *datum)
Definition: list.c:339
tree ctl root
Definition: radixtree.h:1884
RelOptInfo * join_rel
Definition: relnode.c:41
Relids relids
Definition: pathnodes.h:861

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

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

884 {
885  RelOptInfo *joinrel = makeNode(RelOptInfo);
886  AppendRelInfo **appinfos;
887  int nappinfos;
888 
889  /* Only joins between "other" relations land here. */
890  Assert(IS_OTHER_REL(outer_rel) && IS_OTHER_REL(inner_rel));
891 
892  /* The parent joinrel should have consider_partitionwise_join set. */
893  Assert(parent_joinrel->consider_partitionwise_join);
894 
895  /*
896  * Find the AppendRelInfo structures for the child baserels. We'll need
897  * these for computing the child join's relid set, and later for mapping
898  * Vars to the child rel.
899  */
900  appinfos = find_appinfos_by_relids(root,
901  bms_union(outer_rel->relids,
902  inner_rel->relids),
903  &nappinfos);
904 
905  joinrel->reloptkind = RELOPT_OTHER_JOINREL;
906  joinrel->relids = adjust_child_relids(parent_joinrel->relids,
907  nappinfos, appinfos);
908  joinrel->rows = 0;
909  /* cheap startup cost is interesting iff not all tuples to be retrieved */
910  joinrel->consider_startup = (root->tuple_fraction > 0);
911  joinrel->consider_param_startup = false;
912  joinrel->consider_parallel = false;
913  joinrel->reltarget = create_empty_pathtarget();
914  joinrel->pathlist = NIL;
915  joinrel->ppilist = NIL;
916  joinrel->partial_pathlist = NIL;
917  joinrel->cheapest_startup_path = NULL;
918  joinrel->cheapest_total_path = NULL;
919  joinrel->cheapest_unique_path = NULL;
921  joinrel->direct_lateral_relids = NULL;
922  joinrel->lateral_relids = NULL;
923  joinrel->relid = 0; /* indicates not a baserel */
924  joinrel->rtekind = RTE_JOIN;
925  joinrel->min_attr = 0;
926  joinrel->max_attr = 0;
927  joinrel->attr_needed = NULL;
928  joinrel->attr_widths = NULL;
929  joinrel->notnullattnums = NULL;
930  joinrel->nulling_relids = NULL;
931  joinrel->lateral_vars = NIL;
932  joinrel->lateral_referencers = NULL;
933  joinrel->indexlist = NIL;
934  joinrel->pages = 0;
935  joinrel->tuples = 0;
936  joinrel->allvisfrac = 0;
937  joinrel->eclass_indexes = NULL;
938  joinrel->subroot = NULL;
939  joinrel->subplan_params = NIL;
940  joinrel->amflags = 0;
941  joinrel->serverid = InvalidOid;
942  joinrel->userid = InvalidOid;
943  joinrel->useridiscurrent = false;
944  joinrel->fdwroutine = NULL;
945  joinrel->fdw_private = NULL;
946  joinrel->baserestrictinfo = NIL;
947  joinrel->baserestrictcost.startup = 0;
948  joinrel->baserestrictcost.per_tuple = 0;
949  joinrel->joininfo = NIL;
950  joinrel->has_eclass_joins = false;
951  joinrel->consider_partitionwise_join = false; /* might get changed later */
952  joinrel->parent = parent_joinrel;
953  joinrel->top_parent = parent_joinrel->top_parent ? parent_joinrel->top_parent : parent_joinrel;
954  joinrel->top_parent_relids = joinrel->top_parent->relids;
955  joinrel->part_scheme = NULL;
956  joinrel->nparts = -1;
957  joinrel->boundinfo = NULL;
958  joinrel->partbounds_merged = false;
959  joinrel->partition_qual = NIL;
960  joinrel->part_rels = NULL;
961  joinrel->live_parts = NULL;
962  joinrel->all_partrels = NULL;
963  joinrel->partexprs = NULL;
964  joinrel->nullable_partexprs = NULL;
965 
966  /* Compute information relevant to foreign relations. */
967  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
968 
969  /* Set up reltarget struct */
970  build_child_join_reltarget(root, parent_joinrel, joinrel,
971  nappinfos, appinfos);
972 
973  /* Construct joininfo list. */
974  joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
975  (Node *) parent_joinrel->joininfo,
976  nappinfos,
977  appinfos);
978 
979  /*
980  * Lateral relids referred in child join will be same as that referred in
981  * the parent relation.
982  */
983  joinrel->direct_lateral_relids = (Relids) bms_copy(parent_joinrel->direct_lateral_relids);
984  joinrel->lateral_relids = (Relids) bms_copy(parent_joinrel->lateral_relids);
985 
986  /*
987  * If the parent joinrel has pending equivalence classes, so does the
988  * child.
989  */
990  joinrel->has_eclass_joins = parent_joinrel->has_eclass_joins;
991 
992  /* Is the join between partitions itself partitioned? */
993  build_joinrel_partition_info(root, joinrel, outer_rel, inner_rel, sjinfo,
994  restrictlist);
995 
996  /* Child joinrel is parallel safe if parent is parallel safe. */
997  joinrel->consider_parallel = parent_joinrel->consider_parallel;
998 
999  /* Set estimates of the child-joinrel's size. */
1000  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
1001  sjinfo, restrictlist);
1002 
1003  /* We build the join only once. */
1004  Assert(!find_join_rel(root, joinrel->relids));
1005 
1006  /* Add the relation to the PlannerInfo. */
1007  add_join_rel(root, joinrel);
1008 
1009  /*
1010  * We might need EquivalenceClass members corresponding to the child join,
1011  * so that we can represent sort pathkeys for it. As with children of
1012  * baserels, we shouldn't need this unless there are relevant eclass joins
1013  * (implying that a merge join might be possible) or pathkeys to sort by.
1014  */
1015  if (joinrel->has_eclass_joins || has_useful_pathkeys(root, parent_joinrel))
1017  nappinfos, appinfos,
1018  parent_joinrel, joinrel);
1019 
1020  pfree(appinfos);
1021 
1022  return joinrel;
1023 }
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:733
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:196
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:554
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:5318
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
Definition: equivclass.c:2769
void pfree(void *pointer)
Definition: mcxt.c:1520
#define makeNode(_type_)
Definition: nodes.h:155
@ RTE_JOIN
Definition: parsenodes.h:1030
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:2261
Bitmapset * Relids
Definition: pathnodes.h:30
@ RELOPT_OTHER_JOINREL
Definition: pathnodes.h:820
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:844
#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:2017
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:527
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:2425
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:589
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:627
Definition: pg_list.h:54
Definition: nodes.h:129
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
List * baserestrictinfo
Definition: pathnodes.h:975
bool consider_param_startup
Definition: pathnodes.h:875
List * subplan_params
Definition: pathnodes.h:944
List * ppilist
Definition: pathnodes.h:889
bool useridiscurrent
Definition: pathnodes.h:958
uint32 amflags
Definition: pathnodes.h:948
List * joininfo
Definition: pathnodes.h:981
Bitmapset * notnullattnums
Definition: pathnodes.h:926
List * partition_qual
Definition: pathnodes.h:1017
struct PathTarget * reltarget
Definition: pathnodes.h:883
Index relid
Definition: pathnodes.h:908
List * lateral_vars
Definition: pathnodes.h:930
Cardinality tuples
Definition: pathnodes.h:939
bool consider_parallel
Definition: pathnodes.h:877
Relids top_parent_relids
Definition: pathnodes.h:999
bool partbounds_merged
Definition: pathnodes.h:1015
BlockNumber pages
Definition: pathnodes.h:938
Relids lateral_relids
Definition: pathnodes.h:903
List * cheapest_parameterized_paths
Definition: pathnodes.h:894
List * pathlist
Definition: pathnodes.h:888
RelOptKind reloptkind
Definition: pathnodes.h:855
List * indexlist
Definition: pathnodes.h:934
struct Path * cheapest_unique_path
Definition: pathnodes.h:893
Relids lateral_referencers
Definition: pathnodes.h:932
struct Path * cheapest_startup_path
Definition: pathnodes.h:891
QualCost baserestrictcost
Definition: pathnodes.h:977
struct Path * cheapest_total_path
Definition: pathnodes.h:892
Oid userid
Definition: pathnodes.h:956
Bitmapset * eclass_indexes
Definition: pathnodes.h:942
Relids all_partrels
Definition: pathnodes.h:1031
Relids direct_lateral_relids
Definition: pathnodes.h:901
bool has_eclass_joins
Definition: pathnodes.h:983
Oid serverid
Definition: pathnodes.h:954
bool consider_startup
Definition: pathnodes.h:873
Bitmapset * live_parts
Definition: pathnodes.h:1029
bool consider_partitionwise_join
Definition: pathnodes.h:989
List * partial_pathlist
Definition: pathnodes.h:890
PlannerInfo * subroot
Definition: pathnodes.h:943
AttrNumber max_attr
Definition: pathnodes.h:916
Relids nulling_relids
Definition: pathnodes.h:928
double allvisfrac
Definition: pathnodes.h:940
Cardinality rows
Definition: pathnodes.h:867
AttrNumber min_attr
Definition: pathnodes.h:914
RTEKind rtekind
Definition: pathnodes.h:912
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:681

References add_child_join_rel_equivalences(), add_join_rel(), adjust_appendrel_attrs(), adjust_child_relids(), RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, Assert, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_union(), build_child_join_reltarget(), build_joinrel_partition_info(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::cheapest_unique_path, RelOptInfo::consider_parallel, RelOptInfo::consider_param_startup, RelOptInfo::consider_partitionwise_join, RelOptInfo::consider_startup, create_empty_pathtarget(), RelOptInfo::direct_lateral_relids, RelOptInfo::eclass_indexes, find_appinfos_by_relids(), find_join_rel(), RelOptInfo::has_eclass_joins, has_useful_pathkeys(), RelOptInfo::indexlist, InvalidOid, IS_OTHER_REL, RelOptInfo::joininfo, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, RelOptInfo::live_parts, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, NIL, RelOptInfo::notnullattnums, RelOptInfo::nparts, RelOptInfo::nulling_relids, 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, root, 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, 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 2425 of file relnode.c.

2430 {
2431  /* Build the targetlist */
2432  childrel->reltarget->exprs = (List *)
2434  (Node *) parentrel->reltarget->exprs,
2435  nappinfos, appinfos);
2436 
2437  /* Set the cost and width fields */
2438  childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
2439  childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
2440  childrel->reltarget->width = parentrel->reltarget->width;
2441 }
List * exprs
Definition: pathnodes.h:1522
QualCost cost
Definition: pathnodes.h:1528

References adjust_appendrel_attrs(), PathTarget::cost, PathTarget::exprs, QualCost::per_tuple, RelOptInfo::reltarget, root, 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 pushed_down_joins,
List **  restrictlist_ptr 
)

Definition at line 665 of file relnode.c.

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

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(), 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::notnullattnums, 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, root, 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, 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 486 of file relnode.c.

487 {
488  HTAB *hashtab;
489  HASHCTL hash_ctl;
490  ListCell *l;
491 
492  /* Create the hash table */
493  hash_ctl.keysize = sizeof(Relids);
494  hash_ctl.entrysize = sizeof(JoinHashEntry);
495  hash_ctl.hash = bitmap_hash;
496  hash_ctl.match = bitmap_match;
497  hash_ctl.hcxt = CurrentMemoryContext;
498  hashtab = hash_create("JoinRelHashTable",
499  256L,
500  &hash_ctl,
502 
503  /* Insert all the already-existing joinrels */
504  foreach(l, root->join_rel_list)
505  {
506  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
507  JoinHashEntry *hentry;
508  bool found;
509 
510  hentry = (JoinHashEntry *) hash_search(hashtab,
511  &(rel->relids),
512  HASH_ENTER,
513  &found);
514  Assert(!found);
515  hentry->join_rel = rel;
516  }
517 
518  root->join_rel_hash = hashtab;
519 }
uint32 bitmap_hash(const void *key, Size keysize)
Definition: bitmapset.c:1432
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: bitmapset.c:1442
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#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:143
#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, HASHCTL::keysize, lfirst, HASHCTL::match, RelOptInfo::relids, and root.

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

1337 {
1338  List *result;
1339 
1340  /*
1341  * Collect all the clauses that syntactically belong above this level,
1342  * eliminating any duplicates (important since we will see many of the
1343  * same clauses arriving from both input relations).
1344  */
1345  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1346  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1347 
1348  joinrel->joininfo = result;
1349 }
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:1418

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

2021 {
2022  PartitionScheme part_scheme;
2023 
2024  /* Nothing to do if partitionwise join technique is disabled. */
2026  {
2027  Assert(!IS_PARTITIONED_REL(joinrel));
2028  return;
2029  }
2030 
2031  /*
2032  * We can only consider this join as an input to further partitionwise
2033  * joins if (a) the input relations are partitioned and have
2034  * consider_partitionwise_join=true, (b) the partition schemes match, and
2035  * (c) we can identify an equi-join between the partition keys. Note that
2036  * if it were possible for have_partkey_equi_join to return different
2037  * answers for the same joinrel depending on which join ordering we try
2038  * first, this logic would break. That shouldn't happen, though, because
2039  * of the way the query planner deduces implied equalities and reorders
2040  * the joins. Please see optimizer/README for details.
2041  */
2042  if (outer_rel->part_scheme == NULL || inner_rel->part_scheme == NULL ||
2043  !outer_rel->consider_partitionwise_join ||
2044  !inner_rel->consider_partitionwise_join ||
2045  outer_rel->part_scheme != inner_rel->part_scheme ||
2046  !have_partkey_equi_join(root, joinrel, outer_rel, inner_rel,
2047  sjinfo->jointype, restrictlist))
2048  {
2049  Assert(!IS_PARTITIONED_REL(joinrel));
2050  return;
2051  }
2052 
2053  part_scheme = outer_rel->part_scheme;
2054 
2055  /*
2056  * This function will be called only once for each joinrel, hence it
2057  * should not have partitioning fields filled yet.
2058  */
2059  Assert(!joinrel->part_scheme && !joinrel->partexprs &&
2060  !joinrel->nullable_partexprs && !joinrel->part_rels &&
2061  !joinrel->boundinfo);
2062 
2063  /*
2064  * If the join relation is partitioned, it uses the same partitioning
2065  * scheme as the joining relations.
2066  *
2067  * Note: we calculate the partition bounds, number of partitions, and
2068  * child-join relations of the join relation in try_partitionwise_join().
2069  */
2070  joinrel->part_scheme = part_scheme;
2071  set_joinrel_partition_key_exprs(joinrel, outer_rel, inner_rel,
2072  sjinfo->jointype);
2073 
2074  /*
2075  * Set the consider_partitionwise_join flag.
2076  */
2077  Assert(outer_rel->consider_partitionwise_join);
2078  Assert(inner_rel->consider_partitionwise_join);
2079  joinrel->consider_partitionwise_join = true;
2080 }
bool enable_partitionwise_join
Definition: costsize.c:148
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1052
static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
Definition: relnode.c:2281
static bool have_partkey_equi_join(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition: relnode.c:2090

References Assert, RelOptInfo::consider_partitionwise_join, enable_partitionwise_join, have_partkey_equi_join(), IS_PARTITIONED_REL, SpecialJoinInfo::jointype, root, 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 1297 of file relnode.c.

1302 {
1303  List *result;
1304  Relids both_input_relids;
1305 
1306  both_input_relids = bms_union(outer_rel->relids, inner_rel->relids);
1307 
1308  /*
1309  * Collect all the clauses that syntactically belong at this level,
1310  * eliminating any duplicates (important since we will see many of the
1311  * same clauses arriving from both input relations).
1312  */
1313  result = subbuild_joinrel_restrictlist(root, joinrel, outer_rel,
1314  both_input_relids, NIL);
1315  result = subbuild_joinrel_restrictlist(root, joinrel, inner_rel,
1316  both_input_relids, result);
1317 
1318  /*
1319  * Add on any clauses derived from EquivalenceClasses. These cannot be
1320  * redundant with the clauses in the joininfo lists, so don't bother
1321  * checking.
1322  */
1323  result = list_concat(result,
1325  joinrel->relids,
1326  outer_rel->relids,
1327  inner_rel,
1328  sjinfo));
1329 
1330  return result;
1331 }
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: equivclass.c:1392
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
static List * subbuild_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, Relids both_input_relids, List *new_restrictlist)
Definition: relnode.c:1352

References bms_union(), generate_join_implied_equalities(), list_concat(), NIL, RelOptInfo::relids, root, 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,
List pushed_down_joins,
bool  can_null 
)
static

Definition at line 1112 of file relnode.c.

1117 {
1118  Relids relids = joinrel->relids;
1119  int64 tuple_width = joinrel->reltarget->width;
1120  ListCell *vars;
1121  ListCell *lc;
1122 
1123  foreach(vars, input_rel->reltarget->exprs)
1124  {
1125  Var *var = (Var *) lfirst(vars);
1126 
1127  /*
1128  * For a PlaceHolderVar, we have to look up the PlaceHolderInfo.
1129  */
1130  if (IsA(var, PlaceHolderVar))
1131  {
1132  PlaceHolderVar *phv = (PlaceHolderVar *) var;
1133  PlaceHolderInfo *phinfo = find_placeholder_info(root, phv);
1134 
1135  /* Is it still needed above this joinrel? */
1136  if (bms_nonempty_difference(phinfo->ph_needed, relids))
1137  {
1138  /*
1139  * Yup, add it to the output. If this join potentially nulls
1140  * this input, we have to update the PHV's phnullingrels,
1141  * which means making a copy.
1142  */
1143  if (can_null)
1144  {
1145  phv = copyObject(phv);
1146  /* See comments above to understand this logic */
1147  if (sjinfo->ojrelid != 0 &&
1148  bms_is_member(sjinfo->ojrelid, relids) &&
1149  (bms_is_subset(phv->phrels, sjinfo->syn_righthand) ||
1150  (sjinfo->jointype == JOIN_FULL &&
1151  bms_is_subset(phv->phrels, sjinfo->syn_lefthand))))
1153  sjinfo->ojrelid);
1154  foreach(lc, pushed_down_joins)
1155  {
1156  SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
1157 
1158  Assert(bms_is_member(othersj->ojrelid, relids));
1159  if (bms_is_subset(phv->phrels, othersj->syn_righthand))
1161  othersj->ojrelid);
1162  }
1163  phv->phnullingrels =
1164  bms_join(phv->phnullingrels,
1166  relids));
1167  }
1168 
1169  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1170  phv);
1171  /* Bubbling up the precomputed result has cost zero */
1172  tuple_width += phinfo->ph_width;
1173  }
1174  continue;
1175  }
1176 
1177  /*
1178  * Otherwise, anything in a baserel or joinrel targetlist ought to be
1179  * a Var. (More general cases can only appear in appendrel child
1180  * rels, which will never be seen here.)
1181  */
1182  if (!IsA(var, Var))
1183  elog(ERROR, "unexpected node type in rel targetlist: %d",
1184  (int) nodeTag(var));
1185 
1186  if (var->varno == ROWID_VAR)
1187  {
1188  /* UPDATE/DELETE/MERGE row identity vars are always needed */
1189  RowIdentityVarInfo *ridinfo = (RowIdentityVarInfo *)
1190  list_nth(root->row_identity_vars, var->varattno - 1);
1191 
1192  /* Update reltarget width estimate from RowIdentityVarInfo */
1193  tuple_width += ridinfo->rowidwidth;
1194  }
1195  else
1196  {
1197  RelOptInfo *baserel;
1198  int ndx;
1199 
1200  /* Get the Var's original base rel */
1201  baserel = find_base_rel(root, var->varno);
1202 
1203  /* Is it still needed above this joinrel? */
1204  ndx = var->varattno - baserel->min_attr;
1205  if (!bms_nonempty_difference(baserel->attr_needed[ndx], relids))
1206  continue; /* nope, skip it */
1207 
1208  /* Update reltarget width estimate from baserel's attr_widths */
1209  tuple_width += baserel->attr_widths[ndx];
1210  }
1211 
1212  /*
1213  * Add the Var to the output. If this join potentially nulls this
1214  * input, we have to update the Var's varnullingrels, which means
1215  * making a copy. But note that we don't ever add nullingrel bits to
1216  * row identity Vars (cf. comments in setrefs.c).
1217  */
1218  if (can_null && var->varno != ROWID_VAR)
1219  {
1220  var = copyObject(var);
1221  /* See comments above to understand this logic */
1222  if (sjinfo->ojrelid != 0 &&
1223  bms_is_member(sjinfo->ojrelid, relids) &&
1224  (bms_is_member(var->varno, sjinfo->syn_righthand) ||
1225  (sjinfo->jointype == JOIN_FULL &&
1226  bms_is_member(var->varno, sjinfo->syn_lefthand))))
1227  var->varnullingrels = bms_add_member(var->varnullingrels,
1228  sjinfo->ojrelid);
1229  foreach(lc, pushed_down_joins)
1230  {
1231  SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
1232 
1233  Assert(bms_is_member(othersj->ojrelid, relids));
1234  if (bms_is_member(var->varno, othersj->syn_righthand))
1235  var->varnullingrels = bms_add_member(var->varnullingrels,
1236  othersj->ojrelid);
1237  }
1238  var->varnullingrels =
1239  bms_join(var->varnullingrels,
1241  relids));
1242  }
1243 
1244  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1245  var);
1246 
1247  /* Vars have cost zero, so no need to adjust reltarget->cost */
1248  }
1249 
1250  joinrel->reltarget->width = clamp_width_est(tuple_width);
1251 }
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:1230
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:641
int32 clamp_width_est(int64 tuple_width)
Definition: costsize.c:231
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define copyObject(obj)
Definition: nodes.h:224
#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:239
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:414
Relids ph_needed
Definition: pathnodes.h:3082
Relids phnullingrels
Definition: pathnodes.h:2779
Relids commute_above_r
Definition: pathnodes.h:2889
Relids syn_lefthand
Definition: pathnodes.h:2884
Relids syn_righthand
Definition: pathnodes.h:2885
Definition: primnodes.h:248
AttrNumber varattno
Definition: primnodes.h:260
int varno
Definition: primnodes.h:255
Definition: regcomp.c:281

References Assert, bms_add_member(), bms_intersect(), bms_is_member(), bms_is_subset(), bms_join(), bms_nonempty_difference(), clamp_width_est(), 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, root, 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 192 of file relnode.c.

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

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, 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, 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::notnullattnums, RelOptInfo::nparts, RelOptInfo::nulling_relids, RelOptInfo::pages, palloc0(), 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, root, 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, 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 163 of file relnode.c.

164 {
165  int new_size;
166 
167  Assert(add_size > 0);
168 
169  new_size = root->simple_rel_array_size + add_size;
170 
171  root->simple_rel_array =
172  repalloc0_array(root->simple_rel_array, RelOptInfo *, root->simple_rel_array_size, new_size);
173 
174  root->simple_rte_array =
175  repalloc0_array(root->simple_rte_array, RangeTblEntry *, root->simple_rel_array_size, new_size);
176 
177  if (root->append_rel_array)
178  root->append_rel_array =
179  repalloc0_array(root->append_rel_array, AppendRelInfo *, root->simple_rel_array_size, new_size);
180  else
181  root->append_rel_array =
182  palloc0_array(AppendRelInfo *, new_size);
183 
184  root->simple_rel_array_size = new_size;
185 }
#define palloc0_array(type, count)
Definition: fe_memutils.h:65
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:109
Size add_size(Size s1, Size s2)
Definition: shmem.c:493

References add_size(), Assert, palloc0_array, repalloc0_array, and root.

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

1471 {
1472  RelOptInfo *upperrel;
1473  ListCell *lc;
1474 
1475  /*
1476  * For the moment, our indexing data structure is just a List for each
1477  * relation kind. If we ever get so many of one kind that this stops
1478  * working well, we can improve it. No code outside this function should
1479  * assume anything about how to find a particular upperrel.
1480  */
1481 
1482  /* If we already made this upperrel for the query, return it */
1483  foreach(lc, root->upper_rels[kind])
1484  {
1485  upperrel = (RelOptInfo *) lfirst(lc);
1486 
1487  if (bms_equal(upperrel->relids, relids))
1488  return upperrel;
1489  }
1490 
1491  upperrel = makeNode(RelOptInfo);
1492  upperrel->reloptkind = RELOPT_UPPER_REL;
1493  upperrel->relids = bms_copy(relids);
1494 
1495  /* cheap startup cost is interesting iff not all tuples to be retrieved */
1496  upperrel->consider_startup = (root->tuple_fraction > 0);
1497  upperrel->consider_param_startup = false;
1498  upperrel->consider_parallel = false; /* might get changed later */
1499  upperrel->reltarget = create_empty_pathtarget();
1500  upperrel->pathlist = NIL;
1501  upperrel->cheapest_startup_path = NULL;
1502  upperrel->cheapest_total_path = NULL;
1503  upperrel->cheapest_unique_path = NULL;
1504  upperrel->cheapest_parameterized_paths = NIL;
1505 
1506  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1507 
1508  return upperrel;
1509 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
@ RELOPT_UPPER_REL
Definition: pathnodes.h:821

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 root.

Referenced by add_rtes_to_flat_rtable(), build_setop_child_paths(), 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(), preprocess_minmax_aggregates(), 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 454 of file relnode.c.

455 {
456  /* use an unsigned comparison to prevent negative array element access */
457  if ((uint32) relid < (uint32) root->simple_rel_array_size)
458  {
459  RelOptInfo *rel;
460  RangeTblEntry *rte;
461 
462  rel = root->simple_rel_array[relid];
463  if (rel)
464  return rel;
465 
466  /*
467  * We could just return NULL here, but for debugging purposes it seems
468  * best to actually verify that the relid is an outer join and not
469  * something weird.
470  */
471  rte = root->simple_rte_array[relid];
472  if (rte && rte->rtekind == RTE_JOIN && rte->jointype != JOIN_INNER)
473  return NULL;
474  }
475 
476  elog(ERROR, "no relation entry for relid %d", relid);
477 
478  return NULL; /* keep compiler quiet */
479 }
JoinType jointype
Definition: parsenodes.h:1161

References elog, ERROR, JOIN_INNER, RangeTblEntry::jointype, root, 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_base_rel_noerr()

RelOptInfo* find_base_rel_noerr ( PlannerInfo root,
int  relid 
)

Definition at line 436 of file relnode.c.

437 {
438  /* use an unsigned comparison to prevent negative array element access */
439  if ((uint32) relid < (uint32) root->simple_rel_array_size)
440  return root->simple_rel_array[relid];
441  return NULL;
442 }

References root.

Referenced by examine_simple_variable().

◆ find_childrel_parents()

Relids find_childrel_parents ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1521 of file relnode.c.

1522 {
1523  Relids result = NULL;
1524 
1526  Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1527 
1528  do
1529  {
1530  AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1531  Index prelid = appinfo->parent_relid;
1532 
1533  result = bms_add_member(result, prelid);
1534 
1535  /* traverse up to the parent rel, loop if it's also a child rel */
1536  rel = find_base_rel(root, prelid);
1537  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1538 
1539  Assert(rel->reloptkind == RELOPT_BASEREL);
1540 
1541  return result;
1542 }
unsigned int Index
Definition: c.h:614
Index parent_relid
Definition: pathnodes.h:2957

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

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

528 {
529  /*
530  * Switch to using hash lookup when list grows "too long". The threshold
531  * is arbitrary and is known only here.
532  */
533  if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
535 
536  /*
537  * Use either hashtable lookup or linear search, as appropriate.
538  *
539  * Note: the seemingly redundant hashkey variable is used to avoid taking
540  * the address of relids; unless the compiler is exceedingly smart, doing
541  * so would force relids out of a register and thus probably slow down the
542  * list-search case.
543  */
544  if (root->join_rel_hash)
545  {
546  Relids hashkey = relids;
547  JoinHashEntry *hentry;
548 
549  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
550  &hashkey,
551  HASH_FIND,
552  NULL);
553  if (hentry)
554  return hentry->join_rel;
555  }
556  else
557  {
558  ListCell *l;
559 
560  foreach(l, root->join_rel_list)
561  {
562  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
563 
564  if (bms_equal(rel->relids, relids))
565  return rel;
566  }
567  }
568 
569  return NULL;
570 }
@ HASH_FIND
Definition: hsearch.h:113
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:486

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

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

1902 {
1903  ListCell *lc;
1904 
1905  foreach(lc, rel->ppilist)
1906  {
1907  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1908 
1909  if (bms_equal(ppi->ppi_req_outer, required_outer))
1910  return ppi;
1911  }
1912 
1913  return NULL;
1914 }
Relids ppi_req_outer
Definition: pathnodes.h:1568

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

1869 {
1870  ParamPathInfo *ppi;
1871 
1872  /* If rel has LATERAL refs, every path for it should account for them */
1873  Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1874 
1875  /* Unparameterized paths have no ParamPathInfo */
1876  if (bms_is_empty(required_outer))
1877  return NULL;
1878 
1879  Assert(!bms_overlap(appendrel->relids, required_outer));
1880 
1881  /* If we already have a PPI for this parameterization, just return it */
1882  if ((ppi = find_param_path_info(appendrel, required_outer)))
1883  return ppi;
1884 
1885  /* Else build the ParamPathInfo */
1886  ppi = makeNode(ParamPathInfo);
1887  ppi->ppi_req_outer = required_outer;
1888  ppi->ppi_rows = 0;
1889  ppi->ppi_clauses = NIL;
1890  ppi->ppi_serials = NULL;
1891  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1892 
1893  return ppi;
1894 }
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
#define bms_is_empty(a)
Definition: bitmapset.h:118
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1901
Cardinality ppi_rows
Definition: pathnodes.h:1569
List * ppi_clauses
Definition: pathnodes.h:1570
Bitmapset * ppi_serials
Definition: pathnodes.h:1571

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

1559 {
1560  ParamPathInfo *ppi;
1561  Relids joinrelids;
1562  List *pclauses;
1563  List *eqclauses;
1564  Bitmapset *pserials;
1565  double rows;
1566  ListCell *lc;
1567 
1568  /* If rel has LATERAL refs, every path for it should account for them */
1569  Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1570 
1571  /* Unparameterized paths have no ParamPathInfo */
1572  if (bms_is_empty(required_outer))
1573  return NULL;
1574 
1575  Assert(!bms_overlap(baserel->relids, required_outer));
1576 
1577  /* If we already have a PPI for this parameterization, just return it */
1578  if ((ppi = find_param_path_info(baserel, required_outer)))
1579  return ppi;
1580 
1581  /*
1582  * Identify all joinclauses that are movable to this base rel given this
1583  * parameterization.
1584  */
1585  joinrelids = bms_union(baserel->relids, required_outer);
1586  pclauses = NIL;
1587  foreach(lc, baserel->joininfo)
1588  {
1589  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1590 
1591  if (join_clause_is_movable_into(rinfo,
1592  baserel->relids,
1593  joinrelids))
1594  pclauses = lappend(pclauses, rinfo);
1595  }
1596 
1597  /*
1598  * Add in joinclauses generated by EquivalenceClasses, too. (These
1599  * necessarily satisfy join_clause_is_movable_into; but in assert-enabled
1600  * builds, let's verify that.)
1601  */
1603  joinrelids,
1604  required_outer,
1605  baserel,
1606  NULL);
1607 #ifdef USE_ASSERT_CHECKING
1608  foreach(lc, eqclauses)
1609  {
1610  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1611 
1613  baserel->relids,
1614  joinrelids));
1615  }
1616 #endif
1617  pclauses = list_concat(pclauses, eqclauses);
1618 
1619  /* Compute set of serial numbers of the enforced clauses */
1620  pserials = NULL;
1621  foreach(lc, pclauses)
1622  {
1623  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1624 
1625  pserials = bms_add_member(pserials, rinfo->rinfo_serial);
1626  }
1627 
1628  /* Estimate the number of rows returned by the parameterized scan */
1629  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1630 
1631  /* And now we can build the ParamPathInfo */
1632  ppi = makeNode(ParamPathInfo);
1633  ppi->ppi_req_outer = required_outer;
1634  ppi->ppi_rows = rows;
1635  ppi->ppi_clauses = pclauses;
1636  ppi->ppi_serials = pserials;
1637  baserel->ppilist = lappend(baserel->ppilist, ppi);
1638 
1639  return ppi;
1640 }
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:5269
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:670
int rinfo_serial
Definition: pathnodes.h:2624

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, RestrictInfo::rinfo_serial, and root.

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

1677 {
1678  ParamPathInfo *ppi;
1679  Relids join_and_req;
1680  Relids outer_and_req;
1681  Relids inner_and_req;
1682  List *pclauses;
1683  List *eclauses;
1684  List *dropped_ecs;
1685  double rows;
1686  ListCell *lc;
1687 
1688  /* If rel has LATERAL refs, every path for it should account for them */
1689  Assert(bms_is_subset(joinrel->lateral_relids, required_outer));
1690 
1691  /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1692  if (bms_is_empty(required_outer))
1693  return NULL;
1694 
1695  Assert(!bms_overlap(joinrel->relids, required_outer));
1696 
1697  /*
1698  * Identify all joinclauses that are movable to this join rel given this
1699  * parameterization. These are the clauses that are movable into this
1700  * join, but not movable into either input path. Treat an unparameterized
1701  * input path as not accepting parameterized clauses (because it won't,
1702  * per the shortcut exit above), even though the joinclause movement rules
1703  * might allow the same clauses to be moved into a parameterized path for
1704  * that rel.
1705  */
1706  join_and_req = bms_union(joinrel->relids, required_outer);
1707  if (outer_path->param_info)
1708  outer_and_req = bms_union(outer_path->parent->relids,
1709  PATH_REQ_OUTER(outer_path));
1710  else
1711  outer_and_req = NULL; /* outer path does not accept parameters */
1712  if (inner_path->param_info)
1713  inner_and_req = bms_union(inner_path->parent->relids,
1714  PATH_REQ_OUTER(inner_path));
1715  else
1716  inner_and_req = NULL; /* inner path does not accept parameters */
1717 
1718  pclauses = NIL;
1719  foreach(lc, joinrel->joininfo)
1720  {
1721  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1722 
1723  if (join_clause_is_movable_into(rinfo,
1724  joinrel->relids,
1725  join_and_req) &&
1727  outer_path->parent->relids,
1728  outer_and_req) &&
1730  inner_path->parent->relids,
1731  inner_and_req))
1732  pclauses = lappend(pclauses, rinfo);
1733  }
1734 
1735  /* Consider joinclauses generated by EquivalenceClasses, too */
1737  join_and_req,
1738  required_outer,
1739  joinrel,
1740  NULL);
1741  /* We only want ones that aren't movable to lower levels */
1742  dropped_ecs = NIL;
1743  foreach(lc, eclauses)
1744  {
1745  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1746 
1748  joinrel->relids,
1749  join_and_req));
1750  if (join_clause_is_movable_into(rinfo,
1751  outer_path->parent->relids,
1752  outer_and_req))
1753  continue; /* drop if movable into LHS */
1754  if (join_clause_is_movable_into(rinfo,
1755  inner_path->parent->relids,
1756  inner_and_req))
1757  {
1758  /* drop if movable into RHS, but remember EC for use below */
1759  Assert(rinfo->left_ec == rinfo->right_ec);
1760  dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1761  continue;
1762  }
1763  pclauses = lappend(pclauses, rinfo);
1764  }
1765 
1766  /*
1767  * EquivalenceClasses are harder to deal with than we could wish, because
1768  * of the fact that a given EC can generate different clauses depending on
1769  * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1770  * LHS and RHS of the current join and Z is in required_outer, and further
1771  * suppose that the inner_path is parameterized by both X and Z. The code
1772  * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1773  * and in the latter case will have discarded it as being movable into the
1774  * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1775  * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1776  * not have produced both, and we can't readily tell from here which one
1777  * it did pick. If we add no clause to this join, we'll end up with
1778  * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1779  * constrained to be equal to the other members of the EC. (When we come
1780  * to join Z to this X/Y path, we will certainly drop whichever EC clause
1781  * is generated at that join, so this omission won't get fixed later.)
1782  *
1783  * To handle this, for each EC we discarded such a clause from, try to
1784  * generate a clause connecting the required_outer rels to the join's LHS
1785  * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1786  * the clause can't be moved to the LHS, add it to the current join's
1787  * restriction clauses. (If an EC cannot generate such a clause then it
1788  * has nothing that needs to be enforced here, while if the clause can be
1789  * moved into the LHS then it should have been enforced within that path.)
1790  *
1791  * Note that we don't need similar processing for ECs whose clause was
1792  * considered to be movable into the LHS, because the LHS can't refer to
1793  * the RHS so there is no comparable ambiguity about what it might
1794  * actually be enforcing internally.
1795  */
1796  if (dropped_ecs)
1797  {
1798  Relids real_outer_and_req;
1799 
1800  real_outer_and_req = bms_union(outer_path->parent->relids,
1801  required_outer);
1802  eclauses =
1804  dropped_ecs,
1805  real_outer_and_req,
1806  required_outer,
1807  outer_path->parent);
1808  foreach(lc, eclauses)
1809  {
1810  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1811 
1813  outer_path->parent->relids,
1814  real_outer_and_req));
1815  if (!join_clause_is_movable_into(rinfo,
1816  outer_path->parent->relids,
1817  outer_and_req))
1818  pclauses = lappend(pclauses, rinfo);
1819  }
1820  }
1821 
1822  /*
1823  * Now, attach the identified moved-down clauses to the caller's
1824  * restrict_clauses list. By using list_concat in this order, we leave
1825  * the original list structure of restrict_clauses undamaged.
1826  */
1827  *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1828 
1829  /* If we already have a PPI for this parameterization, just return it */
1830  if ((ppi = find_param_path_info(joinrel, required_outer)))
1831  return ppi;
1832 
1833  /* Estimate the number of rows returned by the parameterized join */
1834  rows = get_parameterized_joinrel_size(root, joinrel,
1835  outer_path,
1836  inner_path,
1837  sjinfo,
1838  *restrict_clauses);
1839 
1840  /*
1841  * And now we can build the ParamPathInfo. No point in saving the
1842  * input-pair-dependent clause list, though.
1843  *
1844  * Note: in GEQO mode, we'll be called in a temporary memory context, but
1845  * the joinrel structure is there too, so no problem.
1846  */
1847  ppi = makeNode(ParamPathInfo);
1848  ppi->ppi_req_outer = required_outer;
1849  ppi->ppi_rows = rows;
1850  ppi->ppi_clauses = NIL;
1851  ppi->ppi_serials = NULL;
1852  joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1853 
1854  return ppi;
1855 }
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:5350
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1492
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1658

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, RelOptInfo::relids, and root.

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

1923 {
1924  if (path->param_info == NULL)
1925  return NULL; /* not parameterized */
1926  if (IsA(path, NestPath) ||
1927  IsA(path, MergePath) ||
1928  IsA(path, HashPath))
1929  {
1930  /*
1931  * For a join path, combine clauses enforced within either input path
1932  * with those enforced as joinrestrictinfo in this path. Note that
1933  * joinrestrictinfo may include some non-pushed-down clauses, but for
1934  * current purposes it's okay if we include those in the result. (To
1935  * be more careful, we could check for clause_relids overlapping the
1936  * path parameterization, but it's not worth the cycles for now.)
1937  */
1938  JoinPath *jpath = (JoinPath *) path;
1939  Bitmapset *pserials;
1940  ListCell *lc;
1941 
1942  pserials = NULL;
1943  pserials = bms_add_members(pserials,
1945  pserials = bms_add_members(pserials,
1947  foreach(lc, jpath->joinrestrictinfo)
1948  {
1949  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1950 
1951  pserials = bms_add_member(pserials, rinfo->rinfo_serial);
1952  }
1953  return pserials;
1954  }
1955  else if (IsA(path, AppendPath))
1956  {
1957  /*
1958  * For an appendrel, take the intersection of the sets of clauses
1959  * enforced in each input path.
1960  */
1961  AppendPath *apath = (AppendPath *) path;
1962  Bitmapset *pserials;
1963  ListCell *lc;
1964 
1965  pserials = NULL;
1966  foreach(lc, apath->subpaths)
1967  {
1968  Path *subpath = (Path *) lfirst(lc);
1969  Bitmapset *subserials;
1970 
1971  subserials = get_param_path_clause_serials(subpath);
1972  if (lc == list_head(apath->subpaths))
1973  pserials = bms_copy(subserials);
1974  else
1975  pserials = bms_int_members(pserials, subserials);
1976  }
1977  return pserials;
1978  }
1979  else if (IsA(path, MergeAppendPath))
1980  {
1981  /* Same as AppendPath case */
1982  MergeAppendPath *apath = (MergeAppendPath *) path;
1983  Bitmapset *pserials;
1984  ListCell *lc;
1985 
1986  pserials = NULL;
1987  foreach(lc, apath->subpaths)
1988  {
1989  Path *subpath = (Path *) lfirst(lc);
1990  Bitmapset *subserials;
1991 
1992  subserials = get_param_path_clause_serials(subpath);
1993  if (lc == list_head(apath->subpaths))
1994  pserials = bms_copy(subserials);
1995  else
1996  pserials = bms_int_members(pserials, subserials);
1997  }
1998  return pserials;
1999  }
2000  else
2001  {
2002  /*
2003  * Otherwise, it's a baserel path and we can use the
2004  * previously-computed set of serial numbers.
2005  */
2006  return path->param_info->ppi_serials;
2007  }
2008 }
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:310
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
Bitmapset * get_param_path_clause_serials(Path *path)
Definition: relnode.c:1922
List * subpaths
Definition: pathnodes.h:1921
Path * outerjoinpath
Definition: pathnodes.h:2063
Path * innerjoinpath
Definition: pathnodes.h:2064
List * joinrestrictinfo
Definition: pathnodes.h:2066

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

2093 {
2094  PartitionScheme part_scheme = rel1->part_scheme;
2095  ListCell *lc;
2096  int cnt_pks;
2097  bool pk_has_clause[PARTITION_MAX_KEYS];
2098  bool strict_op;
2099 
2100  /*
2101  * This function must only be called when the joined relations have same
2102  * partitioning scheme.
2103  */
2104  Assert(rel1->part_scheme == rel2->part_scheme);
2105  Assert(part_scheme);
2106 
2107  memset(pk_has_clause, 0, sizeof(pk_has_clause));
2108  foreach(lc, restrictlist)
2109  {
2110  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2111  OpExpr *opexpr;
2112  Expr *expr1;
2113  Expr *expr2;
2114  int ipk1;
2115  int ipk2;
2116 
2117  /* If processing an outer join, only use its own join clauses. */
2118  if (IS_OUTER_JOIN(jointype) &&
2119  RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
2120  continue;
2121 
2122  /* Skip clauses which can not be used for a join. */
2123  if (!rinfo->can_join)
2124  continue;
2125 
2126  /* Skip clauses which are not equality conditions. */
2127  if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
2128  continue;
2129 
2130  /* Should be OK to assume it's an OpExpr. */
2131  opexpr = castNode(OpExpr, rinfo->clause);
2132 
2133  /* Match the operands to the relation. */
2134  if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
2135  bms_is_subset(rinfo->right_relids, rel2->relids))
2136  {
2137  expr1 = linitial(opexpr->args);
2138  expr2 = lsecond(opexpr->args);
2139  }
2140  else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
2141  bms_is_subset(rinfo->right_relids, rel1->relids))
2142  {
2143  expr1 = lsecond(opexpr->args);
2144  expr2 = linitial(opexpr->args);
2145  }
2146  else
2147  continue;
2148 
2149  /*
2150  * Now we need to know whether the join operator is strict; see
2151  * comments in pathnodes.h.
2152  */
2153  strict_op = op_strict(opexpr->opno);
2154 
2155  /*
2156  * Vars appearing in the relation's partition keys will not have any
2157  * varnullingrels, but those in expr1 and expr2 will if we're above
2158  * outer joins that could null the respective rels. It's okay to
2159  * match anyway, if the join operator is strict.
2160  */
2161  if (strict_op)
2162  {
2163  if (bms_overlap(rel1->relids, root->outer_join_rels))
2164  expr1 = (Expr *) remove_nulling_relids((Node *) expr1,
2165  root->outer_join_rels,
2166  NULL);
2167  if (bms_overlap(rel2->relids, root->outer_join_rels))
2168  expr2 = (Expr *) remove_nulling_relids((Node *) expr2,
2169  root->outer_join_rels,
2170  NULL);
2171  }
2172 
2173  /*
2174  * Only clauses referencing the partition keys are useful for
2175  * partitionwise join.
2176  */
2177  ipk1 = match_expr_to_partition_keys(expr1, rel1, strict_op);
2178  if (ipk1 < 0)
2179  continue;
2180  ipk2 = match_expr_to_partition_keys(expr2, rel2, strict_op);
2181  if (ipk2 < 0)
2182  continue;
2183 
2184  /*
2185  * If the clause refers to keys at different ordinal positions, it can
2186  * not be used for partitionwise join.
2187  */
2188  if (ipk1 != ipk2)
2189  continue;
2190 
2191  /*
2192  * The clause allows partitionwise join only if it uses the same
2193  * operator family as that specified by the partition key.
2194  */
2195  if (rel1->part_scheme->strategy == PARTITION_STRATEGY_HASH)
2196  {
2197  if (!OidIsValid(rinfo->hashjoinoperator) ||
2198  !op_in_opfamily(rinfo->hashjoinoperator,
2199  part_scheme->partopfamily[ipk1]))
2200  continue;
2201  }
2202  else if (!list_member_oid(rinfo->mergeopfamilies,
2203  part_scheme->partopfamily[ipk1]))
2204  continue;
2205 
2206  /* Mark the partition key as having an equi-join clause. */
2207  pk_has_clause[ipk1] = true;
2208  }
2209 
2210  /* Check whether every partition key has an equi-join condition. */
2211  for (cnt_pks = 0; cnt_pks < part_scheme->partnatts; cnt_pks++)
2212  {
2213  if (!pk_has_clause[cnt_pks])
2214  return false;
2215  }
2216 
2217  return true;
2218 }
#define OidIsValid(objectId)
Definition: c.h:775
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
bool op_strict(Oid opno)
Definition: lsyscache.c:1477
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:66
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:337
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:874
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2709
#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:2232
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
Oid opno
Definition: primnodes.h:788
List * args
Definition: primnodes.h:806
Expr * clause
Definition: pathnodes.h:2552

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, PARTITION_MAX_KEYS, PARTITION_STRATEGY_HASH, PartitionSchemeData::partnatts, PartitionSchemeData::partopfamily, RelOptInfo::relids, remove_nulling_relids(), RINFO_IS_PUSHED_DOWN, and root.

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

2233 {
2234  int cnt;
2235 
2236  /* This function should be called only for partitioned relations. */
2237  Assert(rel->part_scheme);
2238  Assert(rel->partexprs);
2239  Assert(rel->nullable_partexprs);
2240 
2241  /* Remove any relabel decorations. */
2242  while (IsA(expr, RelabelType))
2243  expr = (Expr *) (castNode(RelabelType, expr))->arg;
2244 
2245  for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
2246  {
2247  ListCell *lc;
2248 
2249  /* We can always match to the non-nullable partition keys. */
2250  foreach(lc, rel->partexprs[cnt])
2251  {
2252  if (equal(lfirst(lc), expr))
2253  return cnt;
2254  }
2255 
2256  if (!strict_op)
2257  continue;
2258 
2259  /*
2260  * If it's a strict join operator then a NULL partition key on one
2261  * side will not join to any partition key on the other side, and in
2262  * particular such a row can't join to a row from a different
2263  * partition on the other side. So, it's okay to search the nullable
2264  * partition keys as well.
2265  */
2266  foreach(lc, rel->nullable_partexprs[cnt])
2267  {
2268  if (equal(lfirst(lc), expr))
2269  return cnt;
2270  }
2271  }
2272 
2273  return -1;
2274 }
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 1034 of file relnode.c.

1038 {
1039  Relids result;
1040 
1041  /*
1042  * Basically we just need the union of the inputs' lateral_relids, less
1043  * whatever is already in the join.
1044  *
1045  * It's not immediately obvious that this is a valid way to compute the
1046  * result, because it might seem that we're ignoring possible lateral refs
1047  * of PlaceHolderVars that are due to be computed at the join but not in
1048  * either input. However, because create_lateral_join_info() already
1049  * charged all such PHV refs to each member baserel of the join, they'll
1050  * be accounted for already in the inputs' lateral_relids. Likewise, we
1051  * do not need to worry about doing transitive closure here, because that
1052  * was already accounted for in the original baserel lateral_relids.
1053  */
1054  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
1055  result = bms_del_members(result, joinrelids);
1056  return result;
1057 }

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

591 {
592  if (OidIsValid(outer_rel->serverid) &&
593  inner_rel->serverid == outer_rel->serverid)
594  {
595  if (inner_rel->userid == outer_rel->userid)
596  {
597  joinrel->serverid = outer_rel->serverid;
598  joinrel->userid = outer_rel->userid;
599  joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
600  joinrel->fdwroutine = outer_rel->fdwroutine;
601  }
602  else if (!OidIsValid(inner_rel->userid) &&
603  outer_rel->userid == GetUserId())
604  {
605  joinrel->serverid = outer_rel->serverid;
606  joinrel->userid = outer_rel->userid;
607  joinrel->useridiscurrent = true;
608  joinrel->fdwroutine = outer_rel->fdwroutine;
609  }
610  else if (!OidIsValid(outer_rel->userid) &&
611  inner_rel->userid == GetUserId())
612  {
613  joinrel->serverid = outer_rel->serverid;
614  joinrel->userid = inner_rel->userid;
615  joinrel->useridiscurrent = true;
616  joinrel->fdwroutine = outer_rel->fdwroutine;
617  }
618  }
619 }
Oid GetUserId(void)
Definition: miscinit.c:514

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

2284 {
2285  PartitionScheme part_scheme = joinrel->part_scheme;
2286  int partnatts = part_scheme->partnatts;
2287 
2288  joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts);
2289  joinrel->nullable_partexprs =
2290  (List **) palloc0(sizeof(List *) * partnatts);
2291 
2292  /*
2293  * The joinrel's partition expressions are the same as those of the input
2294  * rels, but we must properly classify them as nullable or not in the
2295  * joinrel's output. (Also, we add some more partition expressions if
2296  * it's a FULL JOIN.)
2297  */
2298  for (int cnt = 0; cnt < partnatts; cnt++)
2299  {
2300  /* mark these const to enforce that we copy them properly */
2301  const List *outer_expr = outer_rel->partexprs[cnt];
2302  const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
2303  const List *inner_expr = inner_rel->partexprs[cnt];
2304  const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
2305  List *partexpr = NIL;
2306  List *nullable_partexpr = NIL;
2307  ListCell *lc;
2308 
2309  switch (jointype)
2310  {
2311  /*
2312  * A join relation resulting from an INNER join may be
2313  * regarded as partitioned by either of the inner and outer
2314  * relation keys. For example, A INNER JOIN B ON A.a = B.b
2315  * can be regarded as partitioned on either A.a or B.b. So we
2316  * add both keys to the joinrel's partexpr lists. However,
2317  * anything that was already nullable still has to be treated
2318  * as nullable.
2319  */
2320  case JOIN_INNER:
2321  partexpr = list_concat_copy(outer_expr, inner_expr);
2322  nullable_partexpr = list_concat_copy(outer_null_expr,
2323  inner_null_expr);
2324  break;
2325 
2326  /*
2327  * A join relation resulting from a SEMI or ANTI join may be
2328  * regarded as partitioned by the outer relation keys. The
2329  * inner relation's keys are no longer interesting; since they
2330  * aren't visible in the join output, nothing could join to
2331  * them.
2332  */
2333  case JOIN_SEMI:
2334  case JOIN_ANTI:
2335  partexpr = list_copy(outer_expr);
2336  nullable_partexpr = list_copy(outer_null_expr);
2337  break;
2338 
2339  /*
2340  * A join relation resulting from a LEFT OUTER JOIN likewise
2341  * may be regarded as partitioned on the (non-nullable) outer
2342  * relation keys. The inner (nullable) relation keys are okay
2343  * as partition keys for further joins as long as they involve
2344  * strict join operators.
2345  */
2346  case JOIN_LEFT:
2347  partexpr = list_copy(outer_expr);
2348  nullable_partexpr = list_concat_copy(inner_expr,
2349  outer_null_expr);
2350  nullable_partexpr = list_concat(nullable_partexpr,
2351  inner_null_expr);
2352  break;
2353 
2354  /*
2355  * For FULL OUTER JOINs, both relations are nullable, so the
2356  * resulting join relation may be regarded as partitioned on
2357  * either of inner and outer relation keys, but only for joins
2358  * that involve strict join operators.
2359  */
2360  case JOIN_FULL:
2361  nullable_partexpr = list_concat_copy(outer_expr,
2362  inner_expr);
2363  nullable_partexpr = list_concat(nullable_partexpr,
2364  outer_null_expr);
2365  nullable_partexpr = list_concat(nullable_partexpr,
2366  inner_null_expr);
2367 
2368  /*
2369  * Also add CoalesceExprs corresponding to each possible
2370  * full-join output variable (that is, left side coalesced to
2371  * right side), so that we can match equijoin expressions
2372  * using those variables. We really only need these for
2373  * columns merged by JOIN USING, and only with the pairs of
2374  * input items that correspond to the data structures that
2375  * parse analysis would build for such variables. But it's
2376  * hard to tell which those are, so just make all the pairs.
2377  * Extra items in the nullable_partexprs list won't cause big
2378  * problems. (It's possible that such items will get matched
2379  * to user-written COALESCEs, but it should still be valid to
2380  * partition on those, since they're going to be either the
2381  * partition column or NULL; it's the same argument as for
2382  * partitionwise nesting of any outer join.) We assume no
2383  * type coercions are needed to make the coalesce expressions,
2384  * since columns of different types won't have gotten
2385  * classified as the same PartitionScheme. Note that we
2386  * intentionally leave out the varnullingrels decoration that
2387  * would ordinarily appear on the Vars inside these
2388  * CoalesceExprs, because have_partkey_equi_join will strip
2389  * varnullingrels from the expressions it will compare to the
2390  * partexprs.
2391  */
2392  foreach(lc, list_concat_copy(outer_expr, outer_null_expr))
2393  {
2394  Node *larg = (Node *) lfirst(lc);
2395  ListCell *lc2;
2396 
2397  foreach(lc2, list_concat_copy(inner_expr, inner_null_expr))
2398  {
2399  Node *rarg = (Node *) lfirst(lc2);
2401 
2402  c->coalescetype = exprType(larg);
2403  c->coalescecollid = exprCollation(larg);
2404  c->args = list_make2(larg, rarg);
2405  c->location = -1;
2406  nullable_partexpr = lappend(nullable_partexpr, c);
2407  }
2408  }
2409  break;
2410 
2411  default:
2412  elog(ERROR, "unrecognized join type: %d", (int) jointype);
2413  }
2414 
2415  joinrel->partexprs[cnt] = partexpr;
2416  joinrel->nullable_partexprs[cnt] = nullable_partexpr;
2417  }
2418 }
List * list_copy(const List *oldlist)
Definition: list.c:1573
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:816
@ JOIN_SEMI
Definition: nodes.h:307
@ JOIN_LEFT
Definition: nodes.h:294
@ JOIN_ANTI
Definition: nodes.h:308
#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 94 of file relnode.c.

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

References Assert, AppendRelInfo::child_relid, elog, ERROR, lfirst, lfirst_node, list_length(), NIL, palloc0(), root, and 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 1418 of file relnode.c.

1421 {
1422  ListCell *l;
1423 
1424  /* Expected to be called only for join between parent relations. */
1425  Assert(joinrel->reloptkind == RELOPT_JOINREL);
1426 
1427  foreach(l, joininfo_list)
1428  {
1429  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1430 
1431  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1432  {
1433  /*
1434  * This clause becomes a restriction clause for the joinrel, since
1435  * it refers to no outside rels. So we can ignore it in this
1436  * routine.
1437  */
1438  }
1439  else
1440  {
1441  /*
1442  * This clause is still a join clause at this level, so add it to
1443  * the new joininfo list, being careful to eliminate duplicates.
1444  * (Since RestrictInfo nodes in different joinlists will have been
1445  * multiply-linked rather than copied, pointer equality should be
1446  * a sufficient test.)
1447  */
1448  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1449  }
1450  }
1451 
1452  return new_joininfo;
1453 }
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1356
Relids required_relids
Definition: pathnodes.h:2583

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

1357 {
1358  ListCell *l;
1359 
1360  foreach(l, input_rel->joininfo)
1361  {
1362  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1363 
1364  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1365  {
1366  /*
1367  * This clause should become a restriction clause for the joinrel,
1368  * since it refers to no outside rels. However, if it's a clone
1369  * clause then it might be too late to evaluate it, so we have to
1370  * check. (If it is too late, just ignore the clause, taking it
1371  * on faith that another clone was or will be selected.) Clone
1372  * clauses should always be outer-join clauses, so we compare
1373  * against both_input_relids.
1374  */
1375  if (rinfo->has_clone || rinfo->is_clone)
1376  {
1377  Assert(!RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids));
1378  if (!bms_is_subset(rinfo->required_relids, both_input_relids))
1379  continue;
1380  if (bms_overlap(rinfo->incompatible_relids, both_input_relids))
1381  continue;
1382  }
1383  else
1384  {
1385  /*
1386  * For non-clone clauses, we just Assert it's OK. These might
1387  * be either join or filter clauses; if it's a join clause
1388  * then it should not refer to the current join's output.
1389  * (There is little point in checking incompatible_relids,
1390  * because it'll be NULL.)
1391  */
1392  Assert(RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids) ||
1394  both_input_relids));
1395  }
1396 
1397  /*
1398  * OK, so add it to the list, being careful to eliminate
1399  * duplicates. (Since RestrictInfo nodes in different joinlists
1400  * will have been multiply-linked rather than copied, pointer
1401  * equality should be a sufficient test.)
1402  */
1403  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1404  }
1405  else
1406  {
1407  /*
1408  * This clause is still a join clause at this level, so we ignore
1409  * it in this routine.
1410  */
1411  }
1412  }
1413 
1414  return new_restrictlist;
1415 }
Relids incompatible_relids
Definition: pathnodes.h:2586
bool has_clone
Definition: pathnodes.h:2564

References Assert, bms_is_subset(), bms_overlap(), RestrictInfo::has_clone, RestrictInfo::incompatible_relids, 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().