PostgreSQL Source Code git master
Loading...
Searching...
No Matches
relnode.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/nbtree.h"
#include "catalog/pg_constraint.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/inherit.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/planner.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h"
#include "parser/parse_oper.h"
#include "parser/parse_relation.h"
#include "rewrite/rewriteManip.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
#include "utils/typcache.h"
Include dependency graph for relnode.c:

Go to the source code of this file.

Data Structures

struct  JoinHashEntry
 

Typedefs

typedef struct JoinHashEntry JoinHashEntry
 

Functions

static void build_joinrel_tlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, bool can_null)
 
static Listbuild_joinrel_restrictlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
 
static void build_joinrel_joinlist (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static Listsubbuild_joinrel_restrictlist (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, Relids both_input_relids, List *new_restrictlist)
 
static Listsubbuild_joinrel_joinlist (RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
 
static void set_foreign_rel_properties (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
static void add_join_rel (PlannerInfo *root, RelOptInfo *joinrel)
 
static void build_joinrel_partition_info (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
 
static bool have_partkey_equi_join (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
 
static int match_expr_to_partition_keys (Expr *expr, RelOptInfo *rel, bool strict_op)
 
static void set_joinrel_partition_key_exprs (RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
 
static void build_child_join_reltarget (PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
 
static bool eager_aggregation_possible_for_relation (PlannerInfo *root, RelOptInfo *rel)
 
static bool init_grouping_targets (PlannerInfo *root, RelOptInfo *rel, PathTarget *target, PathTarget *agg_input, List **group_clauses, List **group_exprs)
 
static bool is_var_in_aggref_only (PlannerInfo *root, Var *var)
 
static bool is_var_needed_by_join (PlannerInfo *root, Var *var, RelOptInfo *rel)
 
static Index get_expression_sortgroupref (PlannerInfo *root, Expr *expr)
 
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)
 
RelOptInfobuild_simple_grouped_rel (PlannerInfo *root, RelOptInfo *rel)
 
RelOptInfobuild_grouped_rel (PlannerInfo *root, RelOptInfo *rel)
 
RelOptInfofind_base_rel (PlannerInfo *root, int relid)
 
RelOptInfofind_base_rel_noerr (PlannerInfo *root, int relid)
 
RelOptInfofind_base_rel_ignore_join (PlannerInfo *root, int relid)
 
static void build_join_rel_hash (PlannerInfo *root)
 
RelOptInfofind_join_rel (PlannerInfo *root, Relids relids)
 
RelOptInfobuild_join_rel (PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, List **restrictlist_ptr)
 
RelOptInfobuild_child_join_rel (PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo, int nappinfos, AppendRelInfo **appinfos)
 
Relids min_join_parameterization (PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 
RelOptInfofetch_upper_rel (PlannerInfo *root, UpperRelationKind kind, Relids relids)
 
Relids find_childrel_parents (PlannerInfo *root, RelOptInfo *rel)
 
ParamPathInfoget_baserel_parampathinfo (PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
 
ParamPathInfoget_joinrel_parampathinfo (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, Relids required_outer, List **restrict_clauses)
 
ParamPathInfoget_appendrel_parampathinfo (RelOptInfo *appendrel, Relids required_outer)
 
ParamPathInfofind_param_path_info (RelOptInfo *rel, Relids required_outer)
 
Bitmapsetget_param_path_clause_serials (Path *path)
 
RelAggInfocreate_rel_agg_info (PlannerInfo *root, RelOptInfo *rel, bool calculate_grouped_rows)
 

Variables

build_simple_rel_hook_type build_simple_rel_hook = NULL
 
joinrel_setup_hook_type joinrel_setup_hook = NULL
 

Typedef Documentation

◆ JoinHashEntry

Function Documentation

◆ add_join_rel()

static void add_join_rel ( PlannerInfo root,
RelOptInfo joinrel 
)
static

Definition at line 757 of file relnode.c.

758{
759 /* GEQO requires us to append the new joinrel to the end of the list! */
760 root->join_rel_list = lappend(root->join_rel_list, joinrel);
761
762 /* store it into the auxiliary hashtable if there is one. */
763 if (root->join_rel_hash)
764 {
766 bool found;
767
768 hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
769 &(joinrel->relids),
771 &found);
772 Assert(!found);
773 hentry->join_rel = joinrel;
774 }
775}
#define Assert(condition)
Definition c.h:945
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
@ HASH_ENTER
Definition hsearch.h:114
List * lappend(List *list, void *datum)
Definition list.c:339
static int fb(int x)
tree ctl root
Definition radixtree.h:1857
Relids relids
Definition pathnodes.h:1009

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

Referenced by build_child_join_rel(), and build_join_rel().

◆ build_child_join_rel()

RelOptInfo * build_child_join_rel ( PlannerInfo root,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
RelOptInfo parent_joinrel,
List restrictlist,
SpecialJoinInfo sjinfo,
int  nappinfos,
AppendRelInfo **  appinfos 
)

Definition at line 1026 of file relnode.c.

1030{
1031 RelOptInfo *joinrel = makeNode(RelOptInfo);
1032
1033 /* Only joins between "other" relations land here. */
1035
1036 /* The parent joinrel should have consider_partitionwise_join set. */
1037 Assert(parent_joinrel->consider_partitionwise_join);
1038
1040 joinrel->relids = adjust_child_relids(parent_joinrel->relids,
1041 nappinfos, appinfos);
1042 joinrel->rows = 0;
1043 /* cheap startup cost is interesting iff not all tuples to be retrieved */
1044 joinrel->consider_startup = (root->tuple_fraction > 0);
1045 joinrel->consider_param_startup = false;
1046 joinrel->consider_parallel = false;
1047 joinrel->pgs_mask = root->glob->default_pgs_mask;
1049 joinrel->pathlist = NIL;
1050 joinrel->ppilist = NIL;
1051 joinrel->partial_pathlist = NIL;
1052 joinrel->cheapest_startup_path = NULL;
1053 joinrel->cheapest_total_path = NULL;
1055 joinrel->direct_lateral_relids = NULL;
1056 joinrel->lateral_relids = NULL;
1057 joinrel->relid = 0; /* indicates not a baserel */
1058 joinrel->rtekind = RTE_JOIN;
1059 joinrel->min_attr = 0;
1060 joinrel->max_attr = 0;
1061 joinrel->attr_needed = NULL;
1062 joinrel->attr_widths = NULL;
1063 joinrel->notnullattnums = NULL;
1064 joinrel->nulling_relids = NULL;
1065 joinrel->lateral_vars = NIL;
1066 joinrel->lateral_referencers = NULL;
1067 joinrel->indexlist = NIL;
1068 joinrel->pages = 0;
1069 joinrel->tuples = 0;
1070 joinrel->allvisfrac = 0;
1071 joinrel->eclass_indexes = NULL;
1072 joinrel->subroot = NULL;
1073 joinrel->subplan_params = NIL;
1074 joinrel->amflags = 0;
1075 joinrel->serverid = InvalidOid;
1076 joinrel->userid = InvalidOid;
1077 joinrel->useridiscurrent = false;
1078 joinrel->fdwroutine = NULL;
1079 joinrel->fdw_private = NULL;
1080 joinrel->unique_rel = NULL;
1081 joinrel->unique_pathkeys = NIL;
1082 joinrel->unique_groupclause = NIL;
1083 joinrel->baserestrictinfo = NIL;
1084 joinrel->baserestrictcost.startup = 0;
1085 joinrel->baserestrictcost.per_tuple = 0;
1086 joinrel->joininfo = NIL;
1087 joinrel->has_eclass_joins = false;
1088 joinrel->consider_partitionwise_join = false; /* might get changed later */
1089 joinrel->agg_info = NULL;
1090 joinrel->grouped_rel = NULL;
1091 joinrel->parent = parent_joinrel;
1092 joinrel->top_parent = parent_joinrel->top_parent ? parent_joinrel->top_parent : parent_joinrel;
1093 joinrel->top_parent_relids = joinrel->top_parent->relids;
1094 joinrel->part_scheme = NULL;
1095 joinrel->nparts = -1;
1096 joinrel->boundinfo = NULL;
1097 joinrel->partbounds_merged = false;
1098 joinrel->partition_qual = NIL;
1099 joinrel->part_rels = NULL;
1100 joinrel->live_parts = NULL;
1101 joinrel->all_partrels = NULL;
1102 joinrel->partexprs = NULL;
1103 joinrel->nullable_partexprs = NULL;
1104
1105 /* Compute information relevant to foreign relations. */
1107
1108 /* Set up reltarget struct */
1110 nappinfos, appinfos);
1111
1112 /* Construct joininfo list. */
1114 (Node *) parent_joinrel->joininfo,
1115 nappinfos,
1116 appinfos);
1117
1118 /*
1119 * Lateral relids referred in child join will be same as that referred in
1120 * the parent relation.
1121 */
1122 joinrel->direct_lateral_relids = (Relids) bms_copy(parent_joinrel->direct_lateral_relids);
1123 joinrel->lateral_relids = (Relids) bms_copy(parent_joinrel->lateral_relids);
1124
1125 /*
1126 * If the parent joinrel has pending equivalence classes, so does the
1127 * child.
1128 */
1129 joinrel->has_eclass_joins = parent_joinrel->has_eclass_joins;
1130
1131 /* Child joinrel is parallel safe if parent is parallel safe. */
1132 joinrel->consider_parallel = parent_joinrel->consider_parallel;
1133
1134 /* Set estimates of the child-joinrel's size. */
1136 sjinfo, restrictlist);
1137
1138 /*
1139 * Allow a plugin to editorialize on the new joinrel's properties. Actions
1140 * might include altering the size estimate, clearing consider_parallel,
1141 * or adjusting pgs_mask. (However, note that clearing consider_parallel
1142 * would be better done in the parent joinrel rather than here.)
1143 */
1145 (*joinrel_setup_hook) (root, joinrel, outer_rel, inner_rel, sjinfo,
1146 restrictlist);
1147
1148 /* Is the join between partitions itself partitioned? */
1150 restrictlist);
1151
1152 /* We build the join only once. */
1153 Assert(!find_join_rel(root, joinrel->relids));
1154
1155 /* Add the relation to the PlannerInfo. */
1156 add_join_rel(root, joinrel);
1157
1158 /*
1159 * We might need EquivalenceClass members corresponding to the child join,
1160 * so that we can represent sort pathkeys for it. As with children of
1161 * baserels, we shouldn't need this unless there are relevant eclass joins
1162 * (implying that a merge join might be possible) or pathkeys to sort by.
1163 */
1164 if (joinrel->has_eclass_joins || has_useful_pathkeys(root, parent_joinrel))
1166 nappinfos, appinfos,
1167 parent_joinrel, joinrel);
1168
1169 return joinrel;
1170}
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition appendinfo.c:201
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition appendinfo.c:630
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition costsize.c:5571
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
#define makeNode(_type_)
Definition nodes.h:161
@ RTE_JOIN
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition pathkeys.c:2291
Bitmapset * Relids
Definition pathnodes.h:103
@ RELOPT_OTHER_JOINREL
Definition pathnodes.h:968
#define IS_OTHER_REL(rel)
Definition pathnodes.h:992
#define NIL
Definition pg_list.h:68
#define InvalidOid
static void build_joinrel_partition_info(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition relnode.c:2150
joinrel_setup_hook_type joinrel_setup_hook
Definition relnode.c:54
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition relnode.c:657
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition relnode.c:2662
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition relnode.c:719
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition relnode.c:757
Definition pg_list.h:54
Definition nodes.h:135
Cost per_tuple
Definition pathnodes.h:121
Cost startup
Definition pathnodes.h:120
List * baserestrictinfo
Definition pathnodes.h:1130
bool consider_param_startup
Definition pathnodes.h:1023
List * subplan_params
Definition pathnodes.h:1089
List * ppilist
Definition pathnodes.h:1039
bool useridiscurrent
Definition pathnodes.h:1103
uint32 amflags
Definition pathnodes.h:1093
List * joininfo
Definition pathnodes.h:1136
Bitmapset * notnullattnums
Definition pathnodes.h:1071
List * partition_qual
Definition pathnodes.h:1180
struct PathTarget * reltarget
Definition pathnodes.h:1033
Index relid
Definition pathnodes.h:1057
List * lateral_vars
Definition pathnodes.h:1075
struct RelAggInfo * agg_info
Definition pathnodes.h:1150
uint64 pgs_mask
Definition pathnodes.h:1027
List * unique_pathkeys
Definition pathnodes.h:1122
Cardinality tuples
Definition pathnodes.h:1084
bool consider_parallel
Definition pathnodes.h:1025
Relids top_parent_relids
Definition pathnodes.h:1162
bool partbounds_merged
Definition pathnodes.h:1178
BlockNumber pages
Definition pathnodes.h:1083
Relids lateral_relids
Definition pathnodes.h:1052
List * cheapest_parameterized_paths
Definition pathnodes.h:1043
List * pathlist
Definition pathnodes.h:1038
RelOptKind reloptkind
Definition pathnodes.h:1003
List * indexlist
Definition pathnodes.h:1079
Relids lateral_referencers
Definition pathnodes.h:1077
struct Path * cheapest_startup_path
Definition pathnodes.h:1041
QualCost baserestrictcost
Definition pathnodes.h:1132
struct Path * cheapest_total_path
Definition pathnodes.h:1042
List * unique_groupclause
Definition pathnodes.h:1124
struct RelOptInfo * grouped_rel
Definition pathnodes.h:1152
Bitmapset * eclass_indexes
Definition pathnodes.h:1087
Relids all_partrels
Definition pathnodes.h:1194
Relids direct_lateral_relids
Definition pathnodes.h:1050
bool has_eclass_joins
Definition pathnodes.h:1138
bool consider_startup
Definition pathnodes.h:1021
Bitmapset * live_parts
Definition pathnodes.h:1192
bool consider_partitionwise_join
Definition pathnodes.h:1144
List * partial_pathlist
Definition pathnodes.h:1040
PlannerInfo * subroot
Definition pathnodes.h:1088
AttrNumber max_attr
Definition pathnodes.h:1065
Relids nulling_relids
Definition pathnodes.h:1073
double allvisfrac
Definition pathnodes.h:1085
struct RelOptInfo * unique_rel
Definition pathnodes.h:1120
Cardinality rows
Definition pathnodes.h:1015
AttrNumber min_attr
Definition pathnodes.h:1063
RTEKind rtekind
Definition pathnodes.h:1061
PathTarget * create_empty_pathtarget(void)
Definition tlist.c:690

References add_child_join_rel_equivalences(), add_join_rel(), adjust_appendrel_attrs(), adjust_child_relids(), RelOptInfo::agg_info, RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, Assert, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), build_child_join_reltarget(), build_joinrel_partition_info(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_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, fb(), find_join_rel(), RelOptInfo::grouped_rel, RelOptInfo::has_eclass_joins, has_useful_pathkeys(), RelOptInfo::indexlist, InvalidOid, IS_OTHER_REL, RelOptInfo::joininfo, joinrel_setup_hook, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, RelOptInfo::live_parts, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, NIL, RelOptInfo::notnullattnums, RelOptInfo::nparts, RelOptInfo::nulling_relids, RelOptInfo::pages, RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::pgs_mask, RelOptInfo::ppilist, RelOptInfo::relid, RelOptInfo::relids, RELOPT_OTHER_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, root, RelOptInfo::rows, RTE_JOIN, RelOptInfo::rtekind, RelOptInfo::serverid, set_foreign_rel_properties(), set_joinrel_size_estimates(), QualCost::startup, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, RelOptInfo::tuples, RelOptInfo::unique_groupclause, RelOptInfo::unique_pathkeys, RelOptInfo::unique_rel, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by try_partitionwise_join().

◆ build_child_join_reltarget()

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

Definition at line 2662 of file relnode.c.

2667{
2668 /* Build the targetlist */
2669 childrel->reltarget->exprs = (List *)
2671 (Node *) parentrel->reltarget->exprs,
2672 nappinfos, appinfos);
2673
2674 /* Set the cost and width fields */
2675 childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
2676 childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
2677 childrel->reltarget->width = parentrel->reltarget->width;
2678}

References adjust_appendrel_attrs(), PathTarget::exprs, fb(), RelOptInfo::reltarget, and root.

Referenced by build_child_join_rel().

◆ build_grouped_rel()

RelOptInfo * build_grouped_rel ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 499 of file relnode.c.

500{
501 RelOptInfo *grouped_rel;
502
503 grouped_rel = makeNode(RelOptInfo);
504 memcpy(grouped_rel, rel, sizeof(RelOptInfo));
505
506 /*
507 * clear path info
508 */
509 grouped_rel->pathlist = NIL;
510 grouped_rel->ppilist = NIL;
511 grouped_rel->partial_pathlist = NIL;
512 grouped_rel->cheapest_startup_path = NULL;
513 grouped_rel->cheapest_total_path = NULL;
514 grouped_rel->cheapest_parameterized_paths = NIL;
515
516 /*
517 * clear partition info
518 */
519 grouped_rel->part_scheme = NULL;
520 grouped_rel->nparts = -1;
521 grouped_rel->boundinfo = NULL;
522 grouped_rel->partbounds_merged = false;
523 grouped_rel->partition_qual = NIL;
524 grouped_rel->part_rels = NULL;
525 grouped_rel->live_parts = NULL;
526 grouped_rel->all_partrels = NULL;
527 grouped_rel->partexprs = NULL;
528 grouped_rel->nullable_partexprs = NULL;
529 grouped_rel->consider_partitionwise_join = false;
530
531 /*
532 * clear size estimates
533 */
534 grouped_rel->rows = 0;
535
536 return grouped_rel;
537}

References RelOptInfo::all_partrels, RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::consider_partitionwise_join, fb(), RelOptInfo::live_parts, makeNode, NIL, RelOptInfo::nparts, RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, RelOptInfo::ppilist, and RelOptInfo::rows.

Referenced by build_simple_grouped_rel(), and make_grouped_join_rel().

◆ build_join_rel()

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

Definition at line 795 of file relnode.c.

802{
803 RelOptInfo *joinrel;
804 List *restrictlist;
805
806 /* This function should be used only for join between parents. */
808
809 /*
810 * See if we already have a joinrel for this set of base rels.
811 */
812 joinrel = find_join_rel(root, joinrelids);
813
814 if (joinrel)
815 {
816 /*
817 * Yes, so we only need to figure the restrictlist for this particular
818 * pair of component relations.
819 */
822 joinrel,
823 outer_rel,
824 inner_rel,
825 sjinfo);
826 return joinrel;
827 }
828
829 /*
830 * Nope, so make one.
831 */
832 joinrel = makeNode(RelOptInfo);
833 joinrel->reloptkind = RELOPT_JOINREL;
834 joinrel->relids = bms_copy(joinrelids);
835 joinrel->rows = 0;
836 /* cheap startup cost is interesting iff not all tuples to be retrieved */
837 joinrel->consider_startup = (root->tuple_fraction > 0);
838 joinrel->consider_param_startup = false;
839 joinrel->consider_parallel = false;
840 joinrel->pgs_mask = root->glob->default_pgs_mask;
842 joinrel->pathlist = NIL;
843 joinrel->ppilist = NIL;
844 joinrel->partial_pathlist = NIL;
845 joinrel->cheapest_startup_path = NULL;
846 joinrel->cheapest_total_path = NULL;
848 /* init direct_lateral_relids from children; we'll finish it up below */
849 joinrel->direct_lateral_relids =
850 bms_union(outer_rel->direct_lateral_relids,
851 inner_rel->direct_lateral_relids);
854 joinrel->relid = 0; /* indicates not a baserel */
855 joinrel->rtekind = RTE_JOIN;
856 joinrel->min_attr = 0;
857 joinrel->max_attr = 0;
858 joinrel->attr_needed = NULL;
859 joinrel->attr_widths = NULL;
860 joinrel->notnullattnums = NULL;
861 joinrel->nulling_relids = NULL;
862 joinrel->lateral_vars = NIL;
863 joinrel->lateral_referencers = NULL;
864 joinrel->indexlist = NIL;
865 joinrel->statlist = NIL;
866 joinrel->pages = 0;
867 joinrel->tuples = 0;
868 joinrel->allvisfrac = 0;
869 joinrel->eclass_indexes = NULL;
870 joinrel->subroot = NULL;
871 joinrel->subplan_params = NIL;
872 joinrel->rel_parallel_workers = -1;
873 joinrel->amflags = 0;
874 joinrel->serverid = InvalidOid;
875 joinrel->userid = InvalidOid;
876 joinrel->useridiscurrent = false;
877 joinrel->fdwroutine = NULL;
878 joinrel->fdw_private = NULL;
879 joinrel->unique_for_rels = NIL;
880 joinrel->non_unique_for_rels = NIL;
881 joinrel->unique_rel = NULL;
882 joinrel->unique_pathkeys = NIL;
883 joinrel->unique_groupclause = NIL;
884 joinrel->baserestrictinfo = NIL;
885 joinrel->baserestrictcost.startup = 0;
886 joinrel->baserestrictcost.per_tuple = 0;
888 joinrel->joininfo = NIL;
889 joinrel->has_eclass_joins = false;
890 joinrel->consider_partitionwise_join = false; /* might get changed later */
891 joinrel->agg_info = NULL;
892 joinrel->grouped_rel = NULL;
893 joinrel->parent = NULL;
894 joinrel->top_parent = NULL;
895 joinrel->top_parent_relids = NULL;
896 joinrel->part_scheme = NULL;
897 joinrel->nparts = -1;
898 joinrel->boundinfo = NULL;
899 joinrel->partbounds_merged = false;
900 joinrel->partition_qual = NIL;
901 joinrel->part_rels = NULL;
902 joinrel->live_parts = NULL;
903 joinrel->all_partrels = NULL;
904 joinrel->partexprs = NULL;
905 joinrel->nullable_partexprs = NULL;
906
907 /* Compute information relevant to the foreign relations. */
909
910 /*
911 * Fill the joinrel's tlist with just the Vars and PHVs that need to be
912 * output from this join (ie, are needed for higher joinclauses or final
913 * output).
914 *
915 * NOTE: the tlist order for a join rel will depend on which pair of outer
916 * and inner rels we first try to build it from. But the contents should
917 * be the same regardless.
918 */
920 (sjinfo->jointype == JOIN_FULL));
922 (sjinfo->jointype != JOIN_INNER));
924
925 /*
926 * add_placeholders_to_joinrel also took care of adding the ph_lateral
927 * sets of any PlaceHolderVars computed here to direct_lateral_relids, so
928 * now we can finish computing that. This is much like the computation of
929 * the transitively-closed lateral_relids in min_join_parameterization,
930 * except that here we *do* have to consider the added PHVs.
931 */
932 joinrel->direct_lateral_relids =
933 bms_del_members(joinrel->direct_lateral_relids, joinrel->relids);
934
935 /*
936 * Construct restrict and join clause lists for the new joinrel. (The
937 * caller might or might not need the restrictlist, but I need it anyway
938 * for set_joinrel_size_estimates().)
939 */
940 restrictlist = build_joinrel_restrictlist(root, joinrel,
942 sjinfo);
944 *restrictlist_ptr = restrictlist;
946
947 /*
948 * This is also the right place to check whether the joinrel has any
949 * pending EquivalenceClass joins.
950 */
952
953 /*
954 * Set estimates of the joinrel's size.
955 */
957 sjinfo, restrictlist);
958
959 /*
960 * Set the consider_parallel flag if this joinrel could potentially be
961 * scanned within a parallel worker. If this flag is false for either
962 * inner_rel or outer_rel, then it must be false for the joinrel also.
963 * Even if both are true, there might be parallel-restricted expressions
964 * in the targetlist or quals.
965 *
966 * Note that if there are more than two rels in this relation, they could
967 * be divided between inner_rel and outer_rel in any arbitrary way. We
968 * assume this doesn't matter, because we should hit all the same baserels
969 * and joinclauses while building up to this joinrel no matter which we
970 * take; therefore, we should make the same decision here however we get
971 * here.
972 */
973 if (inner_rel->consider_parallel && outer_rel->consider_parallel &&
974 is_parallel_safe(root, (Node *) restrictlist) &&
975 is_parallel_safe(root, (Node *) joinrel->reltarget->exprs))
976 joinrel->consider_parallel = true;
977
978 /*
979 * Allow a plugin to editorialize on the new joinrel's properties. Actions
980 * might include altering the size estimate, clearing consider_parallel,
981 * or adjusting pgs_mask.
982 */
984 (*joinrel_setup_hook) (root, joinrel, outer_rel, inner_rel, sjinfo,
985 restrictlist);
986
987 /* Store the partition information. */
989 restrictlist);
990
991 /* Add the joinrel to the PlannerInfo. */
992 add_join_rel(root, joinrel);
993
994 /*
995 * Also, if dynamic-programming join search is active, add the new joinrel
996 * to the appropriate sublist. Note: you might think the Assert on number
997 * of members should be for equality, but some of the level 1 rels might
998 * have been joinrels already, so we can only assert <=.
999 */
1000 if (root->join_rel_level)
1001 {
1002 Assert(root->join_cur_level > 0);
1003 Assert(root->join_cur_level <= bms_num_members(joinrel->relids));
1004 root->join_rel_level[root->join_cur_level] =
1005 lappend(root->join_rel_level[root->join_cur_level], joinrel);
1006 }
1007
1008 return joinrel;
1009}
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1145
int bms_num_members(const Bitmapset *a)
Definition bitmapset.c:744
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition clauses.c:764
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
@ JOIN_FULL
Definition nodes.h:305
@ JOIN_INNER
Definition nodes.h:303
@ RELOPT_JOINREL
Definition pathnodes.h:966
void add_placeholders_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, bool can_null)
Definition relnode.c:1259
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition relnode.c:1181
static List * build_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition relnode.c:1444
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition relnode.c:1481
List * exprs
Definition pathnodes.h:1866
List * statlist
Definition pathnodes.h:1081
List * unique_for_rels
Definition pathnodes.h:1112
List * non_unique_for_rels
Definition pathnodes.h:1114
int rel_parallel_workers
Definition pathnodes.h:1091
Index baserestrict_min_security
Definition pathnodes.h:1134
JoinType jointype
Definition pathnodes.h:3217

