PostgreSQL Source Code  git master
relnode.c File Reference
#include "postgres.h"
#include <limits.h>
#include "miscadmin.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 "partitioning/partbounds.h"
#include "utils/hsearch.h"
Include dependency graph for relnode.c:

Go to the source code of this file.

Data Structures

struct  JoinHashEntry
 

Typedefs

typedef struct JoinHashEntry JoinHashEntry
 

Functions

static void build_joinrel_tlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel)
 
static Listbuild_joinrel_restrictlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static void build_joinrel_joinlist (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static Listsubbuild_joinrel_restrictlist (RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
 
static Listsubbuild_joinrel_joinlist (RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
 
static void set_foreign_rel_properties (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static void add_join_rel (PlannerInfo *root, RelOptInfo *joinrel)
 
static void build_joinrel_partition_info (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, 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 529 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().

530 {
531  /* GEQO requires us to append the new joinrel to the end of the list! */
532  root->join_rel_list = lappend(root->join_rel_list, joinrel);
533 
534  /* store it into the auxiliary hashtable if there is one. */
535  if (root->join_rel_hash)
536  {
537  JoinHashEntry *hentry;
538  bool found;
539 
540  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
541  &(joinrel->relids),
542  HASH_ENTER,
543  &found);
544  Assert(!found);
545  hentry->join_rel = joinrel;
546  }
547 }
List * join_rel_list
Definition: pathnodes.h:246
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
RelOptInfo * join_rel
Definition: relnode.c:37
Relids relids
Definition: pathnodes.h:643
List * lappend(List *list, void *datum)
Definition: list.c:322
#define Assert(condition)
Definition: c.h:739
struct HTAB * join_rel_hash
Definition: pathnodes.h:247

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

References add_child_join_rel_equivalences(), add_join_rel(), adjust_appendrel_attrs(), 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::partexprs, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::partitioned_child_rels, 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().

776 {
777  RelOptInfo *joinrel = makeNode(RelOptInfo);
778  AppendRelInfo **appinfos;
779  int nappinfos;
780 
781  /* Only joins between "other" relations land here. */
782  Assert(IS_OTHER_REL(outer_rel) && IS_OTHER_REL(inner_rel));
783 
784  /* The parent joinrel should have consider_partitionwise_join set. */
785  Assert(parent_joinrel->consider_partitionwise_join);
786 
787  joinrel->reloptkind = RELOPT_OTHER_JOINREL;
788  joinrel->relids = bms_union(outer_rel->relids, inner_rel->relids);
789  joinrel->rows = 0;
790  /* cheap startup cost is interesting iff not all tuples to be retrieved */
791  joinrel->consider_startup = (root->tuple_fraction > 0);
792  joinrel->consider_param_startup = false;
793  joinrel->consider_parallel = false;
794  joinrel->reltarget = create_empty_pathtarget();
795  joinrel->pathlist = NIL;
796  joinrel->ppilist = NIL;
797  joinrel->partial_pathlist = NIL;
798  joinrel->cheapest_startup_path = NULL;
799  joinrel->cheapest_total_path = NULL;
800  joinrel->cheapest_unique_path = NULL;
802  joinrel->direct_lateral_relids = NULL;
803  joinrel->lateral_relids = NULL;
804  joinrel->relid = 0; /* indicates not a baserel */
805  joinrel->rtekind = RTE_JOIN;
806  joinrel->min_attr = 0;
807  joinrel->max_attr = 0;
808  joinrel->attr_needed = NULL;
809  joinrel->attr_widths = NULL;
810  joinrel->lateral_vars = NIL;
811  joinrel->lateral_referencers = NULL;
812  joinrel->indexlist = NIL;
813  joinrel->pages = 0;
814  joinrel->tuples = 0;
815  joinrel->allvisfrac = 0;
816  joinrel->eclass_indexes = NULL;
817  joinrel->subroot = NULL;
818  joinrel->subplan_params = NIL;
819  joinrel->serverid = InvalidOid;
820  joinrel->userid = InvalidOid;
821  joinrel->useridiscurrent = false;
822  joinrel->fdwroutine = NULL;
823  joinrel->fdw_private = NULL;
824  joinrel->baserestrictinfo = NIL;
825  joinrel->baserestrictcost.startup = 0;
826  joinrel->baserestrictcost.per_tuple = 0;
827  joinrel->joininfo = NIL;
828  joinrel->has_eclass_joins = false;
829  joinrel->consider_partitionwise_join = false; /* might get changed later */
830  joinrel->top_parent_relids = NULL;
831  joinrel->part_scheme = NULL;
832  joinrel->nparts = 0;
833  joinrel->boundinfo = NULL;
834  joinrel->partition_qual = NIL;
835  joinrel->part_rels = NULL;
836  joinrel->partexprs = NULL;
837  joinrel->nullable_partexprs = NULL;
838  joinrel->partitioned_child_rels = NIL;
839 
840  joinrel->top_parent_relids = bms_union(outer_rel->top_parent_relids,
841  inner_rel->top_parent_relids);
842 
843  /* Compute information relevant to foreign relations. */
844  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
845 
846  /* Compute information needed for mapping Vars to the child rel */
847  appinfos = find_appinfos_by_relids(root, joinrel->relids, &nappinfos);
848 
849  /* Set up reltarget struct */
850  build_child_join_reltarget(root, parent_joinrel, joinrel,
851  nappinfos, appinfos);
852 
853  /* Construct joininfo list. */
854  joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
855  (Node *) parent_joinrel->joininfo,
856  nappinfos,
857  appinfos);
858 
859  /*
860  * Lateral relids referred in child join will be same as that referred in
861  * the parent relation.
862  */
863  joinrel->direct_lateral_relids = (Relids) bms_copy(parent_joinrel->direct_lateral_relids);
864  joinrel->lateral_relids = (Relids) bms_copy(parent_joinrel->lateral_relids);
865 
866  /*
867  * If the parent joinrel has pending equivalence classes, so does the
868  * child.
869  */
870  joinrel->has_eclass_joins = parent_joinrel->has_eclass_joins;
871 
872  /* Is the join between partitions itself partitioned? */
873  build_joinrel_partition_info(joinrel, outer_rel, inner_rel, restrictlist,
874  jointype);
875 
876  /* Child joinrel is parallel safe if parent is parallel safe. */
877  joinrel->consider_parallel = parent_joinrel->consider_parallel;
878 
879  /* Set estimates of the child-joinrel's size. */
880  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
881  sjinfo, restrictlist);
882 
883  /* We build the join only once. */
884  Assert(!find_join_rel(root, joinrel->relids));
885 
886  /* Add the relation to the PlannerInfo. */
887  add_join_rel(root, joinrel);
888 
889  /*
890  * We might need EquivalenceClass members corresponding to the child join,
891  * so that we can represent sort pathkeys for it. As with children of
892  * baserels, we shouldn't need this unless there are relevant eclass joins
893  * (implying that a merge join might be possible) or pathkeys to sort by.
894  */
895  if (joinrel->has_eclass_joins || has_useful_pathkeys(root, parent_joinrel))
897  nappinfos, appinfos,
898  parent_joinrel, joinrel);
899 
900  pfree(appinfos);
901 
902  return joinrel;
903 }
bool has_eclass_joins
Definition: pathnodes.h:711
struct Path * cheapest_unique_path
Definition: pathnodes.h:662
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:640
Relids * attr_needed
Definition: pathnodes.h:676
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:428
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:631
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:4483
struct Path * cheapest_startup_path
Definition: pathnodes.h:660
Oid userid
Definition: pathnodes.h:693
double tuples
Definition: pathnodes.h:683
List * baserestrictinfo
Definition: pathnodes.h:705
bool consider_param_startup
Definition: pathnodes.h:650
Definition: nodes.h:525
List * partial_pathlist
Definition: pathnodes.h:659
List * cheapest_parameterized_paths
Definition: pathnodes.h:663
bool useridiscurrent
Definition: pathnodes.h:694
List ** nullable_partexprs
Definition: pathnodes.h:727
Cost startup
Definition: pathnodes.h:45
double allvisfrac
Definition: pathnodes.h:684
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
Definition: equivclass.c:2356
PlannerInfo * subroot
Definition: pathnodes.h:687
bool consider_startup
Definition: pathnodes.h:649
Relids lateral_relids
Definition: pathnodes.h:668
Cost per_tuple
Definition: pathnodes.h:46
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:491
double tuple_fraction
Definition: pathnodes.h:336
void pfree(void *pointer)
Definition: mcxt.c:1056
List ** partexprs
Definition: pathnodes.h:726
struct Path * cheapest_total_path
Definition: pathnodes.h:661
List * joininfo
Definition: pathnodes.h:709
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:696
int nparts
Definition: pathnodes.h:721
Relids relids
Definition: pathnodes.h:643
List * ppilist
Definition: pathnodes.h:658
Index relid
Definition: pathnodes.h:671
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:723
Relids lateral_referencers
Definition: pathnodes.h:679
Oid serverid
Definition: pathnodes.h:692
Relids direct_lateral_relids
Definition: pathnodes.h:667
bool consider_partitionwise_join
Definition: pathnodes.h:714
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:722
RTEKind rtekind
Definition: pathnodes.h:673
List * indexlist
Definition: pathnodes.h:680
double rows
Definition: pathnodes.h:646
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: pathnodes.h:697
#define makeNode(_type_)
Definition: nodes.h:573
BlockNumber pages
Definition: pathnodes.h:682
#define Assert(condition)
Definition: c.h:739
List * lateral_vars
Definition: pathnodes.h:678
struct RelOptInfo ** part_rels
Definition: pathnodes.h:724
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool consider_parallel
Definition: pathnodes.h:651
Bitmapset * Relids
Definition: pathnodes.h:28
List * partitioned_child_rels
Definition: pathnodes.h:728
AttrNumber max_attr
Definition: pathnodes.h:675
static void build_joinrel_partition_info(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, JoinType jointype)
Definition: relnode.c:1616
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1856
PartitionScheme part_scheme
Definition: pathnodes.h:720
List * pathlist
Definition: pathnodes.h:657
List * partition_qual
Definition: pathnodes.h:723
int32 * attr_widths
Definition: pathnodes.h:677
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:654
QualCost baserestrictcost
Definition: pathnodes.h:706
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:529
List * subplan_params
Definition: pathnodes.h:688
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:1785
Bitmapset * eclass_indexes
Definition: pathnodes.h:685
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194
Relids top_parent_relids
Definition: pathnodes.h:716
AttrNumber min_attr
Definition: pathnodes.h:674

◆ build_child_join_reltarget()

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

Definition at line 1785 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().

1790 {
1791  /* Build the targetlist */
1792  childrel->reltarget->exprs = (List *)
1794  (Node *) parentrel->reltarget->exprs,
1795  nappinfos, appinfos);
1796 
1797  /* Set the cost and width fields */
1798  childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
1799  childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
1800  childrel->reltarget->width = parentrel->reltarget->width;
1801 }
Definition: nodes.h:525
Cost startup
Definition: pathnodes.h:45
Cost per_tuple
Definition: pathnodes.h:46
List * exprs
Definition: pathnodes.h:1046
QualCost cost
Definition: pathnodes.h:1048
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:654
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 566 of file relnode.c.

