PostgreSQL Source Code  git master
relnode.c File Reference
#include "postgres.h"
#include <limits.h>
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/inherit.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.h"
Include dependency graph for relnode.c:

Go to the source code of this file.

Data Structures

struct  JoinHashEntry
 

Typedefs

typedef struct JoinHashEntry JoinHashEntry
 

Functions

static void build_joinrel_tlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel)
 
static Listbuild_joinrel_restrictlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static void build_joinrel_joinlist (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static Listsubbuild_joinrel_restrictlist (RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
 
static Listsubbuild_joinrel_joinlist (RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
 
static void set_foreign_rel_properties (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static void add_join_rel (PlannerInfo *root, RelOptInfo *joinrel)
 
static void build_joinrel_partition_info (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, JoinType jointype)
 
static bool have_partkey_equi_join (RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
 
static int match_expr_to_partition_keys (Expr *expr, RelOptInfo *rel, bool strict_op)
 
static void set_joinrel_partition_key_exprs (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
 
static void build_child_join_reltarget (PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
 
void setup_simple_rel_arrays (PlannerInfo *root)
 
void expand_planner_arrays (PlannerInfo *root, int add_size)
 
RelOptInfobuild_simple_rel (PlannerInfo *root, int relid, RelOptInfo *parent)
 
RelOptInfofind_base_rel (PlannerInfo *root, int relid)
 
static void build_join_rel_hash (PlannerInfo *root)
 
RelOptInfofind_join_rel (PlannerInfo *root, Relids relids)
 
RelOptInfobuild_join_rel (PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List **restrictlist_ptr)
 
RelOptInfobuild_child_join_rel (PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo, JoinType jointype)
 
Relids min_join_parameterization (PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
RelOptInfofetch_upper_rel (PlannerInfo *root, UpperRelationKind kind, Relids relids)
 
Relids find_childrel_parents (PlannerInfo *root, RelOptInfo *rel)
 
ParamPathInfoget_baserel_parampathinfo (PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
 
ParamPathInfoget_joinrel_parampathinfo (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, Relids required_outer, List **restrict_clauses)
 
ParamPathInfoget_appendrel_parampathinfo (RelOptInfo *appendrel, Relids required_outer)
 
ParamPathInfofind_param_path_info (RelOptInfo *rel, Relids required_outer)
 

Typedef Documentation

◆ JoinHashEntry

typedef struct JoinHashEntry JoinHashEntry

Function Documentation

◆ add_join_rel()

static void add_join_rel ( PlannerInfo root,
RelOptInfo joinrel 
)
static

Definition at line 539 of file relnode.c.

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

Referenced by build_child_join_rel(), and build_join_rel().

540 {
541  /* GEQO requires us to append the new joinrel to the end of the list! */
542  root->join_rel_list = lappend(root->join_rel_list, joinrel);
543 
544  /* store it into the auxiliary hashtable if there is one. */
545  if (root->join_rel_hash)
546  {
547  JoinHashEntry *hentry;
548  bool found;
549 
550  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
551  &(joinrel->relids),
552  HASH_ENTER,
553  &found);
554  Assert(!found);
555  hentry->join_rel = joinrel;
556  }
557 }
List * join_rel_list
Definition: pathnodes.h:240
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
RelOptInfo * join_rel
Definition: relnode.c:38
Relids relids
Definition: pathnodes.h:666
List * lappend(List *list, void *datum)
Definition: list.c:321
#define Assert(condition)
Definition: c.h:800
struct HTAB * join_rel_hash
Definition: pathnodes.h:241

◆ build_child_join_rel()

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

Definition at line 783 of file relnode.c.

References add_child_join_rel_equivalences(), add_join_rel(), adjust_appendrel_attrs(), RelOptInfo::all_partrels, RelOptInfo::allvisfrac, Assert, RelOptInfo::attr_needed, RelOptInfo::attr_widths, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_union(), RelOptInfo::boundinfo, 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, RelOptInfo::fdw_private, RelOptInfo::fdwroutine, 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, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, NIL, RelOptInfo::nparts, RelOptInfo::nullable_partexprs, RelOptInfo::pages, RelOptInfo::part_rels, RelOptInfo::part_scheme, RelOptInfo::partbounds_merged, RelOptInfo::partexprs, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, pfree(), RelOptInfo::ppilist, RelOptInfo::relid, RelOptInfo::relids, RELOPT_OTHER_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, RelOptInfo::rows, RTE_JOIN, RelOptInfo::rtekind, RelOptInfo::serverid, set_foreign_rel_properties(), set_joinrel_size_estimates(), QualCost::startup, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, PlannerInfo::tuple_fraction, RelOptInfo::tuples, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by try_partitionwise_join().

787 {
788  RelOptInfo *joinrel = makeNode(RelOptInfo);
789  AppendRelInfo **appinfos;
790  int nappinfos;
791 
792  /* Only joins between "other" relations land here. */
793  Assert(IS_OTHER_REL(outer_rel) && IS_OTHER_REL(inner_rel));
794 
795  /* The parent joinrel should have consider_partitionwise_join set. */
796  Assert(parent_joinrel->consider_partitionwise_join);
797 
798  joinrel->reloptkind = RELOPT_OTHER_JOINREL;
799  joinrel->relids = bms_union(outer_rel->relids, inner_rel->relids);
800  joinrel->rows = 0;
801  /* cheap startup cost is interesting iff not all tuples to be retrieved */
802  joinrel->consider_startup = (root->tuple_fraction > 0);
803  joinrel->consider_param_startup = false;
804  joinrel->consider_parallel = false;
805  joinrel->reltarget = create_empty_pathtarget();
806  joinrel->pathlist = NIL;
807  joinrel->ppilist = NIL;
808  joinrel->partial_pathlist = NIL;
809  joinrel->cheapest_startup_path = NULL;
810  joinrel->cheapest_total_path = NULL;
811  joinrel->cheapest_unique_path = NULL;
813  joinrel->direct_lateral_relids = NULL;
814  joinrel->lateral_relids = NULL;
815  joinrel->relid = 0; /* indicates not a baserel */
816  joinrel->rtekind = RTE_JOIN;
817  joinrel->min_attr = 0;
818  joinrel->max_attr = 0;
819  joinrel->attr_needed = NULL;
820  joinrel->attr_widths = NULL;
821  joinrel->lateral_vars = NIL;
822  joinrel->lateral_referencers = NULL;
823  joinrel->indexlist = NIL;
824  joinrel->pages = 0;
825  joinrel->tuples = 0;
826  joinrel->allvisfrac = 0;
827  joinrel->eclass_indexes = NULL;
828  joinrel->subroot = NULL;
829  joinrel->subplan_params = NIL;
830  joinrel->serverid = InvalidOid;
831  joinrel->userid = InvalidOid;
832  joinrel->useridiscurrent = false;
833  joinrel->fdwroutine = NULL;
834  joinrel->fdw_private = NULL;
835  joinrel->baserestrictinfo = NIL;
836  joinrel->baserestrictcost.startup = 0;
837  joinrel->baserestrictcost.per_tuple = 0;
838  joinrel->joininfo = NIL;
839  joinrel->has_eclass_joins = false;
840  joinrel->consider_partitionwise_join = false; /* might get changed later */
841  joinrel->top_parent_relids = NULL;
842  joinrel->part_scheme = NULL;
843  joinrel->nparts = -1;
844  joinrel->boundinfo = NULL;
845  joinrel->partbounds_merged = false;
846  joinrel->partition_qual = NIL;
847  joinrel->part_rels = NULL;
848  joinrel->all_partrels = NULL;
849  joinrel->partexprs = NULL;
850  joinrel->nullable_partexprs = NULL;
851 
852  joinrel->top_parent_relids = bms_union(outer_rel->top_parent_relids,
853  inner_rel->top_parent_relids);
854 
855  /* Compute information relevant to foreign relations. */
856  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
857 
858  /* Compute information needed for mapping Vars to the child rel */
859  appinfos = find_appinfos_by_relids(root, joinrel->relids, &nappinfos);
860 
861  /* Set up reltarget struct */
862  build_child_join_reltarget(root, parent_joinrel, joinrel,
863  nappinfos, appinfos);
864 
865  /* Construct joininfo list. */
866  joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
867  (Node *) parent_joinrel->joininfo,
868  nappinfos,
869  appinfos);
870 
871  /*
872  * Lateral relids referred in child join will be same as that referred in
873  * the parent relation.
874  */
875  joinrel->direct_lateral_relids = (Relids) bms_copy(parent_joinrel->direct_lateral_relids);
876  joinrel->lateral_relids = (Relids) bms_copy(parent_joinrel->lateral_relids);
877 
878  /*
879  * If the parent joinrel has pending equivalence classes, so does the
880  * child.
881  */
882  joinrel->has_eclass_joins = parent_joinrel->has_eclass_joins;
883 
884  /* Is the join between partitions itself partitioned? */
885  build_joinrel_partition_info(joinrel, outer_rel, inner_rel, restrictlist,
886  jointype);
887 
888  /* Child joinrel is parallel safe if parent is parallel safe. */
889  joinrel->consider_parallel = parent_joinrel->consider_parallel;
890 
891  /* Set estimates of the child-joinrel's size. */
892  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
893  sjinfo, restrictlist);
894 
895  /* We build the join only once. */
896  Assert(!find_join_rel(root, joinrel->relids));
897 
898  /* Add the relation to the PlannerInfo. */
899  add_join_rel(root, joinrel);
900 
901  /*
902  * We might need EquivalenceClass members corresponding to the child join,
903  * so that we can represent sort pathkeys for it. As with children of
904  * baserels, we shouldn't need this unless there are relevant eclass joins
905  * (implying that a merge join might be possible) or pathkeys to sort by.
906  */
907  if (joinrel->has_eclass_joins || has_useful_pathkeys(root, parent_joinrel))
909  nappinfos, appinfos,
910  parent_joinrel, joinrel);
911 
912  pfree(appinfos);
913 
914  return joinrel;
915 }
bool has_eclass_joins
Definition: pathnodes.h:734
struct Path * cheapest_unique_path
Definition: pathnodes.h:685
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:696
#define NIL
Definition: pg_list.h:65
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
RelOptKind reloptkind
Definition: pathnodes.h:663
Relids * attr_needed
Definition: pathnodes.h:699
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:438
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:654
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:4730
struct Path * cheapest_startup_path
Definition: pathnodes.h:683
Oid userid
Definition: pathnodes.h:716
double tuples
Definition: pathnodes.h:706
List * baserestrictinfo
Definition: pathnodes.h:728
bool consider_param_startup
Definition: pathnodes.h:673
Definition: nodes.h:527
List * partial_pathlist
Definition: pathnodes.h:682
List * cheapest_parameterized_paths
Definition: pathnodes.h:686
Relids all_partrels
Definition: pathnodes.h:753
bool useridiscurrent
Definition: pathnodes.h:717
List ** nullable_partexprs
Definition: pathnodes.h:755
Cost startup
Definition: pathnodes.h:45
double allvisfrac
Definition: pathnodes.h:707
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
Definition: equivclass.c:2521
PlannerInfo * subroot
Definition: pathnodes.h:710
bool consider_startup
Definition: pathnodes.h:672
Relids lateral_relids
Definition: pathnodes.h:691
Cost per_tuple
Definition: pathnodes.h:46
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:501
double tuple_fraction
Definition: pathnodes.h:330
void pfree(void *pointer)
Definition: mcxt.c:1057
List ** partexprs
Definition: pathnodes.h:754
struct Path * cheapest_total_path
Definition: pathnodes.h:684
List * joininfo
Definition: pathnodes.h:732
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:719
int nparts
Definition: pathnodes.h:744
Relids relids
Definition: pathnodes.h:666
bool partbounds_merged
Definition: pathnodes.h:748
List * ppilist
Definition: pathnodes.h:681
Index relid
Definition: pathnodes.h:694
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:728
Relids lateral_referencers
Definition: pathnodes.h:702
Oid serverid
Definition: pathnodes.h:715
Relids direct_lateral_relids
Definition: pathnodes.h:690
bool consider_partitionwise_join
Definition: pathnodes.h:737
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:747
RTEKind rtekind
Definition: pathnodes.h:696
List * indexlist
Definition: pathnodes.h:703
double rows
Definition: pathnodes.h:669
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: pathnodes.h:720
#define makeNode(_type_)
Definition: nodes.h:575
BlockNumber pages
Definition: pathnodes.h:705
#define Assert(condition)
Definition: c.h:800
List * lateral_vars
Definition: pathnodes.h:701
struct RelOptInfo ** part_rels
Definition: pathnodes.h:751
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool consider_parallel
Definition: pathnodes.h:674
Bitmapset * Relids
Definition: pathnodes.h:28
AttrNumber max_attr
Definition: pathnodes.h:698
static void build_joinrel_partition_info(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, JoinType jointype)
Definition: relnode.c:1627
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1910
PartitionScheme part_scheme
Definition: pathnodes.h:743
List * pathlist
Definition: pathnodes.h:680
List * partition_qual
Definition: pathnodes.h:750
int32 * attr_widths
Definition: pathnodes.h:700
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:677
QualCost baserestrictcost
Definition: pathnodes.h:729
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:539
List * subplan_params
Definition: pathnodes.h:711
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:2010
Bitmapset * eclass_indexes
Definition: pathnodes.h:708
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194
Relids top_parent_relids
Definition: pathnodes.h:739
AttrNumber min_attr
Definition: pathnodes.h:697

◆ build_child_join_reltarget()

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

Definition at line 2010 of file relnode.c.

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

Referenced by build_child_join_rel().

2015 {
2016  /* Build the targetlist */
2017  childrel->reltarget->exprs = (List *)
2019  (Node *) parentrel->reltarget->exprs,
2020  nappinfos, appinfos);
2021 
2022  /* Set the cost and width fields */
2023  childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
2024  childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
2025  childrel->reltarget->width = parentrel->reltarget->width;
2026 }
Definition: nodes.h:527
Cost startup
Definition: pathnodes.h:45
Cost per_tuple
Definition: pathnodes.h:46
List * exprs
Definition: pathnodes.h:1078
QualCost cost
Definition: pathnodes.h:1080
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:677
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194

◆ build_join_rel()

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

Definition at line 576 of file relnode.c.

References add_join_rel(), add_placeholders_to_joinrel(), RelOptInfo::all_partrels, RelOptInfo::allvisfrac, Assert, RelOptInfo::attr_needed, RelOptInfo::attr_widths, RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_del_members(), bms_is_empty(), bms_num_members(), bms_union(), RelOptInfo::boundinfo, build_joinrel_joinlist(), build_joinrel_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, RelOptInfo::fdw_private, RelOptInfo::fdwroutine, find_join_rel(), RelOptInfo::has_eclass_joins, has_relevant_eclass_joinclause(), RelOptInfo::indexlist, InvalidOid, IS_OTHER_REL, is_parallel_safe(), PlannerInfo::join_cur_level, PlannerInfo::join_rel_level, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lappend(), RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, min_join_parameterization(), NIL, RelOptInfo::non_unique_for_rels, RelOptInfo::nparts, RelOptInfo::nullable_partexprs, RelOptInfo::pages, RelOptInfo::part_rels, RelOptInfo::part_scheme, RelOptInfo::partbounds_merged, RelOptInfo::partexprs, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::ppilist, RelOptInfo::rel_parallel_workers, RelOptInfo::relid, RelOptInfo::relids, RELOPT_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, RelOptInfo::rows, RTE_JOIN, RelOptInfo::rtekind, RelOptInfo::serverid, set_foreign_rel_properties(), set_joinrel_size_estimates(), QualCost::startup, RelOptInfo::statlist, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, PlannerInfo::tuple_fraction, RelOptInfo::tuples, RelOptInfo::unique_for_rels, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by make_join_rel().

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

◆ build_join_rel_hash()

static void build_join_rel_hash ( PlannerInfo root)
static

Definition at line 396 of file relnode.c.

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

Referenced by find_join_rel().

397 {
398  HTAB *hashtab;
399  HASHCTL hash_ctl;
400  ListCell *l;
401 
402  /* Create the hash table */
403  MemSet(&hash_ctl, 0, sizeof(hash_ctl));
404  hash_ctl.keysize = sizeof(Relids);
405  hash_ctl.entrysize = sizeof(JoinHashEntry);
406  hash_ctl.hash = bitmap_hash;
407  hash_ctl.match = bitmap_match;
408  hash_ctl.hcxt = CurrentMemoryContext;
409  hashtab = hash_create("JoinRelHashTable",
410  256L,
411  &hash_ctl,
413 
414  /* Insert all the already-existing joinrels */
415  foreach(l, root->join_rel_list)
416  {
417  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
418  JoinHashEntry *hentry;
419  bool found;
420 
421  hentry = (JoinHashEntry *) hash_search(hashtab,
422  &(rel->relids),
423  HASH_ENTER,
424  &found);
425  Assert(!found);
426  hentry->join_rel = rel;
427  }
428 
429  root->join_rel_hash = hashtab;
430 }
#define HASH_CONTEXT
Definition: hsearch.h:91
#define HASH_ELEM
Definition: hsearch.h:85
MemoryContext hcxt
Definition: hsearch.h:77
Size entrysize
Definition: hsearch.h:72
#define MemSet(start, val, len)
Definition: c.h:1004
List * join_rel_list
Definition: pathnodes.h:240
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
Definition: dynahash.c:218
struct JoinHashEntry JoinHashEntry
RelOptInfo * join_rel
Definition: relnode.c:38
Relids relids
Definition: pathnodes.h:666
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: bitmapset.c:1187
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:326
Size keysize
Definition: hsearch.h:71
HashCompareFunc match
Definition: hsearch.h:74
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
#define HASH_COMPARE
Definition: hsearch.h:88
struct HTAB * join_rel_hash
Definition: pathnodes.h:241
Bitmapset * Relids
Definition: pathnodes.h:28
uint32 bitmap_hash(const void *key, Size keysize)
Definition: bitmapset.c:1177
HashValueFunc hash
Definition: hsearch.h:73
#define HASH_FUNCTION
Definition: hsearch.h:87

◆ build_joinrel_joinlist()

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

Definition at line 1085 of file relnode.c.

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

Referenced by build_join_rel().

1088 {
1089  List *result;
1090 
1091  /*
1092  * Collect all the clauses that syntactically belong above this level,
1093  * eliminating any duplicates (important since we will see many of the
1094  * same clauses arriving from both input relations).
1095  */
1096  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1097  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1098 
1099  joinrel->joininfo = result;
1100 }
#define NIL
Definition: pg_list.h:65
List * joininfo
Definition: pathnodes.h:732
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:1137
Definition: pg_list.h:50

◆ build_joinrel_partition_info()

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

Definition at line 1627 of file relnode.c.

References Assert, RelOptInfo::boundinfo, RelOptInfo::consider_partitionwise_join, enable_partitionwise_join, have_partkey_equi_join(), IS_PARTITIONED_REL, RelOptInfo::nullable_partexprs, RelOptInfo::part_rels, RelOptInfo::part_scheme, RelOptInfo::partexprs, and set_joinrel_partition_key_exprs().

Referenced by build_child_join_rel(), and build_join_rel().

1630 {
1631  PartitionScheme part_scheme;
1632 
1633  /* Nothing to do if partitionwise join technique is disabled. */
1635  {
1636  Assert(!IS_PARTITIONED_REL(joinrel));
1637  return;
1638  }
1639 
1640  /*
1641  * We can only consider this join as an input to further partitionwise
1642  * joins if (a) the input relations are partitioned and have
1643  * consider_partitionwise_join=true, (b) the partition schemes match, and
1644  * (c) we can identify an equi-join between the partition keys. Note that
1645  * if it were possible for have_partkey_equi_join to return different
1646  * answers for the same joinrel depending on which join ordering we try
1647  * first, this logic would break. That shouldn't happen, though, because
1648  * of the way the query planner deduces implied equalities and reorders
1649  * the joins. Please see optimizer/README for details.
1650  */
1651  if (outer_rel->part_scheme == NULL || inner_rel->part_scheme == NULL ||
1652  !outer_rel->consider_partitionwise_join ||
1653  !inner_rel->consider_partitionwise_join ||
1654  outer_rel->part_scheme != inner_rel->part_scheme ||
1655  !have_partkey_equi_join(joinrel, outer_rel, inner_rel,
1656  jointype, restrictlist))
1657  {
1658  Assert(!IS_PARTITIONED_REL(joinrel));
1659  return;
1660  }
1661 
1662  part_scheme = outer_rel->part_scheme;
1663 
1664  /*
1665  * This function will be called only once for each joinrel, hence it
1666  * should not have partitioning fields filled yet.
1667  */
1668  Assert(!joinrel->part_scheme && !joinrel->partexprs &&
1669  !joinrel->nullable_partexprs && !joinrel->part_rels &&
1670  !joinrel->boundinfo);
1671 
1672  /*
1673  * If the join relation is partitioned, it uses the same partitioning
1674  * scheme as the joining relations.
1675  *
1676  * Note: we calculate the partition bounds, number of partitions, and
1677  * child-join relations of the join relation in try_partitionwise_join().
1678  */
1679  joinrel->part_scheme = part_scheme;
1680  set_joinrel_partition_key_exprs(joinrel, outer_rel, inner_rel, jointype);
1681 
1682  /*
1683  * Set the consider_partitionwise_join flag.
1684  */
1685  Assert(outer_rel->consider_partitionwise_join);
1686  Assert(inner_rel->consider_partitionwise_join);
1687  joinrel->consider_partitionwise_join = true;
1688 }
static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
Definition: relnode.c:1871
List ** nullable_partexprs
Definition: pathnodes.h:755
List ** partexprs
Definition: pathnodes.h:754
bool consider_partitionwise_join
Definition: pathnodes.h:737
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:747
#define Assert(condition)
Definition: c.h:800
struct RelOptInfo ** part_rels
Definition: pathnodes.h:751
bool enable_partitionwise_join
Definition: costsize.c:145
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:766
PartitionScheme part_scheme
Definition: pathnodes.h:743
static bool have_partkey_equi_join(RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition: relnode.c:1698

◆ build_joinrel_restrictlist()

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

Definition at line 1055 of file relnode.c.

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

Referenced by build_join_rel().

1059 {
1060  List *result;
1061 
1062  /*
1063  * Collect all the clauses that syntactically belong at this level,
1064  * eliminating any duplicates (important since we will see many of the
1065  * same clauses arriving from both input relations).
1066  */
1067  result = subbuild_joinrel_restrictlist(joinrel, outer_rel->joininfo, NIL);
1068  result = subbuild_joinrel_restrictlist(joinrel, inner_rel->joininfo, result);
1069 
1070  /*
1071  * Add on any clauses derived from EquivalenceClasses. These cannot be
1072  * redundant with the clauses in the joininfo lists, so don't bother
1073  * checking.
1074  */
1075  result = list_concat(result,
1077  joinrel->relids,
1078  outer_rel->relids,
1079  inner_rel));
1080 
1081  return result;
1082 }
#define NIL
Definition: pg_list.h:65
static List * subbuild_joinrel_restrictlist(RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
Definition: relnode.c:1103
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1250
List * joininfo
Definition: pathnodes.h:732
Relids relids
Definition: pathnodes.h:666
Definition: pg_list.h:50

◆ build_joinrel_tlist()

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

Definition at line 969 of file relnode.c.

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

Referenced by build_join_rel().

971 {
972  Relids relids = joinrel->relids;
973  ListCell *vars;
974 
975  foreach(vars, input_rel->reltarget->exprs)
976  {
977  Var *var = (Var *) lfirst(vars);
978  RelOptInfo *baserel;
979  int ndx;
980 
981  /*
982  * Ignore PlaceHolderVars in the input tlists; we'll make our own
983  * decisions about whether to copy them.
984  */
985  if (IsA(var, PlaceHolderVar))
986  continue;
987 
988  /*
989  * Otherwise, anything in a baserel or joinrel targetlist ought to be
990  * a Var. (More general cases can only appear in appendrel child
991  * rels, which will never be seen here.)
992  */
993  if (!IsA(var, Var))
994  elog(ERROR, "unexpected node type in rel targetlist: %d",
995  (int) nodeTag(var));
996 
997  /* Get the Var's original base rel */
998  baserel = find_base_rel(root, var->varno);
999 
1000  /* Is it still needed above this joinrel? */
1001  ndx = var->varattno - baserel->min_attr;
1002  if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
1003  {
1004  /* Yup, add it to the output */
1005  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs, var);
1006  /* Vars have cost zero, so no need to adjust reltarget->cost */
1007  joinrel->reltarget->width += baserel->attr_widths[ndx];
1008  }
1009  }
1010 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Relids * attr_needed
Definition: pathnodes.h:699
AttrNumber varattno
Definition: primnodes.h:186
Definition: primnodes.h:181
#define ERROR
Definition: elog.h:43
Relids relids
Definition: pathnodes.h:666
List * lappend(List *list, void *datum)
Definition: list.c:321
Index varno
Definition: primnodes.h:184
List * exprs
Definition: pathnodes.h:1078
#define lfirst(lc)
Definition: pg_list.h:169
#define nodeTag(nodeptr)
Definition: nodes.h:532
#define elog(elevel,...)
Definition: elog.h:228
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373
int32 * attr_widths
Definition: pathnodes.h:700
Definition: regcomp.c:224
struct PathTarget * reltarget
Definition: pathnodes.h:677
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:545
AttrNumber min_attr
Definition: pathnodes.h:697

◆ build_simple_rel()

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

Definition at line 194 of file relnode.c.

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

Referenced by add_base_rels_to_query(), expand_appendrel_subquery(), expand_inherited_rtentry(), expand_partitioned_rtentry(), plan_cluster_use_sort(), plan_create_index_workers(), query_planner(), and recurse_set_operations().

195 {
196  RelOptInfo *rel;
197  RangeTblEntry *rte;
198 
199  /* Rel should not exist already */
200  Assert(relid > 0 && relid < root->simple_rel_array_size);
201  if (root->simple_rel_array[relid] != NULL)
202  elog(ERROR, "rel %d already exists", relid);
203 
204  /* Fetch RTE for relation */
205  rte = root->simple_rte_array[relid];
206  Assert(rte != NULL);
207 
208  rel = makeNode(RelOptInfo);
210  rel->relids = bms_make_singleton(relid);
211  rel->rows = 0;
212  /* cheap startup cost is interesting iff not all tuples to be retrieved */
213  rel->consider_startup = (root->tuple_fraction > 0);
214  rel->consider_param_startup = false; /* might get changed later */
215  rel->consider_parallel = false; /* might get changed later */
217  rel->pathlist = NIL;
218  rel->ppilist = NIL;
219  rel->partial_pathlist = NIL;
220  rel->cheapest_startup_path = NULL;
221  rel->cheapest_total_path = NULL;
222  rel->cheapest_unique_path = NULL;
224  rel->relid = relid;
225  rel->rtekind = rte->rtekind;
226  /* min_attr, max_attr, attr_needed, attr_widths are set below */
227  rel->lateral_vars = NIL;
228  rel->indexlist = NIL;
229  rel->statlist = NIL;
230  rel->pages = 0;
231  rel->tuples = 0;
232  rel->allvisfrac = 0;
233  rel->eclass_indexes = NULL;
234  rel->subroot = NULL;
235  rel->subplan_params = NIL;
236  rel->rel_parallel_workers = -1; /* set up in get_relation_info */
237  rel->serverid = InvalidOid;
238  rel->userid = rte->checkAsUser;
239  rel->useridiscurrent = false;
240  rel->fdwroutine = NULL;
241  rel->fdw_private = NULL;
242  rel->unique_for_rels = NIL;
243  rel->non_unique_for_rels = NIL;
244  rel->baserestrictinfo = NIL;
245  rel->baserestrictcost.startup = 0;
246  rel->baserestrictcost.per_tuple = 0;
247  rel->baserestrict_min_security = UINT_MAX;
248  rel->joininfo = NIL;
249  rel->has_eclass_joins = false;
250  rel->consider_partitionwise_join = false; /* might get changed later */
251  rel->part_scheme = NULL;
252  rel->nparts = -1;
253  rel->boundinfo = NULL;
254  rel->partbounds_merged = false;
255  rel->partition_qual = NIL;
256  rel->part_rels = NULL;
257  rel->all_partrels = NULL;
258  rel->partexprs = NULL;
259  rel->nullable_partexprs = NULL;
260 
261  /*
262  * Pass assorted information down the inheritance hierarchy.
263  */
264  if (parent)
265  {
266  /*
267  * Each direct or indirect child wants to know the relids of its
268  * topmost parent.
269  */
270  if (parent->top_parent_relids)
271  rel->top_parent_relids = parent->top_parent_relids;
272  else
273  rel->top_parent_relids = bms_copy(parent->relids);
274 
275  /*
276  * Also propagate lateral-reference information from appendrel parent
277  * rels to their child rels. We intentionally give each child rel the
278  * same minimum parameterization, even though it's quite possible that
279  * some don't reference all the lateral rels. This is because any
280  * append path for the parent will have to have the same
281  * parameterization for every child anyway, and there's no value in
282  * forcing extra reparameterize_path() calls. Similarly, a lateral
283  * reference to the parent prevents use of otherwise-movable join rels
284  * for each child.
285  *
286  * It's possible for child rels to have their own children, in which
287  * case the topmost parent's lateral info propagates all the way down.
288  */
290  rel->lateral_relids = parent->lateral_relids;
292  }
293  else
294  {
295  rel->top_parent_relids = NULL;
296  rel->direct_lateral_relids = NULL;
297  rel->lateral_relids = NULL;
298  rel->lateral_referencers = NULL;
299  }
300 
301  /* Check type of rtable entry */
302  switch (rte->rtekind)
303  {
304  case RTE_RELATION:
305  /* Table --- retrieve statistics from the system catalogs */
306  get_relation_info(root, rte->relid, rte->inh, rel);
307  break;
308  case RTE_SUBQUERY:
309  case RTE_FUNCTION:
310  case RTE_TABLEFUNC:
311  case RTE_VALUES:
312  case RTE_CTE:
313  case RTE_NAMEDTUPLESTORE:
314 
315  /*
316  * Subquery, function, tablefunc, values list, CTE, or ENR --- set
317  * up attr range and arrays
318  *
319  * Note: 0 is included in range to support whole-row Vars
320  */
321  rel->min_attr = 0;
322  rel->max_attr = list_length(rte->eref->colnames);
323  rel->attr_needed = (Relids *)
324  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
325  rel->attr_widths = (int32 *)
326  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
327  break;
328  case RTE_RESULT:
329  /* RTE_RESULT has no columns, nor could it have whole-row Var */
330  rel->min_attr = 0;
331  rel->max_attr = -1;
332  rel->attr_needed = NULL;
333  rel->attr_widths = NULL;
334  break;
335  default:
336  elog(ERROR, "unrecognized RTE kind: %d",
337  (int) rte->rtekind);
338  break;
339  }
340 
341  /*
342  * Copy the parent's quals to the child, with appropriate substitution of
343  * variables. If any constant false or NULL clauses turn up, we can mark
344  * the child as dummy right away. (We must do this immediately so that
345  * pruning works correctly when recursing in expand_partitioned_rtentry.)
346  */
347  if (parent)
348  {
349  AppendRelInfo *appinfo = root->append_rel_array[relid];
350 
351  Assert(appinfo != NULL);
352  if (!apply_child_basequals(root, parent, rel, rte, appinfo))
353  {
354  /*
355  * Some restriction clause reduced to constant FALSE or NULL after
356  * substitution, so this child need not be scanned.
357  */
358  mark_dummy_rel(rel);
359  }
360  }
361 
362  /* Save the finished struct in the query's simple_rel_array */
363  root->simple_rel_array[relid] = rel;
364 
365  return rel;
366 }
bool has_eclass_joins
Definition: pathnodes.h:734
struct Path * cheapest_unique_path
Definition: pathnodes.h:685
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:696
#define NIL
Definition: pg_list.h:65
List * unique_for_rels
Definition: pathnodes.h:723
List * statlist
Definition: pathnodes.h:704
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
RelOptKind reloptkind
Definition: pathnodes.h:663
Relids * attr_needed
Definition: pathnodes.h:699
List * colnames
Definition: primnodes.h:43
struct Path * cheapest_startup_path
Definition: pathnodes.h:683
Oid userid
Definition: pathnodes.h:716
double tuples
Definition: pathnodes.h:706
List * baserestrictinfo
Definition: pathnodes.h:728
bool consider_param_startup
Definition: pathnodes.h:673
List * partial_pathlist
Definition: pathnodes.h:682
List * cheapest_parameterized_paths
Definition: pathnodes.h:686
Index baserestrict_min_security
Definition: pathnodes.h:730
Relids all_partrels
Definition: pathnodes.h:753
bool useridiscurrent
Definition: pathnodes.h:717
List ** nullable_partexprs
Definition: pathnodes.h:755
Cost startup
Definition: pathnodes.h:45
double allvisfrac
Definition: pathnodes.h:707
signed int int32
Definition: c.h:417
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
PlannerInfo * subroot
Definition: pathnodes.h:710
bool consider_startup
Definition: pathnodes.h:672
bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, RangeTblEntry *childRTE, AppendRelInfo *appinfo)
Definition: inherit.c:693
Relids lateral_relids
Definition: pathnodes.h:691
Cost per_tuple
Definition: pathnodes.h:46
double tuple_fraction
Definition: pathnodes.h:330
List ** partexprs
Definition: pathnodes.h:754
#define ERROR
Definition: elog.h:43
struct Path * cheapest_total_path
Definition: pathnodes.h:684
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
List * joininfo
Definition: pathnodes.h:732
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:719
int nparts
Definition: pathnodes.h:744
Relids relids
Definition: pathnodes.h:666
bool partbounds_merged
Definition: pathnodes.h:748
List * non_unique_for_rels
Definition: pathnodes.h:725
List * ppilist
Definition: pathnodes.h:681
Index relid
Definition: pathnodes.h:694
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:205
Relids lateral_referencers
Definition: pathnodes.h:702
Oid serverid
Definition: pathnodes.h:715
Relids direct_lateral_relids
Definition: pathnodes.h:690
bool consider_partitionwise_join
Definition: pathnodes.h:737
void * palloc0(Size size)
Definition: mcxt.c:981
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1261
int rel_parallel_workers
Definition: pathnodes.h:712
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:213
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:747
RTEKind rtekind
Definition: pathnodes.h:696
List * indexlist
Definition: pathnodes.h:703
double rows
Definition: pathnodes.h:669
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: pathnodes.h:720
#define makeNode(_type_)
Definition: nodes.h:575
BlockNumber pages
Definition: pathnodes.h:705
#define Assert(condition)
Definition: c.h:800
List * lateral_vars
Definition: pathnodes.h:701
struct RelOptInfo ** part_rels
Definition: pathnodes.h:751
static int list_length(const List *l)
Definition: pg_list.h:149
bool consider_parallel
Definition: pathnodes.h:674
Bitmapset * Relids
Definition: pathnodes.h:28
RTEKind rtekind
Definition: parsenodes.h:982
AttrNumber max_attr
Definition: pathnodes.h:698
#define elog(elevel,...)
Definition: elog.h:228
PartitionScheme part_scheme
Definition: pathnodes.h:743
List * pathlist
Definition: pathnodes.h:680
Alias * eref
Definition: parsenodes.h:1121
List * partition_qual
Definition: pathnodes.h:750
int32 * attr_widths
Definition: pathnodes.h:700
struct PathTarget * reltarget
Definition: pathnodes.h:677
QualCost baserestrictcost
Definition: pathnodes.h:729
List * subplan_params
Definition: pathnodes.h:711
Bitmapset * eclass_indexes
Definition: pathnodes.h:708
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition: plancat.c:114
Relids top_parent_relids
Definition: pathnodes.h:739
AttrNumber min_attr
Definition: pathnodes.h:697

◆ expand_planner_arrays()

void expand_planner_arrays ( PlannerInfo root,
int  add_size 
)

Definition at line 152 of file relnode.c.

References add_size(), PlannerInfo::append_rel_array, Assert, MemSet, palloc0(), repalloc(), PlannerInfo::simple_rel_array, PlannerInfo::simple_rel_array_size, and PlannerInfo::simple_rte_array.

Referenced by expand_inherited_rtentry(), and expand_partitioned_rtentry().

153 {
154  int new_size;
155 
156  Assert(add_size > 0);
157 
158  new_size = root->simple_rel_array_size + add_size;
159 
160  root->simple_rel_array = (RelOptInfo **)
162  sizeof(RelOptInfo *) * new_size);
164  0, sizeof(RelOptInfo *) * add_size);
165 
166  root->simple_rte_array = (RangeTblEntry **)
168  sizeof(RangeTblEntry *) * new_size);
170  0, sizeof(RangeTblEntry *) * add_size);
171 
172  if (root->append_rel_array)
173  {
174  root->append_rel_array = (AppendRelInfo **)
176  sizeof(AppendRelInfo *) * new_size);
178  0, sizeof(AppendRelInfo *) * add_size);
179  }
180  else
181  {
182  root->append_rel_array = (AppendRelInfo **)
183  palloc0(sizeof(AppendRelInfo *) * new_size);
184  }
185 
186  root->simple_rel_array_size = new_size;
187 }
#define MemSet(start, val, len)
Definition: c.h:1004
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
int simple_rel_array_size
Definition: pathnodes.h:198
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:205
void * palloc0(Size size)
Definition: mcxt.c:981
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:213
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
#define Assert(condition)
Definition: c.h:800
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070

