PostgreSQL Source Code  git master
placeholder.h File Reference
#include "nodes/pathnodes.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)
 
void find_placeholders_in_jointree (PlannerInfo *root)
 
void fix_placeholder_input_needed_levels (PlannerInfo *root)
 
void rebuild_placeholder_attr_needed (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, SpecialJoinInfo *sjinfo)
 
bool contain_placeholder_references_to (PlannerInfo *root, Node *clause, int relid)
 

Function Documentation

◆ add_placeholders_to_base_rels()

void add_placeholders_to_base_rels ( PlannerInfo root)

Definition at line 356 of file placeholder.c.

357 {
358  ListCell *lc;
359 
360  foreach(lc, root->placeholder_list)
361  {
362  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
363  Relids eval_at = phinfo->ph_eval_at;
364  int varno;
365 
366  if (bms_get_singleton_member(eval_at, &varno) &&
367  bms_nonempty_difference(phinfo->ph_needed, eval_at))
368  {
369  RelOptInfo *rel = find_base_rel(root, varno);
370 
371  /*
372  * As in add_vars_to_targetlist(), a value computed at scan level
373  * has not yet been nulled by any outer join, so its phnullingrels
374  * should be empty.
375  */
376  Assert(phinfo->ph_var->phnullingrels == NULL);
377 
378  /* Copying the PHV might be unnecessary here, but be safe */
379  rel->reltarget->exprs = lappend(rel->reltarget->exprs,
380  copyObject(phinfo->ph_var));
381  /* reltarget's cost and width fields will be updated later */
382  }
383  }
384 }
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:715
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:641
#define Assert(condition)
Definition: c.h:812
List * lappend(List *list, void *datum)
Definition: list.c:339
#define copyObject(obj)
Definition: nodes.h:224
#define lfirst(lc)
Definition: pg_list.h:172
tree ctl root
Definition: radixtree.h:1886
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:414
List * exprs
Definition: pathnodes.h:1542
Relids ph_needed
Definition: pathnodes.h:3104
Relids ph_eval_at
Definition: pathnodes.h:3098
PlaceHolderVar * ph_var
Definition: pathnodes.h:3095
Relids phnullingrels
Definition: pathnodes.h:2801
struct PathTarget * reltarget
Definition: pathnodes.h:893

References Assert, 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, PlaceHolderVar::phnullingrels, RelOptInfo::reltarget, and root.

Referenced by query_planner().

◆ add_placeholders_to_joinrel()

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

Definition at line 400 of file placeholder.c.

403 {
404  Relids relids = joinrel->relids;
405  int64 tuple_width = joinrel->reltarget->width;
406  ListCell *lc;
407 
408  foreach(lc, root->placeholder_list)
409  {
410  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
411 
412  /* Is it computable here? */
413  if (bms_is_subset(phinfo->ph_eval_at, relids))
414  {
415  /* Is it still needed above this joinrel? */
416  if (bms_nonempty_difference(phinfo->ph_needed, relids))
417  {
418  /*
419  * Yes, but only add to tlist if it wasn't computed in either
420  * input; otherwise it should be there already. Also, we
421  * charge the cost of evaluating the contained expression if
422  * the PHV can be computed here but not in either input. This
423  * is a bit bogus because we make the decision based on the
424  * first pair of possible input relations considered for the
425  * joinrel. With other pairs, it might be possible to compute
426  * the PHV in one input or the other, and then we'd be double
427  * charging the PHV's cost for some join paths. For now, live
428  * with that; but we might want to improve it later by
429  * refiguring the reltarget costs for each pair of inputs.
430  */
431  if (!bms_is_subset(phinfo->ph_eval_at, outer_rel->relids) &&
432  !bms_is_subset(phinfo->ph_eval_at, inner_rel->relids))
433  {
434  /* Copying might be unnecessary here, but be safe */
435  PlaceHolderVar *phv = copyObject(phinfo->ph_var);
436  QualCost cost;
437 
438  /*
439  * It'll start out not nulled by anything. Joins above
440  * this one might add to its phnullingrels later, in much
441  * the same way as for Vars.
442  */
443  Assert(phv->phnullingrels == NULL);
444 
445  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
446  phv);
447  cost_qual_eval_node(&cost, (Node *) phv->phexpr, root);
448  joinrel->reltarget->cost.startup += cost.startup;
449  joinrel->reltarget->cost.per_tuple += cost.per_tuple;
450  tuple_width += phinfo->ph_width;
451  }
452  }
453 
454  /*
455  * Also adjust joinrel's direct_lateral_relids to include the
456  * PHV's source rel(s). We must do this even if we're not
457  * actually going to emit the PHV, otherwise join_is_legal() will
458  * reject valid join orderings. (In principle maybe we could
459  * instead remove the joinrel's lateral_relids dependency; but
460  * that's complicated to get right, and cases where we're not
461  * going to emit the PHV are too rare to justify the work.)
462  *
463  * In principle we should only do this if the join doesn't yet
464  * include the PHV's source rel(s). But our caller
465  * build_join_rel() will clean things up by removing the join's
466  * own relids from its direct_lateral_relids, so we needn't
467  * account for that here.
468  */
469  joinrel->direct_lateral_relids =
471  phinfo->ph_lateral);
472  }
473  }
474 
475  joinrel->reltarget->width = clamp_width_est(tuple_width);
476 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
int64_t int64
Definition: c.h:482
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4758
int32 clamp_width_est(int64 tuple_width)
Definition: costsize.c:242
Definition: nodes.h:129
QualCost cost
Definition: pathnodes.h:1548
Relids ph_lateral
Definition: pathnodes.h:3101
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
Relids relids
Definition: pathnodes.h:871
Relids direct_lateral_relids
Definition: pathnodes.h:911

