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:244
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:641
List * lappend(List *list, void *datum)
Definition: list.c:322
#define Assert(condition)
Definition: c.h:732
struct HTAB * join_rel_hash
Definition: pathnodes.h:245

◆ 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_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, 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  appinfos = find_appinfos_by_relids(root, joinrel->relids, &nappinfos);
847 
848  /* Set up reltarget struct */
849  build_child_join_reltarget(root, parent_joinrel, joinrel,
850  nappinfos, appinfos);
851 
852  /* Construct joininfo list. */
853  joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
854  (Node *) parent_joinrel->joininfo,
855  nappinfos,
856  appinfos);
857  pfree(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  return joinrel;
890 }
bool has_eclass_joins
Definition: pathnodes.h:709
struct Path * cheapest_unique_path
Definition: pathnodes.h:660
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:638
Relids * attr_needed
Definition: pathnodes.h:674
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:428
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:629
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:658
Oid userid
Definition: pathnodes.h:691
double tuples
Definition: pathnodes.h:681
List * baserestrictinfo
Definition: pathnodes.h:703
bool consider_param_startup
Definition: pathnodes.h:648
Definition: nodes.h:525
List * partial_pathlist
Definition: pathnodes.h:657
List * cheapest_parameterized_paths
Definition: pathnodes.h:661
bool useridiscurrent
Definition: pathnodes.h:692
List ** nullable_partexprs
Definition: pathnodes.h:725
Cost startup
Definition: pathnodes.h:45
double allvisfrac
Definition: pathnodes.h:682
PlannerInfo * subroot
Definition: pathnodes.h:685
bool consider_startup
Definition: pathnodes.h:647
Relids lateral_relids
Definition: pathnodes.h:666
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:334
void pfree(void *pointer)
Definition: mcxt.c:1056
List ** partexprs
Definition: pathnodes.h:724
struct Path * cheapest_total_path
Definition: pathnodes.h:659
List * joininfo
Definition: pathnodes.h:707
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:694
int nparts
Definition: pathnodes.h:719
Relids relids
Definition: pathnodes.h:641
List * ppilist
Definition: pathnodes.h:656
Index relid
Definition: pathnodes.h:669
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:709
Relids lateral_referencers
Definition: pathnodes.h:677
Oid serverid
Definition: pathnodes.h:690
Relids direct_lateral_relids
Definition: pathnodes.h:665
bool consider_partitionwise_join
Definition: pathnodes.h:712
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:720
RTEKind rtekind
Definition: pathnodes.h:671
List * indexlist
Definition: pathnodes.h:678
double rows
Definition: pathnodes.h:644
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: pathnodes.h:695
#define makeNode(_type_)
Definition: nodes.h:573
BlockNumber pages
Definition: pathnodes.h:680
#define Assert(condition)
Definition: c.h:732
List * lateral_vars
Definition: pathnodes.h:676
struct RelOptInfo ** part_rels
Definition: pathnodes.h:722
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool consider_parallel
Definition: pathnodes.h:649
Bitmapset * Relids
Definition: pathnodes.h:28
List * partitioned_child_rels
Definition: pathnodes.h:726
AttrNumber max_attr
Definition: pathnodes.h:673
static void build_joinrel_partition_info(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, JoinType jointype)
Definition: relnode.c:1603
PartitionScheme part_scheme
Definition: pathnodes.h:718
List * pathlist
Definition: pathnodes.h:655
List * partition_qual
Definition: pathnodes.h:721
int32 * attr_widths
Definition: pathnodes.h:675
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:652
QualCost baserestrictcost
Definition: pathnodes.h:704
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:529
List * subplan_params
Definition: pathnodes.h:686
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:1772
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:180
Relids top_parent_relids
Definition: pathnodes.h:714
AttrNumber min_attr
Definition: pathnodes.h:672

◆ build_child_join_reltarget()

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

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

1777 {
1778  /* Build the targetlist */
1779  childrel->reltarget->exprs = (List *)
1781  (Node *) parentrel->reltarget->exprs,
1782  nappinfos, appinfos);
1783 
1784  /* Set the cost and width fields */
1785  childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
1786  childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
1787  childrel->reltarget->width = parentrel->reltarget->width;
1788 }
Definition: nodes.h:525
Cost startup
Definition: pathnodes.h:45
Cost per_tuple
Definition: pathnodes.h:46
List * exprs
Definition: pathnodes.h:1044
QualCost cost
Definition: pathnodes.h:1046
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:652
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:180

