PostgreSQL Source Code  git master
subselect.h File Reference
#include "nodes/pathnodes.h"
#include "nodes/plannodes.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)
 

Function Documentation

◆ convert_ANY_sublink_to_join()

JoinExpr* convert_ANY_sublink_to_join ( PlannerInfo root,
SubLink sublink,
Relids  available_rels 
)

Definition at line 1212 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, parse(), PlannerInfo::parse, pull_varnos(), JoinExpr::quals, JoinExpr::rarg, Query::rtable, JoinExpr::rtindex, SubLink::subLinkType, SubLink::subselect, SubLink::testexpr, and JoinExpr::usingClause.

Referenced by pull_up_sublinks_qual_recurse().

1214 {
1215  JoinExpr *result;
1216  Query *parse = root->parse;
1217  Query *subselect = (Query *) sublink->subselect;
1218  Relids upper_varnos;
1219  int rtindex;
1220  RangeTblEntry *rte;
1221  RangeTblRef *rtr;
1222  List *subquery_vars;
1223  Node *quals;
1224  ParseState *pstate;
1225 
1226  Assert(sublink->subLinkType == ANY_SUBLINK);
1227 
1228  /*
1229  * The sub-select must not refer to any Vars of the parent query. (Vars of
1230  * higher levels should be okay, though.)
1231  */
1232  if (contain_vars_of_level((Node *) subselect, 1))
1233  return NULL;
1234 
1235  /*
1236  * The test expression must contain some Vars of the parent query, else
1237  * it's not gonna be a join. (Note that it won't have Vars referring to
1238  * the subquery, rather Params.)
1239  */
1240  upper_varnos = pull_varnos(sublink->testexpr);
1241  if (bms_is_empty(upper_varnos))
1242  return NULL;
1243 
1244  /*
1245  * However, it can't refer to anything outside available_rels.
1246  */
1247  if (!bms_is_subset(upper_varnos, available_rels))
1248  return NULL;
1249 
1250  /*
1251  * The combining operators and left-hand expressions mustn't be volatile.
1252  */
1253  if (contain_volatile_functions(sublink->testexpr))
1254  return NULL;
1255 
1256  /* Create a dummy ParseState for addRangeTableEntryForSubquery */
1257  pstate = make_parsestate(NULL);
1258 
1259  /*
1260  * Okay, pull up the sub-select into upper range table.
1261  *
1262  * We rely here on the assumption that the outer query has no references
1263  * to the inner (necessarily true, other than the Vars that we build
1264  * below). Therefore this is a lot easier than what pull_up_subqueries has
1265  * to go through.
1266  */
1267  rte = addRangeTableEntryForSubquery(pstate,
1268  subselect,
1269  makeAlias("ANY_subquery", NIL),
1270  false,
1271  false);
1272  parse->rtable = lappend(parse->rtable, rte);
1273  rtindex = list_length(parse->rtable);
1274 
1275  /*
1276  * Form a RangeTblRef for the pulled-up sub-select.
1277  */
1278  rtr = makeNode(RangeTblRef);
1279  rtr->rtindex = rtindex;
1280 
1281  /*
1282  * Build a list of Vars representing the subselect outputs.
1283  */
1284  subquery_vars = generate_subquery_vars(root,
1285  subselect->targetList,
1286  rtindex);
1287 
1288  /*
1289  * Build the new join's qual expression, replacing Params with these Vars.
1290  */
1291  quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
1292 
1293  /*
1294  * And finally, build the JoinExpr node.
1295  */
1296  result = makeNode(JoinExpr);
1297  result->jointype = JOIN_SEMI;
1298  result->isNatural = false;
1299  result->larg = NULL; /* caller must fill this in */
1300  result->rarg = (Node *) rtr;
1301  result->usingClause = NIL;
1302  result->quals = quals;
1303  result->alias = NULL;
1304  result->rtindex = 0; /* we don't need an RTE for it */
1305 
1306  return result;
1307 }
static Node * convert_testexpr(PlannerInfo *root, Node *testexpr, List *subst_nodes)
Definition: subselect.c:649
#define NIL
Definition: pg_list.h:65
Query * parse
Definition: pathnodes.h:177
Definition: nodes.h:525
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
Node * larg
Definition: primnodes.h:1476
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
List * rtable
Definition: parsenodes.h:137
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
static List * generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
Definition: subselect.c:620
Relids pull_varnos(Node *node)
Definition: var.c:95
List * lappend(List *list, void *datum)
Definition: list.c:322
RangeTblEntry * addRangeTableEntryForSubquery(ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
bool isNatural
Definition: primnodes.h:1475
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
List * usingClause
Definition: primnodes.h:1478
Node * quals
Definition: primnodes.h:1479
#define makeNode(_type_)
Definition: nodes.h:573
Node * rarg
Definition: primnodes.h:1477
Alias * alias
Definition: primnodes.h:1480
JoinType jointype
Definition: primnodes.h:1474
#define Assert(condition)
Definition: c.h:732
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
static int list_length(const List *l)
Definition: pg_list.h:169
int rtindex
Definition: primnodes.h:1481
Definition: pg_list.h:50
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:648

◆ convert_EXISTS_sublink_to_join()

JoinExpr* convert_EXISTS_sublink_to_join ( PlannerInfo root,
SubLink sublink,
bool  under_not,
Relids  available_rels 
)

Definition at line 1317 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, IncrementVarSublevelsUp(), JoinExpr::isNatural, JOIN_ANTI, JOIN_SEMI, Query::jointree, JoinExpr::jointype, JoinExpr::larg, linitial, list_concat(), list_length(), makeNode, NIL, OffsetVarNodes(), parse(), PlannerInfo::parse, pull_varnos(), JoinExpr::quals, FromExpr::quals, JoinExpr::rarg, replace_empty_jointree(), Query::rtable, JoinExpr::rtindex, simplify_EXISTS_query(), SubLink::subLinkType, SubLink::subselect, and JoinExpr::usingClause.

Referenced by pull_up_sublinks_qual_recurse().

1319 {
1320  JoinExpr *result;
1321  Query *parse = root->parse;
1322  Query *subselect = (Query *) sublink->subselect;
1323  Node *whereClause;
1324  int rtoffset;
1325  int varno;
1326  Relids clause_varnos;
1327  Relids upper_varnos;
1328 
1329  Assert(sublink->subLinkType == EXISTS_SUBLINK);
1330 
1331  /*
1332  * Can't flatten if it contains WITH. (We could arrange to pull up the
1333  * WITH into the parent query's cteList, but that risks changing the
1334  * semantics, since a WITH ought to be executed once per associated query
1335  * call.) Note that convert_ANY_sublink_to_join doesn't have to reject
1336  * this case, since it just produces a subquery RTE that doesn't have to
1337  * get flattened into the parent query.
1338  */
1339  if (subselect->cteList)
1340  return NULL;
1341 
1342  /*
1343  * Copy the subquery so we can modify it safely (see comments in
1344  * make_subplan).
1345  */
1346  subselect = copyObject(subselect);
1347 
1348  /*
1349  * See if the subquery can be simplified based on the knowledge that it's
1350  * being used in EXISTS(). If we aren't able to get rid of its
1351  * targetlist, we have to fail, because the pullup operation leaves us
1352  * with noplace to evaluate the targetlist.
1353  */
1354  if (!simplify_EXISTS_query(root, subselect))
1355  return NULL;
1356 
1357  /*
1358  * Separate out the WHERE clause. (We could theoretically also remove
1359  * top-level plain JOIN/ON clauses, but it's probably not worth the
1360  * trouble.)
1361  */
1362  whereClause = subselect->jointree->quals;
1363  subselect->jointree->quals = NULL;
1364 
1365  /*
1366  * The rest of the sub-select must not refer to any Vars of the parent
1367  * query. (Vars of higher levels should be okay, though.)
1368  */
1369  if (contain_vars_of_level((Node *) subselect, 1))
1370  return NULL;
1371 
1372  /*
1373  * On the other hand, the WHERE clause must contain some Vars of the
1374  * parent query, else it's not gonna be a join.
1375  */
1376  if (!contain_vars_of_level(whereClause, 1))
1377  return NULL;
1378 
1379  /*
1380  * We don't risk optimizing if the WHERE clause is volatile, either.
1381  */
1382  if (contain_volatile_functions(whereClause))
1383  return NULL;
1384 
1385  /*
1386  * The subquery must have a nonempty jointree, but we can make it so.
1387  */
1388  replace_empty_jointree(subselect);
1389 
1390  /*
1391  * Prepare to pull up the sub-select into top range table.
1392  *
1393  * We rely here on the assumption that the outer query has no references
1394  * to the inner (necessarily true). Therefore this is a lot easier than
1395  * what pull_up_subqueries has to go through.
1396  *
1397  * In fact, it's even easier than what convert_ANY_sublink_to_join has to
1398  * do. The machinations of simplify_EXISTS_query ensured that there is
1399  * nothing interesting in the subquery except an rtable and jointree, and
1400  * even the jointree FromExpr no longer has quals. So we can just append
1401  * the rtable to our own and use the FromExpr in our jointree. But first,
1402  * adjust all level-zero varnos in the subquery to account for the rtable
1403  * merger.
1404  */
1405  rtoffset = list_length(parse->rtable);
1406  OffsetVarNodes((Node *) subselect, rtoffset, 0);
1407  OffsetVarNodes(whereClause, rtoffset, 0);
1408 
1409  /*
1410  * Upper-level vars in subquery will now be one level closer to their
1411  * parent than before; in particular, anything that had been level 1
1412  * becomes level zero.
1413  */
1414  IncrementVarSublevelsUp((Node *) subselect, -1, 1);
1415  IncrementVarSublevelsUp(whereClause, -1, 1);
1416 
1417  /*
1418  * Now that the WHERE clause is adjusted to match the parent query
1419  * environment, we can easily identify all the level-zero rels it uses.
1420  * The ones <= rtoffset belong to the upper query; the ones > rtoffset do
1421  * not.
1422  */
1423  clause_varnos = pull_varnos(whereClause);
1424  upper_varnos = NULL;
1425  while ((varno = bms_first_member(clause_varnos)) >= 0)
1426  {
1427  if (varno <= rtoffset)
1428  upper_varnos = bms_add_member(upper_varnos, varno);
1429  }
1430  bms_free(clause_varnos);
1431  Assert(!bms_is_empty(upper_varnos));
1432 
1433  /*
1434  * Now that we've got the set of upper-level varnos, we can make the last
1435  * check: only available_rels can be referenced.
1436  */
1437  if (!bms_is_subset(upper_varnos, available_rels))
1438  return NULL;
1439 
1440  /* Now we can attach the modified subquery rtable to the parent */
1441  parse->rtable = list_concat(parse->rtable, subselect->rtable);
1442 
1443  /*
1444  * And finally, build the JoinExpr node.
1445  */
1446  result = makeNode(JoinExpr);
1447  result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
1448  result->isNatural = false;
1449  result->larg = NULL; /* caller must fill this in */
1450  /* flatten out the FromExpr node if it's useless */
1451  if (list_length(subselect->jointree->fromlist) == 1)
1452  result->rarg = (Node *) linitial(subselect->jointree->fromlist);
1453  else
1454  result->rarg = (Node *) subselect->jointree;
1455  result->usingClause = NIL;
1456  result->quals = whereClause;
1457  result->alias = NULL;
1458  result->rtindex = 0; /* we don't need an RTE for it */
1459 
1460  return result;
1461 }
#define NIL
Definition: pg_list.h:65
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:996
Query * parse
Definition: pathnodes.h:177
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:138
Definition: nodes.h:525
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
static bool simplify_EXISTS_query(PlannerInfo *root, Query *query)
Definition: subselect.c:1480
void replace_empty_jointree(Query *parse)
Definition: prepjointree.c:146
Node * quals
Definition: primnodes.h:1497
Node * larg
Definition: primnodes.h:1476
#define linitial(l)
Definition: pg_list.h:195
List * rtable
Definition: parsenodes.h:137
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids pull_varnos(Node *node)
Definition: var.c:95
bool isNatural
Definition: primnodes.h:1475
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
List * usingClause
Definition: primnodes.h:1478
Node * quals
Definition: primnodes.h:1479
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define makeNode(_type_)
Definition: nodes.h:573
Node * rarg
Definition: primnodes.h:1477
Alias * alias
Definition: primnodes.h:1480
JoinType jointype
Definition: primnodes.h:1474
#define Assert(condition)
Definition: c.h:732
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
static int list_length(const List *l)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
List * cteList
Definition: parsenodes.h:135
#define copyObject(obj)
Definition: nodes.h:641
int rtindex
Definition: primnodes.h:1481
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:648

◆ SS_attach_initplans()

void SS_attach_initplans ( PlannerInfo root,
Plan plan 
)

Definition at line 2118 of file subselect.c.

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

Referenced by create_plan().

2119 {
2120  plan->initPlan = root->init_plans;
2121 }
List * init_plans
Definition: pathnodes.h:257
List * initPlan
Definition: plannodes.h:144

◆ SS_charge_for_initplans()

void SS_charge_for_initplans ( PlannerInfo root,
RelOptInfo final_rel 
)

Definition at line 2061 of file subselect.c.

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

Referenced by build_minmax_path(), and subquery_planner().

2062 {
2063  Cost initplan_cost;
2064  ListCell *lc;
2065 
2066  /* Nothing to do if no initPlans */
2067  if (root->init_plans == NIL)
2068  return;
2069 
2070  /*
2071  * Compute the cost increment just once, since it will be the same for all
2072  * Paths. We assume each initPlan gets run once during top plan startup.
2073  * This is a conservative overestimate, since in fact an initPlan might be
2074  * executed later than plan startup, or even not at all.
2075  */
2076  initplan_cost = 0;
2077  foreach(lc, root->init_plans)
2078  {
2079  SubPlan *initsubplan = (SubPlan *) lfirst(lc);
2080 
2081  initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
2082  }
2083 
2084  /*
2085  * Now adjust the costs and parallel_safe flags.
2086  */
2087  foreach(lc, final_rel->pathlist)
2088  {
2089  Path *path = (Path *) lfirst(lc);
2090 
2091  path->startup_cost += initplan_cost;
2092  path->total_cost += initplan_cost;
2093  path->parallel_safe = false;
2094  }
2095 
2096  /*
2097  * Forget about any partial paths and clear consider_parallel, too;
2098  * they're not usable if we attached an initPlan.
2099  */
2100  final_rel->partial_pathlist = NIL;
2101  final_rel->consider_parallel = false;
2102 
2103  /* We needn't do set_cheapest() here, caller will do it */
2104 }
#define NIL
Definition: pg_list.h:65
List * partial_pathlist
Definition: pathnodes.h:657
Cost startup_cost
Definition: pathnodes.h:1125
List * init_plans
Definition: pathnodes.h:257
Cost per_call_cost
Definition: primnodes.h:719
Cost total_cost
Definition: pathnodes.h:1126
#define lfirst(lc)
Definition: pg_list.h:190
bool parallel_safe
Definition: pathnodes.h:1120
bool consider_parallel
Definition: pathnodes.h:649
Cost startup_cost
Definition: primnodes.h:718
List * pathlist
Definition: pathnodes.h:655
double Cost
Definition: nodes.h:659

◆ SS_finalize_plan()

void SS_finalize_plan ( PlannerInfo root,
Plan plan 
)

Definition at line 2133 of file subselect.c.

References finalize_plan(), and PlannerInfo::outer_params.

Referenced by standard_planner().

2134 {
2135  /* No setup needed, just recurse through plan tree. */
2136  (void) finalize_plan(root, plan, -1, root->outer_params, NULL);
2137 }
static Bitmapset * finalize_plan(PlannerInfo *root, Plan *plan, int gather_param, Bitmapset *valid_params, Bitmapset *scan_params)
Definition: subselect.c:2171
Bitmapset * outer_params
Definition: pathnodes.h:192

◆ SS_identify_outer_params()

void SS_identify_outer_params ( PlannerInfo root)

Definition at line 1999 of file subselect.c.

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

Referenced by build_minmax_path(), and subquery_planner().

2000 {
2001  Bitmapset *outer_params;
2002  PlannerInfo *proot;
2003  ListCell *l;
2004 
2005  /*
2006  * If no parameters have been assigned anywhere in the tree, we certainly
2007  * don't need to do anything here.
2008  */
2009  if (root->glob->paramExecTypes == NIL)
2010  return;
2011 
2012  /*
2013  * Scan all query levels above this one to see which parameters are due to
2014  * be available from them, either because lower query levels have
2015  * requested them (via plan_params) or because they will be available from
2016  * initPlans of those levels.
2017  */
2018  outer_params = NULL;
2019  for (proot = root->parent_root; proot != NULL; proot = proot->parent_root)
2020  {
2021  /* Include ordinary Var/PHV/Aggref params */
2022  foreach(l, proot->plan_params)
2023  {
2024  PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
2025 
2026  outer_params = bms_add_member(outer_params, pitem->paramId);
2027  }
2028  /* Include any outputs of outer-level initPlans */
2029  foreach(l, proot->init_plans)
2030  {
2031  SubPlan *initsubplan = (SubPlan *) lfirst(l);
2032  ListCell *l2;
2033 
2034  foreach(l2, initsubplan->setParam)
2035  {
2036  outer_params = bms_add_member(outer_params, lfirst_int(l2));
2037  }
2038  }
2039  /* Include worktable ID, if a recursive query is being planned */
2040  if (proot->wt_param_id >= 0)
2041  outer_params = bms_add_member(outer_params, proot->wt_param_id);
2042  }
2043  root->outer_params = outer_params;
2044 }
#define NIL
Definition: pg_list.h:65
List * plan_params
Definition: pathnodes.h:191
PlannerInfo * parent_root
Definition: pathnodes.h:183
int wt_param_id
Definition: pathnodes.h:351
List * paramExecTypes
Definition: pathnodes.h:129
#define lfirst_int(lc)
Definition: pg_list.h:191
PlannerGlobal * glob
Definition: pathnodes.h:179
List * init_plans
Definition: pathnodes.h:257
#define lfirst(lc)
Definition: pg_list.h:190
List * setParam
Definition: primnodes.h:713
Bitmapset * outer_params
Definition: pathnodes.h:192
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736

◆ SS_make_initplan_from_plan()

void SS_make_initplan_from_plan ( PlannerInfo root,
PlannerInfo subroot,
Plan plan,
Param prm 
)

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

2892 {
2893  SubPlan *node;
2894 
2895  /*
2896  * Add the subplan and its PlannerInfo to the global lists.
2897  */
2898  root->glob->subplans = lappend(root->glob->subplans, plan);
2899  root->glob->subroots = lappend(root->glob->subroots, subroot);
2900 
2901  /*
2902  * Create a SubPlan node and add it to the outer list of InitPlans. Note
2903  * it has to appear after any other InitPlans it might depend on (see
2904  * comments in ExecReScan).
2905  */
2906  node = makeNode(SubPlan);
2907  node->subLinkType = EXPR_SUBLINK;
2908  node->plan_id = list_length(root->glob->subplans);
2909  node->plan_name = psprintf("InitPlan %d (returns $%d)",
2910  node->plan_id, prm->paramid);
2911  get_first_col_type(plan, &node->firstColType, &node->firstColTypmod,
2912  &node->firstColCollation);
2913  node->setParam = list_make1_int(prm->paramid);
2914 
2915  root->init_plans = lappend(root->init_plans, node);
2916 
2917  /*
2918  * The node can't have any inputs (since it's an initplan), so the
2919  * parParam and args lists remain empty.
2920  */
2921 
2922  /* Set costs of SubPlan using info from the plan tree */
2923  cost_subplan(subroot, node, plan);
2924 }
Oid firstColType
Definition: primnodes.h:699
int plan_id
Definition: primnodes.h:695
static void get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod, Oid *colcollation)
Definition: subselect.c:117
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
SubLinkType subLinkType
Definition: primnodes.h:690
List * subplans
Definition: pathnodes.h:111
PlannerGlobal * glob
Definition: pathnodes.h:179
#define list_make1_int(x1)
Definition: pg_list.h:238
List * lappend(List *list, void *datum)
Definition: list.c:322
List * init_plans
Definition: pathnodes.h:257
int32 firstColTypmod
Definition: primnodes.h:700
#define makeNode(_type_)
Definition: nodes.h:573
char * plan_name
Definition: primnodes.h:697
List * setParam
Definition: primnodes.h:713
int paramid
Definition: primnodes.h:249
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:3619
static int list_length(const List *l)
Definition: pg_list.h:169
List * subroots
Definition: pathnodes.h:113
Oid firstColCollation
Definition: primnodes.h:701

