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

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

747{
748 /* GEQO requires us to append the new joinrel to the end of the list! */
749 root->join_rel_list = lappend(root->join_rel_list, joinrel);
750
751 /* store it into the auxiliary hashtable if there is one. */
752 if (root->join_rel_hash)
753 {
755 bool found;
756
757 hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
758 &(joinrel->relids),
760 &found);
761 Assert(!found);
762 hentry->join_rel = joinrel;
763 }
764}
#define Assert(condition)
Definition c.h:873
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:1003

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

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

2656{
2657 /* Build the targetlist */
2658 childrel->reltarget->exprs = (List *)
2660 (Node *) parentrel->reltarget->exprs,
2661 nappinfos, appinfos);
2662
2663 /* Set the cost and width fields */
2664 childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
2665 childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
2666 childrel->reltarget->width = parentrel->reltarget->width;
2667}

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

489{
490 RelOptInfo *grouped_rel;
491
492 grouped_rel = makeNode(RelOptInfo);
493 memcpy(grouped_rel, rel, sizeof(RelOptInfo));
494
495 /*
496 * clear path info
497 */
498 grouped_rel->pathlist = NIL;
499 grouped_rel->ppilist = NIL;
500 grouped_rel->partial_pathlist = NIL;
501 grouped_rel->cheapest_startup_path = NULL;
502 grouped_rel->cheapest_total_path = NULL;
503 grouped_rel->cheapest_parameterized_paths = NIL;
504
505 /*
506 * clear partition info
507 */
508 grouped_rel->part_scheme = NULL;
509 grouped_rel->nparts = -1;
510 grouped_rel->boundinfo = NULL;
511 grouped_rel->partbounds_merged = false;
512 grouped_rel->partition_qual = NIL;
513 grouped_rel->part_rels = NULL;
514 grouped_rel->live_parts = NULL;
515 grouped_rel->all_partrels = NULL;
516 grouped_rel->partexprs = NULL;
517 grouped_rel->nullable_partexprs = NULL;
518 grouped_rel->consider_partitionwise_join = false;
519
520 /*
521 * clear size estimates
522 */
523 grouped_rel->rows = 0;
524
525 return grouped_rel;
526}

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

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

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