◆ fetch_upper_rel()

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

Definition at line 1189 of file relnode.c.

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

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

1190 {
1191  RelOptInfo *upperrel;
1192  ListCell *lc;
1193 
1194  /*
1195  * For the moment, our indexing data structure is just a List for each
1196  * relation kind. If we ever get so many of one kind that this stops
1197  * working well, we can improve it. No code outside this function should
1198  * assume anything about how to find a particular upperrel.
1199  */
1200 
1201  /* If we already made this upperrel for the query, return it */
1202  foreach(lc, root->upper_rels[kind])
1203  {
1204  upperrel = (RelOptInfo *) lfirst(lc);
1205 
1206  if (bms_equal(upperrel->relids, relids))
1207  return upperrel;
1208  }
1209 
1210  upperrel = makeNode(RelOptInfo);
1211  upperrel->reloptkind = RELOPT_UPPER_REL;
1212  upperrel->relids = bms_copy(relids);
1213 
1214  /* cheap startup cost is interesting iff not all tuples to be retrieved */
1215  upperrel->consider_startup = (root->tuple_fraction > 0);
1216  upperrel->consider_param_startup = false;
1217  upperrel->consider_parallel = false; /* might get changed later */
1218  upperrel->reltarget = create_empty_pathtarget();
1219  upperrel->pathlist = NIL;
1220  upperrel->cheapest_startup_path = NULL;
1221  upperrel->cheapest_total_path = NULL;
1222  upperrel->cheapest_unique_path = NULL;
1223  upperrel->cheapest_parameterized_paths = NIL;
1224 
1225  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1226 
1227  return upperrel;
1228 }
struct Path * cheapest_unique_path
Definition: pathnodes.h:685
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:696
#define NIL
Definition: pg_list.h:65
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
RelOptKind reloptkind
Definition: pathnodes.h:663
struct Path * cheapest_startup_path
Definition: pathnodes.h:683
bool consider_param_startup
Definition: pathnodes.h:673
List * cheapest_parameterized_paths
Definition: pathnodes.h:686
bool consider_startup
Definition: pathnodes.h:672
double tuple_fraction
Definition: pathnodes.h:330
struct Path * cheapest_total_path
Definition: pathnodes.h:684
Relids relids
Definition: pathnodes.h:666
List * lappend(List *list, void *datum)
Definition: list.c:321
#define makeNode(_type_)
Definition: nodes.h:575
#define lfirst(lc)
Definition: pg_list.h:169
bool consider_parallel
Definition: pathnodes.h:674
List * pathlist
Definition: pathnodes.h:680
struct PathTarget * reltarget
Definition: pathnodes.h:677
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
List * upper_rels[UPPERREL_FINAL+1]
Definition: pathnodes.h:305