References add_join_rel(), add_placeholders_to_joinrel(), RelOptInfo::allvisfrac, Assert, RelOptInfo::attr_needed, RelOptInfo::attr_widths, RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_del_members(), bms_is_empty(), bms_num_members(), bms_union(), RelOptInfo::boundinfo, build_joinrel_joinlist(), build_joinrel_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::partexprs, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::partitioned_child_rels, 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().

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

◆ build_join_rel_hash()

static void build_join_rel_hash ( PlannerInfo root)
static

Definition at line 386 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().

387 {
388  HTAB *hashtab;
389  HASHCTL hash_ctl;
390  ListCell *l;
391 
392  /* Create the hash table */
393  MemSet(&hash_ctl, 0, sizeof(hash_ctl));
394  hash_ctl.keysize = sizeof(Relids);
395  hash_ctl.entrysize = sizeof(JoinHashEntry);
396  hash_ctl.hash = bitmap_hash;
397  hash_ctl.match = bitmap_match;
398  hash_ctl.hcxt = CurrentMemoryContext;
399  hashtab = hash_create("JoinRelHashTable",
400  256L,
401  &hash_ctl,
403 
404  /* Insert all the already-existing joinrels */
405  foreach(l, root->join_rel_list)
406  {
407  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
408  JoinHashEntry *hentry;
409  bool found;
410 
411  hentry = (JoinHashEntry *) hash_search(hashtab,
412  &(rel->relids),
413  HASH_ENTER,
414  &found);
415  Assert(!found);
416  hentry->join_rel = rel;
417  }
418 
419  root->join_rel_hash = hashtab;
420 }
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:962
List * join_rel_list
Definition: pathnodes.h:246
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
Definition: dynahash.c:208
struct JoinHashEntry JoinHashEntry
uint32 bitmap_hash(const void *key, Size keysize)
Definition: hashfn.c:705
RelOptInfo * join_rel
Definition: relnode.c:37
Relids relids
Definition: pathnodes.h:643
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: hashfn.c:715
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size keysize
Definition: hsearch.h:72
HashCompareFunc match
Definition: hsearch.h:75
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
#define HASH_COMPARE
Definition: hsearch.h:90
struct HTAB * join_rel_hash
Definition: pathnodes.h:247
Bitmapset * Relids
Definition: pathnodes.h:28
HashValueFunc hash
Definition: hsearch.h:74
#define HASH_FUNCTION
Definition: hsearch.h:89

◆ build_joinrel_joinlist()

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

Definition at line 1073 of file relnode.c.

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

Referenced by build_join_rel().

1076 {
1077  List *result;
1078 
1079  /*
1080  * Collect all the clauses that syntactically belong above this level,
1081  * eliminating any duplicates (important since we will see many of the
1082  * same clauses arriving from both input relations).
1083  */
1084  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1085  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1086 
1087  joinrel->joininfo = result;
1088 }
#define NIL
Definition: pg_list.h:65
List * joininfo
Definition: pathnodes.h:709
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:1125
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 1616 of file relnode.c.

References Assert, RelOptInfo::boundinfo, RelOptInfo::consider_partitionwise_join, elog, enable_partitionwise_join, ERROR, have_partkey_equi_join(), IS_PARTITIONED_REL, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, list_concat(), list_concat_copy(), list_copy(), NIL, RelOptInfo::nparts, RelOptInfo::nullable_partexprs, palloc0(), RelOptInfo::part_rels, RelOptInfo::part_scheme, RelOptInfo::partexprs, partition_bounds_equal(), PartitionSchemeData::partnatts, PartitionSchemeData::parttypbyval, PartitionSchemeData::parttyplen, and REL_HAS_ALL_PART_PROPS.

Referenced by build_child_join_rel(), and build_join_rel().

1619 {
1620  int partnatts;
1621  int cnt;
1622  PartitionScheme part_scheme;
1623 
1624  /* Nothing to do if partitionwise join technique is disabled. */
1626  {
1627  Assert(!IS_PARTITIONED_REL(joinrel));
1628  return;
1629  }
1630 
1631  /*
1632  * We can only consider this join as an input to further partitionwise
1633  * joins if (a) the input relations are partitioned and have
1634  * consider_partitionwise_join=true, (b) the partition schemes match, and
1635  * (c) we can identify an equi-join between the partition keys. Note that
1636  * if it were possible for have_partkey_equi_join to return different
1637  * answers for the same joinrel depending on which join ordering we try
1638  * first, this logic would break. That shouldn't happen, though, because
1639  * of the way the query planner deduces implied equalities and reorders
1640  * the joins. Please see optimizer/README for details.
1641  */
1642  if (!IS_PARTITIONED_REL(outer_rel) || !IS_PARTITIONED_REL(inner_rel) ||
1643  !outer_rel->consider_partitionwise_join ||
1644  !inner_rel->consider_partitionwise_join ||
1645  outer_rel->part_scheme != inner_rel->part_scheme ||
1646  !have_partkey_equi_join(joinrel, outer_rel, inner_rel,
1647  jointype, restrictlist))
1648  {
1649  Assert(!IS_PARTITIONED_REL(joinrel));
1650  return;
1651  }
1652 
1653  part_scheme = outer_rel->part_scheme;
1654 
1655  Assert(REL_HAS_ALL_PART_PROPS(outer_rel) &&
1656  REL_HAS_ALL_PART_PROPS(inner_rel));
1657 
1658  /*
1659  * For now, our partition matching algorithm can match partitions only
1660  * when the partition bounds of the joining relations are exactly same.
1661  * So, bail out otherwise.
1662  */
1663  if (outer_rel->nparts != inner_rel->nparts ||
1664  !partition_bounds_equal(part_scheme->partnatts,
1665  part_scheme->parttyplen,
1666  part_scheme->parttypbyval,
1667  outer_rel->boundinfo, inner_rel->boundinfo))
1668  {
1669  Assert(!IS_PARTITIONED_REL(joinrel));
1670  return;
1671  }
1672 
1673  /*
1674  * This function will be called only once for each joinrel, hence it
1675  * should not have partition scheme, partition bounds, partition key
1676  * expressions and array for storing child relations set.
1677  */
1678  Assert(!joinrel->part_scheme && !joinrel->partexprs &&
1679  !joinrel->nullable_partexprs && !joinrel->part_rels &&
1680  !joinrel->boundinfo);
1681 
1682  /*
1683  * Join relation is partitioned using the same partitioning scheme as the
1684  * joining relations and has same bounds.
1685  */
1686  joinrel->part_scheme = part_scheme;
1687  joinrel->boundinfo = outer_rel->boundinfo;
1688  partnatts = joinrel->part_scheme->partnatts;
1689  joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts);
1690  joinrel->nullable_partexprs =
1691  (List **) palloc0(sizeof(List *) * partnatts);
1692  joinrel->nparts = outer_rel->nparts;
1693  joinrel->part_rels =
1694  (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * joinrel->nparts);
1695 
1696  /*
1697  * Set the consider_partitionwise_join flag.
1698  */
1699  Assert(outer_rel->consider_partitionwise_join);
1700  Assert(inner_rel->consider_partitionwise_join);
1701  joinrel->consider_partitionwise_join = true;
1702 
1703  /*
1704  * Construct partition keys for the join.
1705  *
1706  * An INNER join between two partitioned relations can be regarded as
1707  * partitioned by either key expression. For example, A INNER JOIN B ON
1708  * A.a = B.b can be regarded as partitioned on A.a or on B.b; they are
1709  * equivalent.
1710  *
1711  * For a SEMI or ANTI join, the result can only be regarded as being
1712  * partitioned in the same manner as the outer side, since the inner
1713  * columns are not retained.
1714  *
1715  * An OUTER join like (A LEFT JOIN B ON A.a = B.b) may produce rows with
1716  * B.b NULL. These rows may not fit the partitioning conditions imposed on
1717  * B.b. Hence, strictly speaking, the join is not partitioned by B.b and
1718  * thus partition keys of an OUTER join should include partition key
1719  * expressions from the OUTER side only. However, because all
1720  * commonly-used comparison operators are strict, the presence of nulls on
1721  * the outer side doesn't cause any problem; they can't match anything at
1722  * future join levels anyway. Therefore, we track two sets of
1723  * expressions: those that authentically partition the relation
1724  * (partexprs) and those that partition the relation with the exception
1725  * that extra nulls may be present (nullable_partexprs). When the
1726  * comparison operator is strict, the latter is just as good as the
1727  * former.
1728  */
1729  for (cnt = 0; cnt < partnatts; cnt++)
1730  {
1731  /* mark these const to enforce that we copy them properly */
1732  const List *outer_expr = outer_rel->partexprs[cnt];
1733  const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
1734  const List *inner_expr = inner_rel->partexprs[cnt];
1735  const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
1736  List *partexpr = NIL;
1737  List *nullable_partexpr = NIL;
1738 
1739  switch (jointype)
1740  {
1741  case JOIN_INNER:
1742  partexpr = list_concat_copy(outer_expr, inner_expr);
1743  nullable_partexpr = list_concat_copy(outer_null_expr,
1744  inner_null_expr);
1745  break;
1746 
1747  case JOIN_SEMI:
1748  case JOIN_ANTI:
1749  partexpr = list_copy(outer_expr);
1750  nullable_partexpr = list_copy(outer_null_expr);
1751  break;
1752 
1753  case JOIN_LEFT:
1754  partexpr = list_copy(outer_expr);
1755  nullable_partexpr = list_concat_copy(inner_expr,
1756  outer_null_expr);
1757  nullable_partexpr = list_concat(nullable_partexpr,
1758  inner_null_expr);
1759  break;
1760 
1761  case JOIN_FULL:
1762  nullable_partexpr = list_concat_copy(outer_expr,
1763  inner_expr);
1764  nullable_partexpr = list_concat(nullable_partexpr,
1765  outer_null_expr);
1766  nullable_partexpr = list_concat(nullable_partexpr,
1767  inner_null_expr);
1768  break;
1769 
1770  default:
1771  elog(ERROR, "unrecognized join type: %d", (int) jointype);
1772 
1773  }
1774 
1775  joinrel->partexprs[cnt] = partexpr;
1776  joinrel->nullable_partexprs[cnt] = nullable_partexpr;
1777  }
1778 }
#define NIL
Definition: pg_list.h:65
List * list_copy(const List *oldlist)
Definition: list.c:1404
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
List ** nullable_partexprs
Definition: pathnodes.h:727
List ** partexprs
Definition: pathnodes.h:726
#define ERROR
Definition: elog.h:43
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:553
int nparts
Definition: pathnodes.h:721
bool consider_partitionwise_join
Definition: pathnodes.h:714
void * palloc0(Size size)
Definition: mcxt.c:980
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:722
#define Assert(condition)
Definition: c.h:739
struct RelOptInfo ** part_rels
Definition: pathnodes.h:724
bool enable_partitionwise_join
Definition: costsize.c:136
bool have_partkey_equi_join(RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition: joinrels.c:1582
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partbounds.c:667
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:739
#define REL_HAS_ALL_PART_PROPS(rel)
Definition: pathnodes.h:747
#define elog(elevel,...)
Definition: elog.h:228
PartitionScheme part_scheme
Definition: pathnodes.h:720
Definition: pg_list.h:50

◆ build_joinrel_restrictlist()

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

Definition at line 1043 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().

1047 {
1048  List *result;
1049 
1050  /*
1051  * Collect all the clauses that syntactically belong at this level,
1052  * eliminating any duplicates (important since we will see many of the
1053  * same clauses arriving from both input relations).
1054  */
1055  result = subbuild_joinrel_restrictlist(joinrel, outer_rel->joininfo, NIL);
1056  result = subbuild_joinrel_restrictlist(joinrel, inner_rel->joininfo, result);
1057 
1058  /*
1059  * Add on any clauses derived from EquivalenceClasses. These cannot be
1060  * redundant with the clauses in the joininfo lists, so don't bother
1061  * checking.
1062  */
1063  result = list_concat(result,
1065  joinrel->relids,
1066  outer_rel->relids,
1067  inner_rel));
1068 
1069  return result;
1070 }
#define NIL
Definition: pg_list.h:65
static List * subbuild_joinrel_restrictlist(RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
Definition: relnode.c:1091
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1126
List * joininfo
Definition: pathnodes.h:709
Relids relids
Definition: pathnodes.h:643
Definition: pg_list.h:50

◆ build_joinrel_tlist()

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

Definition at line 957 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().

959 {
960  Relids relids = joinrel->relids;
961  ListCell *vars;
962 
963  foreach(vars, input_rel->reltarget->exprs)
964  {
965  Var *var = (Var *) lfirst(vars);
966  RelOptInfo *baserel;
967  int ndx;
968 
969  /*
970  * Ignore PlaceHolderVars in the input tlists; we'll make our own
971  * decisions about whether to copy them.
972  */
973  if (IsA(var, PlaceHolderVar))
974  continue;
975 
976  /*
977  * Otherwise, anything in a baserel or joinrel targetlist ought to be
978  * a Var. (More general cases can only appear in appendrel child
979  * rels, which will never be seen here.)
980  */
981  if (!IsA(var, Var))
982  elog(ERROR, "unexpected node type in rel targetlist: %d",
983  (int) nodeTag(var));
984 
985  /* Get the Var's original base rel */
986  baserel = find_base_rel(root, var->varno);
987 
988  /* Is it still needed above this joinrel? */
989  ndx = var->varattno - baserel->min_attr;
990  if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
991  {
992  /* Yup, add it to the output */
993  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs, var);
994  /* Vars have cost zero, so no need to adjust reltarget->cost */
995  joinrel->reltarget->width += baserel->attr_widths[ndx];
996  }
997  }
998 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Relids * attr_needed
Definition: pathnodes.h:676
AttrNumber varattno
Definition: primnodes.h:172
Definition: primnodes.h:167
#define ERROR
Definition: elog.h:43
Relids relids
Definition: pathnodes.h:643
List * lappend(List *list, void *datum)
Definition: list.c:322
Index varno
Definition: primnodes.h:170
List * exprs
Definition: pathnodes.h:1046
#define lfirst(lc)
Definition: pg_list.h:190
#define nodeTag(nodeptr)
Definition: nodes.h:530
#define elog(elevel,...)
Definition: elog.h:228
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:363
int32 * attr_widths
Definition: pathnodes.h:677
Definition: regcomp.c:224
struct PathTarget * reltarget
Definition: pathnodes.h:654
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:545
AttrNumber min_attr
Definition: pathnodes.h:674

◆ build_simple_rel()

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

Definition at line 185 of file relnode.c.

References 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::partexprs, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::partitioned_child_rels, 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().

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

◆ expand_planner_arrays()

void expand_planner_arrays ( PlannerInfo root,
int  add_size 
)

Definition at line 143 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().

144 {
145  int new_size;
146 
147  Assert(add_size > 0);
148 
149  new_size = root->simple_rel_array_size + add_size;
150 
151  root->simple_rel_array = (RelOptInfo **)
153  sizeof(RelOptInfo *) * new_size);
155  0, sizeof(RelOptInfo *) * add_size);
156 
157  root->simple_rte_array = (RangeTblEntry **)
159  sizeof(RangeTblEntry *) * new_size);
161  0, sizeof(RangeTblEntry *) * add_size);
162 
163  if (root->append_rel_array)
164  {
165  root->append_rel_array = (AppendRelInfo **)
167  sizeof(AppendRelInfo *) * new_size);
169  0, sizeof(AppendRelInfo *) * add_size);
170  }
171  else
172  {
173  root->append_rel_array = (AppendRelInfo **)
174  palloc0(sizeof(AppendRelInfo *) * new_size);
175  }
176 
177  root->simple_rel_array_size = new_size;
178 }
#define MemSet(start, val, len)
Definition: c.h:962
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:203
int simple_rel_array_size
Definition: pathnodes.h:204
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:211
void * palloc0(Size size)
Definition: mcxt.c:980
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:219
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define Assert(condition)
Definition: c.h:739
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069