◆ 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:709
struct Path * cheapest_unique_path
Definition: pathnodes.h:660
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:696
#define NIL
Definition: pg_list.h:65
int join_cur_level
Definition: pathnodes.h:255
List * unique_for_rels
Definition: pathnodes.h:698
List * statlist
Definition: pathnodes.h:679
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
RelOptKind reloptkind
Definition: pathnodes.h:638
Relids * attr_needed
Definition: pathnodes.h:674
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:428
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:629
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:658
Oid userid
Definition: pathnodes.h:691
double tuples
Definition: pathnodes.h:681
List * baserestrictinfo
Definition: pathnodes.h:703
bool consider_param_startup
Definition: pathnodes.h:648
Definition: nodes.h:525
List * partial_pathlist
Definition: pathnodes.h:657
List * cheapest_parameterized_paths
Definition: pathnodes.h:661
Index baserestrict_min_security
Definition: pathnodes.h:705
bool useridiscurrent
Definition: pathnodes.h:692
List ** nullable_partexprs
Definition: pathnodes.h:725
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:682
PlannerInfo * subroot
Definition: pathnodes.h:685
bool consider_startup
Definition: pathnodes.h:647
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:854
Relids lateral_relids
Definition: pathnodes.h:666
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:334
List ** partexprs
Definition: pathnodes.h:724
static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel)
Definition: relnode.c:944
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:901
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
struct Path * cheapest_total_path
Definition: pathnodes.h:659
static List * build_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1030
List * joininfo
Definition: pathnodes.h:707
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:694
int nparts
Definition: pathnodes.h:719
Relids relids
Definition: pathnodes.h:641
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:2559
List * non_unique_for_rels
Definition: pathnodes.h:700
List * ppilist
Definition: pathnodes.h:656
Index relid
Definition: pathnodes.h:669
List * lappend(List *list, void *datum)
Definition: list.c:322
Relids lateral_referencers
Definition: pathnodes.h:677
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Oid serverid
Definition: pathnodes.h:690
List * exprs
Definition: pathnodes.h:1044
Relids direct_lateral_relids
Definition: pathnodes.h:665
bool consider_partitionwise_join
Definition: pathnodes.h:712
int rel_parallel_workers
Definition: pathnodes.h:687
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:720
RTEKind rtekind
Definition: pathnodes.h:671
List * indexlist
Definition: pathnodes.h:678
double rows
Definition: pathnodes.h:644
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: pathnodes.h:695
#define makeNode(_type_)
Definition: nodes.h:573
BlockNumber pages
Definition: pathnodes.h:680
#define Assert(condition)
Definition: c.h:732
List ** join_rel_level
Definition: pathnodes.h:254
List * lateral_vars
Definition: pathnodes.h:676
JoinType jointype
Definition: pathnodes.h:2137
struct RelOptInfo ** part_rels
Definition: pathnodes.h:722
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool consider_parallel
Definition: pathnodes.h:649
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:928
List * partitioned_child_rels
Definition: pathnodes.h:726
AttrNumber max_attr
Definition: pathnodes.h:673
static void build_joinrel_partition_info(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, JoinType jointype)
Definition: relnode.c:1603
PartitionScheme part_scheme
Definition: pathnodes.h:718
List * pathlist
Definition: pathnodes.h:655
List * partition_qual
Definition: pathnodes.h:721
int32 * attr_widths
Definition: pathnodes.h:675
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:652
QualCost baserestrictcost
Definition: pathnodes.h:704
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:529
List * subplan_params
Definition: pathnodes.h:686
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1060
Relids top_parent_relids
Definition: pathnodes.h:714
AttrNumber min_attr
Definition: pathnodes.h:672

◆ 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:955
List * join_rel_list
Definition: pathnodes.h:244
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:641
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:732
#define lfirst(lc)
Definition: pg_list.h:190
#define HASH_COMPARE
Definition: hsearch.h:90
struct HTAB * join_rel_hash
Definition: pathnodes.h:245
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 1060 of file relnode.c.

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