References add_join_rel(), add_placeholders_to_joinrel(), RelOptInfo::agg_info, RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, Assert, RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_copy(), bms_del_members(), bms_num_members(), bms_union(), build_joinrel_joinlist(), build_joinrel_partition_info(), build_joinrel_restrictlist(), build_joinrel_tlist(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, RelOptInfo::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, fb(), find_join_rel(), RelOptInfo::grouped_rel, RelOptInfo::has_eclass_joins, has_relevant_eclass_joinclause(), RelOptInfo::indexlist, InvalidOid, IS_OTHER_REL, is_parallel_safe(), JOIN_FULL, JOIN_INNER, RelOptInfo::joininfo, joinrel_setup_hook, SpecialJoinInfo::jointype, lappend(), RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, RelOptInfo::live_parts, makeNode, RelOptInfo::max_attr, RelOptInfo::min_attr, min_join_parameterization(), NIL, RelOptInfo::non_unique_for_rels, RelOptInfo::notnullattnums, RelOptInfo::nparts, RelOptInfo::nulling_relids, RelOptInfo::pages, RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::pgs_mask, RelOptInfo::ppilist, RelOptInfo::rel_parallel_workers, RelOptInfo::relid, RelOptInfo::relids, RELOPT_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, root, RelOptInfo::rows, RTE_JOIN, RelOptInfo::rtekind, RelOptInfo::serverid, set_foreign_rel_properties(), set_joinrel_size_estimates(), QualCost::startup, RelOptInfo::statlist, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, RelOptInfo::tuples, RelOptInfo::unique_for_rels, RelOptInfo::unique_groupclause, RelOptInfo::unique_pathkeys, RelOptInfo::unique_rel, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by make_join_rel().

◆ build_join_rel_hash()

static void build_join_rel_hash ( PlannerInfo root)
static

Definition at line 616 of file relnode.c.

617{
618 HTAB *hashtab;
620 ListCell *l;
621
622 /* Create the hash table */
623 hash_ctl.keysize = sizeof(Relids);
624 hash_ctl.entrysize = sizeof(JoinHashEntry);
625 hash_ctl.hash = bitmap_hash;
626 hash_ctl.match = bitmap_match;
628 hashtab = hash_create("JoinRelHashTable",
629 256L,
630 &hash_ctl,
632
633 /* Insert all the already-existing joinrels */
634 foreach(l, root->join_rel_list)
635 {
636 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
638 bool found;
639
640 hentry = (JoinHashEntry *) hash_search(hashtab,
641 &(rel->relids),
643 &found);
644 Assert(!found);
645 hentry->join_rel = rel;
646 }
647
648 root->join_rel_hash = hashtab;
649}
uint32 bitmap_hash(const void *key, Size keysize)
Definition bitmapset.c:1418
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition bitmapset.c:1428
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
#define HASH_CONTEXT
Definition hsearch.h:102
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_COMPARE
Definition hsearch.h:99
#define HASH_FUNCTION
Definition hsearch.h:98
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define lfirst(lc)
Definition pg_list.h:172

References Assert, bitmap_hash(), bitmap_match(), CurrentMemoryContext, fb(), HASH_COMPARE, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FUNCTION, hash_search(), lfirst, RelOptInfo::relids, and root.

Referenced by find_join_rel().

◆ build_joinrel_joinlist()

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

Definition at line 1481 of file relnode.c.

1484{
1485 List *result;
1486
1487 /*
1488 * Collect all the clauses that syntactically belong above this level,
1489 * eliminating any duplicates (important since we will see many of the
1490 * same clauses arriving from both input relations).
1491 */
1492 result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1493 result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1494
1495 joinrel->joininfo = result;
1496}
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition relnode.c:1565

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

Referenced by build_join_rel().

◆ build_joinrel_partition_info()

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

Definition at line 2150 of file relnode.c.

2154{
2156
2157 /* Nothing to do if partitionwise join technique is disabled. */
2158 if ((joinrel->pgs_mask & PGS_CONSIDER_PARTITIONWISE) == 0)
2159 {
2160 Assert(!IS_PARTITIONED_REL(joinrel));
2161 return;
2162 }
2163
2164 /*
2165 * We can only consider this join as an input to further partitionwise
2166 * joins if (a) the input relations are partitioned and have
2167 * consider_partitionwise_join=true, (b) the partition schemes match, and
2168 * (c) we can identify an equi-join between the partition keys. Note that
2169 * if it were possible for have_partkey_equi_join to return different
2170 * answers for the same joinrel depending on which join ordering we try
2171 * first, this logic would break. That shouldn't happen, though, because
2172 * of the way the query planner deduces implied equalities and reorders
2173 * the joins. Please see optimizer/README for details.
2174 */
2175 if (outer_rel->part_scheme == NULL || inner_rel->part_scheme == NULL ||
2176 !outer_rel->consider_partitionwise_join ||
2177 !inner_rel->consider_partitionwise_join ||
2178 outer_rel->part_scheme != inner_rel->part_scheme ||
2180 sjinfo->jointype, restrictlist))
2181 {
2182 Assert(!IS_PARTITIONED_REL(joinrel));
2183 return;
2184 }
2185
2186 part_scheme = outer_rel->part_scheme;
2187
2188 /*
2189 * This function will be called only once for each joinrel, hence it
2190 * should not have partitioning fields filled yet.
2191 */
2192 Assert(!joinrel->part_scheme && !joinrel->partexprs &&
2193 !joinrel->nullable_partexprs && !joinrel->part_rels &&
2194 !joinrel->boundinfo);
2195
2196 /*
2197 * If the join relation is partitioned, it uses the same partitioning
2198 * scheme as the joining relations.
2199 *
2200 * Note: we calculate the partition bounds, number of partitions, and
2201 * child-join relations of the join relation in try_partitionwise_join().
2202 */
2203 joinrel->part_scheme = part_scheme;
2205 sjinfo->jointype);
2206
2207 /*
2208 * Set the consider_partitionwise_join flag.
2209 */
2210 Assert(outer_rel->consider_partitionwise_join);
2211 Assert(inner_rel->consider_partitionwise_join);
2212 joinrel->consider_partitionwise_join = true;
2213}
#define IS_PARTITIONED_REL(rel)
Definition pathnodes.h:1219
#define PGS_CONSIDER_PARTITIONWISE
Definition pathnodes.h:83
static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
Definition relnode.c:2519
static bool have_partkey_equi_join(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition relnode.c:2223

References Assert, RelOptInfo::consider_partitionwise_join, fb(), have_partkey_equi_join(), IS_PARTITIONED_REL, SpecialJoinInfo::jointype, PGS_CONSIDER_PARTITIONWISE, RelOptInfo::pgs_mask, root, and set_joinrel_partition_key_exprs().

Referenced by build_child_join_rel(), and build_join_rel().

◆ build_joinrel_restrictlist()

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

Definition at line 1444 of file relnode.c.

1449{
1450 List *result;
1452
1453 both_input_relids = bms_union(outer_rel->relids, inner_rel->relids);
1454
1455 /*
1456 * Collect all the clauses that syntactically belong at this level,
1457 * eliminating any duplicates (important since we will see many of the
1458 * same clauses arriving from both input relations).
1459 */
1460 result = subbuild_joinrel_restrictlist(root, joinrel, outer_rel,
1462 result = subbuild_joinrel_restrictlist(root, joinrel, inner_rel,
1463 both_input_relids, result);
1464
1465 /*
1466 * Add on any clauses derived from EquivalenceClasses. These cannot be
1467 * redundant with the clauses in the joininfo lists, so don't bother
1468 * checking.
1469 */
1470 result = list_concat(result,
1472 joinrel->relids,
1473 outer_rel->relids,
1474 inner_rel,
1475 sjinfo));
1476
1477 return result;
1478}
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
List * list_concat(List *list1, const List *list2)
Definition list.c:561
static List * subbuild_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel, Relids both_input_relids, List *new_restrictlist)
Definition relnode.c:1499