◆ fetch_upper_rel()

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

Definition at line 1177 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().

1178 {
1179  RelOptInfo *upperrel;
1180  ListCell *lc;
1181 
1182  /*
1183  * For the moment, our indexing data structure is just a List for each
1184  * relation kind. If we ever get so many of one kind that this stops
1185  * working well, we can improve it. No code outside this function should
1186  * assume anything about how to find a particular upperrel.
1187  */
1188 
1189  /* If we already made this upperrel for the query, return it */
1190  foreach(lc, root->upper_rels[kind])
1191  {
1192  upperrel = (RelOptInfo *) lfirst(lc);
1193 
1194  if (bms_equal(upperrel->relids, relids))
1195  return upperrel;
1196  }
1197 
1198  upperrel = makeNode(RelOptInfo);
1199  upperrel->reloptkind = RELOPT_UPPER_REL;
1200  upperrel->relids = bms_copy(relids);
1201 
1202  /* cheap startup cost is interesting iff not all tuples to be retrieved */
1203  upperrel->consider_startup = (root->tuple_fraction > 0);
1204  upperrel->consider_param_startup = false;
1205  upperrel->consider_parallel = false; /* might get changed later */
1206  upperrel->reltarget = create_empty_pathtarget();
1207  upperrel->pathlist = NIL;
1208  upperrel->cheapest_startup_path = NULL;
1209  upperrel->cheapest_total_path = NULL;
1210  upperrel->cheapest_unique_path = NULL;
1211  upperrel->cheapest_parameterized_paths = NIL;
1212 
1213  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1214 
1215  return upperrel;
1216 }
struct Path * cheapest_unique_path
Definition: pathnodes.h:662
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:640
struct Path * cheapest_startup_path
Definition: pathnodes.h:660
bool consider_param_startup
Definition: pathnodes.h:650
List * cheapest_parameterized_paths
Definition: pathnodes.h:663
bool consider_startup
Definition: pathnodes.h:649
double tuple_fraction
Definition: pathnodes.h:336
struct Path * cheapest_total_path
Definition: pathnodes.h:661
Relids relids
Definition: pathnodes.h:643
List * lappend(List *list, void *datum)
Definition: list.c:322
#define makeNode(_type_)
Definition: nodes.h:573
#define lfirst(lc)
Definition: pg_list.h:190
bool consider_parallel
Definition: pathnodes.h:651
List * pathlist
Definition: pathnodes.h:657
struct PathTarget * reltarget
Definition: pathnodes.h:654
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
List * upper_rels[UPPERREL_FINAL+1]
Definition: pathnodes.h:311

