PostgreSQL Source Code  git master
placeholder.c File Reference
#include "postgres.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/cost.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/placeholder.h"
#include "optimizer/planmain.h"
#include "utils/lsyscache.h"
Include dependency graph for placeholder.c:

Go to the source code of this file.

Functions

static void find_placeholders_recurse (PlannerInfo *root, Node *jtnode)
 
static void find_placeholders_in_expr (PlannerInfo *root, Node *expr)
 
PlaceHolderVarmake_placeholder_expr (PlannerInfo *root, Expr *expr, Relids phrels)
 
PlaceHolderInfofind_placeholder_info (PlannerInfo *root, PlaceHolderVar *phv, bool create_new_ph)
 
void find_placeholders_in_jointree (PlannerInfo *root)
 
void update_placeholder_eval_levels (PlannerInfo *root, SpecialJoinInfo *new_sjinfo)
 
void fix_placeholder_input_needed_levels (PlannerInfo *root)
 
void add_placeholders_to_base_rels (PlannerInfo *root)
 
void add_placeholders_to_joinrel (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
 

Function Documentation

◆ add_placeholders_to_base_rels()

void add_placeholders_to_base_rels ( PlannerInfo root)

Definition at line 379 of file placeholder.c.

References bms_get_singleton_member(), bms_nonempty_difference(), copyObject, PathTarget::exprs, find_base_rel(), lappend(), lfirst, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_var, PlannerInfo::placeholder_list, and RelOptInfo::reltarget.

Referenced by query_planner().

380 {
381  ListCell *lc;
382 
383  foreach(lc, root->placeholder_list)
384  {
385  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
386  Relids eval_at = phinfo->ph_eval_at;
387  int varno;
388 
389  if (bms_get_singleton_member(eval_at, &varno) &&
390  bms_nonempty_difference(phinfo->ph_needed, eval_at))
391  {
392  RelOptInfo *rel = find_base_rel(root, varno);
393 
394  rel->reltarget->exprs = lappend(rel->reltarget->exprs,
395  copyObject(phinfo->ph_var));
396  /* reltarget's cost and width fields will be updated later */
397  }
398  }
399 }
Relids ph_needed
Definition: pathnodes.h:2270
Relids ph_eval_at
Definition: pathnodes.h:2268
PlaceHolderVar * ph_var
Definition: pathnodes.h:2267
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
List * lappend(List *list, void *datum)
Definition: list.c:322
List * exprs
Definition: pathnodes.h:1046
#define lfirst(lc)
Definition: pg_list.h:190
List * placeholder_list
Definition: pathnodes.h:294
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:363
#define copyObject(obj)
Definition: nodes.h:641
struct PathTarget * reltarget
Definition: pathnodes.h:654
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:545

◆ add_placeholders_to_joinrel()

void add_placeholders_to_joinrel ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outer_rel,
RelOptInfo inner_rel 
)

Definition at line 411 of file placeholder.c.

References bms_add_members(), bms_is_subset(), bms_nonempty_difference(), PathTarget::cost, cost_qual_eval_node(), RelOptInfo::direct_lateral_relids, PathTarget::exprs, lappend(), lfirst, QualCost::per_tuple, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_var, PlaceHolderInfo::ph_width, PlaceHolderVar::phexpr, PlannerInfo::placeholder_list, RelOptInfo::relids, RelOptInfo::reltarget, QualCost::startup, and PathTarget::width.

Referenced by build_join_rel().