References bms_union(), fb(), generate_join_implied_equalities(), list_concat(), NIL, RelOptInfo::relids, root, and subbuild_joinrel_restrictlist().

Referenced by build_join_rel().

◆ build_joinrel_tlist()

static void build_joinrel_tlist ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo input_rel,
SpecialJoinInfo sjinfo,
List pushed_down_joins,
bool  can_null 
)
static

Definition at line 1259 of file relnode.c.

1264{
1265 Relids relids = joinrel->relids;
1266 int64 tuple_width = joinrel->reltarget->width;
1267 ListCell *vars;
1268 ListCell *lc;
1269
1270 foreach(vars, input_rel->reltarget->exprs)
1271 {
1272 Var *var = (Var *) lfirst(vars);
1273
1274 /*
1275 * For a PlaceHolderVar, we have to look up the PlaceHolderInfo.
1276 */
1277 if (IsA(var, PlaceHolderVar))
1278 {
1281
1282 /* Is it still needed above this joinrel? */
1283 if (bms_nonempty_difference(phinfo->ph_needed, relids))
1284 {
1285 /*
1286 * Yup, add it to the output. If this join potentially nulls
1287 * this input, we have to update the PHV's phnullingrels,
1288 * which means making a copy.
1289 */
1290 if (can_null)
1291 {
1292 phv = copyObject(phv);
1293 /* See comments above to understand this logic */
1294 if (sjinfo->ojrelid != 0 &&
1295 bms_is_member(sjinfo->ojrelid, relids) &&
1296 (bms_is_subset(phv->phrels, sjinfo->syn_righthand) ||
1297 (sjinfo->jointype == JOIN_FULL &&
1298 bms_is_subset(phv->phrels, sjinfo->syn_lefthand))))
1299 phv->phnullingrels = bms_add_member(phv->phnullingrels,
1300 sjinfo->ojrelid);
1301 foreach(lc, pushed_down_joins)
1302 {
1304
1305 Assert(bms_is_member(othersj->ojrelid, relids));
1306 if (bms_is_subset(phv->phrels, othersj->syn_righthand))
1307 phv->phnullingrels = bms_add_member(phv->phnullingrels,
1308 othersj->ojrelid);
1309 }
1310 phv->phnullingrels =
1311 bms_join(phv->phnullingrels,
1313 relids));
1314 }
1315
1316 joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1317 phv);
1318 /* Bubbling up the precomputed result has cost zero */
1319 tuple_width += phinfo->ph_width;
1320 }
1321 continue;
1322 }
1323
1324 /*
1325 * Otherwise, anything in a baserel or joinrel targetlist ought to be
1326 * a Var. (More general cases can only appear in appendrel child
1327 * rels, which will never be seen here.)
1328 */
1329 if (!IsA(var, Var))
1330 elog(ERROR, "unexpected node type in rel targetlist: %d",
1331 (int) nodeTag(var));
1332
1333 if (var->varno == ROWID_VAR)
1334 {
1335 /* UPDATE/DELETE/MERGE row identity vars are always needed */
1337 list_nth(root->row_identity_vars, var->varattno - 1);
1338
1339 /* Update reltarget width estimate from RowIdentityVarInfo */
1341 }
1342 else
1343 {
1345 int ndx;
1346
1347 /* Get the Var's original base rel */
1349
1350 /* Is it still needed above this joinrel? */
1351 ndx = var->varattno - baserel->min_attr;
1352 if (!bms_nonempty_difference(baserel->attr_needed[ndx], relids))
1353 continue; /* nope, skip it */
1354
1355 /* Update reltarget width estimate from baserel's attr_widths */
1356 tuple_width += baserel->attr_widths[ndx];
1357 }
1358
1359 /*
1360 * Add the Var to the output. If this join potentially nulls this
1361 * input, we have to update the Var's varnullingrels, which means
1362 * making a copy. But note that we don't ever add nullingrel bits to
1363 * row identity Vars (cf. comments in setrefs.c).
1364 */
1365 if (can_null && var->varno != ROWID_VAR)
1366 {
1367 var = copyObject(var);
1368 /* See comments above to understand this logic */
1369 if (sjinfo->ojrelid != 0 &&
1370 bms_is_member(sjinfo->ojrelid, relids) &&
1371 (bms_is_member(var->varno, sjinfo->syn_righthand) ||
1372 (sjinfo->jointype == JOIN_FULL &&
1373 bms_is_member(var->varno, sjinfo->syn_lefthand))))
1374 var->varnullingrels = bms_add_member(var->varnullingrels,
1375 sjinfo->ojrelid);
1376 foreach(lc, pushed_down_joins)
1377 {
1379
1380 Assert(bms_is_member(othersj->ojrelid, relids));
1381 if (bms_is_member(var->varno, othersj->syn_righthand))
1382 var->varnullingrels = bms_add_member(var->varnullingrels,
1383 othersj->ojrelid);
1384 }
1385 var->varnullingrels =
1386 bms_join(var->varnullingrels,
1388 relids));
1389 }
1390
1391 joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1392 var);
1393
1394 /* Vars have cost zero, so no need to adjust reltarget->cost */
1395 }
1396
1398}
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:292
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:412
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition bitmapset.c:1214
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:634
int64_t int64
Definition c.h:615
int32 clamp_width_est(int64 tuple_width)
Definition costsize.c:243
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
#define nodeTag(nodeptr)
Definition nodes.h:139
static void * list_nth(const List *list, int n)
Definition pg_list.h:299
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition placeholder.c:83
#define ROWID_VAR
Definition primnodes.h:246
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition relnode.c:544
Relids commute_above_r
Definition pathnodes.h:3220
Relids syn_lefthand
Definition pathnodes.h:3215
Relids syn_righthand
Definition pathnodes.h:3216
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270

References Assert, bms_add_member(), bms_intersect(), bms_is_member(), bms_is_subset(), bms_join(), bms_nonempty_difference(), clamp_width_est(), SpecialJoinInfo::commute_above_r, copyObject, elog, ERROR, PathTarget::exprs, fb(), find_base_rel(), find_placeholder_info(), IsA, JOIN_FULL, SpecialJoinInfo::jointype, lappend(), lfirst, list_nth(), nodeTag, SpecialJoinInfo::ojrelid, RelOptInfo::relids, RelOptInfo::reltarget, root, ROWID_VAR, RowIdentityVarInfo::rowidwidth, SpecialJoinInfo::syn_lefthand, SpecialJoinInfo::syn_righthand, Var::varattno, Var::varno, and PathTarget::width.

Referenced by build_join_rel().

◆ build_simple_grouped_rel()

RelOptInfo * build_simple_grouped_rel ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 448 of file relnode.c.

449{
450 RelOptInfo *grouped_rel;
451 RelAggInfo *agg_info;
452
453 /*
454 * We should have available aggregate expressions and grouping
455 * expressions, otherwise we cannot reach here.
456 */
457 Assert(root->agg_clause_list != NIL);
458 Assert(root->group_expr_list != NIL);
459
460 /* nothing to do for dummy rel */
461 if (IS_DUMMY_REL(rel))
462 return NULL;
463
464 /*
465 * Prepare the information needed to create grouped paths for this simple
466 * relation.
467 */
468 agg_info = create_rel_agg_info(root, rel, true);
469 if (agg_info == NULL)
470 return NULL;
471
472 /*
473 * If grouped paths for the given simple relation are not considered
474 * useful, skip building the grouped relation.
475 */
476 if (!agg_info->agg_useful)
477 return NULL;
478
479 /* Track the set of relids at which partial aggregation is applied */
480 agg_info->apply_agg_at = bms_copy(rel->relids);
481
482 /* build the grouped relation */
483 grouped_rel = build_grouped_rel(root, rel);
484 grouped_rel->reltarget = agg_info->target;
485 grouped_rel->rows = agg_info->grouped_rows;
486 grouped_rel->agg_info = agg_info;
487
488 rel->grouped_rel = grouped_rel;
489
490 return grouped_rel;
491}
#define IS_DUMMY_REL(r)
Definition pathnodes.h:2287
RelOptInfo * build_grouped_rel(PlannerInfo *root, RelOptInfo *rel)
Definition relnode.c:499
RelAggInfo * create_rel_agg_info(PlannerInfo *root, RelOptInfo *rel, bool calculate_grouped_rows)
Definition relnode.c:2691
Relids apply_agg_at
Definition pathnodes.h:1290
bool agg_useful
Definition pathnodes.h:1296
Cardinality grouped_rows
Definition pathnodes.h:1293
struct PathTarget * target
Definition pathnodes.h:1279

References RelOptInfo::agg_info, RelAggInfo::agg_useful, RelAggInfo::apply_agg_at, Assert, bms_copy(), build_grouped_rel(), create_rel_agg_info(), fb(), RelOptInfo::grouped_rel, RelAggInfo::grouped_rows, IS_DUMMY_REL, NIL, RelOptInfo::relids, RelOptInfo::reltarget, root, RelOptInfo::rows, and RelAggInfo::target.

Referenced by setup_simple_grouped_rels().

◆ build_simple_rel()

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

Definition at line 212 of file relnode.c.