606{
607 HTAB *hashtab;
609 ListCell *l;
610
611 /* Create the hash table */
612 hash_ctl.keysize = sizeof(Relids);
613 hash_ctl.entrysize = sizeof(JoinHashEntry);
614 hash_ctl.hash = bitmap_hash;
615 hash_ctl.match = bitmap_match;
617 hashtab = hash_create("JoinRelHashTable",
618 256L,
619 &hash_ctl,
621
622 /* Insert all the already-existing joinrels */
623 foreach(l, root->join_rel_list)
624 {
625 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
627 bool found;
628
629 hentry = (JoinHashEntry *) hash_search(hashtab,
630 &(rel->relids),
632 &found);
633 Assert(!found);
634 hentry->join_rel = rel;
635 }
636
637 root->join_rel_hash = hashtab;
638}
uint32 bitmap_hash(const void *key, Size keysize)
Definition bitmapset.c:1433
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition bitmapset.c:1443
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 1470 of file relnode.c.

1473{
1474 List *result;
1475
1476 /*
1477 * Collect all the clauses that syntactically belong above this level,
1478 * eliminating any duplicates (important since we will see many of the
1479 * same clauses arriving from both input relations).
1480 */
1481 result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1482 result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1483
1484 joinrel->joininfo = result;
1485}
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition relnode.c:1554

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

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

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

1438{
1439 List *result;
1441
1442 both_input_relids = bms_union(outer_rel->relids, inner_rel->relids);
1443
1444 /*
1445 * Collect all the clauses that syntactically belong at this level,
1446 * eliminating any duplicates (important since we will see many of the
1447 * same clauses arriving from both input relations).
1448 */
1449 result = subbuild_joinrel_restrictlist(root, joinrel, outer_rel,
1451 result = subbuild_joinrel_restrictlist(root, joinrel, inner_rel,
1452 both_input_relids, result);
1453
1454 /*
1455 * Add on any clauses derived from EquivalenceClasses. These cannot be
1456 * redundant with the clauses in the joininfo lists, so don't bother
1457 * checking.
1458 */
1459 result = list_concat(result,
1461 joinrel->relids,
1462 outer_rel->relids,
1463 inner_rel,
1464 sjinfo));
1465
1466 return result;
1467}
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:1488

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

1253{
1254 Relids relids = joinrel->relids;
1255 int64 tuple_width = joinrel->reltarget->width;
1256 ListCell *vars;
1257 ListCell *lc;
1258
1259 foreach(vars, input_rel->reltarget->exprs)
1260 {
1261 Var *var = (Var *) lfirst(vars);
1262
1263 /*
1264 * For a PlaceHolderVar, we have to look up the PlaceHolderInfo.
1265 */
1266 if (IsA(var, PlaceHolderVar))
1267 {
1270
1271 /* Is it still needed above this joinrel? */
1272 if (bms_nonempty_difference(phinfo->ph_needed, relids))
1273 {
1274 /*
1275 * Yup, add it to the output. If this join potentially nulls
1276 * this input, we have to update the PHV's phnullingrels,
1277 * which means making a copy.
1278 */
1279 if (can_null)
1280 {
1281 phv = copyObject(phv);
1282 /* See comments above to understand this logic */
1283 if (sjinfo->ojrelid != 0 &&
1284 bms_is_member(sjinfo->ojrelid, relids) &&
1285 (bms_is_subset(phv->phrels, sjinfo->syn_righthand) ||
1286 (sjinfo->jointype == JOIN_FULL &&
1287 bms_is_subset(phv->phrels, sjinfo->syn_lefthand))))
1288 phv->phnullingrels = bms_add_member(phv->phnullingrels,
1289 sjinfo->ojrelid);
1290 foreach(lc, pushed_down_joins)
1291 {
1293
1294 Assert(bms_is_member(othersj->ojrelid, relids));
1295 if (bms_is_subset(phv->phrels, othersj->syn_righthand))
1296 phv->phnullingrels = bms_add_member(phv->phnullingrels,
1297 othersj->ojrelid);
1298 }
1299 phv->phnullingrels =
1300 bms_join(phv->phnullingrels,
1302 relids));
1303 }
1304
1305 joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1306 phv);
1307 /* Bubbling up the precomputed result has cost zero */
1308 tuple_width += phinfo->ph_width;
1309 }
1310 continue;
1311 }
1312
1313 /*
1314 * Otherwise, anything in a baserel or joinrel targetlist ought to be
1315 * a Var. (More general cases can only appear in appendrel child
1316 * rels, which will never be seen here.)
1317 */
1318 if (!IsA(var, Var))
1319 elog(ERROR, "unexpected node type in rel targetlist: %d",
1320 (int) nodeTag(var));
1321
1322 if (var->varno == ROWID_VAR)
1323 {
1324 /* UPDATE/DELETE/MERGE row identity vars are always needed */
1326 list_nth(root->row_identity_vars, var->varattno - 1);
1327
1328 /* Update reltarget width estimate from RowIdentityVarInfo */
1330 }
1331 else
1332 {
1334 int ndx;
1335
1336 /* Get the Var's original base rel */
1338
1339 /* Is it still needed above this joinrel? */
1340 ndx = var->varattno - baserel->min_attr;
1341 if (!bms_nonempty_difference(baserel->attr_needed[ndx], relids))
1342 continue; /* nope, skip it */
1343
1344 /* Update reltarget width estimate from baserel's attr_widths */
1345 tuple_width += baserel->attr_widths[ndx];
1346 }
1347
1348 /*
1349 * Add the Var to the output. If this join potentially nulls this
1350 * input, we have to update the Var's varnullingrels, which means
1351 * making a copy. But note that we don't ever add nullingrel bits to
1352 * row identity Vars (cf. comments in setrefs.c).
1353 */
1354 if (can_null && var->varno != ROWID_VAR)
1355 {
1356 var = copyObject(var);
1357 /* See comments above to understand this logic */
1358 if (sjinfo->ojrelid != 0 &&
1359 bms_is_member(sjinfo->ojrelid, relids) &&
1360 (bms_is_member(var->varno, sjinfo->syn_righthand) ||
1361 (sjinfo->jointype == JOIN_FULL &&
1362 bms_is_member(var->varno, sjinfo->syn_lefthand))))
1363 var->varnullingrels = bms_add_member(var->varnullingrels,
1364 sjinfo->ojrelid);
1365 foreach(lc, pushed_down_joins)
1366 {
1368
1369 Assert(bms_is_member(othersj->ojrelid, relids));
1370 if (bms_is_member(var->varno, othersj->syn_righthand))
1371 var->varnullingrels = bms_add_member(var->varnullingrels,
1372 othersj->ojrelid);
1373 }
1374 var->varnullingrels =
1375 bms_join(var->varnullingrels,
1377 relids));
1378 }
1379
1380 joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
1381 var);
1382
1383 /* Vars have cost zero, so no need to adjust reltarget->cost */
1384 }
1385
1387}
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:814
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition bitmapset.c:1229
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:640
int64_t int64
Definition c.h:543
int32 clamp_width_est(int64 tuple_width)
Definition costsize.c:242
#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:245
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition relnode.c:533
Relids commute_above_r
Definition pathnodes.h:3202
Relids syn_lefthand
Definition pathnodes.h:3197
Relids syn_righthand
Definition pathnodes.h:3198
AttrNumber varattno
Definition primnodes.h:274
int varno
Definition primnodes.h:269

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

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

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

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

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(), 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 2680 of file relnode.c.

