PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
subselect.h File Reference
#include "nodes/plannodes.h"
#include "nodes/relation.h"
Include dependency graph for subselect.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void SS_process_ctes (PlannerInfo *root)
 
JoinExprconvert_ANY_sublink_to_join (PlannerInfo *root, SubLink *sublink, Relids available_rels)
 
JoinExprconvert_EXISTS_sublink_to_join (PlannerInfo *root, SubLink *sublink, bool under_not, Relids available_rels)
 
NodeSS_replace_correlation_vars (PlannerInfo *root, Node *expr)
 
NodeSS_process_sublinks (PlannerInfo *root, Node *expr, bool isQual)
 
void SS_identify_outer_params (PlannerInfo *root)
 
void SS_charge_for_initplans (PlannerInfo *root, RelOptInfo *final_rel)
 
void SS_attach_initplans (PlannerInfo *root, Plan *plan)
 
void SS_finalize_plan (PlannerInfo *root, Plan *plan)
 
ParamSS_make_initplan_output_param (PlannerInfo *root, Oid resulttype, int32 resulttypmod, Oid resultcollation)
 
void SS_make_initplan_from_plan (PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
 
Paramassign_nestloop_param_var (PlannerInfo *root, Var *var)
 
Paramassign_nestloop_param_placeholdervar (PlannerInfo *root, PlaceHolderVar *phv)
 
int SS_assign_special_param (PlannerInfo *root)
 

Function Documentation

Param* assign_nestloop_param_placeholdervar ( PlannerInfo root,
PlaceHolderVar phv 
)

Definition at line 278 of file subselect.c.

References Assert, assign_param_for_placeholdervar(), exprCollation(), exprType(), exprTypmod(), i, Param::location, makeNode, PARAM_EXEC, Param::paramcollid, Param::paramid, Param::paramkind, Param::paramtype, Param::paramtypmod, PlaceHolderVar::phexpr, and PlaceHolderVar::phlevelsup.

Referenced by replace_nestloop_params_mutator().

279 {
280  Param *retval;
281  int i;
282 
283  Assert(phv->phlevelsup == 0);
284 
285  i = assign_param_for_placeholdervar(root, phv);
286 
287  retval = makeNode(Param);
288  retval->paramkind = PARAM_EXEC;
289  retval->paramid = i;
290  retval->paramtype = exprType((Node *) phv->phexpr);
291  retval->paramtypmod = exprTypmod((Node *) phv->phexpr);
292  retval->paramcollid = exprCollation((Node *) phv->phexpr);
293  retval->location = -1;
294 
295  return retval;
296 }
static int assign_param_for_placeholdervar(PlannerInfo *root, PlaceHolderVar *phv)
Definition: subselect.c:202
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
ParamKind paramkind
Definition: primnodes.h:244
Definition: nodes.h:506
Expr * phexpr
Definition: relation.h:1802
Oid paramcollid
Definition: primnodes.h:248
int location
Definition: primnodes.h:249
int32 paramtypmod
Definition: primnodes.h:247
#define makeNode(_type_)
Definition: nodes.h:554
#define Assert(condition)
Definition: c.h:675
int paramid
Definition: primnodes.h:245
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
Index phlevelsup
Definition: relation.h:1805
int i
Oid paramtype
Definition: primnodes.h:246
Param* assign_nestloop_param_var ( PlannerInfo root,
Var var 
)

Definition at line 174 of file subselect.c.

References Assert, assign_param_for_var(), i, Var::location, Param::location, makeNode, PARAM_EXEC, Param::paramcollid, Param::paramid, Param::paramkind, Param::paramtype, Param::paramtypmod, Var::varcollid, Var::varlevelsup, Var::vartype, and Var::vartypmod.

Referenced by replace_nestloop_params_mutator().

175 {
176  Param *retval;
177  int i;
178 
179  Assert(var->varlevelsup == 0);
180 
181  i = assign_param_for_var(root, var);
182 
183  retval = makeNode(Param);
184  retval->paramkind = PARAM_EXEC;
185  retval->paramid = i;
186  retval->paramtype = var->vartype;
187  retval->paramtypmod = var->vartypmod;
188  retval->paramcollid = var->varcollid;
189  retval->location = var->location;
190 
191  return retval;
192 }
Index varlevelsup
Definition: primnodes.h:173
static int assign_param_for_var(PlannerInfo *root, Var *var)
Definition: subselect.c:94
ParamKind paramkind
Definition: primnodes.h:244
Oid paramcollid
Definition: primnodes.h:248
int location
Definition: primnodes.h:249
Oid vartype
Definition: primnodes.h:170
int location
Definition: primnodes.h:178
int32 paramtypmod
Definition: primnodes.h:247
#define makeNode(_type_)
Definition: nodes.h:554
#define Assert(condition)
Definition: c.h:675
int paramid
Definition: primnodes.h:245
int i
Oid varcollid
Definition: primnodes.h:172
Oid paramtype
Definition: primnodes.h:246
int32 vartypmod
Definition: primnodes.h:171
JoinExpr* convert_ANY_sublink_to_join ( PlannerInfo root,
SubLink sublink,
Relids  available_rels 
)

Definition at line 1300 of file subselect.c.

References addRangeTableEntryForSubquery(), JoinExpr::alias, ANY_SUBLINK, Assert, bms_is_empty(), bms_is_subset(), contain_vars_of_level(), contain_volatile_functions(), convert_testexpr(), generate_subquery_vars(), JoinExpr::isNatural, JOIN_SEMI, JoinExpr::jointype, lappend(), JoinExpr::larg, list_length(), make_parsestate(), makeAlias(), makeNode, NIL, NULL, parse(), PlannerInfo::parse, pull_varnos(), JoinExpr::quals, JoinExpr::rarg, result, Query::rtable, JoinExpr::rtindex, SubLink::subLinkType, SubLink::subselect, Query::targetList, SubLink::testexpr, and JoinExpr::usingClause.

Referenced by pull_up_sublinks_qual_recurse().

1302 {
1303  JoinExpr *result;
1304  Query *parse = root->parse;
1305  Query *subselect = (Query *) sublink->subselect;
1306  Relids upper_varnos;
1307  int rtindex;
1308  RangeTblEntry *rte;
1309  RangeTblRef *rtr;
1310  List *subquery_vars;
1311  Node *quals;
1312  ParseState *pstate;
1313 
1314  Assert(sublink->subLinkType == ANY_SUBLINK);
1315 
1316  /*
1317  * The sub-select must not refer to any Vars of the parent query. (Vars of
1318  * higher levels should be okay, though.)
1319  */
1320  if (contain_vars_of_level((Node *) subselect, 1))
1321  return NULL;
1322 
1323  /*
1324  * The test expression must contain some Vars of the parent query, else
1325  * it's not gonna be a join. (Note that it won't have Vars referring to
1326  * the subquery, rather Params.)
1327  */
1328  upper_varnos = pull_varnos(sublink->testexpr);
1329  if (bms_is_empty(upper_varnos))
1330  return NULL;
1331 
1332  /*
1333  * However, it can't refer to anything outside available_rels.
1334  */
1335  if (!bms_is_subset(upper_varnos, available_rels))
1336  return NULL;
1337 
1338  /*
1339  * The combining operators and left-hand expressions mustn't be volatile.
1340  */
1341  if (contain_volatile_functions(sublink->testexpr))
1342  return NULL;
1343 
1344  /* Create a dummy ParseState for addRangeTableEntryForSubquery */
1345  pstate = make_parsestate(NULL);
1346 
1347  /*
1348  * Okay, pull up the sub-select into upper range table.
1349  *
1350  * We rely here on the assumption that the outer query has no references
1351  * to the inner (necessarily true, other than the Vars that we build
1352  * below). Therefore this is a lot easier than what pull_up_subqueries has
1353  * to go through.
1354  */
1355  rte = addRangeTableEntryForSubquery(pstate,
1356  subselect,
1357  makeAlias("ANY_subquery", NIL),
1358  false,
1359  false);
1360  parse->rtable = lappend(parse->rtable, rte);
1361  rtindex = list_length(parse->rtable);
1362 
1363  /*
1364  * Form a RangeTblRef for the pulled-up sub-select.
1365  */
1366  rtr = makeNode(RangeTblRef);
1367  rtr->rtindex = rtindex;
1368 
1369  /*
1370  * Build a list of Vars representing the subselect outputs.
1371  */
1372  subquery_vars = generate_subquery_vars(root,
1373  subselect->targetList,
1374  rtindex);
1375 
1376  /*
1377  * Build the new join's qual expression, replacing Params with these Vars.
1378  */
1379  quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
1380 
1381  /*
1382  * And finally, build the JoinExpr node.
1383  */
1384  result = makeNode(JoinExpr);
1385  result->jointype = JOIN_SEMI;
1386  result->isNatural = false;
1387  result->larg = NULL; /* caller must fill this in */
1388  result->rarg = (Node *) rtr;
1389  result->usingClause = NIL;
1390  result->quals = quals;
1391  result->alias = NULL;
1392  result->rtindex = 0; /* we don't need an RTE for it */
1393 
1394  return result;
1395 }
static Node * convert_testexpr(PlannerInfo *root, Node *testexpr, List *subst_nodes)
Definition: subselect.c:964
#define NIL
Definition: pg_list.h:69
Query * parse
Definition: relation.h:154
Definition: nodes.h:506
return result
Definition: formatting.c:1618
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
List * targetList
Definition: parsenodes.h:131
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
Node * larg
Definition: primnodes.h:1435
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
List * rtable
Definition: parsenodes.h:128
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
static List * generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
Definition: subselect.c:935
Relids pull_varnos(Node *node)
Definition: var.c:95
List * lappend(List *list, void *datum)
Definition: list.c:128
RangeTblEntry * addRangeTableEntryForSubquery(ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
bool isNatural
Definition: primnodes.h:1434
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * usingClause
Definition: primnodes.h:1437
Node * quals
Definition: primnodes.h:1438
#define makeNode(_type_)
Definition: nodes.h:554
Node * rarg
Definition: primnodes.h:1436
Alias * alias
Definition: primnodes.h:1439
#define NULL
Definition: c.h:229
JoinType jointype
Definition: primnodes.h:1433
#define Assert(condition)
Definition: c.h:675
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
static int list_length(const List *l)
Definition: pg_list.h:89
int rtindex
Definition: primnodes.h:1440
Definition: pg_list.h:45
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:649
JoinExpr* convert_EXISTS_sublink_to_join ( PlannerInfo root,
SubLink sublink,
bool  under_not,
Relids  available_rels 
)

Definition at line 1405 of file subselect.c.

References JoinExpr::alias, Assert, bms_add_member(), bms_first_member(), bms_free(), bms_is_empty(), bms_is_subset(), contain_vars_of_level(), contain_volatile_functions(), copyObject(), Query::cteList, EXISTS_SUBLINK, FromExpr::fromlist, IncrementVarSublevelsUp(), JoinExpr::isNatural, JOIN_ANTI, JOIN_SEMI, Query::jointree, JoinExpr::jointype, JoinExpr::larg, linitial, list_concat(), list_length(), makeNode, NIL, NULL, OffsetVarNodes(), parse(), PlannerInfo::parse, pull_varnos(), JoinExpr::quals, FromExpr::quals, JoinExpr::rarg, result, Query::rtable, JoinExpr::rtindex, simplify_EXISTS_query(), SubLink::subLinkType, SubLink::subselect, and JoinExpr::usingClause.

Referenced by pull_up_sublinks_qual_recurse().

1407 {
1408  JoinExpr *result;
1409  Query *parse = root->parse;
1410  Query *subselect = (Query *) sublink->subselect;
1411  Node *whereClause;
1412  int rtoffset;
1413  int varno;
1414  Relids clause_varnos;
1415  Relids upper_varnos;
1416 
1417  Assert(sublink->subLinkType == EXISTS_SUBLINK);
1418 
1419  /*
1420  * Can't flatten if it contains WITH. (We could arrange to pull up the
1421  * WITH into the parent query's cteList, but that risks changing the
1422  * semantics, since a WITH ought to be executed once per associated query
1423  * call.) Note that convert_ANY_sublink_to_join doesn't have to reject
1424  * this case, since it just produces a subquery RTE that doesn't have to
1425  * get flattened into the parent query.
1426  */
1427  if (subselect->cteList)
1428  return NULL;
1429 
1430  /*
1431  * Copy the subquery so we can modify it safely (see comments in
1432  * make_subplan).
1433  */
1434  subselect = (Query *) copyObject(subselect);
1435 
1436  /*
1437  * See if the subquery can be simplified based on the knowledge that it's
1438  * being used in EXISTS(). If we aren't able to get rid of its
1439  * targetlist, we have to fail, because the pullup operation leaves us
1440  * with noplace to evaluate the targetlist.
1441  */
1442  if (!simplify_EXISTS_query(root, subselect))
1443  return NULL;
1444 
1445  /*
1446  * The subquery must have a nonempty jointree, else we won't have a join.
1447  */
1448  if (subselect->jointree->fromlist == NIL)
1449  return NULL;
1450 
1451  /*
1452  * Separate out the WHERE clause. (We could theoretically also remove
1453  * top-level plain JOIN/ON clauses, but it's probably not worth the
1454  * trouble.)
1455  */
1456  whereClause = subselect->jointree->quals;
1457  subselect->jointree->quals = NULL;
1458 
1459  /*
1460  * The rest of the sub-select must not refer to any Vars of the parent
1461  * query. (Vars of higher levels should be okay, though.)
1462  */
1463  if (contain_vars_of_level((Node *) subselect, 1))
1464  return NULL;
1465 
1466  /*
1467  * On the other hand, the WHERE clause must contain some Vars of the
1468  * parent query, else it's not gonna be a join.
1469  */
1470  if (!contain_vars_of_level(whereClause, 1))
1471  return NULL;
1472 
1473  /*
1474  * We don't risk optimizing if the WHERE clause is volatile, either.
1475  */
1476  if (contain_volatile_functions(whereClause))
1477  return NULL;
1478 
1479  /*
1480  * Prepare to pull up the sub-select into top range table.
1481  *
1482  * We rely here on the assumption that the outer query has no references
1483  * to the inner (necessarily true). Therefore this is a lot easier than
1484  * what pull_up_subqueries has to go through.
1485  *
1486  * In fact, it's even easier than what convert_ANY_sublink_to_join has to
1487  * do. The machinations of simplify_EXISTS_query ensured that there is
1488  * nothing interesting in the subquery except an rtable and jointree, and
1489  * even the jointree FromExpr no longer has quals. So we can just append
1490  * the rtable to our own and use the FromExpr in our jointree. But first,
1491  * adjust all level-zero varnos in the subquery to account for the rtable
1492  * merger.
1493  */
1494  rtoffset = list_length(parse->rtable);
1495  OffsetVarNodes((Node *) subselect, rtoffset, 0);
1496  OffsetVarNodes(whereClause, rtoffset, 0);
1497 
1498  /*
1499  * Upper-level vars in subquery will now be one level closer to their
1500  * parent than before; in particular, anything that had been level 1
1501  * becomes level zero.
1502  */
1503  IncrementVarSublevelsUp((Node *) subselect, -1, 1);
1504  IncrementVarSublevelsUp(whereClause, -1, 1);
1505 
1506  /*
1507  * Now that the WHERE clause is adjusted to match the parent query
1508  * environment, we can easily identify all the level-zero rels it uses.
1509  * The ones <= rtoffset belong to the upper query; the ones > rtoffset do
1510  * not.
1511  */
1512  clause_varnos = pull_varnos(whereClause);
1513  upper_varnos = NULL;
1514  while ((varno = bms_first_member(clause_varnos)) >= 0)
1515  {
1516  if (varno <= rtoffset)
1517  upper_varnos = bms_add_member(upper_varnos, varno);
1518  }
1519  bms_free(clause_varnos);
1520  Assert(!bms_is_empty(upper_varnos));
1521 
1522  /*
1523  * Now that we've got the set of upper-level varnos, we can make the last
1524  * check: only available_rels can be referenced.
1525  */
1526  if (!bms_is_subset(upper_varnos, available_rels))
1527  return NULL;
1528 
1529  /* Now we can attach the modified subquery rtable to the parent */
1530  parse->rtable = list_concat(parse->rtable, subselect->rtable);
1531 
1532  /*
1533  * And finally, build the JoinExpr node.
1534  */
1535  result = makeNode(JoinExpr);
1536  result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
1537  result->isNatural = false;
1538  result->larg = NULL; /* caller must fill this in */
1539  /* flatten out the FromExpr node if it's useless */
1540  if (list_length(subselect->jointree->fromlist) == 1)
1541  result->rarg = (Node *) linitial(subselect->jointree->fromlist);
1542  else
1543  result->rarg = (Node *) subselect->jointree;
1544  result->usingClause = NIL;
1545  result->quals = whereClause;
1546  result->alias = NULL;
1547  result->rtindex = 0; /* we don't need an RTE for it */
1548 
1549  return result;
1550 }
#define NIL
Definition: pg_list.h:69
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:885
Query * parse
Definition: relation.h:154
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
Definition: rewriteManip.c:424
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:773
FromExpr * jointree
Definition: parsenodes.h:129
Definition: nodes.h:506
List * list_concat(List *list1, List *list2)
Definition: list.c:321
return result
Definition: formatting.c:1618
List * fromlist
Definition: primnodes.h:1455
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
static bool simplify_EXISTS_query(PlannerInfo *root, Query *query)
Definition: subselect.c:1569
Node * quals
Definition: primnodes.h:1456
void * copyObject(const void *from)
Definition: copyfuncs.c:4619
Node * larg
Definition: primnodes.h:1435
#define linitial(l)
Definition: pg_list.h:110
List * rtable
Definition: parsenodes.h:128
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids pull_varnos(Node *node)
Definition: var.c:95
bool isNatural
Definition: primnodes.h:1434
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * usingClause
Definition: primnodes.h:1437
Node * quals
Definition: primnodes.h:1438
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define makeNode(_type_)
Definition: nodes.h:554
Node * rarg
Definition: primnodes.h:1436
Alias * alias
Definition: primnodes.h:1439
#define NULL
Definition: c.h:229
JoinType jointype
Definition: primnodes.h:1433
#define Assert(condition)
Definition: c.h:675
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
static int list_length(const List *l)
Definition: pg_list.h:89
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
List * cteList
Definition: parsenodes.h:126
int rtindex
Definition: primnodes.h:1440
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:649
int SS_assign_special_param ( PlannerInfo root)

Definition at line 415 of file subselect.c.

References PlannerInfo::glob, and PlannerGlobal::nParamExec.

Referenced by grouping_planner(), inheritance_planner(), SS_process_ctes(), and subquery_planner().

416 {
417  return root->glob->nParamExec++;
418 }
PlannerGlobal * glob
Definition: relation.h:156
int nParamExec
Definition: relation.h:116
void SS_attach_initplans ( PlannerInfo root,
Plan plan 
)

Definition at line 2203 of file subselect.c.

References PlannerInfo::init_plans, and Plan::initPlan.

Referenced by create_plan().

2204 {
2205  plan->initPlan = root->init_plans;
2206 }
List * init_plans
Definition: relation.h:227
List * initPlan
Definition: plannodes.h:136
void SS_charge_for_initplans ( PlannerInfo root,
RelOptInfo final_rel 
)

Definition at line 2153 of file subselect.c.

References PlannerInfo::init_plans, lfirst, NIL, Path::parallel_safe, RelOptInfo::pathlist, SubPlan::per_call_cost, SubPlan::startup_cost, Path::startup_cost, and Path::total_cost.

Referenced by build_minmax_path(), and subquery_planner().

2154 {
2155  Cost initplan_cost;
2156  ListCell *lc;
2157 
2158  /* Nothing to do if no initPlans */
2159  if (root->init_plans == NIL)
2160  return;
2161 
2162  /*
2163  * Compute the cost increment just once, since it will be the same for all
2164  * Paths. We assume each initPlan gets run once during top plan startup.
2165  * This is a conservative overestimate, since in fact an initPlan might be
2166  * executed later than plan startup, or even not at all.
2167  */
2168  initplan_cost = 0;
2169  foreach(lc, root->init_plans)
2170  {
2171  SubPlan *initsubplan = (SubPlan *) lfirst(lc);
2172 
2173  initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
2174  }
2175 
2176  /*
2177  * Now adjust the costs and parallel_safe flags.
2178  */
2179  foreach(lc, final_rel->pathlist)
2180  {
2181  Path *path = (Path *) lfirst(lc);
2182 
2183  path->startup_cost += initplan_cost;
2184  path->total_cost += initplan_cost;
2185  path->parallel_safe = false;
2186  }
2187 
2188  /* We needn't do set_cheapest() here, caller will do it */
2189 }
#define NIL
Definition: pg_list.h:69
Cost startup_cost
Definition: relation.h:929
List * init_plans
Definition: relation.h:227
Cost per_call_cost
Definition: primnodes.h:711
Cost total_cost
Definition: relation.h:930
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:923
Cost startup_cost
Definition: primnodes.h:710
List * pathlist
Definition: relation.h:508
Definition: relation.h:911
double Cost
Definition: nodes.h:630
void SS_finalize_plan ( PlannerInfo root,
Plan plan 
)

Definition at line 2218 of file subselect.c.

References finalize_plan(), NULL, and PlannerInfo::outer_params.

Referenced by finalize_plan(), and standard_planner().

2219 {
2220  /* No setup needed, just recurse through plan tree. */
2221  (void) finalize_plan(root, plan, root->outer_params, NULL);
2222 }
static Bitmapset * finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params, Bitmapset *scan_params)
Definition: subselect.c:2253
#define NULL
Definition: c.h:229
Bitmapset * outer_params
Definition: relation.h:169
void SS_identify_outer_params ( PlannerInfo root)

Definition at line 2091 of file subselect.c.

References bms_add_member(), PlannerInfo::glob, PlannerInfo::init_plans, lfirst, lfirst_int, PlannerGlobal::nParamExec, NULL, PlannerInfo::outer_params, PlannerParamItem::paramId, PlannerInfo::parent_root, PlannerInfo::plan_params, SubPlan::setParam, and PlannerInfo::wt_param_id.

Referenced by build_minmax_path(), and subquery_planner().

2092 {
2093  Bitmapset *outer_params;
2094  PlannerInfo *proot;
2095  ListCell *l;
2096 
2097  /*
2098  * If no parameters have been assigned anywhere in the tree, we certainly
2099  * don't need to do anything here.
2100  */
2101  if (root->glob->nParamExec == 0)
2102  return;
2103 
2104  /*
2105  * Scan all query levels above this one to see which parameters are due to
2106  * be available from them, either because lower query levels have
2107  * requested them (via plan_params) or because they will be available from
2108  * initPlans of those levels.
2109  */
2110  outer_params = NULL;
2111  for (proot = root->parent_root; proot != NULL; proot = proot->parent_root)
2112  {
2113  /* Include ordinary Var/PHV/Aggref params */
2114  foreach(l, proot->plan_params)
2115  {
2116  PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
2117 
2118  outer_params = bms_add_member(outer_params, pitem->paramId);
2119  }
2120  /* Include any outputs of outer-level initPlans */
2121  foreach(l, proot->init_plans)
2122  {
2123  SubPlan *initsubplan = (SubPlan *) lfirst(l);
2124  ListCell *l2;
2125 
2126  foreach(l2, initsubplan->setParam)
2127  {
2128  outer_params = bms_add_member(outer_params, lfirst_int(l2));
2129  }
2130  }
2131  /* Include worktable ID, if a recursive query is being planned */
2132  if (proot->wt_param_id >= 0)
2133  outer_params = bms_add_member(outer_params, proot->wt_param_id);
2134  }
2135  root->outer_params = outer_params;
2136 }
List * plan_params
Definition: relation.h:168
int wt_param_id
Definition: relation.h:307
#define lfirst_int(lc)
Definition: pg_list.h:107
PlannerGlobal * glob
Definition: relation.h:156
struct PlannerInfo * parent_root
Definition: relation.h:160
List * init_plans
Definition: relation.h:227
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * setParam
Definition: primnodes.h:705
Bitmapset * outer_params
Definition: relation.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
int nParamExec
Definition: relation.h:116
void SS_make_initplan_from_plan ( PlannerInfo root,
PlannerInfo subroot,
Plan plan,
Param prm 
)

Definition at line 2899 of file subselect.c.

References cost_subplan(), EXPR_SUBLINK, SubPlan::firstColCollation, SubPlan::firstColType, SubPlan::firstColTypmod, get_first_col_type(), PlannerInfo::glob, PlannerInfo::init_plans, lappend(), list_length(), list_make1_int, makeNode, Param::paramid, SubPlan::plan_id, SubPlan::plan_name, psprintf(), SubPlan::setParam, SubPlan::subLinkType, PlannerGlobal::subplans, and PlannerGlobal::subroots.

Referenced by create_minmaxagg_plan().

2902 {
2903  SubPlan *node;
2904 
2905  /*
2906  * Add the subplan and its PlannerInfo to the global lists.
2907  */
2908  root->glob->subplans = lappend(root->glob->subplans, plan);
2909  root->glob->subroots = lappend(root->glob->subroots, subroot);
2910 
2911  /*
2912  * Create a SubPlan node and add it to the outer list of InitPlans. Note
2913  * it has to appear after any other InitPlans it might depend on (see
2914  * comments in ExecReScan).
2915  */
2916  node = makeNode(SubPlan);
2917  node->subLinkType = EXPR_SUBLINK;
2918  node->plan_id = list_length(root->glob->subplans);
2919  node->plan_name = psprintf("InitPlan %d (returns $%d)",
2920  node->plan_id, prm->paramid);
2921  get_first_col_type(plan, &node->firstColType, &node->firstColTypmod,
2922  &node->firstColCollation);
2923  node->setParam = list_make1_int(prm->paramid);
2924 
2925  root->init_plans = lappend(root->init_plans, node);
2926 
2927  /*
2928  * The node can't have any inputs (since it's an initplan), so the
2929  * parParam and args lists remain empty.
2930  */
2931 
2932  /* Set costs of SubPlan using info from the plan tree */
2933  cost_subplan(subroot, node, plan);
2934 }
Oid firstColType
Definition: primnodes.h:692
int plan_id
Definition: primnodes.h:688
static void get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod, Oid *colcollation)
Definition: subselect.c:430
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
SubLinkType subLinkType
Definition: primnodes.h:683
List * subplans
Definition: relation.h:98
PlannerGlobal * glob
Definition: relation.h:156
#define list_make1_int(x1)
Definition: pg_list.h:139
List * lappend(List *list, void *datum)
Definition: list.c:128
List * init_plans
Definition: relation.h:227
int32 firstColTypmod
Definition: primnodes.h:693
#define makeNode(_type_)
Definition: nodes.h:554
char * plan_name
Definition: primnodes.h:690
List * setParam
Definition: primnodes.h:705
int paramid
Definition: primnodes.h:245
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:3164
static int list_length(const List *l)
Definition: pg_list.h:89
List * subroots
Definition: relation.h:100
Oid firstColCollation
Definition: primnodes.h:694
Param* SS_make_initplan_output_param ( PlannerInfo root,
Oid  resulttype,
int32  resulttypmod,
Oid  resultcollation 
)