◆ find_base_rel()

RelOptInfo* find_base_rel ( PlannerInfo root,
int  relid 
)

Definition at line 363 of file relnode.c.

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

Referenced by add_join_clause_to_rels(), add_placeholders_to_base_rels(), add_vars_to_targetlist(), build_joinrel_tlist(), clause_selectivity(), create_lateral_join_info(), distribute_restrictinfo_to_rels(), examine_simple_variable(), examine_variable(), finalize_plan(), find_childrel_parents(), find_join_input_rel(), find_single_rel_for_clauses(), get_foreign_key_join_selectivity(), join_is_removable(), make_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().

364 {
365  RelOptInfo *rel;
366 
367  Assert(relid > 0);
368 
369  if (relid < root->simple_rel_array_size)
370  {
371  rel = root->simple_rel_array[relid];
372  if (rel)
373  return rel;
374  }
375 
376  elog(ERROR, "no relation entry for relid %d", relid);
377 
378  return NULL; /* keep compiler quiet */
379 }
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:203
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:739
#define elog(elevel,...)
Definition: elog.h:228

◆ find_childrel_parents()

Relids find_childrel_parents ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1228 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().

1229 {
1230  Relids result = NULL;
1231 
1233  Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1234 
1235  do
1236  {
1237  AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1238  Index prelid = appinfo->parent_relid;
1239 
1240  result = bms_add_member(result, prelid);
1241 
1242  /* traverse up to the parent rel, loop if it's also a child rel */
1243  rel = find_base_rel(root, prelid);
1244  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1245 
1246  Assert(rel->reloptkind == RELOPT_BASEREL);
1247 
1248  return result;
1249 }
RelOptKind reloptkind
Definition: pathnodes.h:640
int simple_rel_array_size
Definition: pathnodes.h:204
Index relid
Definition: pathnodes.h:671
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:219
unsigned int Index
Definition: c.h:476
#define Assert(condition)
Definition: c.h:739
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:363
Index parent_relid
Definition: pathnodes.h:2189

◆ find_join_rel()

RelOptInfo* find_join_rel ( PlannerInfo root,
Relids  relids 
)

Definition at line 428 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(), and postgresPlanDirectModify().

429 {
430  /*
431  * Switch to using hash lookup when list grows "too long". The threshold
432  * is arbitrary and is known only here.
433  */
434  if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
435  build_join_rel_hash(root);
436 
437  /*
438  * Use either hashtable lookup or linear search, as appropriate.
439  *
440  * Note: the seemingly redundant hashkey variable is used to avoid taking
441  * the address of relids; unless the compiler is exceedingly smart, doing
442  * so would force relids out of a register and thus probably slow down the
443  * list-search case.
444  */
445  if (root->join_rel_hash)
446  {
447  Relids hashkey = relids;
448  JoinHashEntry *hentry;
449 
450  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
451  &hashkey,
452  HASH_FIND,
453  NULL);
454  if (hentry)
455  return hentry->join_rel;
456  }
457  else
458  {
459  ListCell *l;
460 
461  foreach(l, root->join_rel_list)
462  {
463  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
464 
465  if (bms_equal(rel->relids, relids))
466  return rel;
467  }
468  }
469 
470  return NULL;
471 }
List * join_rel_list
Definition: pathnodes.h:246
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
RelOptInfo * join_rel
Definition: relnode.c:37
Relids relids
Definition: pathnodes.h:643
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:386
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
struct HTAB * join_rel_hash
Definition: pathnodes.h:247
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 1593 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().