413 {
414  Relids relids = joinrel->relids;
415  ListCell *lc;
416 
417  foreach(lc, root->placeholder_list)
418  {
419  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
420 
421  /* Is it still needed above this joinrel? */
422  if (bms_nonempty_difference(phinfo->ph_needed, relids))
423  {
424  /* Is it computable here? */
425  if (bms_is_subset(phinfo->ph_eval_at, relids))
426  {
427  /* Yup, add it to the output */
428  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
429  phinfo->ph_var);
430  joinrel->reltarget->width += phinfo->ph_width;
431 
432  /*
433  * Charge the cost of evaluating the contained expression if
434  * the PHV can be computed here but not in either input. This
435  * is a bit bogus because we make the decision based on the
436  * first pair of possible input relations considered for the
437  * joinrel. With other pairs, it might be possible to compute
438  * the PHV in one input or the other, and then we'd be double
439  * charging the PHV's cost for some join paths. For now, live
440  * with that; but we might want to improve it later by
441  * refiguring the reltarget costs for each pair of inputs.
442  */
443  if (!bms_is_subset(phinfo->ph_eval_at, outer_rel->relids) &&
444  !bms_is_subset(phinfo->ph_eval_at, inner_rel->relids))
445  {
446  QualCost cost;
447 
448  cost_qual_eval_node(&cost, (Node *) phinfo->ph_var->phexpr,
449  root);
450  joinrel->reltarget->cost.startup += cost.startup;
451  joinrel->reltarget->cost.per_tuple += cost.per_tuple;
452  }
453 
454  /* Adjust joinrel's direct_lateral_relids as needed */
455  joinrel->direct_lateral_relids =
457  phinfo->ph_lateral);
458  }
459  }
460  }
461 }
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:3845
Relids ph_needed
Definition: pathnodes.h:2270
Relids ph_eval_at
Definition: pathnodes.h:2268
PlaceHolderVar * ph_var
Definition: pathnodes.h:2267
Definition: nodes.h:525
Cost startup
Definition: pathnodes.h:45
Cost per_tuple
Definition: pathnodes.h:46
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:643
List * lappend(List *list, void *datum)
Definition: list.c:322
List * exprs
Definition: pathnodes.h:1046
Relids direct_lateral_relids
Definition: pathnodes.h:667
Relids ph_lateral
Definition: pathnodes.h:2269
#define lfirst(lc)
Definition: pg_list.h:190
QualCost cost
Definition: pathnodes.h:1048
List * placeholder_list
Definition: pathnodes.h:294
struct PathTarget * reltarget
Definition: pathnodes.h:654
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:545

◆ find_placeholder_info()

PlaceHolderInfo* find_placeholder_info ( PlannerInfo root,
PlaceHolderVar phv,
bool  create_new_ph 
)

Definition at line 69 of file placeholder.c.

References Assert, bms_copy(), bms_difference(), bms_int_members(), bms_is_empty(), copyObject, elog, ERROR, exprType(), exprTypmod(), find_placeholders_in_expr(), get_typavgwidth(), lappend(), lfirst, makeNode, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_var, PlaceHolderInfo::ph_width, PlaceHolderVar::phexpr, PlaceHolderVar::phid, PlaceHolderInfo::phid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, PlannerInfo::placeholder_list, and pull_varnos().

Referenced by add_vars_to_targetlist(), create_lateral_join_info(), find_placeholders_in_expr(), identify_current_nestloop_params(), process_subquery_nestloop_params(), replace_nestloop_params_mutator(), and set_rel_width().