◆ SS_make_initplan_output_param()

Param* SS_make_initplan_output_param ( PlannerInfo root,
Oid  resulttype,
int32  resulttypmod,
Oid  resultcollation 
)

Definition at line 2873 of file subselect.c.

References generate_new_exec_param().

Referenced by preprocess_minmax_aggregates().

2876 {
2877  return generate_new_exec_param(root, resulttype,
2878  resulttypmod, resultcollation);
2879 }
Param * generate_new_exec_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod, Oid paramcollation)
Definition: paramassign.c:558

◆ SS_process_ctes()

void SS_process_ctes ( PlannerInfo root)

Definition at line 830 of file subselect.c.

References SubPlan::args, Assert, assign_special_exec_param(), RelOptInfo::cheapest_total_path, CMD_SELECT, contain_dml(), contain_outer_selfref(), contain_volatile_functions(), copyObject, cost_subplan(), create_plan(), PlannerInfo::cte_plan_ids, CTE_SUBLINK, Query::cteList, CommonTableExpr::ctematerialized, CTEMaterializeDefault, CTEMaterializeNever, 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, inline_cte(), lappend(), lappend_int(), lfirst, list_length(), list_make1_int, makeNode, NIL, SubPlan::parallel_safe, SubPlan::paramIds, SubPlan::parParam, PlannerInfo::parse, SubPlan::plan_id, SubPlan::plan_name, PlannerInfo::plan_params, psprintf(), SubPlan::setParam, splan, SubPlan::subLinkType, PlannerGlobal::subplans, subquery_planner(), PlannerGlobal::subroots, SubPlan::testexpr, SubPlan::unknownEqFalse, UPPERREL_FINAL, and SubPlan::useHashTable.