213{
214 RelOptInfo *rel;
216
217 /* Rel should not exist already */
218 Assert(relid > 0 && relid < root->simple_rel_array_size);
219 if (root->simple_rel_array[relid] != NULL)
220 elog(ERROR, "rel %d already exists", relid);
221
222 /* Fetch RTE for relation */
223 rte = root->simple_rte_array[relid];
224 Assert(rte != NULL);
225
226 rel = makeNode(RelOptInfo);
228 rel->relids = bms_make_singleton(relid);
229 rel->rows = 0;
230 /* cheap startup cost is interesting iff not all tuples to be retrieved */
231 rel->consider_startup = (root->tuple_fraction > 0);
232 rel->consider_param_startup = false; /* might get changed later */
233 rel->consider_parallel = false; /* might get changed later */
234 rel->pgs_mask = root->glob->default_pgs_mask;
236 rel->pathlist = NIL;
237 rel->ppilist = NIL;
238 rel->partial_pathlist = NIL;
242 rel->relid = relid;
243 rel->rtekind = rte->rtekind;
244 /* min_attr, max_attr, attr_needed, attr_widths are set below */
245 rel->notnullattnums = NULL;
246 rel->lateral_vars = NIL;
247 rel->indexlist = NIL;
248 rel->statlist = NIL;
249 rel->pages = 0;
250 rel->tuples = 0;
251 rel->allvisfrac = 0;
252 rel->eclass_indexes = NULL;
253 rel->subroot = NULL;
254 rel->subplan_params = NIL;
255 rel->rel_parallel_workers = -1; /* set up in get_relation_info */
256 rel->amflags = 0;
257 rel->serverid = InvalidOid;
258 if (rte->rtekind == RTE_RELATION)
259 {
260 Assert(parent == NULL ||
261 parent->rtekind == RTE_RELATION ||
262 parent->rtekind == RTE_SUBQUERY);
263
264 /*
265 * For any RELATION rte, we need a userid with which to check
266 * permission access. Baserels simply use their own
267 * RTEPermissionInfo's checkAsUser.
268 *
269 * For otherrels normally there's no RTEPermissionInfo, so we use the
270 * parent's, which normally has one. The exceptional case is that the
271 * parent is a subquery, in which case the otherrel will have its own.
272 */
273 if (rel->reloptkind == RELOPT_BASEREL ||
275 parent->rtekind == RTE_SUBQUERY))
276 {
278
279 perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
280 rel->userid = perminfo->checkAsUser;
281 }
282 else
283 rel->userid = parent->userid;
284 }
285 else
286 rel->userid = InvalidOid;
287 rel->useridiscurrent = false;
288 rel->fdwroutine = NULL;
289 rel->fdw_private = NULL;
290 rel->unique_for_rels = NIL;
292 rel->unique_rel = NULL;
293 rel->unique_pathkeys = NIL;
294 rel->unique_groupclause = NIL;
295 rel->baserestrictinfo = NIL;
296 rel->baserestrictcost.startup = 0;
299 rel->joininfo = NIL;
300 rel->has_eclass_joins = false;
301 rel->consider_partitionwise_join = false; /* might get changed later */
302 rel->agg_info = NULL;
303 rel->grouped_rel = NULL;
304 rel->part_scheme = NULL;
305 rel->nparts = -1;
306 rel->boundinfo = NULL;
307 rel->partbounds_merged = false;
308 rel->partition_qual = NIL;
309 rel->part_rels = NULL;
310 rel->live_parts = NULL;
311 rel->all_partrels = NULL;
312 rel->partexprs = NULL;
313 rel->nullable_partexprs = NULL;
314
315 /*
316 * Pass assorted information down the inheritance hierarchy.
317 */
318 if (parent)
319 {
320 /* We keep back-links to immediate parent and topmost parent. */
321 rel->parent = parent;
322 rel->top_parent = parent->top_parent ? parent->top_parent : parent;
323 rel->top_parent_relids = rel->top_parent->relids;
324
325 /*
326 * A child rel is below the same outer joins as its parent. (We
327 * presume this info was already calculated for the parent.)
328 */
329 rel->nulling_relids = parent->nulling_relids;
330
331 /*
332 * Also propagate lateral-reference information from appendrel parent
333 * rels to their child rels. We intentionally give each child rel the
334 * same minimum parameterization, even though it's quite possible that
335 * some don't reference all the lateral rels. This is because any
336 * append path for the parent will have to have the same
337 * parameterization for every child anyway, and there's no value in
338 * forcing extra reparameterize_path() calls. Similarly, a lateral
339 * reference to the parent prevents use of otherwise-movable join rels
340 * for each child.
341 *
342 * It's possible for child rels to have their own children, in which
343 * case the topmost parent's lateral info propagates all the way down.
344 */
346 rel->lateral_relids = parent->lateral_relids;
348 }
349 else
350 {
351 rel->parent = NULL;
352 rel->top_parent = NULL;
353 rel->top_parent_relids = NULL;
354 rel->nulling_relids = NULL;
356 rel->lateral_relids = NULL;
358 }
359
360 /* Check type of rtable entry */
361 switch (rte->rtekind)
362 {
363 case RTE_RELATION:
364 /* Table --- retrieve statistics from the system catalogs */
365 get_relation_info(root, rte->relid, rte->inh, rel);
366 break;
367 case RTE_SUBQUERY:
368 case RTE_FUNCTION:
369 case RTE_TABLEFUNC:
370 case RTE_VALUES:
371 case RTE_CTE:
373
374 /*
375 * Subquery, function, tablefunc, values list, CTE, or ENR --- set
376 * up attr range and arrays
377 *
378 * Note: 0 is included in range to support whole-row Vars
379 */
380 rel->min_attr = 0;
381 rel->max_attr = list_length(rte->eref->colnames);
382 rel->attr_needed = (Relids *)
383 palloc0_array(Relids, rel->max_attr - rel->min_attr + 1);
384 rel->attr_widths = (int32 *)
385 palloc0_array(int32, rel->max_attr - rel->min_attr + 1);
386 break;
387 case RTE_RESULT:
388 /* RTE_RESULT has no columns, nor could it have whole-row Var */
389 rel->min_attr = 0;
390 rel->max_attr = -1;
391 rel->attr_needed = NULL;
392 rel->attr_widths = NULL;
393 break;
394 default:
395 elog(ERROR, "unrecognized RTE kind: %d",
396 (int) rte->rtekind);
397 break;
398 }
399
400 /*
401 * Allow a plugin to editorialize on the new RelOptInfo. This could
402 * involve editorializing on the information which get_relation_info
403 * obtained from the catalogs, such as altering the assumed relation size,
404 * removing an index, or adding a hypothetical index to the indexlist.
405 *
406 * An extension can also modify rel->pgs_mask here to control path
407 * generation.
408 */
410 (*build_simple_rel_hook) (root, rel, rte);
411
412 /*
413 * Apply the parent's quals to the child, with appropriate substitution of
414 * variables. If any resulting clause is reduced to constant FALSE or
415 * NULL, apply_child_basequals returns false to indicate that scanning
416 * this relation won't yield any rows. In this case, we mark the child as
417 * dummy right away. (We must do this immediately so that pruning works
418 * correctly when recursing in expand_partitioned_rtentry.)
419 */
420 if (parent)
421 {
422 AppendRelInfo *appinfo = root->append_rel_array[relid];
423
424 Assert(appinfo != NULL);
425 if (!apply_child_basequals(root, parent, rel, rte, appinfo))
426 {
427 /*
428 * A restriction clause reduced to constant FALSE or NULL after
429 * substitution. Mark the child as dummy so that it need not be
430 * scanned.
431 */
432 mark_dummy_rel(rel);
433 }
434 }
435
436 /* Save the finished struct in the query's simple_rel_array */
437 root->simple_rel_array[relid] = rel;
438
439 return rel;
440}
Bitmapset * bms_make_singleton(int x)
Definition bitmapset.c:216
int32_t int32
Definition c.h:614
#define palloc0_array(type, count)
Definition fe_memutils.h:77
bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, RangeTblEntry *childRTE, AppendRelInfo *appinfo)
Definition inherit.c:837
void mark_dummy_rel(RelOptInfo *rel)
Definition joinrels.c:1513
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
@ RTE_CTE
@ RTE_NAMEDTUPLESTORE
@ RTE_VALUES
@ RTE_SUBQUERY
@ RTE_RESULT
@ RTE_FUNCTION
@ RTE_TABLEFUNC
@ RTE_RELATION
@ RELOPT_BASEREL
Definition pathnodes.h:965
@ RELOPT_OTHER_MEMBER_REL
Definition pathnodes.h:967
static int list_length(const List *l)
Definition pg_list.h:152
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition plancat.c:121
build_simple_rel_hook_type build_simple_rel_hook
Definition relnode.c:51

References RelOptInfo::agg_info, RelOptInfo::all_partrels, RelOptInfo::allvisfrac, RelOptInfo::amflags, apply_child_basequals(), Assert, RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictcost, RelOptInfo::baserestrictinfo, bms_make_singleton(), build_simple_rel_hook, RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_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, elog, ERROR, fb(), get_relation_info(), getRTEPermissionInfo(), RelOptInfo::grouped_rel, RelOptInfo::has_eclass_joins, RelOptInfo::indexlist, InvalidOid, RelOptInfo::joininfo, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, list_length(), RelOptInfo::live_parts, makeNode, mark_dummy_rel(), RelOptInfo::max_attr, RelOptInfo::min_attr, NIL, RelOptInfo::non_unique_for_rels, RelOptInfo::notnullattnums, RelOptInfo::nparts, RelOptInfo::nulling_relids, RelOptInfo::pages, palloc0_array, RelOptInfo::partbounds_merged, RelOptInfo::partial_pathlist, RelOptInfo::partition_qual, RelOptInfo::pathlist, QualCost::per_tuple, RelOptInfo::pgs_mask, RelOptInfo::ppilist, RelOptInfo::rel_parallel_workers, RelOptInfo::relid, RelOptInfo::relids, RELOPT_BASEREL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, root, RelOptInfo::rows, RTE_CTE, RTE_FUNCTION, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RelOptInfo::rtekind, RelOptInfo::serverid, QualCost::startup, RelOptInfo::statlist, RelOptInfo::subplan_params, RelOptInfo::subroot, RelOptInfo::top_parent_relids, RelOptInfo::tuples, RelOptInfo::unique_for_rels, RelOptInfo::unique_groupclause, RelOptInfo::unique_pathkeys, RelOptInfo::unique_rel, 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().

◆ create_rel_agg_info()

RelAggInfo * create_rel_agg_info ( PlannerInfo root,
RelOptInfo rel,
bool  calculate_grouped_rows 
)

Definition at line 2691 of file relnode.c.

2693{
2694 ListCell *lc;
2695 RelAggInfo *result;
2696 PathTarget *agg_input;
2697 PathTarget *target;
2698 List *group_clauses = NIL;
2699 List *group_exprs = NIL;
2700
2701 /*
2702 * The lists of aggregate expressions and grouping expressions should have
2703 * been constructed.
2704 */
2705 Assert(root->agg_clause_list != NIL);
2706 Assert(root->group_expr_list != NIL);
2707
2708 /*
2709 * If this is a child rel, the grouped rel for its parent rel must have
2710 * been created if it can. So we can just use parent's RelAggInfo if
2711 * there is one, with appropriate variable substitutions.
2712 */
2713 if (IS_OTHER_REL(rel))
2714 {
2715 RelOptInfo *grouped_rel;
2716 RelAggInfo *agg_info;
2717
2718 grouped_rel = rel->top_parent->grouped_rel;
2719 if (grouped_rel == NULL)
2720 return NULL;
2721
2722 Assert(IS_GROUPED_REL(grouped_rel));
2723
2724 /* Must do multi-level transformation */
2725 agg_info = (RelAggInfo *)
2727 (Node *) grouped_rel->agg_info,
2728 rel,
2729 rel->top_parent);
2730
2731 agg_info->apply_agg_at = NULL; /* caller will change this later */
2732
2734 {
2735 agg_info->grouped_rows =
2737 rel->rows, NULL, NULL);
2738
2739 /*
2740 * The grouped paths for the given relation are considered useful
2741 * iff the average group size is no less than
2742 * min_eager_agg_group_size.
2743 */
2744 agg_info->agg_useful =
2745 (rel->rows / agg_info->grouped_rows) >= min_eager_agg_group_size;
2746 }
2747
2748 return agg_info;
2749 }
2750
2751 /* Check if it's possible to produce grouped paths for this relation. */
2753 return NULL;
2754
2755 /*
2756 * Create targets for the grouped paths and for the input paths of the
2757 * grouped paths.
2758 */
2759 target = create_empty_pathtarget();
2760 agg_input = create_empty_pathtarget();
2761
2762 /* ... and initialize these targets */
2763 if (!init_grouping_targets(root, rel, target, agg_input,
2764 &group_clauses, &group_exprs))
2765 return NULL;
2766
2767 /*
2768 * Eager aggregation is not applicable if there are no available grouping
2769 * expressions.
2770 */
2771 if (group_clauses == NIL)
2772 return NULL;
2773
2774 /* Add aggregates to the grouping target */
2775 foreach(lc, root->agg_clause_list)
2776 {
2778 Aggref *aggref;
2779
2780 Assert(IsA(ac_info->aggref, Aggref));
2781
2782 aggref = (Aggref *) copyObject(ac_info->aggref);
2784
2785 add_column_to_pathtarget(target, (Expr *) aggref, 0);
2786 }
2787
2788 /* Set the estimated eval cost and output width for both targets */
2790 set_pathtarget_cost_width(root, agg_input);
2791
2792 /* build the RelAggInfo result */
2793 result = makeNode(RelAggInfo);
2794 result->target = target;
2795 result->agg_input = agg_input;
2796 result->group_clauses = group_clauses;
2797 result->group_exprs = group_exprs;
2798 result->apply_agg_at = NULL; /* caller will change this later */
2799
2801 {
2803 rel->rows, NULL, NULL);
2804
2805 /*
2806 * The grouped paths for the given relation are considered useful iff
2807 * the average group size is no less than min_eager_agg_group_size.
2808 */
2809 result->agg_useful =
2810 (rel->rows / result->grouped_rows) >= min_eager_agg_group_size;
2811 }
2812
2813 return result;
2814}
double min_eager_agg_group_size
Definition allpaths.c:84
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, RelOptInfo *childrel, RelOptInfo *parentrel)
Definition appendinfo.c:597
PathTarget * set_pathtarget_cost_width(PlannerInfo *root, PathTarget *target)
Definition costsize.c:6510
@ AGGSPLIT_INITIAL_SERIAL
Definition nodes.h:389
#define IS_GROUPED_REL(rel)
Definition pathnodes.h:1245
#define lfirst_node(type, lc)
Definition pg_list.h:176
void mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
Definition planner.c:5818
static bool init_grouping_targets(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, PathTarget *agg_input, List **group_clauses, List **group_exprs)
Definition relnode.c:2901
static bool eager_aggregation_possible_for_relation(PlannerInfo *root, RelOptInfo *rel)
Definition relnode.c:2821
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
Definition selfuncs.c:3788
List * group_exprs
Definition pathnodes.h:1287
List * group_clauses
Definition pathnodes.h:1285
struct PathTarget * agg_input
Definition pathnodes.h:1282
void add_column_to_pathtarget(PathTarget *target, Expr *expr, Index sortgroupref)
Definition tlist.c:704

