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:276
ParamKind paramkind
Definition: primnodes.h:244
Definition: nodes.h:509
Expr * phexpr
Definition: relation.h:1850
Oid paramcollid
Definition: primnodes.h:248
int location
Definition: primnodes.h:249
int32 paramtypmod
Definition: primnodes.h:247
#define makeNode(_type_)
Definition: nodes.h:557
#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:748
Index phlevelsup
Definition: relation.h:1853
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:557
#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 1299 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().

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

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

Definition at line 2202 of file subselect.c.

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

Referenced by create_plan().

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

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

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

Definition at line 2217 of file subselect.c.

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

Referenced by finalize_plan(), and standard_planner().

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

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

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

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

2905 {
2906  SubPlan *node;
2907 
2908  /*
2909  * Add the subplan and its PlannerInfo to the global lists.
2910  */
2911  root->glob->subplans = lappend(root->glob->subplans, plan);
2912  root->glob->subroots = lappend(root->glob->subroots, subroot);
2913 
2914  /*
2915  * Create a SubPlan node and add it to the outer list of InitPlans. Note
2916  * it has to appear after any other InitPlans it might depend on (see
2917  * comments in ExecReScan).
2918  */
2919  node = makeNode(SubPlan);
2920  node->subLinkType = EXPR_SUBLINK;
2921  node->plan_id = list_length(root->glob->subplans);
2922  node->plan_name = psprintf("InitPlan %d (returns $%d)",
2923  node->plan_id, prm->paramid);
2924  get_first_col_type(plan, &node->firstColType, &node->firstColTypmod,
2925  &node->firstColCollation);
2926  node->setParam = list_make1_int(prm->paramid);
2927 
2928  root->init_plans = lappend(root->init_plans, node);
2929 
2930  /*
2931  * The node can't have any inputs (since it's an initplan), so the
2932  * parParam and args lists remain empty.
2933  */
2934 
2935  /* Set costs of SubPlan using info from the plan tree */
2936  cost_subplan(subroot, node, plan);
2937 }
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:157
#define list_make1_int(x1)
Definition: pg_list.h:145
List * lappend(List *list, void *datum)
Definition: list.c:128
List * init_plans
Definition: relation.h:228
int32 firstColTypmod
Definition: primnodes.h:693
#define makeNode(_type_)
Definition: nodes.h:557
char * plan_name
Definition: primnodes.h:690
List * setParam
Definition: primnodes.h:706
int paramid
Definition: primnodes.h:245
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:3228
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 2887 of file subselect.c.

References generate_new_param().

Referenced by preprocess_minmax_aggregates().

2890 {
2891  return generate_new_param(root, resulttype, resulttypmod, resultcollation);
2892 }
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 1140 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().

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

Definition at line 1943 of file subselect.c.

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

Referenced by build_subplan(), and preprocess_expression().

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

Definition at line 1898 of file subselect.c.

References replace_correlation_vars_mutator().

Referenced by preprocess_expression().

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