◆ find_base_rel()

RelOptInfo* find_base_rel ( PlannerInfo root,
int  relid 
)

Definition at line 373 of file relnode.c.

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

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

374 {
375  RelOptInfo *rel;
376 
377  Assert(relid > 0);
378 
379  if (relid < root->simple_rel_array_size)
380  {
381  rel = root->simple_rel_array[relid];
382  if (rel)
383  return rel;
384  }
385 
386  elog(ERROR, "no relation entry for relid %d", relid);
387 
388  return NULL; /* keep compiler quiet */
389 }
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:800
#define elog(elevel,...)
Definition: elog.h:228

◆ find_childrel_parents()

Relids find_childrel_parents ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1240 of file relnode.c.

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

Referenced by check_index_predicates(), and generate_implied_equalities_for_column().

1241 {
1242  Relids result = NULL;
1243 
1245  Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1246 
1247  do
1248  {
1249  AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1250  Index prelid = appinfo->parent_relid;
1251 
1252  result = bms_add_member(result, prelid);
1253 
1254  /* traverse up to the parent rel, loop if it's also a child rel */
1255  rel = find_base_rel(root, prelid);
1256  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1257 
1258  Assert(rel->reloptkind == RELOPT_BASEREL);
1259 
1260  return result;
1261 }
RelOptKind reloptkind
Definition: pathnodes.h:663
int simple_rel_array_size
Definition: pathnodes.h:198
Index relid
Definition: pathnodes.h:694
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:213
unsigned int Index
Definition: c.h:537
#define Assert(condition)
Definition: c.h:800
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373
Index parent_relid
Definition: pathnodes.h:2238

◆ find_join_rel()

RelOptInfo* find_join_rel ( PlannerInfo root,
Relids  relids 
)

Definition at line 438 of file relnode.c.

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

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

439 {
440  /*
441  * Switch to using hash lookup when list grows "too long". The threshold
442  * is arbitrary and is known only here.
443  */
444  if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
445  build_join_rel_hash(root);
446 
447  /*
448  * Use either hashtable lookup or linear search, as appropriate.
449  *
450  * Note: the seemingly redundant hashkey variable is used to avoid taking
451  * the address of relids; unless the compiler is exceedingly smart, doing
452  * so would force relids out of a register and thus probably slow down the
453  * list-search case.
454  */
455  if (root->join_rel_hash)
456  {
457  Relids hashkey = relids;
458  JoinHashEntry *hentry;
459 
460  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
461  &hashkey,
462  HASH_FIND,
463  NULL);
464  if (hentry)
465  return hentry->join_rel;
466  }
467  else
468  {
469  ListCell *l;
470 
471  foreach(l, root->join_rel_list)
472  {
473  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
474 
475  if (bms_equal(rel->relids, relids))
476  return rel;
477  }
478  }
479 
480  return NULL;
481 }
List * join_rel_list
Definition: pathnodes.h:240
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
RelOptInfo * join_rel
Definition: relnode.c:38
Relids relids
Definition: pathnodes.h:666
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:396
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
struct HTAB * join_rel_hash
Definition: pathnodes.h:241
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ find_param_path_info()