71 {
72  PlaceHolderInfo *phinfo;
73  Relids rels_used;
74  ListCell *lc;
75 
76  /* if this ever isn't true, we'd need to be able to look in parent lists */
77  Assert(phv->phlevelsup == 0);
78 
79  foreach(lc, root->placeholder_list)
80  {
81  phinfo = (PlaceHolderInfo *) lfirst(lc);
82  if (phinfo->phid == phv->phid)
83  return phinfo;
84  }
85 
86  /* Not found, so create it */
87  if (!create_new_ph)
88  elog(ERROR, "too late to create a new PlaceHolderInfo");
89 
90  phinfo = makeNode(PlaceHolderInfo);
91 
92  phinfo->phid = phv->phid;
93  phinfo->ph_var = copyObject(phv);
94 
95  /*
96  * Any referenced rels that are outside the PHV's syntactic scope are
97  * LATERAL references, which should be included in ph_lateral but not in
98  * ph_eval_at. If no referenced rels are within the syntactic scope,
99  * force evaluation at the syntactic location.
100  */
101  rels_used = pull_varnos((Node *) phv->phexpr);
102  phinfo->ph_lateral = bms_difference(rels_used, phv->phrels);
103  if (bms_is_empty(phinfo->ph_lateral))
104  phinfo->ph_lateral = NULL; /* make it exactly NULL if empty */
105  phinfo->ph_eval_at = bms_int_members(rels_used, phv->phrels);
106  /* If no contained vars, force evaluation at syntactic location */
107  if (bms_is_empty(phinfo->ph_eval_at))
108  {
109  phinfo->ph_eval_at = bms_copy(phv->phrels);
110  Assert(!bms_is_empty(phinfo->ph_eval_at));
111  }
112  /* ph_eval_at may change later, see update_placeholder_eval_levels */
113  phinfo->ph_needed = NULL; /* initially it's unused */
114  /* for the moment, estimate width using just the datatype info */
115  phinfo->ph_width = get_typavgwidth(exprType((Node *) phv->phexpr),
116  exprTypmod((Node *) phv->phexpr));
117 
118  root->placeholder_list = lappend(root->placeholder_list, phinfo);
119 
120  /*
121  * The PHV's contained expression may contain other, lower-level PHVs. We
122  * now know we need to get those into the PlaceHolderInfo list, too, so we
123  * may as well do that immediately.
124  */
125  find_placeholders_in_expr(root, (Node *) phinfo->ph_var->phexpr);
126 
127  return phinfo;
128 }
Relids ph_needed
Definition: pathnodes.h:2270
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Relids ph_eval_at
Definition: pathnodes.h:2268
PlaceHolderVar * ph_var
Definition: pathnodes.h:2267
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
Definition: nodes.h:525
#define ERROR
Definition: elog.h:43
Relids pull_varnos(Node *node)
Definition: var.c:95
List * lappend(List *list, void *datum)
Definition: list.c:322
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Relids ph_lateral
Definition: pathnodes.h:2269
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2356
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Index phlevelsup
Definition: pathnodes.h:2067
#define elog(elevel,...)
Definition: elog.h:228
List * placeholder_list
Definition: pathnodes.h:294
static void find_placeholders_in_expr(PlannerInfo *root, Node *expr)
Definition: placeholder.c:213
#define copyObject(obj)
Definition: nodes.h:641
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:902

◆ find_placeholders_in_expr()

static void find_placeholders_in_expr ( PlannerInfo root,
Node expr 
)
static

Definition at line 213 of file placeholder.c.

References find_placeholder_info(), IsA, lfirst, list_free(), pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, and PVC_RECURSE_WINDOWFUNCS.

Referenced by find_placeholder_info(), and find_placeholders_recurse().