2682{
2683 ListCell *lc;
2684 RelAggInfo *result;
2685 PathTarget *agg_input;
2686 PathTarget *target;
2687 List *group_clauses = NIL;
2688 List *group_exprs = NIL;
2689
2690 /*
2691 * The lists of aggregate expressions and grouping expressions should have
2692 * been constructed.
2693 */
2694 Assert(root->agg_clause_list != NIL);
2695 Assert(root->group_expr_list != NIL);
2696
2697 /*
2698 * If this is a child rel, the grouped rel for its parent rel must have
2699 * been created if it can. So we can just use parent's RelAggInfo if
2700 * there is one, with appropriate variable substitutions.
2701 */
2702 if (IS_OTHER_REL(rel))
2703 {
2704 RelOptInfo *grouped_rel;
2705 RelAggInfo *agg_info;
2706
2707 grouped_rel = rel->top_parent->grouped_rel;
2708 if (grouped_rel == NULL)
2709 return NULL;
2710
2711 Assert(IS_GROUPED_REL(grouped_rel));
2712
2713 /* Must do multi-level transformation */
2714 agg_info = (RelAggInfo *)
2716 (Node *) grouped_rel->agg_info,
2717 rel,
2718 rel->top_parent);
2719
2720 agg_info->apply_agg_at = NULL; /* caller will change this later */
2721
2723 {
2724 agg_info->grouped_rows =
2726 rel->rows, NULL, NULL);
2727
2728 /*
2729 * The grouped paths for the given relation are considered useful
2730 * iff the average group size is no less than
2731 * min_eager_agg_group_size.
2732 */
2733 agg_info->agg_useful =
2734 (rel->rows / agg_info->grouped_rows) >= min_eager_agg_group_size;
2735 }
2736
2737 return agg_info;
2738 }
2739
2740 /* Check if it's possible to produce grouped paths for this relation. */
2742 return NULL;
2743
2744 /*
2745 * Create targets for the grouped paths and for the input paths of the
2746 * grouped paths.
2747 */
2748 target = create_empty_pathtarget();
2749 agg_input = create_empty_pathtarget();
2750
2751 /* ... and initialize these targets */
2752 if (!init_grouping_targets(root, rel, target, agg_input,
2753 &group_clauses, &group_exprs))
2754 return NULL;
2755
2756 /*
2757 * Eager aggregation is not applicable if there are no available grouping
2758 * expressions.
2759 */
2760 if (group_clauses == NIL)
2761 return NULL;
2762
2763 /* Add aggregates to the grouping target */
2764 foreach(lc, root->agg_clause_list)
2765 {
2767 Aggref *aggref;
2768
2769 Assert(IsA(ac_info->aggref, Aggref));
2770
2771 aggref = (Aggref *) copyObject(ac_info->aggref);
2773
2774 add_column_to_pathtarget(target, (Expr *) aggref, 0);
2775 }
2776
2777 /* Set the estimated eval cost and output width for both targets */
2779 set_pathtarget_cost_width(root, agg_input);
2780
2781 /* build the RelAggInfo result */
2782 result = makeNode(RelAggInfo);
2783 result->target = target;
2784 result->agg_input = agg_input;
2785 result->group_clauses = group_clauses;
2786 result->group_exprs = group_exprs;
2787 result->apply_agg_at = NULL; /* caller will change this later */
2788
2790 {
2792 rel->rows, NULL, NULL);
2793
2794 /*
2795 * The grouped paths for the given relation are considered useful iff
2796 * the average group size is no less than min_eager_agg_group_size.
2797 */
2798 result->agg_useful =
2799 (rel->rows / result->grouped_rows) >= min_eager_agg_group_size;
2800 }
2801
2802 return result;
2803}
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:592
PathTarget * set_pathtarget_cost_width(PlannerInfo *root, PathTarget *target)
Definition costsize.c:6515
@ AGGSPLIT_INITIAL_SERIAL
Definition nodes.h:389
#define IS_GROUPED_REL(rel)
Definition pathnodes.h:1239
#define lfirst_node(type, lc)
Definition pg_list.h:176
void mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
Definition planner.c:5816
static bool init_grouping_targets(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, PathTarget *agg_input, List **group_clauses, List **group_exprs)
Definition relnode.c:2890
static bool eager_aggregation_possible_for_relation(PlannerInfo *root, RelOptInfo *rel)
Definition relnode.c:2810
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
Definition selfuncs.c:3771
List * group_exprs
Definition pathnodes.h:1281
List * group_clauses
Definition pathnodes.h:1279
struct PathTarget * agg_input
Definition pathnodes.h:1276
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 2810 of file relnode.c.

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

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

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

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

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

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

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

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