1594 {
1595  ListCell *lc;
1596 
1597  foreach(lc, rel->ppilist)
1598  {
1599  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1600 
1601  if (bms_equal(ppi->ppi_req_outer, required_outer))
1602  return ppi;
1603  }
1604 
1605  return NULL;
1606 }
List * ppilist
Definition: pathnodes.h:658
#define lfirst(lc)
Definition: pg_list.h:190
Relids ppi_req_outer
Definition: pathnodes.h:1075
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 1561 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().

1562 {
1563  ParamPathInfo *ppi;
1564 
1565  /* If rel has LATERAL refs, every path for it should account for them */
1566  Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1567 
1568  /* Unparameterized paths have no ParamPathInfo */
1569  if (bms_is_empty(required_outer))
1570  return NULL;
1571 
1572  Assert(!bms_overlap(appendrel->relids, required_outer));
1573 
1574  /* If we already have a PPI for this parameterization, just return it */
1575  if ((ppi = find_param_path_info(appendrel, required_outer)))
1576  return ppi;
1577 
1578  /* Else build the ParamPathInfo */
1579  ppi = makeNode(ParamPathInfo);
1580  ppi->ppi_req_outer = required_outer;
1581  ppi->ppi_rows = 0;
1582  ppi->ppi_clauses = NIL;
1583  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1584 
1585  return ppi;
1586 }
#define NIL
Definition: pg_list.h:65
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1593
Relids lateral_relids
Definition: pathnodes.h:668
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:643
List * ppilist
Definition: pathnodes.h:658
List * lappend(List *list, void *datum)
Definition: list.c:322
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
List * ppi_clauses
Definition: pathnodes.h:1077
double ppi_rows
Definition: pathnodes.h:1076
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Relids ppi_req_outer
Definition: pathnodes.h:1075