References add_column_to_pathtarget(), adjust_appendrel_attrs_multilevel(), RelOptInfo::agg_info, RelAggInfo::agg_input, RelAggInfo::agg_useful, AGGSPLIT_INITIAL_SERIAL, RelAggInfo::apply_agg_at, Assert, copyObject, create_empty_pathtarget(), eager_aggregation_possible_for_relation(), estimate_num_groups(), fb(), RelAggInfo::group_clauses, RelAggInfo::group_exprs, RelOptInfo::grouped_rel, RelAggInfo::grouped_rows, init_grouping_targets(), IS_GROUPED_REL, IS_OTHER_REL, IsA, lfirst_node, makeNode, mark_partial_aggref(), min_eager_agg_group_size, NIL, root, RelOptInfo::rows, set_pathtarget_cost_width(), and RelAggInfo::target.

Referenced by build_simple_grouped_rel(), and make_grouped_join_rel().

◆ eager_aggregation_possible_for_relation()

static bool eager_aggregation_possible_for_relation ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 2821 of file relnode.c.

2822{
2823 ListCell *lc;
2824 int cur_relid;
2825
2826 /*
2827 * Check to see if the given relation is in the nullable side of an outer
2828 * join. In this case, we cannot push a partial aggregation down to the
2829 * relation, because the NULL-extended rows produced by the outer join
2830 * would not be available when we perform the partial aggregation, while
2831 * with a non-eager-aggregation plan these rows are available for the
2832 * top-level aggregation. Doing so may result in the rows being grouped
2833 * differently than expected, or produce incorrect values from the
2834 * aggregate functions.
2835 */
2836 cur_relid = -1;
2837 while ((cur_relid = bms_next_member(rel->relids, cur_relid)) >= 0)
2838 {
2840
2841 if (baserel == NULL)
2842 continue; /* ignore outer joins in rel->relids */
2843
2844 if (!bms_is_subset(baserel->nulling_relids, rel->relids))
2845 return false;
2846 }
2847
2848 /*
2849 * For now we don't try to support PlaceHolderVars.
2850 */
2851 foreach(lc, rel->reltarget->exprs)
2852 {
2853 Expr *expr = lfirst(lc);
2854
2855 if (IsA(expr, PlaceHolderVar))
2856 return false;
2857 }
2858
2859 /* Caller should only pass base relations or joins. */
2861 rel->reloptkind == RELOPT_JOINREL);
2862
2863 /*
2864 * Check if all aggregate expressions can be evaluated on this relation
2865 * level.
2866 */
2867 foreach(lc, root->agg_clause_list)
2868 {
2870
2871 Assert(IsA(ac_info->aggref, Aggref));
2872
2873 /*
2874 * Give up if any aggregate requires relations other than the current
2875 * one. If the aggregate requires the current relation plus
2876 * additional relations, grouping the current relation could make some
2877 * input rows unavailable for the higher aggregate and may reduce the
2878 * number of input rows it receives. If the aggregate does not
2879 * require the current relation at all, it should not be grouped, as
2880 * we do not support joining two grouped relations.
2881 */
2882 if (!bms_is_subset(ac_info->agg_eval_at, rel->relids))
2883 return false;
2884 }
2885
2886 return true;
2887}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
RelOptInfo * find_base_rel_ignore_join(PlannerInfo *root, int relid)
Definition relnode.c:584

References Assert, bms_is_subset(), bms_next_member(), PathTarget::exprs, fb(), find_base_rel_ignore_join(), IsA, lfirst, lfirst_node, RelOptInfo::relids, RELOPT_BASEREL, RELOPT_JOINREL, RelOptInfo::reloptkind, RelOptInfo::reltarget, and root.

Referenced by create_rel_agg_info().

◆ expand_planner_arrays()

void expand_planner_arrays ( PlannerInfo root,
int  add_size 
)

Definition at line 183 of file relnode.c.

184{
185 int new_size;
186
187 Assert(add_size > 0);
188
189 new_size = root->simple_rel_array_size + add_size;
190
191 root->simple_rel_array =
192 repalloc0_array(root->simple_rel_array, RelOptInfo *, root->simple_rel_array_size, new_size);
193
194 root->simple_rte_array =
195 repalloc0_array(root->simple_rte_array, RangeTblEntry *, root->simple_rel_array_size, new_size);
196
197 if (root->append_rel_array)
198 root->append_rel_array =
199 repalloc0_array(root->append_rel_array, AppendRelInfo *, root->simple_rel_array_size, new_size);
200 else
201 root->append_rel_array =
203
204 root->simple_rel_array_size = new_size;
205}
#define repalloc0_array(pointer, type, oldcount, count)
Definition palloc.h:109
Size add_size(Size s1, Size s2)
Definition shmem.c:485

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

Referenced by expand_inherited_rtentry(), and expand_partitioned_rtentry().

◆ fetch_upper_rel()

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

Definition at line 1617 of file relnode.c.

1618{
1620 ListCell *lc;
1621
1622 /*
1623 * For the moment, our indexing data structure is just a List for each
1624 * relation kind. If we ever get so many of one kind that this stops
1625 * working well, we can improve it. No code outside this function should
1626 * assume anything about how to find a particular upperrel.
1627 */
1628
1629 /* If we already made this upperrel for the query, return it */
1630 foreach(lc, root->upper_rels[kind])
1631 {
1633
1634 if (bms_equal(upperrel->relids, relids))
1635 return upperrel;
1636 }
1637
1639 upperrel->reloptkind = RELOPT_UPPER_REL;
1640 upperrel->relids = bms_copy(relids);
1641 upperrel->pgs_mask = root->glob->default_pgs_mask;
1642
1643 /* cheap startup cost is interesting iff not all tuples to be retrieved */
1644 upperrel->consider_startup = (root->tuple_fraction > 0);
1645 upperrel->consider_param_startup = false;
1646 upperrel->consider_parallel = false; /* might get changed later */
1647 upperrel->reltarget = create_empty_pathtarget();
1648 upperrel->pathlist = NIL;
1649 upperrel->cheapest_startup_path = NULL;
1650 upperrel->cheapest_total_path = NULL;
1651 upperrel->cheapest_parameterized_paths = NIL;
1652
1653 root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1654
1655 return upperrel;
1656}
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:142
@ RELOPT_UPPER_REL
Definition pathnodes.h:969

References bms_copy(), bms_equal(), create_empty_pathtarget(), fb(), lappend(), lfirst, makeNode, NIL, RELOPT_UPPER_REL, and root.

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

◆ find_base_rel()

◆ find_base_rel_ignore_join()

RelOptInfo * find_base_rel_ignore_join ( PlannerInfo root,
int  relid 
)

Definition at line 584 of file relnode.c.

585{
586 /* use an unsigned comparison to prevent negative array element access */
587 if ((uint32) relid < (uint32) root->simple_rel_array_size)
588 {
589 RelOptInfo *rel;
591
592 rel = root->simple_rel_array[relid];
593 if (rel)
594 return rel;
595
596 /*
597 * We could just return NULL here, but for debugging purposes it seems
598 * best to actually verify that the relid is an outer join and not
599 * something weird.
600 */
601 rte = root->simple_rte_array[relid];
602 if (rte && rte->rtekind == RTE_JOIN && rte->jointype != JOIN_INNER)
603 return NULL;
604 }
605
606 elog(ERROR, "no relation entry for relid %d", relid);
607
608 return NULL; /* keep compiler quiet */
609}

References elog, ERROR, fb(), JOIN_INNER, root, and RTE_JOIN.

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

◆ find_base_rel_noerr()

RelOptInfo * find_base_rel_noerr ( PlannerInfo root,
int  relid 
)

Definition at line 566 of file relnode.c.

567{
568 /* use an unsigned comparison to prevent negative array element access */
569 if ((uint32) relid < (uint32) root->simple_rel_array_size)
570 return root->simple_rel_array[relid];
571 return NULL;
572}

References fb(), and root.

Referenced by all_rows_selectable().

◆ find_childrel_parents()

Relids find_childrel_parents ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1668 of file relnode.c.

1669{
1670 Relids result = NULL;
1671
1673 Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1674
1675 do
1676 {
1677 AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1679
1680 result = bms_add_member(result, prelid);
1681
1682 /* traverse up to the parent rel, loop if it's also a child rel */
1683 rel = find_base_rel(root, prelid);
1684 } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1685
1687
1688 return result;
1689}
unsigned int Index
Definition c.h:700
Index parent_relid
Definition pathnodes.h:3288

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

Referenced by check_index_predicates(), and generate_implied_equalities_for_column().

◆ find_join_rel()

RelOptInfo * find_join_rel ( PlannerInfo root,
Relids  relids 
)

Definition at line 657 of file relnode.c.

658{
659 /*
660 * Switch to using hash lookup when list grows "too long". The threshold
661 * is arbitrary and is known only here.
662 */
663 if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
665
666 /*
667 * Use either hashtable lookup or linear search, as appropriate.
668 *
669 * Note: the seemingly redundant hashkey variable is used to avoid taking
670 * the address of relids; unless the compiler is exceedingly smart, doing
671 * so would force relids out of a register and thus probably slow down the
672 * list-search case.
673 */
674 if (root->join_rel_hash)
675 {
676 Relids hashkey = relids;
678
679 hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
680 &hashkey,
681 HASH_FIND,
682 NULL);
683 if (hentry)
684 return hentry->join_rel;
685 }
686 else
687 {
688 ListCell *l;
689
690 foreach(l, root->join_rel_list)
691 {
692 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
693
694 if (bms_equal(rel->relids, relids))
695 return rel;
696 }
697 }
698
699 return NULL;
700}
@ HASH_FIND
Definition hsearch.h:113
static void build_join_rel_hash(PlannerInfo *root)
Definition relnode.c:616
RelOptInfo * join_rel
Definition relnode.c:47

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

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

◆ find_param_path_info()

ParamPathInfo * find_param_path_info ( RelOptInfo rel,
Relids  required_outer 
)

Definition at line 2048 of file relnode.c.

2049{
2050 ListCell *lc;
2051
2052 foreach(lc, rel->ppilist)
2053 {
2055
2056 if (bms_equal(ppi->ppi_req_outer, required_outer))
2057 return ppi;
2058 }
2059
2060 return NULL;
2061}

References bms_equal(), fb(), lfirst, and RelOptInfo::ppilist.

Referenced by get_appendrel_parampathinfo(), get_baserel_parampathinfo(), get_joinrel_parampathinfo(), and reparameterize_path_by_child().

◆ get_appendrel_parampathinfo()

ParamPathInfo * get_appendrel_parampathinfo ( RelOptInfo appendrel,
Relids  required_outer 
)

Definition at line 2015 of file relnode.c.

2016{
2018
2019 /* If rel has LATERAL refs, every path for it should account for them */
2020 Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
2021
2022 /* Unparameterized paths have no ParamPathInfo */
2024 return NULL;
2025
2027
2028 /* If we already have a PPI for this parameterization, just return it */
2030 return ppi;
2031
2032 /* Else build the ParamPathInfo */
2034 ppi->ppi_req_outer = required_outer;
2035 ppi->ppi_rows = 0;
2036 ppi->ppi_clauses = NIL;
2037 ppi->ppi_serials = NULL;
2038 appendrel->ppilist = lappend(appendrel->ppilist, ppi);
2039
2040 return ppi;
2041}
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:575
#define bms_is_empty(a)
Definition bitmapset.h:118
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition relnode.c:2048

References Assert, bms_is_empty, bms_is_subset(), bms_overlap(), fb(), find_param_path_info(), lappend(), makeNode, and NIL.

Referenced by create_append_path().

◆ get_baserel_parampathinfo()

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

Definition at line 1704 of file relnode.c.

1706{
1709 List *pclauses;
1710 List *eqclauses;
1712 double rows;
1713 ListCell *lc;
1714
1715 /* If rel has LATERAL refs, every path for it should account for them */
1716 Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1717
1718 /* Unparameterized paths have no ParamPathInfo */
1720 return NULL;
1721
1723
1724 /* If we already have a PPI for this parameterization, just return it */
1726 return ppi;
1727
1728 /*
1729 * Identify all joinclauses that are movable to this base rel given this
1730 * parameterization.
1731 */
1733 pclauses = NIL;
1734 foreach(lc, baserel->joininfo)
1735 {
1736 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1737
1739 baserel->relids,
1740 joinrelids))
1741 pclauses = lappend(pclauses, rinfo);
1742 }
1743
1744 /*
1745 * Add in joinclauses generated by EquivalenceClasses, too. (These
1746 * necessarily satisfy join_clause_is_movable_into; but in assert-enabled
1747 * builds, let's verify that.)
1748 */
1750 joinrelids,
1752 baserel,
1753 NULL);
1754#ifdef USE_ASSERT_CHECKING
1755 foreach(lc, eqclauses)
1756 {
1757 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1758
1760 baserel->relids,
1761 joinrelids));
1762 }
1763#endif
1765
1766 /* Compute set of serial numbers of the enforced clauses */
1767 pserials = NULL;
1768 foreach(lc, pclauses)
1769 {
1770 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1771
1773 }
1774
1775 /* Estimate the number of rows returned by the parameterized scan */
1777
1778 /* And now we can build the ParamPathInfo */
1780 ppi->ppi_req_outer = required_outer;
1781 ppi->ppi_rows = rows;
1782 ppi->ppi_clauses = pclauses;
1783 ppi->ppi_serials = pserials;
1784 baserel->ppilist = lappend(baserel->ppilist, ppi);
1785
1786 return ppi;
1787}
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition costsize.c:5522
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)

References Assert, bms_add_member(), bms_is_empty, bms_is_subset(), bms_overlap(), bms_union(), fb(), find_param_path_info(), generate_join_implied_equalities(), get_parameterized_baserel_size(), join_clause_is_movable_into(), lappend(), lfirst, list_concat(), makeNode, NIL, RestrictInfo::rinfo_serial, and root.

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

◆ get_expression_sortgroupref()

static Index get_expression_sortgroupref ( PlannerInfo root,
Expr expr 
)
static

Definition at line 3172 of file relnode.c.