556{
557 /* use an unsigned comparison to prevent negative array element access */
558 if ((uint32) relid < (uint32) root->simple_rel_array_size)
559 return root->simple_rel_array[relid];
560 return NULL;
561}

References fb(), and root.

Referenced by all_rows_selectable().

◆ find_childrel_parents()

Relids find_childrel_parents ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 1657 of file relnode.c.

1658{
1659 Relids result = NULL;
1660
1662 Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1663
1664 do
1665 {
1666 AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1668
1669 result = bms_add_member(result, prelid);
1670
1671 /* traverse up to the parent rel, loop if it's also a child rel */
1672 rel = find_base_rel(root, prelid);
1673 } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1674
1676
1677 return result;
1678}
unsigned int Index
Definition c.h:628
Index parent_relid
Definition pathnodes.h:3270

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

647{
648 /*
649 * Switch to using hash lookup when list grows "too long". The threshold
650 * is arbitrary and is known only here.
651 */
652 if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
654
655 /*
656 * Use either hashtable lookup or linear search, as appropriate.
657 *
658 * Note: the seemingly redundant hashkey variable is used to avoid taking
659 * the address of relids; unless the compiler is exceedingly smart, doing
660 * so would force relids out of a register and thus probably slow down the
661 * list-search case.
662 */
663 if (root->join_rel_hash)
664 {
665 Relids hashkey = relids;
667
668 hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
669 &hashkey,
670 HASH_FIND,
671 NULL);
672 if (hentry)
673 return hentry->join_rel;
674 }
675 else
676 {
677 ListCell *l;
678
679 foreach(l, root->join_rel_list)
680 {
681 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
682
683 if (bms_equal(rel->relids, relids))
684 return rel;
685 }
686 }
687
688 return NULL;
689}
@ HASH_FIND
Definition hsearch.h:113
static void build_join_rel_hash(PlannerInfo *root)
Definition relnode.c:605
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 2037 of file relnode.c.

2038{
2039 ListCell *lc;
2040
2041 foreach(lc, rel->ppilist)
2042 {
2044
2045 if (bms_equal(ppi->ppi_req_outer, required_outer))
2046 return ppi;
2047 }
2048
2049 return NULL;
2050}

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

2005{
2007
2008 /* If rel has LATERAL refs, every path for it should account for them */
2009 Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
2010
2011 /* Unparameterized paths have no ParamPathInfo */
2013 return NULL;
2014
2016
2017 /* If we already have a PPI for this parameterization, just return it */
2019 return ppi;
2020
2021 /* Else build the ParamPathInfo */
2023 ppi->ppi_req_outer = required_outer;
2024 ppi->ppi_rows = 0;
2025 ppi->ppi_clauses = NIL;
2026 ppi->ppi_serials = NULL;
2027 appendrel->ppilist = lappend(appendrel->ppilist, ppi);
2028
2029 return ppi;
2030}
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:581
#define bms_is_empty(a)
Definition bitmapset.h:118
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition relnode.c:2037

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

1695{
1698 List *pclauses;
1699 List *eqclauses;
1701 double rows;
1702 ListCell *lc;
1703
1704 /* If rel has LATERAL refs, every path for it should account for them */
1705 Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1706
1707 /* Unparameterized paths have no ParamPathInfo */
1709 return NULL;
1710
1712
1713 /* If we already have a PPI for this parameterization, just return it */
1715 return ppi;
1716
1717 /*
1718 * Identify all joinclauses that are movable to this base rel given this
1719 * parameterization.
1720 */
1722 pclauses = NIL;
1723 foreach(lc, baserel->joininfo)
1724 {
1725 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1726
1728 baserel->relids,
1729 joinrelids))
1730 pclauses = lappend(pclauses, rinfo);
1731 }
1732
1733 /*
1734 * Add in joinclauses generated by EquivalenceClasses, too. (These
1735 * necessarily satisfy join_clause_is_movable_into; but in assert-enabled
1736 * builds, let's verify that.)
1737 */
1739 joinrelids,
1741 baserel,
1742 NULL);
1743#ifdef USE_ASSERT_CHECKING
1744 foreach(lc, eqclauses)
1745 {
1746 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1747
1749 baserel->relids,
1750 joinrelids));
1751 }
1752#endif
1754
1755 /* Compute set of serial numbers of the enforced clauses */
1756 pserials = NULL;
1757 foreach(lc, pclauses)
1758 {
1759 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1760
1762 }
1763
1764 /* Estimate the number of rows returned by the parameterized scan */
1766
1767 /* And now we can build the ParamPathInfo */
1769 ppi->ppi_req_outer = required_outer;
1770 ppi->ppi_rows = rows;
1771 ppi->ppi_clauses = pclauses;
1772 ppi->ppi_serials = pserials;
1773 baserel->ppilist = lappend(baserel->ppilist, ppi);
1774
1775 return ppi;
1776}
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition costsize.c:5527
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 3161 of file relnode.c.