ParamPathInfo* find_param_path_info ( RelOptInfo rel,
Relids  required_outer 
)

Definition at line 1605 of file relnode.c.

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

1606 {
1607  ListCell *lc;
1608 
1609  foreach(lc, rel->ppilist)
1610  {
1611  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1612 
1613  if (bms_equal(ppi->ppi_req_outer, required_outer))
1614  return ppi;
1615  }
1616 
1617  return NULL;
1618 }
List * ppilist
Definition: pathnodes.h:681
#define lfirst(lc)
Definition: pg_list.h:169
Relids ppi_req_outer
Definition: pathnodes.h:1107
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ get_appendrel_parampathinfo()

ParamPathInfo* get_appendrel_parampathinfo ( RelOptInfo appendrel,
Relids  required_outer 
)

Definition at line 1573 of file relnode.c.

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

Referenced by create_append_path(), and create_merge_append_path().

1574 {
1575  ParamPathInfo *ppi;
1576 
1577  /* If rel has LATERAL refs, every path for it should account for them */
1578  Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1579 
1580  /* Unparameterized paths have no ParamPathInfo */
1581  if (bms_is_empty(required_outer))
1582  return NULL;
1583 
1584  Assert(!bms_overlap(appendrel->relids, required_outer));
1585 
1586  /* If we already have a PPI for this parameterization, just return it */
1587  if ((ppi = find_param_path_info(appendrel, required_outer)))
1588  return ppi;
1589 
1590  /* Else build the ParamPathInfo */
1591  ppi = makeNode(ParamPathInfo);
1592  ppi->ppi_req_outer = required_outer;
1593  ppi->ppi_rows = 0;
1594  ppi->ppi_clauses = NIL;
1595  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1596 
1597  return ppi;
1598 }
#define NIL
Definition: pg_list.h:65
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1605
Relids lateral_relids
Definition: pathnodes.h:691
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:666
List * ppilist
Definition: pathnodes.h:681
List * lappend(List *list, void *datum)
Definition: list.c:321
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define makeNode(_type_)
Definition: nodes.h:575
#define Assert(condition)
Definition: c.h:800
List * ppi_clauses
Definition: pathnodes.h:1109
double ppi_rows
Definition: pathnodes.h:1108
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Relids ppi_req_outer
Definition: pathnodes.h:1107

