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

Go to the source code of this file.

Functions

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

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: relation.h:2068
Relids ph_eval_at
Definition: relation.h:2066
PlaceHolderVar * ph_var
Definition: relation.h:2065
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:569
List * lappend(List *list, void *datum)
Definition: list.c:128
List * exprs
Definition: relation.h:884
#define lfirst(lc)
Definition: pg_list.h:106
List * placeholder_list
Definition: relation.h:258
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:243
#define copyObject(obj)
Definition: nodes.h:622
struct PathTarget * reltarget
Definition: relation.h:536
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
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:3489
Relids ph_needed
Definition: relation.h:2068
Relids ph_eval_at
Definition: relation.h:2066
PlaceHolderVar * ph_var
Definition: relation.h:2065
Definition: nodes.h:509
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
Expr * phexpr
Definition: relation.h:1852
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids relids
Definition: relation.h:525
List * lappend(List *list, void *datum)
Definition: list.c:128
List * exprs
Definition: relation.h:884
Relids direct_lateral_relids
Definition: relation.h:549
Relids ph_lateral
Definition: relation.h:2067
#define lfirst(lc)
Definition: pg_list.h:106
QualCost cost
Definition: relation.h:886
int width
Definition: relation.h:887
List * placeholder_list
Definition: relation.h:258
struct PathTarget * reltarget
Definition: relation.h:536
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:755
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
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(), create_nestloop_plan(), find_placeholders_in_expr(), 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: relation.h:2068
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
Relids ph_eval_at
Definition: relation.h:2066
PlaceHolderVar * ph_var
Definition: relation.h:2065
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
Definition: nodes.h:509
Relids phrels
Definition: relation.h:1853
#define ERROR
Definition: elog.h:43
Expr * phexpr
Definition: relation.h:1852
Relids pull_varnos(Node *node)
Definition: var.c:95
List * lappend(List *list, void *datum)
Definition: list.c:128
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
Relids ph_lateral
Definition: relation.h:2067
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2328
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Index phlevelsup
Definition: relation.h:1855
List * placeholder_list
Definition: relation.h:258
static void find_placeholders_in_expr(PlannerInfo *root, Node *expr)
Definition: placeholder.c:213
#define elog
Definition: elog.h:219
#define copyObject(obj)
Definition: nodes.h:622
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:791
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:560
Query * parse
Definition: relation.h:155
FromExpr * jointree
Definition: parsenodes.h:136
Definition: nodes.h:509
PlannerGlobal * glob
Definition: relation.h:157
Index lastPHId
Definition: relation.h:119
static void find_placeholders_recurse(PlannerInfo *root, Node *jtnode)
Definition: placeholder.c:163
#define Assert(condition)
Definition: c.h:664
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: relation.h:2066
PlaceHolderVar * ph_var
Definition: relation.h:2065
#define PVC_RECURSE_AGGREGATES
Definition: var.h:21
Definition: nodes.h:509
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:198
#define PVC_INCLUDE_PLACEHOLDERS
Definition: var.h:24
Expr * phexpr
Definition: relation.h:1852
#define PVC_RECURSE_WINDOWFUNCS
Definition: var.h:23
#define lfirst(lc)
Definition: pg_list.h:106
void list_free(List *list)
Definition: list.c:1133
List * placeholder_list
Definition: relation.h:258
Definition: regcomp.c:224
Definition: pg_list.h:45
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 }
Relids phrels
Definition: relation.h:1853
Expr * phexpr
Definition: relation.h:1852
PlannerGlobal * glob
Definition: relation.h:157
Index lastPHId
Definition: relation.h:119
#define makeNode(_type_)
Definition: nodes.h:557
Index phlevelsup
Definition: relation.h:1855
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: relation.h:2066
PlaceHolderVar * ph_var
Definition: relation.h:2065
List * join_info_list
Definition: relation.h:250
Relids min_righthand
Definition: relation.h:1920
Relids syn_lefthand
Definition: relation.h:1921
Relids syn_righthand
Definition: relation.h:1922
Relids phrels
Definition: relation.h:1853
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
JoinType jointype
Definition: relation.h:1923
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
List * placeholder_list
Definition: relation.h:258
Relids min_lefthand
Definition: relation.h:1919
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:755