Referenced by subquery_planner().

831 {
832  ListCell *lc;
833 
834  Assert(root->cte_plan_ids == NIL);
835 
836  foreach(lc, root->parse->cteList)
837  {
838  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
839  CmdType cmdType = ((Query *) cte->ctequery)->commandType;
840  Query *subquery;
841  PlannerInfo *subroot;
842  RelOptInfo *final_rel;
843  Path *best_path;
844  Plan *plan;
845  SubPlan *splan;
846  int paramid;
847 
848  /*
849  * Ignore SELECT CTEs that are not actually referenced anywhere.
850  */
851  if (cte->cterefcount == 0 && cmdType == CMD_SELECT)
852  {
853  /* Make a dummy entry in cte_plan_ids */
854  root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
855  continue;
856  }
857 
858  /*
859  * Consider inlining the CTE (creating RTE_SUBQUERY RTE(s)) instead of
860  * implementing it as a separately-planned CTE.
861  *
862  * We cannot inline if any of these conditions hold:
863  *
864  * 1. The user said not to (the CTEMaterializeAlways option).
865  *
866  * 2. The CTE is recursive.
867  *
868  * 3. The CTE has side-effects; this includes either not being a plain
869  * SELECT, or containing volatile functions. Inlining might change
870  * the side-effects, which would be bad.
871  *
872  * 4. The CTE is multiply-referenced and contains a self-reference to
873  * a recursive CTE outside itself. Inlining would result in multiple
874  * recursive self-references, which we don't support.
875  *
876  * Otherwise, we have an option whether to inline or not. That should
877  * always be a win if there's just a single reference, but if the CTE
878  * is multiply-referenced then it's unclear: inlining adds duplicate
879  * computations, but the ability to absorb restrictions from the outer
880  * query level could outweigh that. We do not have nearly enough
881  * information at this point to tell whether that's true, so we let
882  * the user express a preference. Our default behavior is to inline
883  * only singly-referenced CTEs, but a CTE marked CTEMaterializeNever
884  * will be inlined even if multiply referenced.
885  *
886  * Note: we check for volatile functions last, because that's more
887  * expensive than the other tests needed.
888  */
889  if ((cte->ctematerialized == CTEMaterializeNever ||
891  cte->cterefcount == 1)) &&
892  !cte->cterecursive &&
893  cmdType == CMD_SELECT &&
894  !contain_dml(cte->ctequery) &&
895  (cte->cterefcount <= 1 ||
898  {
899  inline_cte(root, cte);
900  /* Make a dummy entry in cte_plan_ids */
901  root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
902  continue;
903  }
904 
905  /*
906  * Copy the source Query node. Probably not necessary, but let's keep
907  * this similar to make_subplan.
908  */
909  subquery = (Query *) copyObject(cte->ctequery);
910 
911  /* plan_params should not be in use in current query level */
912  Assert(root->plan_params == NIL);
913 
914  /*
915  * Generate Paths for the CTE query. Always plan for full retrieval
916  * --- we don't have enough info to predict otherwise.
917  */
918  subroot = subquery_planner(root->glob, subquery,
919  root,
920  cte->cterecursive, 0.0);
921 
922  /*
923  * Since the current query level doesn't yet contain any RTEs, it
924  * should not be possible for the CTE to have requested parameters of
925  * this level.
926  */
927  if (root->plan_params)
928  elog(ERROR, "unexpected outer reference in CTE query");
929 
930  /*
931  * Select best Path and turn it into a Plan. At least for now, there
932  * seems no reason to postpone doing that.
933  */
934  final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
935  best_path = final_rel->cheapest_total_path;
936 
937  plan = create_plan(subroot, best_path);
938 
939  /*
940  * Make a SubPlan node for it. This is just enough unlike
941  * build_subplan that we can't share code.
942  *
943  * Note plan_id, plan_name, and cost fields are set further down.
944  */
945  splan = makeNode(SubPlan);
946  splan->subLinkType = CTE_SUBLINK;
947  splan->testexpr = NULL;
948  splan->paramIds = NIL;
949  get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
950  &splan->firstColCollation);
951  splan->useHashTable = false;
952  splan->unknownEqFalse = false;
953 
954  /*
955  * CTE scans are not considered for parallelism (cf
956  * set_rel_consider_parallel), and even if they were, initPlans aren't
957  * parallel-safe.
958  */
959  splan->parallel_safe = false;
960  splan->setParam = NIL;
961  splan->parParam = NIL;
962  splan->args = NIL;
963 
964  /*
965  * The node can't have any inputs (since it's an initplan), so the
966  * parParam and args lists remain empty. (It could contain references
967  * to earlier CTEs' output param IDs, but CTE outputs are not
968  * propagated via the args list.)
969  */
970 
971  /*
972  * Assign a param ID to represent the CTE's output. No ordinary
973  * "evaluation" of this param slot ever happens, but we use the param
974  * ID for setParam/chgParam signaling just as if the CTE plan were
975  * returning a simple scalar output. (Also, the executor abuses the
976  * ParamExecData slot for this param ID for communication among
977  * multiple CteScan nodes that might be scanning this CTE.)
978  */
979  paramid = assign_special_exec_param(root);
980  splan->setParam = list_make1_int(paramid);
981 
982  /*
983  * Add the subplan and its PlannerInfo to the global lists.
984  */
985  root->glob->subplans = lappend(root->glob->subplans, plan);
986  root->glob->subroots = lappend(root->glob->subroots, subroot);
987  splan->plan_id = list_length(root->glob->subplans);
988 
989  root->init_plans = lappend(root->init_plans, splan);
990 
991  root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
992 
993  /* Label the subplan for EXPLAIN purposes */
994  splan->plan_name = psprintf("CTE %s", cte->ctename);
995 
996  /* Lastly, fill in the cost estimates for use later */
997  cost_subplan(root, splan, plan);
998  }
999 }
#define NIL
Definition: pg_list.h:65
Oid firstColType
Definition: primnodes.h:699
Query * parse
Definition: pathnodes.h:177
int plan_id
Definition: primnodes.h:695
List * plan_params
Definition: pathnodes.h:191
static void get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod, Oid *colcollation)
Definition: subselect.c:117
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
SubLinkType subLinkType
Definition: primnodes.h:690
List * paramIds
Definition: primnodes.h:693
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:585
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:313
#define ERROR
Definition: elog.h:43
static void inline_cte(PlannerInfo *root, CommonTableExpr *cte)
Definition: subselect.c:1089
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1164
struct Path * cheapest_total_path
Definition: pathnodes.h:659
List * subplans
Definition: pathnodes.h:111
PlannerGlobal * glob
Definition: pathnodes.h:179
static SPIPlanPtr splan
Definition: regress.c:259
#define list_make1_int(x1)
Definition: pg_list.h:238
List * lappend_int(List *list, int datum)
Definition: list.c:340
List * lappend(List *list, void *datum)
Definition: list.c:322
List * cte_plan_ids
Definition: pathnodes.h:259
Node * testexpr
Definition: primnodes.h:692
List * init_plans
Definition: pathnodes.h:257
static bool contain_dml(Node *node)
Definition: subselect.c:1007
static bool contain_outer_selfref(Node *node)
Definition: subselect.c:1034
int32 firstColTypmod
Definition: primnodes.h:700
#define makeNode(_type_)
Definition: nodes.h:573
char * plan_name
Definition: primnodes.h:697
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
CTEMaterialize ctematerialized
Definition: parsenodes.h:1429
List * setParam
Definition: primnodes.h:713
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:3619
bool unknownEqFalse
Definition: primnodes.h:706
static int list_length(const List *l)
Definition: pg_list.h:169
List * subroots
Definition: pathnodes.h:113
List * parParam
Definition: primnodes.h:715
bool parallel_safe
Definition: primnodes.h:709
List * cteList
Definition: parsenodes.h:135
#define elog(elevel,...)
Definition: elog.h:226
bool useHashTable
Definition: primnodes.h:704
Oid firstColCollation
Definition: primnodes.h:701
#define copyObject(obj)
Definition: nodes.h:641
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
Definition: planner.c:596
CmdType
Definition: nodes.h:668
List * args
Definition: primnodes.h:716

◆ SS_process_sublinks()

Node* SS_process_sublinks ( PlannerInfo root,
Node expr,
bool  isQual 
)

Definition at line 1852 of file subselect.c.

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

Referenced by build_subplan(), and preprocess_expression().

1853 {
1854  process_sublinks_context context;
1855 
1856  context.root = root;
1857  context.isTopQual = isQual;
1858  return process_sublinks_mutator(expr, &context);
1859 }
static Node * process_sublinks_mutator(Node *node, process_sublinks_context *context)
Definition: subselect.c:1862

◆ SS_replace_correlation_vars()

Node* SS_replace_correlation_vars ( PlannerInfo root,
Node expr 
)

Definition at line 1807 of file subselect.c.

References replace_correlation_vars_mutator().

Referenced by preprocess_expression().

1808 {
1809  /* No setup needed for tree walk, so away we go */
1810  return replace_correlation_vars_mutator(expr, root);
1811 }
static Node * replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
Definition: subselect.c:1814