◆ get_baserel_parampathinfo()

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

Definition at line 1276 of file relnode.c.

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

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

1278 {
1279  ParamPathInfo *ppi;
1280  Relids joinrelids;
1281  List *pclauses;
1282  double rows;
1283  ListCell *lc;
1284 
1285  /* If rel has LATERAL refs, every path for it should account for them */
1286  Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1287 
1288  /* Unparameterized paths have no ParamPathInfo */
1289  if (bms_is_empty(required_outer))
1290  return NULL;
1291 
1292  Assert(!bms_overlap(baserel->relids, required_outer));
1293 
1294  /* If we already have a PPI for this parameterization, just return it */
1295  if ((ppi = find_param_path_info(baserel, required_outer)))
1296  return ppi;
1297 
1298  /*
1299  * Identify all joinclauses that are movable to this base rel given this
1300  * parameterization.
1301  */
1302  joinrelids = bms_union(baserel->relids, required_outer);
1303  pclauses = NIL;
1304  foreach(lc, baserel->joininfo)
1305  {
1306  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1307 
1308  if (join_clause_is_movable_into(rinfo,
1309  baserel->relids,
1310  joinrelids))
1311  pclauses = lappend(pclauses, rinfo);
1312  }
1313 
1314  /*
1315  * Add in joinclauses generated by EquivalenceClasses, too. (These
1316  * necessarily satisfy join_clause_is_movable_into.)
1317  */
1318  pclauses = list_concat(pclauses,
1320  joinrelids,
1321  required_outer,
1322  baserel));
1323 
1324  /* Estimate the number of rows returned by the parameterized scan */
1325  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1326 
1327  /* And now we can build the ParamPathInfo */
1328  ppi = makeNode(ParamPathInfo);
1329  ppi->ppi_req_outer = required_outer;
1330  ppi->ppi_rows = rows;
1331  ppi->ppi_clauses = pclauses;
1332  baserel->ppilist = lappend(baserel->ppilist, ppi);
1333 
1334  return ppi;
1335 }
#define NIL
Definition: pg_list.h:65
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1605
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
Relids lateral_relids
Definition: pathnodes.h:691
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1250
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
List * joininfo
Definition: pathnodes.h:732
Relids relids
Definition: pathnodes.h:666
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:577
List * ppilist
Definition: pathnodes.h:681
List * lappend(List *list, void *datum)
Definition: list.c:321
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:4681
#define makeNode(_type_)
Definition: nodes.h:575
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
List * ppi_clauses
Definition: pathnodes.h:1109
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
double ppi_rows
Definition: pathnodes.h:1108
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Relids ppi_req_outer
Definition: pathnodes.h:1107
Definition: pg_list.h:50

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

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, RestrictInfo::left_ec, lfirst, list_concat(), makeNode, NIL, Path::param_info, Path::parent, PATH_REQ_OUTER, ParamPathInfo::ppi_clauses, ParamPathInfo::ppi_req_outer, ParamPathInfo::ppi_rows, RelOptInfo::ppilist, RelOptInfo::relids, and RestrictInfo::right_ec.

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