◆ get_baserel_parampathinfo()

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

Definition at line 1264 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 bitmap_and_cost_est(), bitmap_scan_cost_est(), create_append_path(), create_bitmap_heap_path(), create_ctescan_path(), create_foreignscan_path(), create_functionscan_path(), create_gather_merge_path(), create_gather_path(), create_index_path(), create_namedtuplestorescan_path(), create_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().

1266 {
1267  ParamPathInfo *ppi;
1268  Relids joinrelids;
1269  List *pclauses;
1270  double rows;
1271  ListCell *lc;
1272 
1273  /* If rel has LATERAL refs, every path for it should account for them */
1274  Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1275 
1276  /* Unparameterized paths have no ParamPathInfo */
1277  if (bms_is_empty(required_outer))
1278  return NULL;
1279 
1280  Assert(!bms_overlap(baserel->relids, required_outer));
1281 
1282  /* If we already have a PPI for this parameterization, just return it */
1283  if ((ppi = find_param_path_info(baserel, required_outer)))
1284  return ppi;
1285 
1286  /*
1287  * Identify all joinclauses that are movable to this base rel given this
1288  * parameterization.
1289  */
1290  joinrelids = bms_union(baserel->relids, required_outer);
1291  pclauses = NIL;
1292  foreach(lc, baserel->joininfo)
1293  {
1294  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1295 
1296  if (join_clause_is_movable_into(rinfo,
1297  baserel->relids,
1298  joinrelids))
1299  pclauses = lappend(pclauses, rinfo);
1300  }
1301 
1302  /*
1303  * Add in joinclauses generated by EquivalenceClasses, too. (These
1304  * necessarily satisfy join_clause_is_movable_into.)
1305  */
1306  pclauses = list_concat(pclauses,
1308  joinrelids,
1309  required_outer,
1310  baserel));
1311 
1312  /* Estimate the number of rows returned by the parameterized scan */
1313  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1314 
1315  /* And now we can build the ParamPathInfo */
1316  ppi = makeNode(ParamPathInfo);
1317  ppi->ppi_req_outer = required_outer;
1318  ppi->ppi_rows = rows;
1319  ppi->ppi_clauses = pclauses;
1320  baserel->ppilist = lappend(baserel->ppilist, ppi);
1321 
1322  return ppi;
1323 }
#define NIL
Definition: pg_list.h:65
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1593
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
Relids lateral_relids
Definition: pathnodes.h:668
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1126
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
List * joininfo
Definition: pathnodes.h:709
Relids relids
Definition: pathnodes.h:643
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:577
List * ppilist
Definition: pathnodes.h:658
List * lappend(List *list, void *datum)
Definition: list.c:322
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:4434
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
List * ppi_clauses
Definition: pathnodes.h:1077
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
double ppi_rows
Definition: pathnodes.h:1076
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Relids ppi_req_outer
Definition: pathnodes.h:1075
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 1354 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().

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