Referenced by build_join_rel().

1063 {
1064  List *result;
1065 
1066  /*
1067  * Collect all the clauses that syntactically belong above this level,
1068  * eliminating any duplicates (important since we will see many of the
1069  * same clauses arriving from both input relations).
1070  */
1071  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1072  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1073 
1074  joinrel->joininfo = result;
1075 }
#define NIL
Definition: pg_list.h:65
List * joininfo
Definition: pathnodes.h:707
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:1112
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 1603 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().

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

1034 {
1035  List *result;
1036 
1037  /*
1038  * Collect all the clauses that syntactically belong at this level,
1039  * eliminating any duplicates (important since we will see many of the
1040  * same clauses arriving from both input relations).
1041  */
1042  result = subbuild_joinrel_restrictlist(joinrel, outer_rel->joininfo, NIL);
1043  result = subbuild_joinrel_restrictlist(joinrel, inner_rel->joininfo, result);
1044 
1045  /*
1046  * Add on any clauses derived from EquivalenceClasses. These cannot be
1047  * redundant with the clauses in the joininfo lists, so don't bother
1048  * checking.
1049  */
1050  result = list_concat(result,
1052  joinrel->relids,
1053  outer_rel->relids,
1054  inner_rel));
1055 
1056  return result;
1057 }
#define NIL
Definition: pg_list.h:65
static List * subbuild_joinrel_restrictlist(RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
Definition: relnode.c:1078
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:707
Relids relids
Definition: pathnodes.h:641
Definition: pg_list.h:50

◆ build_joinrel_tlist()

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

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

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

◆ 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:709
struct Path * cheapest_unique_path
Definition: pathnodes.h:660
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:696
#define NIL
Definition: pg_list.h:65
List * unique_for_rels
Definition: pathnodes.h:698
List * statlist
Definition: pathnodes.h:679
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
RelOptKind reloptkind
Definition: pathnodes.h:638
Relids * attr_needed
Definition: pathnodes.h:674
List * colnames
Definition: primnodes.h:43
struct Path * cheapest_startup_path
Definition: pathnodes.h:658
Oid userid
Definition: pathnodes.h:691
double tuples
Definition: pathnodes.h:681
List * baserestrictinfo
Definition: pathnodes.h:703
bool consider_param_startup
Definition: pathnodes.h:648
List * partial_pathlist
Definition: pathnodes.h:657
List * cheapest_parameterized_paths
Definition: pathnodes.h:661
Index baserestrict_min_security
Definition: pathnodes.h:705
bool useridiscurrent
Definition: pathnodes.h:692
List ** nullable_partexprs
Definition: pathnodes.h:725
Cost startup
Definition: pathnodes.h:45
double allvisfrac
Definition: pathnodes.h:682
signed int int32
Definition: c.h:346
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
PlannerInfo * subroot
Definition: pathnodes.h:685
bool consider_startup
Definition: pathnodes.h:647
bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, RangeTblEntry *childRTE, AppendRelInfo *appinfo)
Definition: inherit.c:627
Relids lateral_relids
Definition: pathnodes.h:666
Cost per_tuple
Definition: pathnodes.h:46
double tuple_fraction
Definition: pathnodes.h:334
List ** partexprs
Definition: pathnodes.h:724
#define ERROR
Definition: elog.h:43
struct Path * cheapest_total_path
Definition: pathnodes.h:659
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
List * joininfo
Definition: pathnodes.h:707
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:694
int nparts
Definition: pathnodes.h:719
Relids relids
Definition: pathnodes.h:641
List * non_unique_for_rels
Definition: pathnodes.h:700
List * ppilist
Definition: pathnodes.h:656
Index relid
Definition: pathnodes.h:669
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:209
Relids lateral_referencers
Definition: pathnodes.h:677
Oid serverid
Definition: pathnodes.h:690
Relids direct_lateral_relids
Definition: pathnodes.h:665
bool consider_partitionwise_join
Definition: pathnodes.h:712
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:687
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:217
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:720
RTEKind rtekind
Definition: pathnodes.h:671
List * indexlist
Definition: pathnodes.h:678
double rows
Definition: pathnodes.h:644
#define InvalidOid
Definition: postgres_ext.h:36
void * fdw_private
Definition: pathnodes.h:695
#define makeNode(_type_)
Definition: nodes.h:573
BlockNumber pages
Definition: pathnodes.h:680
#define Assert(condition)
Definition: c.h:732
List * lateral_vars
Definition: pathnodes.h:676
struct RelOptInfo ** part_rels
Definition: pathnodes.h:722
static int list_length(const List *l)
Definition: pg_list.h:169
bool consider_parallel
Definition: pathnodes.h:649
Bitmapset * Relids
Definition: pathnodes.h:28
RTEKind rtekind
Definition: parsenodes.h:974
List * partitioned_child_rels
Definition: pathnodes.h:726
AttrNumber max_attr
Definition: pathnodes.h:673
#define elog(elevel,...)
Definition: elog.h:226
PartitionScheme part_scheme
Definition: pathnodes.h:718
List * pathlist
Definition: pathnodes.h:655
Alias * eref
Definition: parsenodes.h:1092
List * partition_qual
Definition: pathnodes.h:721
int32 * attr_widths
Definition: pathnodes.h:675
struct PathTarget * reltarget
Definition: pathnodes.h:652
QualCost baserestrictcost
Definition: pathnodes.h:704
List * subplan_params
Definition: pathnodes.h:686
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition: plancat.c:114
Relids top_parent_relids
Definition: pathnodes.h:714
AttrNumber min_attr
Definition: pathnodes.h:672