Definition at line 2884 of file subselect.c.

References generate_new_param().

Referenced by preprocess_minmax_aggregates().

2887 {
2888  return generate_new_param(root, resulttype, resulttypmod, resultcollation);
2889 }
static Param * generate_new_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod, Oid paramcollation)
Definition: subselect.c:390
void SS_process_ctes ( PlannerInfo root)

Definition at line 1142 of file subselect.c.

References SubPlan::args, Assert, RelOptInfo::cheapest_total_path, CMD_SELECT, copyObject(), cost_subplan(), create_plan(), PlannerInfo::cte_plan_ids, CTE_SUBLINK, Query::cteList, CommonTableExpr::ctename, CommonTableExpr::ctequery, CommonTableExpr::cterecursive, CommonTableExpr::cterefcount, elog, ERROR, fetch_upper_rel(), SubPlan::firstColCollation, SubPlan::firstColType, SubPlan::firstColTypmod, get_first_col_type(), PlannerInfo::glob, PlannerInfo::init_plans, lappend(), lappend_int(), lfirst, list_length(), list_make1_int, makeNode, NIL, NULL, SubPlan::parallel_safe, SubPlan::paramIds, SubPlan::parParam, PlannerInfo::parse, SubPlan::plan_id, SubPlan::plan_name, PlannerInfo::plan_params, psprintf(), SubPlan::setParam, splan, SS_assign_special_param(), SubPlan::subLinkType, PlannerGlobal::subplans, subquery_planner(), PlannerGlobal::subroots, SubPlan::testexpr, SubPlan::unknownEqFalse, UPPERREL_FINAL, and SubPlan::useHashTable.