214 {
215  List *vars;
216  ListCell *vl;
217 
218  /*
219  * pull_var_clause does more than we need here, but it'll do and it's
220  * convenient to use.
221  */
222  vars = pull_var_clause(expr,
226  foreach(vl, vars)
227  {
228  PlaceHolderVar *phv = (PlaceHolderVar *) lfirst(vl);
229 
230  /* Ignore any plain Vars */
231  if (!IsA(phv, PlaceHolderVar))
232  continue;
233 
234  /* Create a PlaceHolderInfo entry if there's not one already */
235  (void) find_placeholder_info(root, phv, true);
236  }
237  list_free(vars);
238 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv, bool create_new_ph)
Definition: placeholder.c:69
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:174
#define lfirst(lc)
Definition: pg_list.h:190
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:173
void list_free(List *list)
Definition: list.c:1377
Definition: regcomp.c:224
Definition: pg_list.h:50
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:171

◆ find_placeholders_in_jointree()

void find_placeholders_in_jointree ( PlannerInfo root)

Definition at line 144 of file placeholder.c.

References Assert, find_placeholders_recurse(), PlannerInfo::glob, IsA, Query::jointree, PlannerGlobal::lastPHId, and PlannerInfo::parse.

Referenced by query_planner().

145 {
146  /* We need do nothing if the query contains no PlaceHolderVars */
147  if (root->glob->lastPHId != 0)
148  {
149  /* Start recursion at top of jointree */
150  Assert(root->parse->jointree != NULL &&
151  IsA(root->parse->jointree, FromExpr));
152  find_placeholders_recurse(root, (Node *) root->parse->jointree);
153  }
154 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Query * parse
Definition: pathnodes.h:179
FromExpr * jointree
Definition: parsenodes.h:138
Definition: nodes.h:525
PlannerGlobal * glob
Definition: pathnodes.h:181
Index lastPHId
Definition: pathnodes.h:133
static void find_placeholders_recurse(PlannerInfo *root, Node *jtnode)
Definition: placeholder.c:163
#define Assert(condition)
Definition: c.h:739

◆ find_placeholders_recurse()

static void find_placeholders_recurse ( PlannerInfo root,
Node jtnode 
)
static

Definition at line 163 of file placeholder.c.

References elog, ERROR, find_placeholders_in_expr(), FromExpr::fromlist, IsA, JoinExpr::larg, lfirst, nodeTag, JoinExpr::quals, FromExpr::quals, and JoinExpr::rarg.

Referenced by find_placeholders_in_jointree().

164 {
165  if (jtnode == NULL)
166  return;
167  if (IsA(jtnode, RangeTblRef))
168  {
169  /* No quals to deal with here */
170  }
171  else if (IsA(jtnode, FromExpr))
172  {
173  FromExpr *f = (FromExpr *) jtnode;
174  ListCell *l;
175 
176  /*
177  * First, recurse to handle child joins.
178  */
179  foreach(l, f->fromlist)
180  {
182  }
183 
184  /*
185  * Now process the top-level quals.
186  */
188  }
189  else if (IsA(jtnode, JoinExpr))
190  {
191  JoinExpr *j = (JoinExpr *) jtnode;
192 
193  /*
194  * First, recurse to handle child joins.
195  */
198 
199  /* Process the qual clauses */
201  }
202  else
203  elog(ERROR, "unrecognized node type: %d",
204  (int) nodeTag(jtnode));
205 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
List * fromlist
Definition: primnodes.h:1496
Node * quals
Definition: primnodes.h:1497
Node * larg
Definition: primnodes.h:1476
#define ERROR
Definition: elog.h:43
Node * quals
Definition: primnodes.h:1479
static void find_placeholders_recurse(PlannerInfo *root, Node *jtnode)
Definition: placeholder.c:163
Node * rarg
Definition: primnodes.h:1477
#define lfirst(lc)
Definition: pg_list.h:190
#define nodeTag(nodeptr)
Definition: nodes.h:530
#define elog(elevel,...)
Definition: elog.h:228
static void find_placeholders_in_expr(PlannerInfo *root, Node *expr)
Definition: placeholder.c:213

◆ fix_placeholder_input_needed_levels()

void fix_placeholder_input_needed_levels ( PlannerInfo root)

Definition at line 350 of file placeholder.c.

References add_vars_to_targetlist(), lfirst, list_free(), PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_var, PlaceHolderVar::phexpr, PlannerInfo::placeholder_list, pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, and PVC_RECURSE_WINDOWFUNCS.

Referenced by query_planner().

351 {
352  ListCell *lc;
353 
354  foreach(lc, root->placeholder_list)
355  {
356  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
357  List *vars = pull_var_clause((Node *) phinfo->ph_var->phexpr,
361 
362  add_vars_to_targetlist(root, vars, phinfo->ph_eval_at, false);
363  list_free(vars);
364  }
365 }
Relids ph_eval_at
Definition: pathnodes.h:2268
PlaceHolderVar * ph_var
Definition: pathnodes.h:2267
Definition: nodes.h:525
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:229
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:174
#define lfirst(lc)
Definition: pg_list.h:190
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:173
void list_free(List *list)
Definition: list.c:1377
List * placeholder_list
Definition: pathnodes.h:294
Definition: regcomp.c:224
Definition: pg_list.h:50
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:171

◆ make_placeholder_expr()

PlaceHolderVar* make_placeholder_expr ( PlannerInfo root,
Expr expr,
Relids  phrels 
)

Definition at line 39 of file placeholder.c.

References PlannerInfo::glob, PlannerGlobal::lastPHId, makeNode, PlaceHolderVar::phexpr, PlaceHolderVar::phid, PlaceHolderVar::phlevelsup, and PlaceHolderVar::phrels.

Referenced by pullup_replace_vars_callback().

40 {
42 
43  phv->phexpr = expr;
44  phv->phrels = phrels;
45  phv->phid = ++(root->glob->lastPHId);
46  phv->phlevelsup = 0;
47 
48  return phv;
49 }
PlannerGlobal * glob
Definition: pathnodes.h:181
Index lastPHId
Definition: pathnodes.h:133
#define makeNode(_type_)
Definition: nodes.h:573
Index phlevelsup
Definition: pathnodes.h:2067

◆ update_placeholder_eval_levels()

void update_placeholder_eval_levels ( PlannerInfo root,
SpecialJoinInfo new_sjinfo 
)

Definition at line 265 of file placeholder.c.

References Assert, bms_add_members(), bms_is_subset(), bms_overlap(), JOIN_FULL, PlannerInfo::join_info_list, SpecialJoinInfo::jointype, lfirst, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_var, PlaceHolderVar::phrels, PlannerInfo::placeholder_list, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by deconstruct_recurse().

266 {
267  ListCell *lc1;
268 
269  foreach(lc1, root->placeholder_list)
270  {
271  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc1);
272  Relids syn_level = phinfo->ph_var->phrels;
273  Relids eval_at;
274  bool found_some;
275  ListCell *lc2;
276 
277  /*
278  * We don't need to do any work on this placeholder unless the
279  * newly-added outer join is syntactically beneath its location.
280  */
281  if (!bms_is_subset(new_sjinfo->syn_lefthand, syn_level) ||
282  !bms_is_subset(new_sjinfo->syn_righthand, syn_level))
283  continue;
284 
285  /*
286  * Check for delays due to lower outer joins. This is the same logic
287  * as in check_outerjoin_delay in initsplan.c, except that we don't
288  * have anything to do with the delay_upper_joins flags; delay of
289  * upper outer joins will be handled later, based on the eval_at
290  * values we compute now.
291  */
292  eval_at = phinfo->ph_eval_at;
293 
294  do
295  {
296  found_some = false;
297  foreach(lc2, root->join_info_list)
298  {
299  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc2);
300 
301  /* disregard joins not within the PHV's sub-select */
302  if (!bms_is_subset(sjinfo->syn_lefthand, syn_level) ||
303  !bms_is_subset(sjinfo->syn_righthand, syn_level))
304  continue;
305 
306  /* do we reference any nullable rels of this OJ? */
307  if (bms_overlap(eval_at, sjinfo->min_righthand) ||
308  (sjinfo->jointype == JOIN_FULL &&
309  bms_overlap(eval_at, sjinfo->min_lefthand)))
310  {
311  /* yes; have we included all its rels in eval_at? */
312  if (!bms_is_subset(sjinfo->min_lefthand, eval_at) ||
313  !bms_is_subset(sjinfo->min_righthand, eval_at))
314  {
315  /* no, so add them in */
316  eval_at = bms_add_members(eval_at,
317  sjinfo->min_lefthand);
318  eval_at = bms_add_members(eval_at,
319  sjinfo->min_righthand);
320  /* we'll need another iteration */
321  found_some = true;
322  }
323  }
324  }
325  } while (found_some);
326 
327  /* Can't move the PHV's eval_at level to above its syntactic level */
328  Assert(bms_is_subset(eval_at, syn_level));
329 
330  phinfo->ph_eval_at = eval_at;
331  }
332 }
Relids ph_eval_at
Definition: pathnodes.h:2268
PlaceHolderVar * ph_var
Definition: pathnodes.h:2267
List * join_info_list
Definition: pathnodes.h:283
Relids min_righthand
Definition: pathnodes.h:2136
Relids syn_lefthand
Definition: pathnodes.h:2137
Relids syn_righthand
Definition: pathnodes.h:2138
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
JoinType jointype
Definition: pathnodes.h:2139
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
List * placeholder_list
Definition: pathnodes.h:294
Relids min_lefthand
Definition: pathnodes.h:2135
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793