3162{
3163 ListCell *lc;
3164
3165 Assert(IsA(expr, Var));
3166
3167 foreach(lc, root->group_expr_list)
3168 {
3170 ListCell *lc1;
3171
3172 Assert(IsA(ge_info->expr, Var));
3173 Assert(ge_info->sortgroupref > 0);
3174
3175 if (equal(expr, ge_info->expr))
3176 return ge_info->sortgroupref;
3177
3178 if (ge_info->ec == NULL ||
3179 !bms_is_member(((Var *) expr)->varno, ge_info->ec->ec_relids))
3180 continue;
3181
3182 /*
3183 * Scan the EquivalenceClass, looking for a match to the given
3184 * expression. We ignore child members here.
3185 */
3186 foreach(lc1, ge_info->ec->ec_members)
3187 {
3189
3190 /* Child members should not exist in ec_members */
3191 Assert(!em->em_is_child);
3192
3193 if (equal(expr, em->em_expr))
3194 return ge_info->sortgroupref;
3195 }
3196 }
3197
3198 /* no match is found */
3199 return 0;
3200}
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 1807 of file relnode.c.

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

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

2059{
2060 if (path->param_info == NULL)
2061 return NULL; /* not parameterized */
2062
2063 /*
2064 * We don't currently support parameterized MergeAppend paths, as
2065 * explained in the comments for generate_orderedappend_paths.
2066 */
2067 Assert(!IsA(path, MergeAppendPath));
2068
2069 if (IsA(path, NestPath) ||
2070 IsA(path, MergePath) ||
2071 IsA(path, HashPath))
2072 {
2073 /*
2074 * For a join path, combine clauses enforced within either input path
2075 * with those enforced as joinrestrictinfo in this path. Note that
2076 * joinrestrictinfo may include some non-pushed-down clauses, but for
2077 * current purposes it's okay if we include those in the result. (To
2078 * be more careful, we could check for clause_relids overlapping the
2079 * path parameterization, but it's not worth the cycles for now.)
2080 */
2081 JoinPath *jpath = (JoinPath *) path;
2083 ListCell *lc;
2084
2085 pserials = NULL;
2090 foreach(lc, jpath->joinrestrictinfo)
2091 {
2092 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2093
2095 }
2096 return pserials;
2097 }
2098 else if (IsA(path, AppendPath))
2099 {
2100 /*
2101 * For an appendrel, take the intersection of the sets of clauses
2102 * enforced in each input path.
2103 */
2104 AppendPath *apath = (AppendPath *) path;
2106 ListCell *lc;
2107
2108 pserials = NULL;
2109 foreach(lc, apath->subpaths)
2110 {
2111 Path *subpath = (Path *) lfirst(lc);
2113
2115 if (lc == list_head(apath->subpaths))
2117 else
2119 }
2120 return pserials;
2121 }
2122 else
2123 {
2124 /*
2125 * Otherwise, it's a baserel path and we can use the
2126 * previously-computed set of serial numbers.
2127 */
2128 return path->param_info->ppi_serials;
2129 }
2130}
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1108
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:916
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:2058
Path * outerjoinpath
Definition pathnodes.h:2374
Path * innerjoinpath
Definition pathnodes.h:2375
List * joinrestrictinfo
Definition pathnodes.h:2377

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

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

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