References Assert, bms_add_members(), bms_is_subset(), bms_nonempty_difference(), clamp_width_est(), copyObject, 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::phnullingrels, RelOptInfo::relids, RelOptInfo::reltarget, root, QualCost::startup, and PathTarget::width.

Referenced by build_join_rel().

◆ contain_placeholder_references_to()

bool contain_placeholder_references_to ( PlannerInfo root,
Node clause,
int  relid 
)

Definition at line 491 of file placeholder.c.

493 {
495 
496  /* We can answer quickly in the common case that there's no PHVs at all */
497  if (root->glob->lastPHId == 0)
498  return false;
499  /* Else run the recursive search */
500  context.relid = relid;
501  context.sublevels_up = 0;
503 }
static bool contain_placeholder_references_walker(Node *node, contain_placeholder_references_context *context)
Definition: placeholder.c:506
tree context
Definition: radixtree.h:1835

References contain_placeholder_references_walker(), context, and root.

Referenced by make_outerjoininfo().

◆ find_placeholder_info()

PlaceHolderInfo* find_placeholder_info ( PlannerInfo root,
PlaceHolderVar phv 
)

Definition at line 83 of file placeholder.c.

84 {
85  PlaceHolderInfo *phinfo;
86  Relids rels_used;
87 
88  /* if this ever isn't true, we'd need to be able to look in parent lists */
89  Assert(phv->phlevelsup == 0);
90 
91  /* Use placeholder_array to look up existing PlaceHolderInfo quickly */
92  if (phv->phid < root->placeholder_array_size)
93  phinfo = root->placeholder_array[phv->phid];
94  else
95  phinfo = NULL;
96  if (phinfo != NULL)
97  {
98  Assert(phinfo->phid == phv->phid);
99  return phinfo;
100  }
101 
102  /* Not found, so create it */
103  if (root->placeholdersFrozen)
104  elog(ERROR, "too late to create a new PlaceHolderInfo");
105 
106  phinfo = makeNode(PlaceHolderInfo);
107 
108  phinfo->phid = phv->phid;
109  phinfo->ph_var = copyObject(phv);
110 
111  /*
112  * By convention, phinfo->ph_var->phnullingrels is always empty, since the
113  * PlaceHolderInfo represents the initially-calculated state of the
114  * PlaceHolderVar. PlaceHolderVars appearing in the query tree might have
115  * varying values of phnullingrels, reflecting outer joins applied above
116  * the calculation level.
117  */
118  phinfo->ph_var->phnullingrels = NULL;
119 
120  /*
121  * Any referenced rels that are outside the PHV's syntactic scope are
122  * LATERAL references, which should be included in ph_lateral but not in
123  * ph_eval_at. If no referenced rels are within the syntactic scope,
124  * force evaluation at the syntactic location.
125  */
126  rels_used = pull_varnos(root, (Node *) phv->phexpr);
127  phinfo->ph_lateral = bms_difference(rels_used, phv->phrels);
128  phinfo->ph_eval_at = bms_int_members(rels_used, phv->phrels);
129  /* If no contained vars, force evaluation at syntactic location */
130  if (bms_is_empty(phinfo->ph_eval_at))
131  {
132  phinfo->ph_eval_at = bms_copy(phv->phrels);
133  Assert(!bms_is_empty(phinfo->ph_eval_at));
134  }
135  phinfo->ph_needed = NULL; /* initially it's unused */
136  /* for the moment, estimate width using just the datatype info */
137  phinfo->ph_width = get_typavgwidth(exprType((Node *) phv->phexpr),
138  exprTypmod((Node *) phv->phexpr));
139 
140  /*
141  * Add to both placeholder_list and placeholder_array. Note: because we
142  * store pointers to the PlaceHolderInfos in two data structures, it'd be
143  * unsafe to pass the whole placeholder_list structure through
144  * expression_tree_mutator or the like --- or at least, you'd have to
145  * rebuild the placeholder_array afterwards.
146  */
147  root->placeholder_list = lappend(root->placeholder_list, phinfo);
148 
149  if (phinfo->phid >= root->placeholder_array_size)
150  {
151  /* Must allocate or enlarge placeholder_array */
152  int new_size;
153 
154  new_size = root->placeholder_array_size ? root->placeholder_array_size * 2 : 8;
155  while (phinfo->phid >= new_size)
156  new_size *= 2;
157  if (root->placeholder_array)
158  root->placeholder_array =
159  repalloc0_array(root->placeholder_array, PlaceHolderInfo *, root->placeholder_array_size, new_size);
160  else
161  root->placeholder_array =
162  palloc0_array(PlaceHolderInfo *, new_size);
163  root->placeholder_array_size = new_size;
164  }
165  root->placeholder_array[phinfo->phid] = phinfo;
166 
167  /*
168  * The PHV's contained expression may contain other, lower-level PHVs. We
169  * now know we need to get those into the PlaceHolderInfo list, too, so we
170  * may as well do that immediately.
171  */
172  find_placeholders_in_expr(root, (Node *) phinfo->ph_var->phexpr);
173 
174  return phinfo;
175 }
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
#define bms_is_empty(a)
Definition: bitmapset.h:118
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define palloc0_array(type, count)
Definition: fe_memutils.h:77
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2578
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
#define makeNode(_type_)
Definition: nodes.h:155
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:109
static void find_placeholders_in_expr(PlannerInfo *root, Node *expr)
Definition: placeholder.c:257
Index phlevelsup
Definition: pathnodes.h:2807
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:113