3173{
3174 ListCell *lc;
3175
3176 Assert(IsA(expr, Var));
3177
3178 foreach(lc, root->group_expr_list)
3179 {
3181 ListCell *lc1;
3182
3183 Assert(IsA(ge_info->expr, Var));
3184 Assert(ge_info->sortgroupref > 0);
3185
3186 if (equal(expr, ge_info->expr))
3187 return ge_info->sortgroupref;
3188
3189 if (ge_info->ec == NULL ||
3190 !bms_is_member(((Var *) expr)->varno, ge_info->ec->ec_relids))
3191 continue;
3192
3193 /*
3194 * Scan the EquivalenceClass, looking for a match to the given
3195 * expression. We ignore child members here.
3196 */
3197 foreach(lc1, ge_info->ec->ec_members)
3198 {
3200
3201 /* Child members should not exist in ec_members */
3202 Assert(!em->em_is_child);
3203
3204 if (equal(expr, em->em_expr))
3205 return ge_info->sortgroupref;
3206 }
3207 }
3208
3209 /* no match is found */
3210 return 0;
3211}
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223

References Assert, bms_is_member(), equal(), fb(), IsA, lfirst, lfirst_node, and root.

Referenced by init_grouping_targets().

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

1824{
1829 List *pclauses;
1830 List *eclauses;
1832 double rows;
1833 ListCell *lc;
1834
1835 /* If rel has LATERAL refs, every path for it should account for them */
1837
1838 /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1840 return NULL;
1841
1843
1844 /*
1845 * Identify all joinclauses that are movable to this join rel given this
1846 * parameterization. These are the clauses that are movable into this
1847 * join, but not movable into either input path. Treat an unparameterized
1848 * input path as not accepting parameterized clauses (because it won't,
1849 * per the shortcut exit above), even though the joinclause movement rules
1850 * might allow the same clauses to be moved into a parameterized path for
1851 * that rel.
1852 */
1854 if (outer_path->param_info)
1855 outer_and_req = bms_union(outer_path->parent->relids,
1857 else
1858 outer_and_req = NULL; /* outer path does not accept parameters */
1859 if (inner_path->param_info)
1860 inner_and_req = bms_union(inner_path->parent->relids,
1862 else
1863 inner_and_req = NULL; /* inner path does not accept parameters */
1864
1865 pclauses = NIL;
1866 foreach(lc, joinrel->joininfo)
1867 {
1868 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1869
1871 joinrel->relids,
1872 join_and_req) &&
1874 outer_path->parent->relids,
1875 outer_and_req) &&
1877 inner_path->parent->relids,
1879 pclauses = lappend(pclauses, rinfo);
1880 }
1881
1882 /* Consider joinclauses generated by EquivalenceClasses, too */
1886 joinrel,
1887 NULL);
1888 /* We only want ones that aren't movable to lower levels */
1889 dropped_ecs = NIL;
1890 foreach(lc, eclauses)
1891 {
1892 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1893
1895 joinrel->relids,
1896 join_and_req));
1898 outer_path->parent->relids,
1900 continue; /* drop if movable into LHS */
1902 inner_path->parent->relids,
1904 {
1905 /* drop if movable into RHS, but remember EC for use below */
1906 Assert(rinfo->left_ec == rinfo->right_ec);
1907 dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1908 continue;
1909 }
1910 pclauses = lappend(pclauses, rinfo);
1911 }
1912
1913 /*
1914 * EquivalenceClasses are harder to deal with than we could wish, because
1915 * of the fact that a given EC can generate different clauses depending on
1916 * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1917 * LHS and RHS of the current join and Z is in required_outer, and further
1918 * suppose that the inner_path is parameterized by both X and Z. The code
1919 * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1920 * and in the latter case will have discarded it as being movable into the
1921 * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1922 * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1923 * not have produced both, and we can't readily tell from here which one
1924 * it did pick. If we add no clause to this join, we'll end up with
1925 * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1926 * constrained to be equal to the other members of the EC. (When we come
1927 * to join Z to this X/Y path, we will certainly drop whichever EC clause
1928 * is generated at that join, so this omission won't get fixed later.)
1929 *
1930 * To handle this, for each EC we discarded such a clause from, try to
1931 * generate a clause connecting the required_outer rels to the join's LHS
1932 * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1933 * the clause can't be moved to the LHS, add it to the current join's
1934 * restriction clauses. (If an EC cannot generate such a clause then it
1935 * has nothing that needs to be enforced here, while if the clause can be
1936 * moved into the LHS then it should have been enforced within that path.)
1937 *
1938 * Note that we don't need similar processing for ECs whose clause was
1939 * considered to be movable into the LHS, because the LHS can't refer to
1940 * the RHS so there is no comparable ambiguity about what it might
1941 * actually be enforcing internally.
1942 */
1943 if (dropped_ecs)
1944 {
1946
1947 real_outer_and_req = bms_union(outer_path->parent->relids,
1949 eclauses =
1954 outer_path->parent);
1955 foreach(lc, eclauses)
1956 {
1957 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1958
1960 outer_path->parent->relids,
1962 if (!join_clause_is_movable_into(rinfo,
1963 outer_path->parent->relids,
1965 pclauses = lappend(pclauses, rinfo);
1966 }
1967 }
1968
1969 /*
1970 * Now, attach the identified moved-down clauses to the caller's
1971 * restrict_clauses list. By using list_concat in this order, we leave
1972 * the original list structure of restrict_clauses undamaged.
1973 */
1975
1976 /* If we already have a PPI for this parameterization, just return it */
1977 if ((ppi = find_param_path_info(joinrel, required_outer)))
1978 return ppi;
1979
1980 /* Estimate the number of rows returned by the parameterized join */
1981 rows = get_parameterized_joinrel_size(root, joinrel,
1982 outer_path,
1983 inner_path,
1984 sjinfo,
1986
1987 /*
1988 * And now we can build the ParamPathInfo. No point in saving the
1989 * input-pair-dependent clause list, though.
1990 *
1991 * Note: in GEQO mode, we'll be called in a temporary memory context, but
1992 * the joinrel structure is there too, so no problem.
1993 */
1995 ppi->ppi_req_outer = required_outer;
1996 ppi->ppi_rows = rows;
1997 ppi->ppi_clauses = NIL;
1998 ppi->ppi_serials = NULL;
1999 joinrel->ppilist = lappend(joinrel->ppilist, ppi);
2000
2001 return ppi;
2002}
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition costsize.c:5603
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
#define PATH_REQ_OUTER(path)
Definition pathnodes.h:2003

References Assert, bms_is_empty, bms_is_subset(), bms_overlap(), bms_union(), fb(), find_param_path_info(), generate_join_implied_equalities(), generate_join_implied_equalities_for_ecs(), get_parameterized_joinrel_size(), join_clause_is_movable_into(), RelOptInfo::joininfo, lappend(), RelOptInfo::lateral_relids, lfirst, list_concat(), makeNode, NIL, PATH_REQ_OUTER, RelOptInfo::ppilist, RelOptInfo::relids, and root.

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

◆ get_param_path_clause_serials()

Bitmapset * get_param_path_clause_serials ( Path path)

Definition at line 2069 of file relnode.c.

2070{
2071 if (path->param_info == NULL)
2072 return NULL; /* not parameterized */
2073
2074 /*
2075 * We don't currently support parameterized MergeAppend paths, as
2076 * explained in the comments for generate_orderedappend_paths.
2077 */
2078 Assert(!IsA(path, MergeAppendPath));
2079
2080 if (IsA(path, NestPath) ||
2081 IsA(path, MergePath) ||
2082 IsA(path, HashPath))
2083 {
2084 /*
2085 * For a join path, combine clauses enforced within either input path
2086 * with those enforced as joinrestrictinfo in this path. Note that
2087 * joinrestrictinfo may include some non-pushed-down clauses, but for
2088 * current purposes it's okay if we include those in the result. (To
2089 * be more careful, we could check for clause_relids overlapping the
2090 * path parameterization, but it's not worth the cycles for now.)
2091 */
2092 JoinPath *jpath = (JoinPath *) path;
2094 ListCell *lc;
2095
2096 pserials = NULL;
2101 foreach(lc, jpath->joinrestrictinfo)
2102 {
2103 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2104
2106 }
2107 return pserials;
2108 }
2109 else if (IsA(path, AppendPath))
2110 {
2111 /*
2112 * For an appendrel, take the intersection of the sets of clauses
2113 * enforced in each input path.
2114 */
2115 AppendPath *apath = (AppendPath *) path;
2117 ListCell *lc;
2118
2119 pserials = NULL;
2120 foreach(lc, apath->subpaths)
2121 {
2122 Path *subpath = (Path *) lfirst(lc);
2124
2126 if (lc == list_head(apath->subpaths))
2128 else
2130 }
2131 return pserials;
2132 }
2133 else
2134 {
2135 /*
2136 * Otherwise, it's a baserel path and we can use the
2137 * previously-computed set of serial numbers.
2138 */
2139 return path->param_info->ppi_serials;
2140 }
2141}
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1093
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:901
Datum subpath(PG_FUNCTION_ARGS)
Definition ltree_op.c:311
static ListCell * list_head(const List *l)
Definition pg_list.h:128
Bitmapset * get_param_path_clause_serials(Path *path)
Definition relnode.c:2069
Path * outerjoinpath
Definition pathnodes.h:2392
Path * innerjoinpath
Definition pathnodes.h:2393
List * joinrestrictinfo
Definition pathnodes.h:2395

References Assert, bms_add_member(), bms_add_members(), bms_copy(), bms_int_members(), fb(), get_param_path_clause_serials(), JoinPath::innerjoinpath, IsA, JoinPath::joinrestrictinfo, lfirst, list_head(), JoinPath::outerjoinpath, RestrictInfo::rinfo_serial, and subpath().

Referenced by create_nestloop_path(), and get_param_path_clause_serials().

◆ have_partkey_equi_join()

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

Definition at line 2223 of file relnode.c.

2226{
2227 PartitionScheme part_scheme = rel1->part_scheme;
2229 int num_equal_pks;
2230 ListCell *lc;
2231
2232 /*
2233 * This function must only be called when the joined relations have same
2234 * partitioning scheme.
2235 */
2236 Assert(rel1->part_scheme == rel2->part_scheme);
2238
2239 /* We use a bool array to track which partkey columns are known equal */
2241 /* ... as well as a count of how many are known equal */
2242 num_equal_pks = 0;
2243
2244 /* First, look through the join's restriction clauses */
2245 foreach(lc, restrictlist)
2246 {
2248 OpExpr *opexpr;
2249 Expr *expr1;
2250 Expr *expr2;
2251 bool strict_op;
2252 int ipk1;
2253 int ipk2;
2254
2255 /* If processing an outer join, only use its own join clauses. */
2256 if (IS_OUTER_JOIN(jointype) &&
2257 RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
2258 continue;
2259
2260 /* Skip clauses which can not be used for a join. */
2261 if (!rinfo->can_join)
2262 continue;
2263
2264 /* Skip clauses which are not equality conditions. */
2265 if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
2266 continue;
2267
2268 /* Should be OK to assume it's an OpExpr. */
2269 opexpr = castNode(OpExpr, rinfo->clause);
2270
2271 /* Match the operands to the relation. */
2272 if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
2273 bms_is_subset(rinfo->right_relids, rel2->relids))
2274 {
2275 expr1 = linitial(opexpr->args);
2276 expr2 = lsecond(opexpr->args);
2277 }
2278 else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
2279 bms_is_subset(rinfo->right_relids, rel1->relids))
2280 {
2281 expr1 = lsecond(opexpr->args);
2282 expr2 = linitial(opexpr->args);
2283 }
2284 else
2285 continue;
2286
2287 /*
2288 * Now we need to know whether the join operator is strict; see
2289 * comments in pathnodes.h.
2290 */
2291 strict_op = op_strict(opexpr->opno);
2292
2293 /*
2294 * Vars appearing in the relation's partition keys will not have any
2295 * varnullingrels, but those in expr1 and expr2 will if we're above
2296 * outer joins that could null the respective rels. It's okay to
2297 * match anyway, if the join operator is strict.
2298 */
2299 if (strict_op)
2300 {
2301 if (bms_overlap(rel1->relids, root->outer_join_rels))
2303 root->outer_join_rels,
2304 NULL);
2305 if (bms_overlap(rel2->relids, root->outer_join_rels))
2307 root->outer_join_rels,
2308 NULL);
2309 }
2310
2311 /*
2312 * Only clauses referencing the partition keys are useful for
2313 * partitionwise join.
2314 */
2316 if (ipk1 < 0)
2317 continue;
2319 if (ipk2 < 0)
2320 continue;
2321
2322 /*
2323 * If the clause refers to keys at different ordinal positions, it can
2324 * not be used for partitionwise join.
2325 */
2326 if (ipk1 != ipk2)
2327 continue;
2328
2329 /* Ignore clause if we already proved these keys equal. */
2330 if (pk_known_equal[ipk1])
2331 continue;
2332
2333 /* Reject if the partition key collation differs from the clause's. */
2334 if (rel1->part_scheme->partcollation[ipk1] != opexpr->inputcollid)
2335 return false;
2336
2337 /*
2338 * The clause allows partitionwise join only if it uses the same
2339 * operator family as that specified by the partition key.
2340 */
2341 if (part_scheme->strategy == PARTITION_STRATEGY_HASH)
2342 {
2343 if (!OidIsValid(rinfo->hashjoinoperator) ||
2344 !op_in_opfamily(rinfo->hashjoinoperator,
2345 part_scheme->partopfamily[ipk1]))
2346 continue;
2347 }
2348 else if (!list_member_oid(rinfo->mergeopfamilies,
2349 part_scheme->partopfamily[ipk1]))
2350 continue;
2351
2352 /* Mark the partition key as having an equi-join clause. */
2353 pk_known_equal[ipk1] = true;
2354
2355 /* We can stop examining clauses once we prove all keys equal. */
2356 if (++num_equal_pks == part_scheme->partnatts)
2357 return true;
2358 }
2359
2360 /*
2361 * Also check to see if any keys are known equal by equivclass.c. In most
2362 * cases there would have been a join restriction clause generated from
2363 * any EC that had such knowledge, but there might be no such clause, or
2364 * it might happen to constrain other members of the ECs than the ones we
2365 * are looking for.
2366 */
2367 for (int ipk = 0; ipk < part_scheme->partnatts; ipk++)
2368 {
2370
2371 /* Ignore if we already proved these keys equal. */
2372 if (pk_known_equal[ipk])
2373 continue;
2374
2375 /*
2376 * We need a btree opfamily to ask equivclass.c about. If the
2377 * partopfamily is a hash opfamily, look up its equality operator, and
2378 * select some btree opfamily that that operator is part of. (Any
2379 * such opfamily should be good enough, since equivclass.c will track
2380 * multiple opfamilies as appropriate.)
2381 */
2382 if (part_scheme->strategy == PARTITION_STRATEGY_HASH)
2383 {
2384 Oid eq_op;
2386
2387 eq_op = get_opfamily_member(part_scheme->partopfamily[ipk],
2388 part_scheme->partopcintype[ipk],
2389 part_scheme->partopcintype[ipk],
2391 if (!OidIsValid(eq_op))
2392 break; /* we're not going to succeed */
2394 if (eq_opfamilies == NIL)
2395 break; /* we're not going to succeed */
2397 }
2398 else
2399 btree_opfamily = part_scheme->partopfamily[ipk];
2400
2401 /*
2402 * We consider only non-nullable partition keys here; nullable ones
2403 * would not be treated as part of the same equivalence classes as
2404 * non-nullable ones.
2405 */
2406 foreach(lc, rel1->partexprs[ipk])
2407 {
2408 Node *expr1 = (Node *) lfirst(lc);
2409 ListCell *lc2;
2410 Oid partcoll1 = rel1->part_scheme->partcollation[ipk];
2412
2413 foreach(lc2, rel2->partexprs[ipk])
2414 {
2415 Node *expr2 = (Node *) lfirst(lc2);
2416
2418 {
2419 /*
2420 * Ensure that the collation of the expression matches
2421 * that of the partition key. Checking just one collation
2422 * (partcoll1 and exprcoll1) suffices because partcoll1
2423 * and partcoll2, as well as exprcoll1 and exprcoll2,
2424 * should be identical. This holds because both rel1 and
2425 * rel2 use the same PartitionScheme and expr1 and expr2
2426 * are equal.
2427 */
2428 if (partcoll1 == exprcoll1)
2429 {
2431 rel2->part_scheme->partcollation[ipk];
2434
2436 pk_known_equal[ipk] = true;
2437 break;
2438 }
2439 }
2440 }
2441 if (pk_known_equal[ipk])
2442 break;
2443 }
2444
2445 if (pk_known_equal[ipk])
2446 {
2447 /* We can stop examining keys once we prove all keys equal. */
2448 if (++num_equal_pks == part_scheme->partnatts)
2449 return true;
2450 }
2451 else
2452 break; /* no chance to succeed, give up */
2453 }
2454
2455 return false;
2456}
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:243
#define OidIsValid(objectId)
Definition c.h:860
bool exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2, Oid opfamily)
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
bool op_strict(Oid opno)
Definition lsyscache.c:1697
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition lsyscache.c:170
List * get_mergejoin_opfamilies(Oid opno)
Definition lsyscache.c:430
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition lsyscache.c:70
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
#define IS_OUTER_JOIN(jointype)
Definition nodes.h:348
#define castNode(_type_, nodeptr)
Definition nodes.h:182
@ PARTITION_STRATEGY_HASH
Definition parsenodes.h:916
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition pathnodes.h:3045
#define PARTITION_MAX_KEYS
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
#define linitial_oid(l)
Definition pg_list.h:180
unsigned int Oid
static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
Definition relnode.c:2470
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
#define HTEqualStrategyNumber
Definition stratnum.h:41
Oid opno
Definition primnodes.h:851
List * args
Definition primnodes.h:869
Expr * clause
Definition pathnodes.h:2888