◆ min_join_parameterization()

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

Definition at line 914 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().

918 {
919  Relids result;
920 
921  /*
922  * Basically we just need the union of the inputs' lateral_relids, less
923  * whatever is already in the join.
924  *
925  * It's not immediately obvious that this is a valid way to compute the
926  * result, because it might seem that we're ignoring possible lateral refs
927  * of PlaceHolderVars that are due to be computed at the join but not in
928  * either input. However, because create_lateral_join_info() already
929  * charged all such PHV refs to each member baserel of the join, they'll
930  * be accounted for already in the inputs' lateral_relids. Likewise, we
931  * do not need to worry about doing transitive closure here, because that
932  * was already accounted for in the original baserel lateral_relids.
933  */
934  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
935  result = bms_del_members(result, joinrelids);
936 
937  /* Maintain invariant that result is exactly NULL if empty */
938  if (bms_is_empty(result))
939  result = NULL;
940 
941  return result;
942 }
Relids lateral_relids
Definition: pathnodes.h:668
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 491 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().

493 {
494  if (OidIsValid(outer_rel->serverid) &&
495  inner_rel->serverid == outer_rel->serverid)
496  {
497  if (inner_rel->userid == outer_rel->userid)
498  {
499  joinrel->serverid = outer_rel->serverid;
500  joinrel->userid = outer_rel->userid;
501  joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
502  joinrel->fdwroutine = outer_rel->fdwroutine;
503  }
504  else if (!OidIsValid(inner_rel->userid) &&
505  outer_rel->userid == GetUserId())
506  {
507  joinrel->serverid = outer_rel->serverid;
508  joinrel->userid = outer_rel->userid;
509  joinrel->useridiscurrent = true;
510  joinrel->fdwroutine = outer_rel->fdwroutine;
511  }
512  else if (!OidIsValid(outer_rel->userid) &&
513  inner_rel->userid == GetUserId())
514  {
515  joinrel->serverid = outer_rel->serverid;
516  joinrel->userid = inner_rel->userid;
517  joinrel->useridiscurrent = true;
518  joinrel->fdwroutine = outer_rel->fdwroutine;
519  }
520  }
521 }
Oid GetUserId(void)
Definition: miscinit.c:380
Oid userid
Definition: pathnodes.h:693
bool useridiscurrent
Definition: pathnodes.h:694
#define OidIsValid(objectId)
Definition: c.h:645
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:696
Oid serverid
Definition: pathnodes.h:692