1372 {
1373  ParamPathInfo *ppi;
1374  Relids join_and_req;
1375  Relids outer_and_req;
1376  Relids inner_and_req;
1377  List *pclauses;
1378  List *eclauses;
1379  List *dropped_ecs;
1380  double rows;
1381  ListCell *lc;
1382 
1383  /* If rel has LATERAL refs, every path for it should account for them */
1384  Assert(bms_is_subset(joinrel->lateral_relids, required_outer));
1385 
1386  /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1387  if (bms_is_empty(required_outer))
1388  return NULL;
1389 
1390  Assert(!bms_overlap(joinrel->relids, required_outer));
1391 
1392  /*
1393  * Identify all joinclauses that are movable to this join rel given this
1394  * parameterization. These are the clauses that are movable into this
1395  * join, but not movable into either input path. Treat an unparameterized
1396  * input path as not accepting parameterized clauses (because it won't,
1397  * per the shortcut exit above), even though the joinclause movement rules
1398  * might allow the same clauses to be moved into a parameterized path for
1399  * that rel.
1400  */
1401  join_and_req = bms_union(joinrel->relids, required_outer);
1402  if (outer_path->param_info)
1403  outer_and_req = bms_union(outer_path->parent->relids,
1404  PATH_REQ_OUTER(outer_path));
1405  else
1406  outer_and_req = NULL; /* outer path does not accept parameters */
1407  if (inner_path->param_info)
1408  inner_and_req = bms_union(inner_path->parent->relids,
1409  PATH_REQ_OUTER(inner_path));
1410  else
1411  inner_and_req = NULL; /* inner path does not accept parameters */
1412 
1413  pclauses = NIL;
1414  foreach(lc, joinrel->joininfo)
1415  {
1416  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1417 
1418  if (join_clause_is_movable_into(rinfo,
1419  joinrel->relids,
1420  join_and_req) &&
1422  outer_path->parent->relids,
1423  outer_and_req) &&
1425  inner_path->parent->relids,
1426  inner_and_req))
1427  pclauses = lappend(pclauses, rinfo);
1428  }
1429 
1430  /* Consider joinclauses generated by EquivalenceClasses, too */
1431  eclauses = generate_join_implied_equalities(root,
1432  join_and_req,
1433  required_outer,
1434  joinrel);
1435  /* We only want ones that aren't movable to lower levels */
1436  dropped_ecs = NIL;
1437  foreach(lc, eclauses)
1438  {
1439  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1440 
1441  /*
1442  * In principle, join_clause_is_movable_into() should accept anything
1443  * returned by generate_join_implied_equalities(); but because its
1444  * analysis is only approximate, sometimes it doesn't. So we
1445  * currently cannot use this Assert; instead just assume it's okay to
1446  * apply the joinclause at this level.
1447  */
1448 #ifdef NOT_USED
1450  joinrel->relids,
1451  join_and_req));
1452 #endif
1453  if (join_clause_is_movable_into(rinfo,
1454  outer_path->parent->relids,
1455  outer_and_req))
1456  continue; /* drop if movable into LHS */
1457  if (join_clause_is_movable_into(rinfo,
1458  inner_path->parent->relids,
1459  inner_and_req))
1460  {
1461  /* drop if movable into RHS, but remember EC for use below */
1462  Assert(rinfo->left_ec == rinfo->right_ec);
1463  dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1464  continue;
1465  }
1466  pclauses = lappend(pclauses, rinfo);
1467  }
1468 
1469  /*
1470  * EquivalenceClasses are harder to deal with than we could wish, because
1471  * of the fact that a given EC can generate different clauses depending on
1472  * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1473  * LHS and RHS of the current join and Z is in required_outer, and further
1474  * suppose that the inner_path is parameterized by both X and Z. The code
1475  * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1476  * and in the latter case will have discarded it as being movable into the
1477  * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1478  * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1479  * not have produced both, and we can't readily tell from here which one
1480  * it did pick. If we add no clause to this join, we'll end up with
1481  * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1482  * constrained to be equal to the other members of the EC. (When we come
1483  * to join Z to this X/Y path, we will certainly drop whichever EC clause
1484  * is generated at that join, so this omission won't get fixed later.)
1485  *
1486  * To handle this, for each EC we discarded such a clause from, try to
1487  * generate a clause connecting the required_outer rels to the join's LHS
1488  * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1489  * the clause can't be moved to the LHS, add it to the current join's
1490  * restriction clauses. (If an EC cannot generate such a clause then it
1491  * has nothing that needs to be enforced here, while if the clause can be
1492  * moved into the LHS then it should have been enforced within that path.)
1493  *
1494  * Note that we don't need similar processing for ECs whose clause was
1495  * considered to be movable into the LHS, because the LHS can't refer to
1496  * the RHS so there is no comparable ambiguity about what it might
1497  * actually be enforcing internally.
1498  */
1499  if (dropped_ecs)
1500  {
1501  Relids real_outer_and_req;
1502 
1503  real_outer_and_req = bms_union(outer_path->parent->relids,
1504  required_outer);
1505  eclauses =
1507  dropped_ecs,
1508  real_outer_and_req,
1509  required_outer,
1510  outer_path->parent);
1511  foreach(lc, eclauses)
1512  {
1513  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1514 
1515  /* As above, can't quite assert this here */
1516 #ifdef NOT_USED
1518  outer_path->parent->relids,
1519  real_outer_and_req));
1520 #endif
1521  if (!join_clause_is_movable_into(rinfo,
1522  outer_path->parent->relids,
1523  outer_and_req))
1524  pclauses = lappend(pclauses, rinfo);
1525  }
1526  }
1527 
1528  /*
1529  * Now, attach the identified moved-down clauses to the caller's
1530  * restrict_clauses list. By using list_concat in this order, we leave
1531  * the original list structure of restrict_clauses undamaged.
1532  */
1533  *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1534 
1535  /* If we already have a PPI for this parameterization, just return it */
1536  if ((ppi = find_param_path_info(joinrel, required_outer)))
1537  return ppi;
1538 
1539  /* Estimate the number of rows returned by the parameterized join */
1540  rows = get_parameterized_joinrel_size(root, joinrel,
1541  outer_path,
1542  inner_path,
1543  sjinfo,
1544  *restrict_clauses);
1545 
1546  /*
1547  * And now we can build the ParamPathInfo. No point in saving the
1548  * input-pair-dependent clause list, though.
1549  *
1550  * Note: in GEQO mode, we'll be called in a temporary memory context, but
1551  * the joinrel structure is there too, so no problem.
1552  */
1553  ppi = makeNode(ParamPathInfo);
1554  ppi->ppi_req_outer = required_outer;
1555  ppi->ppi_rows = rows;
1556  ppi->ppi_clauses = NIL;
1557  joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1558 
1559  return ppi;
1560 }
#define NIL
Definition: pg_list.h:65
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1605
ParamPathInfo * param_info
Definition: pathnodes.h:1151
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
EquivalenceClass * right_ec
Definition: pathnodes.h:2043
Relids lateral_relids
Definition: pathnodes.h:691
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1250
RelOptInfo * parent
Definition: pathnodes.h:1148
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:4762
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1167
List * joininfo
Definition: pathnodes.h:732
Relids relids
Definition: pathnodes.h:666
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:577
List * ppilist
Definition: pathnodes.h:681
List * lappend(List *list, void *datum)
Definition: list.c:321
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1328
#define makeNode(_type_)
Definition: nodes.h:575
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
List * ppi_clauses
Definition: pathnodes.h:1109
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
double ppi_rows
Definition: pathnodes.h:1108
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
EquivalenceClass * left_ec
Definition: pathnodes.h:2042
Relids ppi_req_outer
Definition: pathnodes.h:1107
Definition: pg_list.h:50