References OpExpr::args, Assert, bms_is_subset(), bms_overlap(), castNode, RestrictInfo::clause, exprCollation(), exprs_known_equal(), fb(), get_mergejoin_opfamilies(), get_opfamily_member(), HTEqualStrategyNumber, IS_OUTER_JOIN, lfirst, lfirst_node, linitial, linitial_oid, list_member_oid(), lsecond, match_expr_to_partition_keys(), NIL, OidIsValid, op_in_opfamily(), op_strict(), OpExpr::opno, PARTITION_MAX_KEYS, PARTITION_STRATEGY_HASH, PG_USED_FOR_ASSERTS_ONLY, RelOptInfo::relids, remove_nulling_relids(), RINFO_IS_PUSHED_DOWN, and root.

Referenced by build_joinrel_partition_info().

◆ init_grouping_targets()

static bool init_grouping_targets ( PlannerInfo root,
RelOptInfo rel,
PathTarget target,
PathTarget agg_input,
List **  group_clauses,
List **  group_exprs 
)
static

Definition at line 2901 of file relnode.c.

2904{
2905 ListCell *lc;
2908
2909 /* Identify the max sortgroupref */
2910 maxSortGroupRef = 0;
2911 foreach(lc, root->processed_tlist)
2912 {
2913 Index ref = ((TargetEntry *) lfirst(lc))->ressortgroupref;
2914
2915 if (ref > maxSortGroupRef)
2917 }
2918
2919 /*
2920 * At this point, all Vars from this relation that are needed by upper
2921 * joins or are required in the final targetlist should already be present
2922 * in its reltarget. Therefore, we can safely iterate over this
2923 * relation's reltarget->exprs to construct the PathTarget and grouping
2924 * clauses for the grouped paths.
2925 */
2926 foreach(lc, rel->reltarget->exprs)
2927 {
2928 Expr *expr = (Expr *) lfirst(lc);
2929 Index sortgroupref;
2930
2931 /*
2932 * Given that PlaceHolderVar currently prevents us from doing eager
2933 * aggregation, the source target cannot contain anything more complex
2934 * than a Var.
2935 */
2936 Assert(IsA(expr, Var));
2937
2938 /*
2939 * Get the sortgroupref of the expr if it is found among, or can be
2940 * deduced from, the original grouping expressions.
2941 */
2942 sortgroupref = get_expression_sortgroupref(root, expr);
2943 if (sortgroupref > 0)
2944 {
2946
2947 /* Find the matching SortGroupClause */
2948 sgc = get_sortgroupref_clause(sortgroupref, root->processed_groupClause);
2949 Assert(sgc->tleSortGroupRef <= maxSortGroupRef);
2950
2951 /*
2952 * If the target expression is to be used as a grouping key, it
2953 * should be emitted by the grouped paths that have been pushed
2954 * down to this relation level.
2955 */
2956 add_column_to_pathtarget(target, expr, sortgroupref);
2957
2958 /*
2959 * ... and it also should be emitted by the input paths.
2960 */
2961 add_column_to_pathtarget(agg_input, expr, sortgroupref);
2962
2963 /*
2964 * Record this SortGroupClause and grouping expression. Note that
2965 * this SortGroupClause might have already been recorded.
2966 */
2967 if (!list_member(*group_clauses, sgc))
2968 {
2969 *group_clauses = lappend(*group_clauses, sgc);
2970 *group_exprs = lappend(*group_exprs, expr);
2971 }
2972 }
2973 else if (is_var_needed_by_join(root, (Var *) expr, rel))
2974 {
2975 /*
2976 * The expression is needed for an upper join but is neither in
2977 * the GROUP BY clause nor derivable from it using EC (otherwise,
2978 * it would have already been included in the targets above). We
2979 * need to create a special SortGroupClause for this expression.
2980 *
2981 * It is important to include such expressions in the grouping
2982 * keys. This is essential to ensure that an aggregated row from
2983 * the partial aggregation matches the other side of the join if
2984 * and only if each row in the partial group does. This ensures
2985 * that all rows within the same partial group share the same
2986 * 'destiny', which is crucial for maintaining correctness.
2987 */
2991
2992 /*
2993 * But first, check if equality implies image equality for this
2994 * expression. If not, we cannot use it as a grouping key. See
2995 * comments in create_grouping_expr_infos().
2996 */
2997 tce = lookup_type_cache(exprType((Node *) expr),
2999 if (!OidIsValid(tce->btree_opf) ||
3000 !OidIsValid(tce->btree_opintype))
3001 return false;
3002
3004 tce->btree_opintype,
3005 tce->btree_opintype,
3007 if (!OidIsValid(equalimageproc) ||
3009 tce->typcollation,
3010 ObjectIdGetDatum(tce->btree_opintype))))
3011 return false;
3012
3013 /* Create the SortGroupClause. */
3015
3016 /* Initialize the SortGroupClause. */
3017 sgc->tleSortGroupRef = ++maxSortGroupRef;
3019 false, true, false,
3020 &sgc->sortop, &sgc->eqop, NULL,
3021 &sgc->hashable);
3022
3023 /* This expression should be emitted by the grouped paths */
3024 add_column_to_pathtarget(target, expr, sgc->tleSortGroupRef);
3025
3026 /* ... and it also should be emitted by the input paths. */
3027 add_column_to_pathtarget(agg_input, expr, sgc->tleSortGroupRef);
3028
3029 /* Record this SortGroupClause and grouping expression */
3030 *group_clauses = lappend(*group_clauses, sgc);
3031 *group_exprs = lappend(*group_exprs, expr);
3032 }
3033 else if (is_var_in_aggref_only(root, (Var *) expr))
3034 {
3035 /*
3036 * The expression is referenced by an aggregate function pushed
3037 * down to this relation and does not appear elsewhere in the
3038 * targetlist or havingQual. Add it to 'agg_input' but not to
3039 * 'target'.
3040 */
3041 add_new_column_to_pathtarget(agg_input, expr);
3042 }
3043 else
3044 {
3045 /*
3046 * The expression may be functionally dependent on other
3047 * expressions in the target, but we cannot verify this until all
3048 * target expressions have been constructed.
3049 */
3051 }
3052 }
3053
3054 /*
3055 * Now we can verify whether an expression is functionally dependent on
3056 * others.
3057 */
3058 foreach(lc, possibly_dependent)
3059 {
3060 Var *tvar;
3061 List *deps = NIL;
3063
3064 tvar = lfirst_node(Var, lc);
3065 rte = root->simple_rte_array[tvar->varno];
3066
3067 if (check_functional_grouping(rte->relid, tvar->varno,
3068 tvar->varlevelsup,
3069 target->exprs, &deps))
3070 {
3071 /*
3072 * The expression is functionally dependent on other target
3073 * expressions, so it can be included in the targets. Since it
3074 * will not be used as a grouping key, a sortgroupref is not
3075 * needed for it.
3076 */
3078 add_new_column_to_pathtarget(agg_input, (Expr *) tvar);
3079 }
3080 else
3081 {
3082 /*
3083 * We may arrive here with a grouping expression that is proven
3084 * redundant by EquivalenceClass processing, such as 't1.a' in the
3085 * query below.
3086 *
3087 * select max(t1.c) from t t1, t t2 where t1.a = 1 group by t1.a,
3088 * t1.b;
3089 *
3090 * For now we just give up in this case.
3091 */
3092 return false;
3093 }
3094 }
3095
3096 return true;
3097}
Datum OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
Definition fmgr.c:1413
bool list_member(const List *list, const void *datum)
Definition list.c:661
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition lsyscache.c:915
#define BTEQUALIMAGE_PROC
Definition nbtree.h:720
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition parse_oper.c:182
bool check_functional_grouping(Oid relid, Index varno, Index varlevelsup, List *grouping_columns, List **constraintDeps)
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
static Index get_expression_sortgroupref(PlannerInfo *root, Expr *expr)
Definition relnode.c:3172
static bool is_var_in_aggref_only(PlannerInfo *root, Var *var)
Definition relnode.c:3105
static bool is_var_needed_by_join(PlannerInfo *root, Var *var, RelOptInfo *rel)
Definition relnode.c:3144
SortGroupClause * get_sortgroupref_clause(Index sortref, List *clauses)
Definition tlist.c:431
void add_new_column_to_pathtarget(PathTarget *target, Expr *expr)
Definition tlist.c:750
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition typcache.c:389
#define TYPECACHE_BTREE_OPFAMILY
Definition typcache.h:147

References add_column_to_pathtarget(), add_new_column_to_pathtarget(), Assert, BTEQUALIMAGE_PROC, check_functional_grouping(), DatumGetBool(), PathTarget::exprs, exprType(), fb(), get_expression_sortgroupref(), get_opfamily_proc(), get_sort_group_operators(), get_sortgroupref_clause(), is_var_in_aggref_only(), is_var_needed_by_join(), IsA, lappend(), lfirst, lfirst_node, list_member(), lookup_type_cache(), makeNode, NIL, ObjectIdGetDatum(), OidFunctionCall1Coll(), OidIsValid, RelOptInfo::reltarget, root, and TYPECACHE_BTREE_OPFAMILY.

Referenced by create_rel_agg_info().

◆ is_var_in_aggref_only()

static bool is_var_in_aggref_only ( PlannerInfo root,
Var var 
)
static

Definition at line 3105 of file relnode.c.