◆ 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:955
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
int simple_rel_array_size
Definition: pathnodes.h:202
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:209
void * palloc0(Size size)
Definition: mcxt.c:980
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:217
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define Assert(condition)
Definition: c.h:732
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 1164 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().

1165 {
1166  RelOptInfo *upperrel;
1167  ListCell *lc;
1168 
1169  /*
1170  * For the moment, our indexing data structure is just a List for each
1171  * relation kind. If we ever get so many of one kind that this stops
1172  * working well, we can improve it. No code outside this function should
1173  * assume anything about how to find a particular upperrel.
1174  */
1175 
1176  /* If we already made this upperrel for the query, return it */
1177  foreach(lc, root->upper_rels[kind])
1178  {
1179  upperrel = (RelOptInfo *) lfirst(lc);
1180 
1181  if (bms_equal(upperrel->relids, relids))
1182  return upperrel;
1183  }
1184 
1185  upperrel = makeNode(RelOptInfo);
1186  upperrel->reloptkind = RELOPT_UPPER_REL;
1187  upperrel->relids = bms_copy(relids);
1188 
1189  /* cheap startup cost is interesting iff not all tuples to be retrieved */
1190  upperrel->consider_startup = (root->tuple_fraction > 0);
1191  upperrel->consider_param_startup = false;
1192  upperrel->consider_parallel = false; /* might get changed later */
1193  upperrel->reltarget = create_empty_pathtarget();
1194  upperrel->pathlist = NIL;
1195  upperrel->cheapest_startup_path = NULL;
1196  upperrel->cheapest_total_path = NULL;
1197  upperrel->cheapest_unique_path = NULL;
1198  upperrel->cheapest_parameterized_paths = NIL;
1199 
1200  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1201 
1202  return upperrel;
1203 }
struct Path * cheapest_unique_path
Definition: pathnodes.h:660
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:638
struct Path * cheapest_startup_path
Definition: pathnodes.h:658
bool consider_param_startup
Definition: pathnodes.h:648
List * cheapest_parameterized_paths
Definition: pathnodes.h:661
bool consider_startup
Definition: pathnodes.h:647
double tuple_fraction
Definition: pathnodes.h:334
struct Path * cheapest_total_path
Definition: pathnodes.h:659
Relids relids
Definition: pathnodes.h:641
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:649
List * pathlist
Definition: pathnodes.h:655
struct PathTarget * reltarget
Definition: pathnodes.h:652
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
List * upper_rels[UPPERREL_FINAL+1]
Definition: pathnodes.h:309

◆ 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:201
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:732
#define elog(elevel,...)
Definition: elog.h:226

◆ find_childrel_parents()

Relids find_childrel_parents ( PlannerInfo root,
RelOptInfo rel 
)

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