◆ setup_simple_rel_arrays()

void setup_simple_rel_arrays ( PlannerInfo root)

Definition at line 74 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().

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

◆ subbuild_joinrel_joinlist()

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

Definition at line 1125 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().

1128 {
1129  ListCell *l;
1130 
1131  /* Expected to be called only for join between parent relations. */
1132  Assert(joinrel->reloptkind == RELOPT_JOINREL);
1133 
1134  foreach(l, joininfo_list)
1135  {
1136  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1137 
1138  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1139  {
1140  /*
1141  * This clause becomes a restriction clause for the joinrel, since
1142  * it refers to no outside rels. So we can ignore it in this
1143  * routine.
1144  */
1145  }
1146  else
1147  {
1148  /*
1149  * This clause is still a join clause at this level, so add it to
1150  * the new joininfo list, being careful to eliminate duplicates.
1151  * (Since RestrictInfo nodes in different joinlists will have been
1152  * multiply-linked rather than copied, pointer equality should be
1153  * a sufficient test.)
1154  */
1155  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1156  }
1157  }
1158 
1159  return new_joininfo;
1160 }
RelOptKind reloptkind
Definition: pathnodes.h:640
Relids required_relids
Definition: pathnodes.h:1963
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1194
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:643
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190

◆ subbuild_joinrel_restrictlist()

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

Definition at line 1091 of file relnode.c.

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

Referenced by build_joinrel_restrictlist().

1094 {
1095  ListCell *l;
1096 
1097  foreach(l, joininfo_list)
1098  {
1099  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1100 
1101  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1102  {
1103  /*
1104  * This clause becomes a restriction clause for the joinrel, since
1105  * it refers to no outside rels. Add it to the list, being
1106  * careful to eliminate duplicates. (Since RestrictInfo nodes in
1107  * different joinlists will have been multiply-linked rather than
1108  * copied, pointer equality should be a sufficient test.)
1109  */
1110  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1111  }
1112  else
1113  {
1114  /*
1115  * This clause is still a join clause at this level, so we ignore
1116  * it in this routine.
1117  */
1118  }
1119  }
1120 
1121  return new_restrictlist;
1122 }
Relids required_relids
Definition: pathnodes.h:1963
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1194
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:643
#define lfirst(lc)
Definition: pg_list.h:190