2893{
2894 ListCell *lc;
2897
2898 /* Identify the max sortgroupref */
2899 maxSortGroupRef = 0;
2900 foreach(lc, root->processed_tlist)
2901 {
2902 Index ref = ((TargetEntry *) lfirst(lc))->ressortgroupref;
2903
2904 if (ref > maxSortGroupRef)
2906 }
2907
2908 /*
2909 * At this point, all Vars from this relation that are needed by upper
2910 * joins or are required in the final targetlist should already be present
2911 * in its reltarget. Therefore, we can safely iterate over this
2912 * relation's reltarget->exprs to construct the PathTarget and grouping
2913 * clauses for the grouped paths.
2914 */
2915 foreach(lc, rel->reltarget->exprs)
2916 {
2917 Expr *expr = (Expr *) lfirst(lc);
2918 Index sortgroupref;
2919
2920 /*
2921 * Given that PlaceHolderVar currently prevents us from doing eager
2922 * aggregation, the source target cannot contain anything more complex
2923 * than a Var.
2924 */
2925 Assert(IsA(expr, Var));
2926
2927 /*
2928 * Get the sortgroupref of the expr if it is found among, or can be
2929 * deduced from, the original grouping expressions.
2930 */
2931 sortgroupref = get_expression_sortgroupref(root, expr);
2932 if (sortgroupref > 0)
2933 {
2935
2936 /* Find the matching SortGroupClause */
2937 sgc = get_sortgroupref_clause(sortgroupref, root->processed_groupClause);
2938 Assert(sgc->tleSortGroupRef <= maxSortGroupRef);
2939
2940 /*
2941 * If the target expression is to be used as a grouping key, it
2942 * should be emitted by the grouped paths that have been pushed
2943 * down to this relation level.
2944 */
2945 add_column_to_pathtarget(target, expr, sortgroupref);
2946
2947 /*
2948 * ... and it also should be emitted by the input paths.
2949 */
2950 add_column_to_pathtarget(agg_input, expr, sortgroupref);
2951
2952 /*
2953 * Record this SortGroupClause and grouping expression. Note that
2954 * this SortGroupClause might have already been recorded.
2955 */
2956 if (!list_member(*group_clauses, sgc))
2957 {
2958 *group_clauses = lappend(*group_clauses, sgc);
2959 *group_exprs = lappend(*group_exprs, expr);
2960 }
2961 }
2962 else if (is_var_needed_by_join(root, (Var *) expr, rel))
2963 {
2964 /*
2965 * The expression is needed for an upper join but is neither in
2966 * the GROUP BY clause nor derivable from it using EC (otherwise,
2967 * it would have already been included in the targets above). We
2968 * need to create a special SortGroupClause for this expression.
2969 *
2970 * It is important to include such expressions in the grouping
2971 * keys. This is essential to ensure that an aggregated row from
2972 * the partial aggregation matches the other side of the join if
2973 * and only if each row in the partial group does. This ensures
2974 * that all rows within the same partial group share the same
2975 * 'destiny', which is crucial for maintaining correctness.
2976 */
2980
2981 /*
2982 * But first, check if equality implies image equality for this
2983 * expression. If not, we cannot use it as a grouping key. See
2984 * comments in create_grouping_expr_infos().
2985 */
2986 tce = lookup_type_cache(exprType((Node *) expr),
2988 if (!OidIsValid(tce->btree_opf) ||
2989 !OidIsValid(tce->btree_opintype))
2990 return false;
2991
2993 tce->btree_opintype,
2994 tce->btree_opintype,
2996 if (!OidIsValid(equalimageproc) ||
2998 tce->typcollation,
2999 ObjectIdGetDatum(tce->btree_opintype))))
3000 return false;
3001
3002 /* Create the SortGroupClause. */
3004
3005 /* Initialize the SortGroupClause. */
3006 sgc->tleSortGroupRef = ++maxSortGroupRef;
3008 false, true, false,
3009 &sgc->sortop, &sgc->eqop, NULL,
3010 &sgc->hashable);
3011
3012 /* This expression should be emitted by the grouped paths */
3013 add_column_to_pathtarget(target, expr, sgc->tleSortGroupRef);
3014
3015 /* ... and it also should be emitted by the input paths. */
3016 add_column_to_pathtarget(agg_input, expr, sgc->tleSortGroupRef);
3017
3018 /* Record this SortGroupClause and grouping expression */
3019 *group_clauses = lappend(*group_clauses, sgc);
3020 *group_exprs = lappend(*group_exprs, expr);
3021 }
3022 else if (is_var_in_aggref_only(root, (Var *) expr))
3023 {
3024 /*
3025 * The expression is referenced by an aggregate function pushed
3026 * down to this relation and does not appear elsewhere in the
3027 * targetlist or havingQual. Add it to 'agg_input' but not to
3028 * 'target'.
3029 */
3030 add_new_column_to_pathtarget(agg_input, expr);
3031 }
3032 else
3033 {
3034 /*
3035 * The expression may be functionally dependent on other
3036 * expressions in the target, but we cannot verify this until all
3037 * target expressions have been constructed.
3038 */
3040 }
3041 }
3042
3043 /*
3044 * Now we can verify whether an expression is functionally dependent on
3045 * others.
3046 */
3047 foreach(lc, possibly_dependent)
3048 {
3049 Var *tvar;
3050 List *deps = NIL;
3052
3053 tvar = lfirst_node(Var, lc);
3054 rte = root->simple_rte_array[tvar->varno];
3055
3056 if (check_functional_grouping(rte->relid, tvar->varno,
3057 tvar->varlevelsup,
3058 target->exprs, &deps))
3059 {
3060 /*
3061 * The expression is functionally dependent on other target
3062 * expressions, so it can be included in the targets. Since it
3063 * will not be used as a grouping key, a sortgroupref is not
3064 * needed for it.
3065 */
3067 add_new_column_to_pathtarget(agg_input, (Expr *) tvar);
3068 }
3069 else
3070 {
3071 /*
3072 * We may arrive here with a grouping expression that is proven
3073 * redundant by EquivalenceClass processing, such as 't1.a' in the
3074 * query below.
3075 *
3076 * select max(t1.c) from t t1, t t2 where t1.a = 1 group by t1.a,
3077 * t1.b;
3078 *
3079 * For now we just give up in this case.
3080 */
3081 return false;
3082 }
3083 }
3084
3085 return true;
3086}
Datum OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
Definition fmgr.c:1412
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:872
#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:181
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:262
static Index get_expression_sortgroupref(PlannerInfo *root, Expr *expr)
Definition relnode.c:3161
static bool is_var_in_aggref_only(PlannerInfo *root, Var *var)
Definition relnode.c:3094
static bool is_var_needed_by_join(PlannerInfo *root, Var *var, RelOptInfo *rel)
Definition relnode.c:3133
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:386
#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 3094 of file relnode.c.