1216 {
1217  Relids result = NULL;
1218 
1220  Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1221 
1222  do
1223  {
1224  AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1225  Index prelid = appinfo->parent_relid;
1226 
1227  result = bms_add_member(result, prelid);
1228 
1229  /* traverse up to the parent rel, loop if it's also a child rel */
1230  rel = find_base_rel(root, prelid);
1231  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1232 
1233  Assert(rel->reloptkind == RELOPT_BASEREL);
1234 
1235  return result;
1236 }
RelOptKind reloptkind
Definition: pathnodes.h:638
int simple_rel_array_size
Definition: pathnodes.h:202
Index relid
Definition: pathnodes.h:669
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:217
unsigned int Index
Definition: c.h:475
#define Assert(condition)
Definition: c.h:732
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:2191

◆ 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:244
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:641
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:245
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 1580 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().

1581 {
1582  ListCell *lc;
1583 
1584  foreach(lc, rel->ppilist)
1585  {
1586  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1587 
1588  if (bms_equal(ppi->ppi_req_outer, required_outer))
1589  return ppi;
1590  }
1591 
1592  return NULL;
1593 }
List * ppilist
Definition: pathnodes.h:656
#define lfirst(lc)
Definition: pg_list.h:190
Relids ppi_req_outer
Definition: pathnodes.h:1073
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 1548 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().

1549 {
1550  ParamPathInfo *ppi;
1551 
1552  /* If rel has LATERAL refs, every path for it should account for them */
1553  Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1554 
1555  /* Unparameterized paths have no ParamPathInfo */
1556  if (bms_is_empty(required_outer))
1557  return NULL;
1558 
1559  Assert(!bms_overlap(appendrel->relids, required_outer));
1560 
1561  /* If we already have a PPI for this parameterization, just return it */
1562  if ((ppi = find_param_path_info(appendrel, required_outer)))
1563  return ppi;
1564 
1565  /* Else build the ParamPathInfo */
1566  ppi = makeNode(ParamPathInfo);
1567  ppi->ppi_req_outer = required_outer;
1568  ppi->ppi_rows = 0;
1569  ppi->ppi_clauses = NIL;
1570  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1571 
1572  return ppi;
1573 }
#define NIL
Definition: pg_list.h:65
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1580
Relids lateral_relids
Definition: pathnodes.h:666
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:641
List * ppilist
Definition: pathnodes.h:656
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:732
List * ppi_clauses
Definition: pathnodes.h:1075
double ppi_rows
Definition: pathnodes.h:1074
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Relids ppi_req_outer
Definition: pathnodes.h:1073

◆ get_baserel_parampathinfo()

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

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

1253 {
1254  ParamPathInfo *ppi;
1255  Relids joinrelids;
1256  List *pclauses;
1257  double rows;
1258  ListCell *lc;
1259 
1260  /* If rel has LATERAL refs, every path for it should account for them */
1261  Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1262 
1263  /* Unparameterized paths have no ParamPathInfo */
1264  if (bms_is_empty(required_outer))
1265  return NULL;
1266 
1267  Assert(!bms_overlap(baserel->relids, required_outer));
1268 
1269  /* If we already have a PPI for this parameterization, just return it */
1270  if ((ppi = find_param_path_info(baserel, required_outer)))
1271  return ppi;
1272 
1273  /*
1274  * Identify all joinclauses that are movable to this base rel given this
1275  * parameterization.
1276  */
1277  joinrelids = bms_union(baserel->relids, required_outer);
1278  pclauses = NIL;
1279  foreach(lc, baserel->joininfo)
1280  {
1281  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1282 
1283  if (join_clause_is_movable_into(rinfo,
1284  baserel->relids,
1285  joinrelids))
1286  pclauses = lappend(pclauses, rinfo);
1287  }
1288 
1289  /*
1290  * Add in joinclauses generated by EquivalenceClasses, too. (These
1291  * necessarily satisfy join_clause_is_movable_into.)
1292  */
1293  pclauses = list_concat(pclauses,
1295  joinrelids,
1296  required_outer,
1297  baserel));
1298 
1299  /* Estimate the number of rows returned by the parameterized scan */
1300  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1301 
1302  /* And now we can build the ParamPathInfo */
1303  ppi = makeNode(ParamPathInfo);
1304  ppi->ppi_req_outer = required_outer;
1305  ppi->ppi_rows = rows;
1306  ppi->ppi_clauses = pclauses;
1307  baserel->ppilist = lappend(baserel->ppilist, ppi);
1308 
1309  return ppi;
1310 }
#define NIL
Definition: pg_list.h:65
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1580
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
Relids lateral_relids
Definition: pathnodes.h:666
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:707
Relids relids
Definition: pathnodes.h:641
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:577
List * ppilist
Definition: pathnodes.h:656
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:732
#define lfirst(lc)
Definition: pg_list.h:190
List * ppi_clauses
Definition: pathnodes.h:1075
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
double ppi_rows
Definition: pathnodes.h:1074
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Relids ppi_req_outer
Definition: pathnodes.h:1073
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 1341 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().

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