◆ have_partkey_equi_join()

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

Definition at line 1698 of file relnode.c.

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

Referenced by build_joinrel_partition_info().

1701 {
1702  PartitionScheme part_scheme = rel1->part_scheme;
1703  ListCell *lc;
1704  int cnt_pks;
1705  bool pk_has_clause[PARTITION_MAX_KEYS];
1706  bool strict_op;
1707 
1708  /*
1709  * This function must only be called when the joined relations have same
1710  * partitioning scheme.
1711  */
1712  Assert(rel1->part_scheme == rel2->part_scheme);
1713  Assert(part_scheme);
1714 
1715  memset(pk_has_clause, 0, sizeof(pk_has_clause));
1716  foreach(lc, restrictlist)
1717  {
1718  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1719  OpExpr *opexpr;
1720  Expr *expr1;
1721  Expr *expr2;
1722  int ipk1;
1723  int ipk2;
1724 
1725  /* If processing an outer join, only use its own join clauses. */
1726  if (IS_OUTER_JOIN(jointype) &&
1727  RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1728  continue;
1729 
1730  /* Skip clauses which can not be used for a join. */
1731  if (!rinfo->can_join)
1732  continue;
1733 
1734  /* Skip clauses which are not equality conditions. */
1735  if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
1736  continue;
1737 
1738  /* Should be OK to assume it's an OpExpr. */
1739  opexpr = castNode(OpExpr, rinfo->clause);
1740 
1741  /* Match the operands to the relation. */
1742  if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
1743  bms_is_subset(rinfo->right_relids, rel2->relids))
1744  {
1745  expr1 = linitial(opexpr->args);
1746  expr2 = lsecond(opexpr->args);
1747  }
1748  else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
1749  bms_is_subset(rinfo->right_relids, rel1->relids))
1750  {
1751  expr1 = lsecond(opexpr->args);
1752  expr2 = linitial(opexpr->args);
1753  }
1754  else
1755  continue;
1756 
1757  /*
1758  * Now we need to know whether the join operator is strict; see
1759  * comments in pathnodes.h.
1760  */
1761  strict_op = op_strict(opexpr->opno);
1762 
1763  /*
1764  * Only clauses referencing the partition keys are useful for
1765  * partitionwise join.
1766  */
1767  ipk1 = match_expr_to_partition_keys(expr1, rel1, strict_op);
1768  if (ipk1 < 0)
1769  continue;
1770  ipk2 = match_expr_to_partition_keys(expr2, rel2, strict_op);
1771  if (ipk2 < 0)
1772  continue;
1773 
1774  /*
1775  * If the clause refers to keys at different ordinal positions, it can
1776  * not be used for partitionwise join.
1777  */
1778  if (ipk1 != ipk2)
1779  continue;
1780 
1781  /*
1782  * The clause allows partitionwise join only if it uses the same
1783  * operator family as that specified by the partition key.
1784  */
1786  {
1787  if (!OidIsValid(rinfo->hashjoinoperator) ||
1789  part_scheme->partopfamily[ipk1]))
1790  continue;
1791  }
1792  else if (!list_member_oid(rinfo->mergeopfamilies,
1793  part_scheme->partopfamily[ipk1]))
1794  continue;
1795 
1796  /* Mark the partition key as having an equi-join clause. */
1797  pk_has_clause[ipk1] = true;
1798  }
1799 
1800  /* Check whether every partition key has an equi-join condition. */
1801  for (cnt_pks = 0; cnt_pks < part_scheme->partnatts; cnt_pks++)
1802  {
1803  if (!pk_has_clause[cnt_pks])
1804  return false;
1805  }
1806 
1807  return true;
1808 }
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:64
bool op_strict(Oid opno)
Definition: lsyscache.c:1394
#define castNode(_type_, nodeptr)
Definition: nodes.h:596
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:743
#define PARTITION_MAX_KEYS
Relids left_relids
Definition: pathnodes.h:2021
#define OidIsValid(objectId)
Definition: c.h:706
List * mergeopfamilies
Definition: pathnodes.h:2039
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
#define lfirst_node(type, lc)
Definition: pg_list.h:172
static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
Definition: relnode.c:1822
Relids relids
Definition: pathnodes.h:666
Expr * clause
Definition: pathnodes.h:1994
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:802
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2071
Relids right_relids
Definition: pathnodes.h:2022
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
#define Assert(condition)
Definition: c.h:800
Oid hashjoinoperator
Definition: pathnodes.h:2052
PartitionScheme part_scheme
Definition: pathnodes.h:743
Oid opno
Definition: primnodes.h:528
List * args
Definition: primnodes.h:534

◆ match_expr_to_partition_keys()

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

Definition at line 1822 of file relnode.c.

References arg, Assert, castNode, equal(), IsA, lfirst, RelOptInfo::nullable_partexprs, RelOptInfo::part_scheme, RelOptInfo::partexprs, and PartitionSchemeData::partnatts.

Referenced by have_partkey_equi_join().