3095{
3096 ListCell *lc;
3097
3098 /*
3099 * Search the list of aggregate expressions for the Var.
3100 */
3101 foreach(lc, root->agg_clause_list)
3102 {
3104 List *vars;
3105
3106 Assert(IsA(ac_info->aggref, Aggref));
3107
3108 if (!bms_is_member(var->varno, ac_info->agg_eval_at))
3109 continue;
3110
3111 vars = pull_var_clause((Node *) ac_info->aggref,
3115
3116 if (list_member(vars, var))
3117 {
3118 list_free(vars);
3119 break;
3120 }
3121
3122 list_free(vars);
3123 }
3124
3125 return (lc != NULL && !list_member(root->tlist_vars, var));
3126}
void list_free(List *list)
Definition list.c:1546
#define PVC_RECURSE_AGGREGATES
Definition optimizer.h:189
#define PVC_RECURSE_PLACEHOLDERS
Definition optimizer.h:193
#define PVC_RECURSE_WINDOWFUNCS
Definition optimizer.h:191
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 3133 of file relnode.c.

3134{
3135 Relids relids;
3136 int attno;
3138
3139 /*
3140 * Note that when checking if the Var is needed by joins above, we want to
3141 * exclude cases where the Var is only needed in the final targetlist. So
3142 * include "relation 0" in the check.
3143 */
3144 relids = bms_copy(rel->relids);
3145 relids = bms_add_member(relids, 0);
3146
3148 attno = var->varattno - baserel->min_attr;
3149
3150 return bms_nonempty_difference(baserel->attr_needed[attno], relids);
3151}

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

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

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

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

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

710{
711 if (OidIsValid(outer_rel->serverid) &&
712 inner_rel->serverid == outer_rel->serverid)
713 {
714 if (inner_rel->userid == outer_rel->userid)
715 {
716 joinrel->serverid = outer_rel->serverid;
717 joinrel->userid = outer_rel->userid;
718 joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
719 joinrel->fdwroutine = outer_rel->fdwroutine;
720 }
721 else if (!OidIsValid(inner_rel->userid) &&
722 outer_rel->userid == GetUserId())
723 {
724 joinrel->serverid = outer_rel->serverid;
725 joinrel->userid = outer_rel->userid;
726 joinrel->useridiscurrent = true;
727 joinrel->fdwroutine = outer_rel->fdwroutine;
728 }
729 else if (!OidIsValid(outer_rel->userid) &&
730 inner_rel->userid == GetUserId())
731 {
732 joinrel->serverid = outer_rel->serverid;
733 joinrel->userid = inner_rel->userid;
734 joinrel->useridiscurrent = true;
735 joinrel->fdwroutine = outer_rel->fdwroutine;
736 }
737 }
738}
Oid GetUserId(void)
Definition miscinit.c:469

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

2511{
2512 PartitionScheme part_scheme = joinrel->part_scheme;
2513 int partnatts = part_scheme->partnatts;
2514
2515 joinrel->partexprs = palloc0_array(List *, partnatts);
2516 joinrel->nullable_partexprs = palloc0_array(List *, partnatts);
2517
2518 /*
2519 * The joinrel's partition expressions are the same as those of the input
2520 * rels, but we must properly classify them as nullable or not in the
2521 * joinrel's output. (Also, we add some more partition expressions if
2522 * it's a FULL JOIN.)
2523 */
2524 for (int cnt = 0; cnt < partnatts; cnt++)
2525 {
2526 /* mark these const to enforce that we copy them properly */
2527 const List *outer_expr = outer_rel->partexprs[cnt];
2528 const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
2529 const List *inner_expr = inner_rel->partexprs[cnt];
2530 const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
2531 List *partexpr = NIL;
2533 ListCell *lc;
2534
2535 switch (jointype)
2536 {
2537 /*
2538 * A join relation resulting from an INNER join may be
2539 * regarded as partitioned by either of the inner and outer
2540 * relation keys. For example, A INNER JOIN B ON A.a = B.b
2541 * can be regarded as partitioned on either A.a or B.b. So we
2542 * add both keys to the joinrel's partexpr lists. However,
2543 * anything that was already nullable still has to be treated
2544 * as nullable.
2545 */
2546 case JOIN_INNER:
2550 break;
2551
2552 /*
2553 * A join relation resulting from a SEMI or ANTI join may be
2554 * regarded as partitioned by the outer relation keys. The
2555 * inner relation's keys are no longer interesting; since they
2556 * aren't visible in the join output, nothing could join to
2557 * them.
2558 */
2559 case JOIN_SEMI:
2560 case JOIN_ANTI:
2563 break;
2564
2565 /*
2566 * A join relation resulting from a LEFT OUTER JOIN likewise
2567 * may be regarded as partitioned on the (non-nullable) outer
2568 * relation keys. The inner (nullable) relation keys are okay
2569 * as partition keys for further joins as long as they involve
2570 * strict join operators.
2571 */
2572 case JOIN_LEFT:
2578 break;
2579
2580 /*
2581 * For FULL OUTER JOINs, both relations are nullable, so the
2582 * resulting join relation may be regarded as partitioned on
2583 * either of inner and outer relation keys, but only for joins
2584 * that involve strict join operators.
2585 */
2586 case JOIN_FULL:
2588 inner_expr);
2593
2594 /*
2595 * Also add CoalesceExprs corresponding to each possible
2596 * full-join output variable (that is, left side coalesced to
2597 * right side), so that we can match equijoin expressions
2598 * using those variables. We really only need these for
2599 * columns merged by JOIN USING, and only with the pairs of
2600 * input items that correspond to the data structures that
2601 * parse analysis would build for such variables. But it's
2602 * hard to tell which those are, so just make all the pairs.
2603 * Extra items in the nullable_partexprs list won't cause big
2604 * problems. (It's possible that such items will get matched
2605 * to user-written COALESCEs, but it should still be valid to
2606 * partition on those, since they're going to be either the
2607 * partition column or NULL; it's the same argument as for
2608 * partitionwise nesting of any outer join.) We assume no
2609 * type coercions are needed to make the coalesce expressions,
2610 * since columns of different types won't have gotten
2611 * classified as the same PartitionScheme. Note that we
2612 * intentionally leave out the varnullingrels decoration that
2613 * would ordinarily appear on the Vars inside these
2614 * CoalesceExprs, because have_partkey_equi_join will strip
2615 * varnullingrels from the expressions it will compare to the
2616 * partexprs.
2617 */
2619 {
2620 Node *larg = (Node *) lfirst(lc);
2621 ListCell *lc2;
2622
2624 {
2625 Node *rarg = (Node *) lfirst(lc2);
2627
2628 c->coalescetype = exprType(larg);
2629 c->coalescecollid = exprCollation(larg);
2630 c->args = list_make2(larg, rarg);
2631 c->location = -1;
2633 }
2634 }
2635 break;
2636
2637 default:
2638 elog(ERROR, "unrecognized join type: %d", (int) jointype);
2639 }
2640
2641 joinrel->partexprs[cnt] = partexpr;
2642 joinrel->nullable_partexprs[cnt] = nullable_partexpr;
2643 }
2644}
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 111 of file relnode.c.

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

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

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

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

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

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

◆ joinrel_setup_hook

joinrel_setup_hook_type joinrel_setup_hook = NULL

Definition at line 51 of file relnode.c.

Referenced by build_child_join_rel(), and build_join_rel().