Referenced by subquery_planner().

1143 {
1144  ListCell *lc;
1145 
1146  Assert(root->cte_plan_ids == NIL);
1147 
1148  foreach(lc, root->parse->cteList)
1149  {
1150  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1151  CmdType cmdType = ((Query *) cte->ctequery)->commandType;
1152  Query *subquery;
1153  PlannerInfo *subroot;
1154  RelOptInfo *final_rel;
1155  Path *best_path;
1156  Plan *plan;
1157  SubPlan *splan;
1158  int paramid;
1159 
1160  /*
1161  * Ignore SELECT CTEs that are not actually referenced anywhere.
1162  */
1163  if (cte->cterefcount == 0 && cmdType == CMD_SELECT)
1164  {
1165  /* Make a dummy entry in cte_plan_ids */
1166  root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
1167  continue;
1168  }
1169 
1170  /*
1171  * Copy the source Query node. Probably not necessary, but let's keep
1172  * this similar to make_subplan.
1173  */
1174  subquery = (Query *) copyObject(cte->ctequery);
1175 
1176  /* plan_params should not be in use in current query level */
1177  Assert(root->plan_params == NIL);
1178 
1179  /*
1180  * Generate Paths for the CTE query. Always plan for full retrieval
1181  * --- we don't have enough info to predict otherwise.
1182  */
1183  subroot = subquery_planner(root->glob, subquery,
1184  root,
1185  cte->cterecursive, 0.0);
1186 
1187  /*
1188  * Since the current query level doesn't yet contain any RTEs, it
1189  * should not be possible for the CTE to have requested parameters of
1190  * this level.
1191  */
1192  if (root->plan_params)
1193  elog(ERROR, "unexpected outer reference in CTE query");
1194 
1195  /*
1196  * Select best Path and turn it into a Plan. At least for now, there
1197  * seems no reason to postpone doing that.
1198  */
1199  final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
1200  best_path = final_rel->cheapest_total_path;
1201 
1202  plan = create_plan(subroot, best_path);
1203 
1204  /*
1205  * Make a SubPlan node for it. This is just enough unlike
1206  * build_subplan that we can't share code.
1207  *
1208  * Note plan_id, plan_name, and cost fields are set further down.
1209  */
1210  splan = makeNode(SubPlan);
1211  splan->subLinkType = CTE_SUBLINK;
1212  splan->testexpr = NULL;
1213  splan->paramIds = NIL;
1214  get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
1215  &splan->firstColCollation);
1216  splan->useHashTable = false;
1217  splan->unknownEqFalse = false;
1218 
1219  /*
1220  * CTE scans are not considered for parallelism (cf
1221  * set_rel_consider_parallel).
1222  */
1223  splan->parallel_safe = false;
1224  splan->setParam = NIL;
1225  splan->parParam = NIL;
1226  splan->args = NIL;
1227 
1228  /*
1229  * The node can't have any inputs (since it's an initplan), so the
1230  * parParam and args lists remain empty. (It could contain references
1231  * to earlier CTEs' output param IDs, but CTE outputs are not
1232  * propagated via the args list.)
1233  */
1234 
1235  /*
1236  * Assign a param ID to represent the CTE's output. No ordinary
1237  * "evaluation" of this param slot ever happens, but we use the param
1238  * ID for setParam/chgParam signaling just as if the CTE plan were
1239  * returning a simple scalar output. (Also, the executor abuses the
1240  * ParamExecData slot for this param ID for communication among
1241  * multiple CteScan nodes that might be scanning this CTE.)
1242  */
1243  paramid = SS_assign_special_param(root);
1244  splan->setParam = list_make1_int(paramid);
1245 
1246  /*
1247  * Add the subplan and its PlannerInfo to the global lists.
1248  */
1249  root->glob->subplans = lappend(root->glob->subplans, plan);
1250  root->glob->subroots = lappend(root->glob->subroots, subroot);
1251  splan->plan_id = list_length(root->glob->subplans);
1252 
1253  root->init_plans = lappend(root->init_plans, splan);
1254 
1255  root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
1256 
1257  /* Label the subplan for EXPLAIN purposes */
1258  splan->plan_name = psprintf("CTE %s", cte->ctename);
1259 
1260  /* Lastly, fill in the cost estimates for use later */
1261  cost_subplan(root, splan, plan);
1262  }
1263 }
#define NIL
Definition: pg_list.h:69
Oid firstColType
Definition: primnodes.h:692
Query * parse
Definition: relation.h:154
int plan_id
Definition: primnodes.h:688
List * plan_params
Definition: relation.h:168
int SS_assign_special_param(PlannerInfo *root)
Definition: subselect.c:415
static void get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod, Oid *colcollation)
Definition: subselect.c:430
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
SubLinkType subLinkType
Definition: primnodes.h:683
List * paramIds
Definition: primnodes.h:686
void * copyObject(const void *from)
Definition: copyfuncs.c:4619
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:300
#define ERROR
Definition: elog.h:43
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:895
struct Path * cheapest_total_path
Definition: relation.h:512
List * subplans
Definition: relation.h:98
PlannerGlobal * glob
Definition: relation.h:156
static SPIPlanPtr splan
Definition: regress.c:461
#define list_make1_int(x1)
Definition: pg_list.h:139
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
List * cte_plan_ids
Definition: relation.h:229
Node * testexpr
Definition: primnodes.h:685
List * init_plans
Definition: relation.h:227
int32 firstColTypmod
Definition: primnodes.h:693
#define makeNode(_type_)
Definition: nodes.h:554
char * plan_name
Definition: primnodes.h:690
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
List * setParam
Definition: primnodes.h:705
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:3164
bool unknownEqFalse
Definition: primnodes.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
List * subroots
Definition: relation.h:100
List * parParam
Definition: primnodes.h:707
bool parallel_safe
Definition: primnodes.h:702
List * cteList
Definition: parsenodes.h:126
bool useHashTable
Definition: primnodes.h:697
Oid firstColCollation
Definition: primnodes.h:694
#define elog
Definition: elog.h:219
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
Definition: planner.c:482
CmdType
Definition: nodes.h:639
Definition: relation.h:911
List * args
Definition: primnodes.h:708
Node* SS_process_sublinks ( PlannerInfo root,
Node expr,
bool  isQual 
)

Definition at line 1944 of file subselect.c.

References process_sublinks_context::isTopQual, process_sublinks_mutator(), and process_sublinks_context::root.

Referenced by build_subplan(), and preprocess_expression().

1945 {
1946  process_sublinks_context context;
1947 
1948  context.root = root;
1949  context.isTopQual = isQual;
1950  return process_sublinks_mutator(expr, &context);
1951 }
static Node * process_sublinks_mutator(Node *node, process_sublinks_context *context)
Definition: subselect.c:1954
Node* SS_replace_correlation_vars ( PlannerInfo root,
Node expr 
)

Definition at line 1899 of file subselect.c.

References replace_correlation_vars_mutator().

Referenced by preprocess_expression().

1900 {
1901  /* No setup needed for tree walk, so away we go */
1902  return replace_correlation_vars_mutator(expr, root);
1903 }
static Node * replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
Definition: subselect.c:1906