1823 {
1824  int cnt;
1825 
1826  /* This function should be called only for partitioned relations. */
1827  Assert(rel->part_scheme);
1828  Assert(rel->partexprs);
1829  Assert(rel->nullable_partexprs);
1830 
1831  /* Remove any relabel decorations. */
1832  while (IsA(expr, RelabelType))
1833  expr = (Expr *) (castNode(RelabelType, expr))->arg;
1834 
1835  for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
1836  {
1837  ListCell *lc;
1838 
1839  /* We can always match to the non-nullable partition keys. */
1840  foreach(lc, rel->partexprs[cnt])
1841  {
1842  if (equal(lfirst(lc), expr))
1843  return cnt;
1844  }
1845 
1846  if (!strict_op)
1847  continue;
1848 
1849  /*
1850  * If it's a strict join operator then a NULL partition key on one
1851  * side will not join to any partition key on the other side, and in
1852  * particular such a row can't join to a row from a different
1853  * partition on the other side. So, it's okay to search the nullable
1854  * partition keys as well.
1855  */
1856  foreach(lc, rel->nullable_partexprs[cnt])
1857  {
1858  if (equal(lfirst(lc), expr))
1859  return cnt;
1860  }
1861  }
1862 
1863  return -1;
1864 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3029
#define castNode(_type_, nodeptr)
Definition: nodes.h:596
List ** nullable_partexprs
Definition: pathnodes.h:755
List ** partexprs
Definition: pathnodes.h:754
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
void * arg
PartitionScheme part_scheme
Definition: pathnodes.h:743

◆ min_join_parameterization()

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

Definition at line 926 of file relnode.c.

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

Referenced by build_join_rel(), and join_is_legal().

930 {
931  Relids result;
932 
933  /*
934  * Basically we just need the union of the inputs' lateral_relids, less
935  * whatever is already in the join.
936  *
937  * It's not immediately obvious that this is a valid way to compute the
938  * result, because it might seem that we're ignoring possible lateral refs
939  * of PlaceHolderVars that are due to be computed at the join but not in
940  * either input. However, because create_lateral_join_info() already
941  * charged all such PHV refs to each member baserel of the join, they'll
942  * be accounted for already in the inputs' lateral_relids. Likewise, we
943  * do not need to worry about doing transitive closure here, because that
944  * was already accounted for in the original baserel lateral_relids.
945  */
946  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
947  result = bms_del_members(result, joinrelids);
948 
949  /* Maintain invariant that result is exactly NULL if empty */
950  if (bms_is_empty(result))
951  result = NULL;
952 
953  return result;
954 }
Relids lateral_relids
Definition: pathnodes.h:691
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:928

◆ set_foreign_rel_properties()

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

Definition at line 501 of file relnode.c.

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

Referenced by build_child_join_rel(), and build_join_rel().

503 {
504  if (OidIsValid(outer_rel->serverid) &&
505  inner_rel->serverid == outer_rel->serverid)
506  {
507  if (inner_rel->userid == outer_rel->userid)
508  {
509  joinrel->serverid = outer_rel->serverid;
510  joinrel->userid = outer_rel->userid;
511  joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
512  joinrel->fdwroutine = outer_rel->fdwroutine;
513  }
514  else if (!OidIsValid(inner_rel->userid) &&
515  outer_rel->userid == GetUserId())
516  {
517  joinrel->serverid = outer_rel->serverid;
518  joinrel->userid = outer_rel->userid;
519  joinrel->useridiscurrent = true;
520  joinrel->fdwroutine = outer_rel->fdwroutine;
521  }
522  else if (!OidIsValid(outer_rel->userid) &&
523  inner_rel->userid == GetUserId())
524  {
525  joinrel->serverid = outer_rel->serverid;
526  joinrel->userid = inner_rel->userid;
527  joinrel->useridiscurrent = true;
528  joinrel->fdwroutine = outer_rel->fdwroutine;
529  }
530  }
531 }
Oid GetUserId(void)
Definition: miscinit.c:476
Oid userid
Definition: pathnodes.h:716
bool useridiscurrent
Definition: pathnodes.h:717
#define OidIsValid(objectId)
Definition: c.h:706
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:719
Oid serverid
Definition: pathnodes.h:715

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

References CoalesceExpr::args, CoalesceExpr::coalescecollid, CoalesceExpr::coalescetype, 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, CoalesceExpr::location, makeNode, NIL, RelOptInfo::nullable_partexprs, palloc0(), RelOptInfo::part_scheme, RelOptInfo::partexprs, and PartitionSchemeData::partnatts.

Referenced by build_joinrel_partition_info().

1874 {
1875  PartitionScheme part_scheme = joinrel->part_scheme;
1876  int partnatts = part_scheme->partnatts;
1877 
1878  joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts);
1879  joinrel->nullable_partexprs =
1880  (List **) palloc0(sizeof(List *) * partnatts);
1881 
1882  /*
1883  * The joinrel's partition expressions are the same as those of the input
1884  * rels, but we must properly classify them as nullable or not in the
1885  * joinrel's output. (Also, we add some more partition expressions if
1886  * it's a FULL JOIN.)
1887  */
1888  for (int cnt = 0; cnt < partnatts; cnt++)
1889  {
1890  /* mark these const to enforce that we copy them properly */
1891  const List *outer_expr = outer_rel->partexprs[cnt];
1892  const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
1893  const List *inner_expr = inner_rel->partexprs[cnt];
1894  const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
1895  List *partexpr = NIL;
1896  List *nullable_partexpr = NIL;
1897  ListCell *lc;
1898 
1899  switch (jointype)
1900  {
1901  /*
1902  * A join relation resulting from an INNER join may be
1903  * regarded as partitioned by either of the inner and outer
1904  * relation keys. For example, A INNER JOIN B ON A.a = B.b
1905  * can be regarded as partitioned on either A.a or B.b. So we
1906  * add both keys to the joinrel's partexpr lists. However,
1907  * anything that was already nullable still has to be treated
1908  * as nullable.
1909  */
1910  case JOIN_INNER:
1911  partexpr = list_concat_copy(outer_expr, inner_expr);
1912  nullable_partexpr = list_concat_copy(outer_null_expr,
1913  inner_null_expr);
1914  break;
1915 
1916  /*
1917  * A join relation resulting from a SEMI or ANTI join may be
1918  * regarded as partitioned by the outer relation keys. The
1919  * inner relation's keys are no longer interesting; since they
1920  * aren't visible in the join output, nothing could join to
1921  * them.
1922  */
1923  case JOIN_SEMI:
1924  case JOIN_ANTI:
1925  partexpr = list_copy(outer_expr);
1926  nullable_partexpr = list_copy(outer_null_expr);
1927  break;
1928 
1929  /*
1930  * A join relation resulting from a LEFT OUTER JOIN likewise
1931  * may be regarded as partitioned on the (non-nullable) outer
1932  * relation keys. The inner (nullable) relation keys are okay
1933  * as partition keys for further joins as long as they involve
1934  * strict join operators.
1935  */
1936  case JOIN_LEFT:
1937  partexpr = list_copy(outer_expr);
1938  nullable_partexpr = list_concat_copy(inner_expr,
1939  outer_null_expr);
1940  nullable_partexpr = list_concat(nullable_partexpr,
1941  inner_null_expr);
1942  break;
1943 
1944  /*
1945  * For FULL OUTER JOINs, both relations are nullable, so the
1946  * resulting join relation may be regarded as partitioned on
1947  * either of inner and outer relation keys, but only for joins
1948  * that involve strict join operators.
1949  */
1950  case JOIN_FULL:
1951  nullable_partexpr = list_concat_copy(outer_expr,
1952  inner_expr);
1953  nullable_partexpr = list_concat(nullable_partexpr,
1954  outer_null_expr);
1955  nullable_partexpr = list_concat(nullable_partexpr,
1956  inner_null_expr);
1957 
1958  /*
1959  * Also add CoalesceExprs corresponding to each possible
1960  * full-join output variable (that is, left side coalesced to
1961  * right side), so that we can match equijoin expressions
1962  * using those variables. We really only need these for
1963  * columns merged by JOIN USING, and only with the pairs of
1964  * input items that correspond to the data structures that
1965  * parse analysis would build for such variables. But it's
1966  * hard to tell which those are, so just make all the pairs.
1967  * Extra items in the nullable_partexprs list won't cause big
1968  * problems. (It's possible that such items will get matched
1969  * to user-written COALESCEs, but it should still be valid to
1970  * partition on those, since they're going to be either the
1971  * partition column or NULL; it's the same argument as for
1972  * partitionwise nesting of any outer join.) We assume no
1973  * type coercions are needed to make the coalesce expressions,
1974  * since columns of different types won't have gotten
1975  * classified as the same PartitionScheme.
1976  */
1977  foreach(lc, list_concat_copy(outer_expr, outer_null_expr))
1978  {
1979  Node *larg = (Node *) lfirst(lc);
1980  ListCell *lc2;
1981 
1982  foreach(lc2, list_concat_copy(inner_expr, inner_null_expr))
1983  {
1984  Node *rarg = (Node *) lfirst(lc2);
1986 
1987  c->coalescetype = exprType(larg);
1988  c->coalescecollid = exprCollation(larg);
1989  c->args = list_make2(larg, rarg);
1990  c->location = -1;
1991  nullable_partexpr = lappend(nullable_partexpr, c);
1992  }
1993  }
1994  break;
1995 
1996  default:
1997  elog(ERROR, "unrecognized join type: %d", (int) jointype);
1998  }
1999 
2000  joinrel->partexprs[cnt] = partexpr;
2001  joinrel->nullable_partexprs[cnt] = nullable_partexpr;
2002  }
2003 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
#define NIL
Definition: pg_list.h:65
List * list_copy(const List *oldlist)
Definition: list.c:1403
Definition: nodes.h:527
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
List ** nullable_partexprs
Definition: pathnodes.h:755
List ** partexprs
Definition: pathnodes.h:754
#define ERROR
Definition: elog.h:43
List * args
Definition: primnodes.h:1101
char * c
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:552
List * lappend(List *list, void *datum)
Definition: list.c:321
void * palloc0(Size size)
Definition: mcxt.c:981
#define makeNode(_type_)
Definition: nodes.h:575
#define lfirst(lc)
Definition: pg_list.h:169
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:768
#define elog(elevel,...)
Definition: elog.h:228
Oid coalescetype
Definition: primnodes.h:1099
PartitionScheme part_scheme
Definition: pathnodes.h:743
Definition: pg_list.h:50
Oid coalescecollid
Definition: primnodes.h:1100

◆ setup_simple_rel_arrays()

void setup_simple_rel_arrays ( PlannerInfo root)

Definition at line 83 of file relnode.c.

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

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

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

◆ subbuild_joinrel_joinlist()

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

Definition at line 1137 of file relnode.c.

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

1140 {
1141  ListCell *l;
1142 
1143  /* Expected to be called only for join between parent relations. */
1144  Assert(joinrel->reloptkind == RELOPT_JOINREL);
1145 
1146  foreach(l, joininfo_list)
1147  {
1148  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1149 
1150  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1151  {
1152  /*
1153  * This clause becomes a restriction clause for the joinrel, since
1154  * it refers to no outside rels. So we can ignore it in this
1155  * routine.
1156  */
1157  }
1158  else
1159  {
1160  /*
1161  * This clause is still a join clause at this level, so add it to
1162  * the new joininfo list, being careful to eliminate duplicates.
1163  * (Since RestrictInfo nodes in different joinlists will have been
1164  * multiply-linked rather than copied, pointer equality should be
1165  * a sufficient test.)
1166  */
1167  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1168  }
1169  }
1170 
1171  return new_joininfo;
1172 }
RelOptKind reloptkind
Definition: pathnodes.h:663
Relids required_relids
Definition: pathnodes.h:2012
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1193
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:666
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169

◆ subbuild_joinrel_restrictlist()

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

Definition at line 1103 of file relnode.c.

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

Referenced by build_joinrel_restrictlist().

1106 {
1107  ListCell *l;
1108 
1109  foreach(l, joininfo_list)
1110  {
1111  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1112 
1113  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1114  {
1115  /*
1116  * This clause becomes a restriction clause for the joinrel, since
1117  * it refers to no outside rels. Add it to the list, being
1118  * careful to eliminate duplicates. (Since RestrictInfo nodes in
1119  * different joinlists will have been multiply-linked rather than
1120  * copied, pointer equality should be a sufficient test.)
1121  */
1122  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1123  }
1124  else
1125  {
1126  /*
1127  * This clause is still a join clause at this level, so we ignore
1128  * it in this routine.
1129  */
1130  }
1131  }
1132 
1133  return new_restrictlist;
1134 }
Relids required_relids
Definition: pathnodes.h:2012
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1193
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:666
#define lfirst(lc)
Definition: pg_list.h:169