905 {
906  Relids result;
907 
908  /*
909  * Basically we just need the union of the inputs' lateral_relids, less
910  * whatever is already in the join.
911  *
912  * It's not immediately obvious that this is a valid way to compute the
913  * result, because it might seem that we're ignoring possible lateral refs
914  * of PlaceHolderVars that are due to be computed at the join but not in
915  * either input. However, because create_lateral_join_info() already
916  * charged all such PHV refs to each member baserel of the join, they'll
917  * be accounted for already in the inputs' lateral_relids. Likewise, we
918  * do not need to worry about doing transitive closure here, because that
919  * was already accounted for in the original baserel lateral_relids.
920  */
921  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
922  result = bms_del_members(result, joinrelids);
923 
924  /* Maintain invariant that result is exactly NULL if empty */
925  if (bms_is_empty(result))
926  result = NULL;
927 
928  return result;
929 }
Relids lateral_relids
Definition: pathnodes.h:666
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:691
bool useridiscurrent
Definition: pathnodes.h:692
#define OidIsValid(objectId)
Definition: c.h:638
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:694
Oid serverid
Definition: pathnodes.h:690

◆ 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:177
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
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:202
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:209
void * palloc0(Size size)
Definition: mcxt.c:980
List * append_rel_list
Definition: pathnodes.h:288
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:217
unsigned int Index
Definition: c.h:475
#define Assert(condition)
Definition: c.h:732
#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:226
Index child_relid
Definition: pathnodes.h:2192

◆ subbuild_joinrel_joinlist()

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

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

1115 {
1116  ListCell *l;
1117 
1118  /* Expected to be called only for join between parent relations. */
1119  Assert(joinrel->reloptkind == RELOPT_JOINREL);
1120 
1121  foreach(l, joininfo_list)
1122  {
1123  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1124 
1125  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1126  {
1127  /*
1128  * This clause becomes a restriction clause for the joinrel, since
1129  * it refers to no outside rels. So we can ignore it in this
1130  * routine.
1131  */
1132  }
1133  else
1134  {
1135  /*
1136  * This clause is still a join clause at this level, so add it to
1137  * the new joininfo list, being careful to eliminate duplicates.
1138  * (Since RestrictInfo nodes in different joinlists will have been
1139  * multiply-linked rather than copied, pointer equality should be
1140  * a sufficient test.)
1141  */
1142  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1143  }
1144  }
1145 
1146  return new_joininfo;
1147 }
RelOptKind reloptkind
Definition: pathnodes.h:638
Relids required_relids
Definition: pathnodes.h:1961
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:641
#define Assert(condition)
Definition: c.h:732
#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 1078 of file relnode.c.

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

Referenced by build_joinrel_restrictlist().

1081 {
1082  ListCell *l;
1083 
1084  foreach(l, joininfo_list)
1085  {
1086  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1087 
1088  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1089  {
1090  /*
1091  * This clause becomes a restriction clause for the joinrel, since
1092  * it refers to no outside rels. Add it to the list, being
1093  * careful to eliminate duplicates. (Since RestrictInfo nodes in
1094  * different joinlists will have been multiply-linked rather than
1095  * copied, pointer equality should be a sufficient test.)
1096  */
1097  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1098  }
1099  else
1100  {
1101  /*
1102  * This clause is still a join clause at this level, so we ignore
1103  * it in this routine.
1104  */
1105  }
1106  }
1107 
1108  return new_restrictlist;
1109 }
Relids required_relids
Definition: pathnodes.h:1961
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:641
#define lfirst(lc)
Definition: pg_list.h:190