References Assert, bms_copy(), bms_difference(), bms_int_members(), bms_is_empty, copyObject, elog, ERROR, exprType(), exprTypmod(), find_placeholders_in_expr(), get_typavgwidth(), lappend(), makeNode, palloc0_array, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_var, PlaceHolderInfo::ph_width, PlaceHolderVar::phid, PlaceHolderInfo::phid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, pull_varnos(), repalloc0_array, and root.

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

◆ find_placeholders_in_jointree()

void find_placeholders_in_jointree ( PlannerInfo root)

Definition at line 185 of file placeholder.c.

186 {
187  /* This must be done before freezing the set of PHIs */
188  Assert(!root->placeholdersFrozen);
189 
190  /* We need do nothing if the query contains no PlaceHolderVars */
191  if (root->glob->lastPHId != 0)
192  {
193  /* Start recursion at top of jointree */
194  Assert(root->parse->jointree != NULL &&
195  IsA(root->parse->jointree, FromExpr));
196  find_placeholders_recurse(root, (Node *) root->parse->jointree);
197  }
198 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
static void find_placeholders_recurse(PlannerInfo *root, Node *jtnode)
Definition: placeholder.c:207

References Assert, find_placeholders_recurse(), IsA, and root.

Referenced by query_planner().

◆ fix_placeholder_input_needed_levels()

void fix_placeholder_input_needed_levels ( PlannerInfo root)

Definition at line 300 of file placeholder.c.

301 {
302  ListCell *lc;
303 
304  foreach(lc, root->placeholder_list)
305  {
306  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
307  List *vars = pull_var_clause((Node *) phinfo->ph_var->phexpr,
311 
313  list_free(vars);
314  }
315 }
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
Definition: initsplan.c:281
void list_free(List *list)
Definition: list.c:1546
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:188
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:190
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:191
Definition: pg_list.h:54
Definition: regcomp.c:282
List * pull_var_clause(Node *node, int flags)
Definition: var.c:609

References add_vars_to_targetlist(), lfirst, list_free(), PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_var, pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, and root.

Referenced by query_planner().

◆ make_placeholder_expr()

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

Definition at line 54 of file placeholder.c.

55 {
57 
58  phv->phexpr = expr;
59  phv->phrels = phrels;
60  phv->phnullingrels = NULL; /* caller may change this later */
61  phv->phid = ++(root->glob->lastPHId);
62  phv->phlevelsup = 0; /* caller may change this later */
63 
64  return phv;
65 }

References makeNode, PlaceHolderVar::phid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, and root.

Referenced by add_nullingrels_if_needed(), mark_nullable_by_grouping(), and pullup_replace_vars_callback().

◆ rebuild_placeholder_attr_needed()

void rebuild_placeholder_attr_needed ( PlannerInfo root)

Definition at line 327 of file placeholder.c.

328 {
329  ListCell *lc;
330 
331  foreach(lc, root->placeholder_list)
332  {
333  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
334  List *vars = pull_var_clause((Node *) phinfo->ph_var->phexpr,
338 
340  list_free(vars);
341  }
342 }
void add_vars_to_attr_needed(PlannerInfo *root, List *vars, Relids where_needed)
Definition: initsplan.c:352

References add_vars_to_attr_needed(), lfirst, list_free(), PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_var, pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, and root.

Referenced by remove_rel_from_query().