3106{
3107 ListCell *lc;
3108
3109 /*
3110 * Search the list of aggregate expressions for the Var.
3111 */
3112 foreach(lc, root->agg_clause_list)
3113 {
3115 List *vars;
3116
3117 Assert(IsA(ac_info->aggref, Aggref));
3118
3119 if (!bms_is_member(var->varno, ac_info->agg_eval_at))
3120 continue;
3121
3122 vars = pull_var_clause((Node *) ac_info->aggref,
3126
3127 if (list_member(vars, var))
3128 {
3129 list_free(vars);
3130 break;
3131 }
3132
3133 list_free(vars);
3134 }
3135
3136 return (lc != NULL && !list_member(root->tlist_vars, var));
3137}
void list_free(List *list)
Definition list.c:1546
#define PVC_RECURSE_AGGREGATES
Definition optimizer.h:198
#define PVC_RECURSE_PLACEHOLDERS
Definition optimizer.h:202
#define PVC_RECURSE_WINDOWFUNCS
Definition optimizer.h:200
List * pull_var_clause(Node *node, int flags)
Definition var.c:653

References Assert, bms_is_member(), fb(), IsA, lfirst_node, list_free(), list_member(), pull_var_clause(), PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, root, and Var::varno.

Referenced by init_grouping_targets().

◆ is_var_needed_by_join()

static bool is_var_needed_by_join ( PlannerInfo root,
Var var,
RelOptInfo rel 
)
static

Definition at line 3144 of file relnode.c.

3145{
3146 Relids relids;
3147 int attno;
3149
3150 /*
3151 * Note that when checking if the Var is needed by joins above, we want to
3152 * exclude cases where the Var is only needed in the final targetlist. So
3153 * include "relation 0" in the check.
3154 */
3155 relids = bms_copy(rel->relids);
3156 relids = bms_add_member(relids, 0);
3157
3159 attno = var->varattno - baserel->min_attr;
3160
3161 return bms_nonempty_difference(baserel->attr_needed[attno], relids);
3162}

References bms_add_member(), bms_copy(), bms_nonempty_difference(), fb(), find_base_rel(), RelOptInfo::relids, root, Var::varattno, and Var::varno.

Referenced by init_grouping_targets().

◆ match_expr_to_partition_keys()

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

Definition at line 2470 of file relnode.c.

2471{
2472 int cnt;
2473
2474 /* This function should be called only for partitioned relations. */
2475 Assert(rel->part_scheme);
2476 Assert(rel->partexprs);
2477 Assert(rel->nullable_partexprs);
2478
2479 /* Remove any relabel decorations. */
2480 while (IsA(expr, RelabelType))
2481 expr = (Expr *) (castNode(RelabelType, expr))->arg;
2482
2483 for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
2484 {
2485 ListCell *lc;
2486
2487 /* We can always match to the non-nullable partition keys. */
2488 foreach(lc, rel->partexprs[cnt])
2489 {
2490 if (equal(lfirst(lc), expr))
2491 return cnt;
2492 }
2493
2494 if (!strict_op)
2495 continue;
2496
2497 /*
2498 * If it's a strict join operator then a NULL partition key on one
2499 * side will not join to any partition key on the other side, and in
2500 * particular such a row can't join to a row from a different
2501 * partition on the other side. So, it's okay to search the nullable
2502 * partition keys as well.
2503 */
2504 foreach(lc, rel->nullable_partexprs[cnt])
2505 {
2506 if (equal(lfirst(lc), expr))
2507 return cnt;
2508 }
2509 }
2510
2511 return -1;
2512}
Datum arg
Definition elog.c:1322

References arg, Assert, castNode, equal(), fb(), IsA, and lfirst.

Referenced by have_partkey_equi_join().

◆ min_join_parameterization()

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

Definition at line 1181 of file relnode.c.

1185{
1186 Relids result;
1187
1188 /*
1189 * Basically we just need the union of the inputs' lateral_relids, less
1190 * whatever is already in the join.
1191 *
1192 * It's not immediately obvious that this is a valid way to compute the
1193 * result, because it might seem that we're ignoring possible lateral refs
1194 * of PlaceHolderVars that are due to be computed at the join but not in
1195 * either input. However, because create_lateral_join_info() already
1196 * charged all such PHV refs to each member baserel of the join, they'll
1197 * be accounted for already in the inputs' lateral_relids. Likewise, we
1198 * do not need to worry about doing transitive closure here, because that
1199 * was already accounted for in the original baserel lateral_relids.
1200 */
1201 result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
1202 result = bms_del_members(result, joinrelids);
1203 return result;
1204}

References bms_del_members(), bms_union(), and fb().

Referenced by build_join_rel(), and join_is_legal().

◆ set_foreign_rel_properties()

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

Definition at line 719 of file relnode.c.

721{
722 if (OidIsValid(outer_rel->serverid) &&
723 inner_rel->serverid == outer_rel->serverid)
724 {
725 if (inner_rel->userid == outer_rel->userid)
726 {
727 joinrel->serverid = outer_rel->serverid;
728 joinrel->userid = outer_rel->userid;
729 joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
730 joinrel->fdwroutine = outer_rel->fdwroutine;
731 }
732 else if (!OidIsValid(inner_rel->userid) &&
733 outer_rel->userid == GetUserId())
734 {
735 joinrel->serverid = outer_rel->serverid;
736 joinrel->userid = outer_rel->userid;
737 joinrel->useridiscurrent = true;
738 joinrel->fdwroutine = outer_rel->fdwroutine;
739 }
740 else if (!OidIsValid(outer_rel->userid) &&
741 inner_rel->userid == GetUserId())
742 {
743 joinrel->serverid = outer_rel->serverid;
744 joinrel->userid = inner_rel->userid;
745 joinrel->useridiscurrent = true;
746 joinrel->fdwroutine = outer_rel->fdwroutine;
747 }
748 }
749}
Oid GetUserId(void)
Definition miscinit.c:470

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

Referenced by build_child_join_rel(), and build_join_rel().

◆ set_joinrel_partition_key_exprs()

static void set_joinrel_partition_key_exprs ( RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
JoinType  jointype 
)
static

Definition at line 2519 of file relnode.c.

2522{
2523 PartitionScheme part_scheme = joinrel->part_scheme;
2524 int partnatts = part_scheme->partnatts;
2525
2526 joinrel->partexprs = palloc0_array(List *, partnatts);
2527 joinrel->nullable_partexprs = palloc0_array(List *, partnatts);
2528
2529 /*
2530 * The joinrel's partition expressions are the same as those of the input
2531 * rels, but we must properly classify them as nullable or not in the
2532 * joinrel's output. (Also, we add some more partition expressions if
2533 * it's a FULL JOIN.)
2534 */
2535 for (int cnt = 0; cnt < partnatts; cnt++)
2536 {
2537 /* mark these const to enforce that we copy them properly */
2538 const List *outer_expr = outer_rel->partexprs[cnt];
2539 const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
2540 const List *inner_expr = inner_rel->partexprs[cnt];
2541 const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
2542 List *partexpr = NIL;
2544 ListCell *lc;
2545
2546 switch (jointype)
2547 {
2548 /*
2549 * A join relation resulting from an INNER join may be
2550 * regarded as partitioned by either of the inner and outer
2551 * relation keys. For example, A INNER JOIN B ON A.a = B.b
2552 * can be regarded as partitioned on either A.a or B.b. So we
2553 * add both keys to the joinrel's partexpr lists. However,
2554 * anything that was already nullable still has to be treated
2555 * as nullable.
2556 */
2557 case JOIN_INNER:
2561 break;
2562
2563 /*
2564 * A join relation resulting from a SEMI or ANTI join may be
2565 * regarded as partitioned by the outer relation keys. The
2566 * inner relation's keys are no longer interesting; since they
2567 * aren't visible in the join output, nothing could join to
2568 * them.
2569 */
2570 case JOIN_SEMI:
2571 case JOIN_ANTI:
2574 break;
2575
2576 /*
2577 * A join relation resulting from a LEFT OUTER JOIN likewise
2578 * may be regarded as partitioned on the (non-nullable) outer
2579 * relation keys. The inner (nullable) relation keys are okay
2580 * as partition keys for further joins as long as they involve
2581 * strict join operators.
2582 */
2583 case JOIN_LEFT:
2589 break;
2590
2591 /*
2592 * For FULL OUTER JOINs, both relations are nullable, so the
2593 * resulting join relation may be regarded as partitioned on
2594 * either of inner and outer relation keys, but only for joins
2595 * that involve strict join operators.
2596 */
2597 case JOIN_FULL:
2599 inner_expr);
2604
2605 /*
2606 * Also add CoalesceExprs corresponding to each possible
2607 * full-join output variable (that is, left side coalesced to
2608 * right side), so that we can match equijoin expressions
2609 * using those variables. We really only need these for
2610 * columns merged by JOIN USING, and only with the pairs of
2611 * input items that correspond to the data structures that
2612 * parse analysis would build for such variables. But it's
2613 * hard to tell which those are, so just make all the pairs.
2614 * Extra items in the nullable_partexprs list won't cause big
2615 * problems. (It's possible that such items will get matched
2616 * to user-written COALESCEs, but it should still be valid to
2617 * partition on those, since they're going to be either the
2618 * partition column or NULL; it's the same argument as for
2619 * partitionwise nesting of any outer join.) We assume no
2620 * type coercions are needed to make the coalesce expressions,
2621 * since columns of different types won't have gotten
2622 * classified as the same PartitionScheme. Note that we
2623 * intentionally leave out the varnullingrels decoration that
2624 * would ordinarily appear on the Vars inside these
2625 * CoalesceExprs, because have_partkey_equi_join will strip
2626 * varnullingrels from the expressions it will compare to the
2627 * partexprs.
2628 */
2630 {
2631 Node *larg = (Node *) lfirst(lc);
2632 ListCell *lc2;
2633
2635 {
2636 Node *rarg = (Node *) lfirst(lc2);
2638
2639 c->coalescetype = exprType(larg);
2640 c->coalescecollid = exprCollation(larg);
2641 c->args = list_make2(larg, rarg);
2642 c->location = -1;
2644 }
2645 }
2646 break;
2647
2648 default:
2649 elog(ERROR, "unrecognized join type: %d", (int) jointype);
2650 }
2651
2652 joinrel->partexprs[cnt] = partexpr;
2653 joinrel->nullable_partexprs[cnt] = nullable_partexpr;
2654 }
2655}
List * list_concat_copy(const List *list1, const List *list2)
Definition list.c:598
List * list_copy(const List *oldlist)
Definition list.c:1573
@ JOIN_SEMI
Definition nodes.h:317
@ JOIN_LEFT
Definition nodes.h:304
@ JOIN_ANTI
Definition nodes.h:318
#define list_make2(x1, x2)
Definition pg_list.h:214
char * c

References elog, ERROR, exprCollation(), exprType(), fb(), JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, lappend(), lfirst, list_concat(), list_concat_copy(), list_copy(), list_make2, makeNode, NIL, and palloc0_array.

Referenced by build_joinrel_partition_info().

◆ setup_simple_rel_arrays()

void setup_simple_rel_arrays ( PlannerInfo root)

Definition at line 114 of file relnode.c.

115{
116 int size;
117 Index rti;
118 ListCell *lc;
119
120 /* Arrays are accessed using RT indexes (1..N) */
121 size = list_length(root->parse->rtable) + 1;
122 root->simple_rel_array_size = size;
123
124 /*
125 * simple_rel_array is initialized to all NULLs, since no RelOptInfos
126 * exist yet. It'll be filled by later calls to build_simple_rel().
127 */
128 root->simple_rel_array = (RelOptInfo **)
129 palloc0_array(RelOptInfo *, size);
130
131 /* simple_rte_array is an array equivalent of the rtable list */
132 root->simple_rte_array = (RangeTblEntry **)
134 rti = 1;
135 foreach(lc, root->parse->rtable)
136 {
138
139 root->simple_rte_array[rti++] = rte;
140 }
141
142 /* append_rel_array is not needed if there are no AppendRelInfos */
143 if (root->append_rel_list == NIL)
144 {
145 root->append_rel_array = NULL;
146 return;
147 }
148
149 root->append_rel_array = (AppendRelInfo **)
151
152 /*
153 * append_rel_array is filled with any already-existing AppendRelInfos,
154 * which currently could only come from UNION ALL flattening. We might
155 * add more later during inheritance expansion, but it's the
156 * responsibility of the expansion code to update the array properly.
157 */
158 foreach(lc, root->append_rel_list)
159 {
161 int child_relid = appinfo->child_relid;
162
163 /* Sanity check */
164 Assert(child_relid < size);
165
166 if (root->append_rel_array[child_relid])
167 elog(ERROR, "child relation already exists");
168
169 root->append_rel_array[child_relid] = appinfo;
170 }
171}

References Assert, elog, ERROR, fb(), lfirst, lfirst_node, list_length(), NIL, palloc0_array, and root.

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

◆ subbuild_joinrel_joinlist()

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

Definition at line 1565 of file relnode.c.

1568{
1569 ListCell *l;
1570
1571 /* Expected to be called only for join between parent relations. */
1572 Assert(joinrel->reloptkind == RELOPT_JOINREL);
1573
1574 foreach(l, joininfo_list)
1575 {
1576 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1577
1578 if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1579 {
1580 /*
1581 * This clause becomes a restriction clause for the joinrel, since
1582 * it refers to no outside rels. So we can ignore it in this
1583 * routine.
1584 */
1585 }
1586 else
1587 {
1588 /*
1589 * This clause is still a join clause at this level, so add it to
1590 * the new joininfo list, being careful to eliminate duplicates.
1591 * (Since RestrictInfo nodes in different joinlists will have been
1592 * multiply-linked rather than copied, pointer equality should be
1593 * a sufficient test.)
1594 */
1596 }
1597 }
1598
1599 return new_joininfo;
1600}
List * list_append_unique_ptr(List *list, void *datum)
Definition list.c:1356
Relids required_relids
Definition pathnodes.h:2919

References Assert, bms_is_subset(), fb(), lfirst, list_append_unique_ptr(), RelOptInfo::relids, RELOPT_JOINREL, RelOptInfo::reloptkind, and RestrictInfo::required_relids.

Referenced by build_joinrel_joinlist().

◆ subbuild_joinrel_restrictlist()

static List * subbuild_joinrel_restrictlist ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo input_rel,
Relids  both_input_relids,
List new_restrictlist 
)
static

Definition at line 1499 of file relnode.c.

1504{
1505 ListCell *l;
1506
1507 foreach(l, input_rel->joininfo)
1508 {
1509 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1510
1511 if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1512 {
1513 /*
1514 * This clause should become a restriction clause for the joinrel,
1515 * since it refers to no outside rels. However, if it's a clone
1516 * clause then it might be too late to evaluate it, so we have to
1517 * check. (If it is too late, just ignore the clause, taking it
1518 * on faith that another clone was or will be selected.) Clone
1519 * clauses should always be outer-join clauses, so we compare
1520 * against both_input_relids.
1521 */
1522 if (rinfo->has_clone || rinfo->is_clone)
1523 {
1524 Assert(!RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids));
1526 continue;
1528 continue;
1529 }
1530 else
1531 {
1532 /*
1533 * For non-clone clauses, we just Assert it's OK. These might
1534 * be either join or filter clauses; if it's a join clause
1535 * then it should not refer to the current join's output.
1536 * (There is little point in checking incompatible_relids,
1537 * because it'll be NULL.)
1538 */
1539 Assert(RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids) ||
1542 }
1543
1544 /*
1545 * OK, so add it to the list, being careful to eliminate
1546 * duplicates. (Since RestrictInfo nodes in different joinlists
1547 * will have been multiply-linked rather than copied, pointer
1548 * equality should be a sufficient test.)
1549 */
1551 }
1552 else
1553 {
1554 /*
1555 * This clause is still a join clause at this level, so we ignore
1556 * it in this routine.
1557 */
1558 }
1559 }
1560
1561 return new_restrictlist;
1562}
Relids incompatible_relids
Definition pathnodes.h:2922

References Assert, bms_is_subset(), bms_overlap(), fb(), RestrictInfo::has_clone, RestrictInfo::incompatible_relids, RestrictInfo::is_clone, lfirst, list_append_unique_ptr(), RelOptInfo::relids, RestrictInfo::required_relids, and RINFO_IS_PUSHED_DOWN.

Referenced by build_joinrel_restrictlist().

Variable Documentation

◆ build_simple_rel_hook

build_simple_rel_hook_type build_simple_rel_hook = NULL

Definition at line 51 of file relnode.c.

Referenced by build_simple_rel(), and pgpa_planner_install_hooks().

◆ joinrel_setup_hook

joinrel_setup_hook_type joinrel_setup_hook = NULL

Definition at line 54 of file relnode.c.

Referenced by build_child_join_rel(), build_join_rel(), and pgpa_planner_install_hooks().