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 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 401 of file placeholder.c.

402 {
403  ListCell *lc;
404 
405  foreach(lc, root->placeholder_list)
406  {
407  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
408  Relids eval_at = phinfo->ph_eval_at;
409  int varno;
410 
411  if (bms_get_singleton_member(eval_at, &varno) &&
412  bms_nonempty_difference(phinfo->ph_needed, eval_at))
413  {
414  RelOptInfo *rel = find_base_rel(root, varno);
415 
416  rel->reltarget->exprs = lappend(rel->reltarget->exprs,
417  copyObject(phinfo->ph_var));
418  /* reltarget's cost and width fields will be updated later */
419  }
420  }
421 }
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:618
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:548
List * lappend(List *list, void *datum)
Definition: list.c:338
#define copyObject(obj)
Definition: nodes.h:233
#define lfirst(lc)
Definition: pg_list.h:170
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:379
List * exprs
Definition: pathnodes.h:1427
Relids ph_needed
Definition: pathnodes.h:2882
Relids ph_eval_at
Definition: pathnodes.h:2876
PlaceHolderVar * ph_var
Definition: pathnodes.h:2873
List * placeholder_list
Definition: pathnodes.h:364
struct PathTarget * reltarget
Definition: pathnodes.h:846

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().

◆ add_placeholders_to_joinrel()

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

Definition at line 437 of file placeholder.c.

439 {
440  Relids relids = joinrel->relids;
441  ListCell *lc;
442 
443  foreach(lc, root->placeholder_list)
444  {
445  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
446 
447  /* Is it computable here? */
448  if (bms_is_subset(phinfo->ph_eval_at, relids))
449  {
450  /* Is it still needed above this joinrel? */
451  if (bms_nonempty_difference(phinfo->ph_needed, relids))
452  {
453  /*
454  * Yes, but only add to tlist if it wasn't computed in either
455  * input; otherwise it should be there already. Also, we
456  * charge the cost of evaluating the contained expression if
457  * the PHV can be computed here but not in either input. This
458  * is a bit bogus because we make the decision based on the
459  * first pair of possible input relations considered for the
460  * joinrel. With other pairs, it might be possible to compute
461  * the PHV in one input or the other, and then we'd be double
462  * charging the PHV's cost for some join paths. For now, live
463  * with that; but we might want to improve it later by
464  * refiguring the reltarget costs for each pair of inputs.
465  */
466  if (!bms_is_subset(phinfo->ph_eval_at, outer_rel->relids) &&
467  !bms_is_subset(phinfo->ph_eval_at, inner_rel->relids))
468  {
469  PlaceHolderVar *phv = phinfo->ph_var;
470  QualCost cost;
471 
472  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
473  phv);
474  cost_qual_eval_node(&cost, (Node *) phv->phexpr, root);
475  joinrel->reltarget->cost.startup += cost.startup;
476  joinrel->reltarget->cost.per_tuple += cost.per_tuple;
477  joinrel->reltarget->width += phinfo->ph_width;
478  }
479  }
480 
481  /*
482  * Also adjust joinrel's direct_lateral_relids to include the
483  * PHV's source rel(s). We must do this even if we're not
484  * actually going to emit the PHV, otherwise join_is_legal() will
485  * reject valid join orderings. (In principle maybe we could
486  * instead remove the joinrel's lateral_relids dependency; but
487  * that's complicated to get right, and cases where we're not
488  * going to emit the PHV are too rare to justify the work.)
489  *
490  * In principle we should only do this if the join doesn't yet
491  * include the PHV's source rel(s). But our caller
492  * build_join_rel() will clean things up by removing the join's
493  * own relids from its direct_lateral_relids, so we needn't
494  * account for that here.
495  */
496  joinrel->direct_lateral_relids =
498  phinfo->ph_lateral);
499  }
500  }
501 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:316
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:796
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4394
Definition: nodes.h:118
QualCost cost
Definition: pathnodes.h:1433
Relids ph_lateral
Definition: pathnodes.h:2879
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
Relids relids
Definition: pathnodes.h:824
Relids direct_lateral_relids
Definition: pathnodes.h:864

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, PlannerInfo::placeholder_list, RelOptInfo::relids, RelOptInfo::reltarget, QualCost::startup, and PathTarget::width.

Referenced by build_join_rel().

◆ find_placeholder_info()

PlaceHolderInfo* find_placeholder_info ( PlannerInfo root,
PlaceHolderVar phv 
)

Definition at line 67 of file placeholder.c.

68 {
69  PlaceHolderInfo *phinfo;
70  Relids rels_used;
71 
72  /* if this ever isn't true, we'd need to be able to look in parent lists */
73  Assert(phv->phlevelsup == 0);
74 
75  /* Use placeholder_array to look up existing PlaceHolderInfo quickly */
76  if (phv->phid < root->placeholder_array_size)
77  phinfo = root->placeholder_array[phv->phid];
78  else
79  phinfo = NULL;
80  if (phinfo != NULL)
81  {
82  Assert(phinfo->phid == phv->phid);
83  return phinfo;
84  }
85 
86  /* Not found, so create it */
87  if (root->placeholdersFrozen)
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(root, (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  /*
119  * Add to both placeholder_list and placeholder_array. Note: because we
120  * store pointers to the PlaceHolderInfos in two data structures, it'd be
121  * unsafe to pass the whole placeholder_list structure through
122  * expression_tree_mutator or the like --- or at least, you'd have to
123  * rebuild the placeholder_array afterwards.
124  */
125  root->placeholder_list = lappend(root->placeholder_list, phinfo);
126 
127  if (phinfo->phid >= root->placeholder_array_size)
128  {
129  /* Must allocate or enlarge placeholder_array */
130  int new_size;
131 
132  new_size = root->placeholder_array_size ? root->placeholder_array_size * 2 : 8;
133  while (phinfo->phid >= new_size)
134  new_size *= 2;
135  if (root->placeholder_array)
136  root->placeholder_array =
137  repalloc0_array(root->placeholder_array, PlaceHolderInfo *, root->placeholder_array_size, new_size);
138  else
139  root->placeholder_array =
140  palloc0_array(PlaceHolderInfo *, new_size);
141  root->placeholder_array_size = new_size;
142  }
143  root->placeholder_array[phinfo->phid] = phinfo;
144 
145  /*
146  * The PHV's contained expression may contain other, lower-level PHVs. We
147  * now know we need to get those into the PlaceHolderInfo list, too, so we
148  * may as well do that immediately.
149  */
150  find_placeholders_in_expr(root, (Node *) phinfo->ph_var->phexpr);
151 
152  return phinfo;
153 }
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:906
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:704
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
#define ERROR
Definition: elog.h:35
#define palloc0_array(type, count)
Definition: fe_memutils.h:65
Assert(fmt[strlen(fmt) - 1] !='\n')
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2536
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:266
#define makeNode(_type_)
Definition: nodes.h:165
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:107
static void find_placeholders_in_expr(PlannerInfo *root, Node *expr)
Definition: placeholder.c:235
Index phlevelsup
Definition: pathnodes.h:2633
bool placeholdersFrozen
Definition: pathnodes.h:464
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:100

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, PlannerInfo::placeholder_list, PlannerInfo::placeholdersFrozen, pull_varnos(), and repalloc0_array.

Referenced by add_vars_to_targetlist(), build_joinrel_tlist(), create_lateral_join_info(), 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 163 of file placeholder.c.

164 {
165  /* This must be done before freezing the set of PHIs */
166  Assert(!root->placeholdersFrozen);
167 
168  /* We need do nothing if the query contains no PlaceHolderVars */
169  if (root->glob->lastPHId != 0)
170  {
171  /* Start recursion at top of jointree */
172  Assert(root->parse->jointree != NULL &&
173  IsA(root->parse->jointree, FromExpr));
174  find_placeholders_recurse(root, (Node *) root->parse->jointree);
175  }
176 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:168
static void find_placeholders_recurse(PlannerInfo *root, Node *jtnode)
Definition: placeholder.c:185
Index lastPHId
Definition: pathnodes.h:141
PlannerGlobal * glob
Definition: pathnodes.h:205
Query * parse
Definition: pathnodes.h:202
FromExpr * jointree
Definition: parsenodes.h:158

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

Referenced by query_planner().

◆ fix_placeholder_input_needed_levels()

void fix_placeholder_input_needed_levels ( PlannerInfo root)

Definition at line 372 of file placeholder.c.

373 {
374  ListCell *lc;
375 
376  foreach(lc, root->placeholder_list)
377  {
378  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
379  List *vars = pull_var_clause((Node *) phinfo->ph_var->phexpr,
383 
384  add_vars_to_targetlist(root, vars, phinfo->ph_eval_at);
385  list_free(vars);
386  }
387 }
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
Definition: initsplan.c:228
void list_free(List *list)
Definition: list.c:1545
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:184
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:186
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:187
Definition: pg_list.h:52
Definition: regcomp.c:282
List * pull_var_clause(Node *node, int flags)
Definition: var.c:597

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

Referenced by query_planner().

◆ make_placeholder_expr()

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

Definition at line 39 of file placeholder.c.

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 }

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

Referenced by pullup_replace_vars_callback().

◆ update_placeholder_eval_levels()

void update_placeholder_eval_levels ( PlannerInfo root,
SpecialJoinInfo new_sjinfo 
)

Definition at line 287 of file placeholder.c.

288 {
289  ListCell *lc1;
290 
291  foreach(lc1, root->placeholder_list)
292  {
293  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc1);
294  Relids syn_level = phinfo->ph_var->phrels;
295  Relids eval_at;
296  bool found_some;
297  ListCell *lc2;
298 
299  /*
300  * We don't need to do any work on this placeholder unless the
301  * newly-added outer join is syntactically beneath its location.
302  */
303  if (!bms_is_subset(new_sjinfo->syn_lefthand, syn_level) ||
304  !bms_is_subset(new_sjinfo->syn_righthand, syn_level))
305  continue;
306 
307  /*
308  * Check for delays due to lower outer joins. This is the same logic
309  * as in check_outerjoin_delay in initsplan.c, except that we don't
310  * have anything to do with the delay_upper_joins flags; delay of
311  * upper outer joins will be handled later, based on the eval_at
312  * values we compute now.
313  */
314  eval_at = phinfo->ph_eval_at;
315 
316  do
317  {
318  found_some = false;
319  foreach(lc2, root->join_info_list)
320  {
321  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc2);
322 
323  /* disregard joins not within the PHV's sub-select */
324  if (!bms_is_subset(sjinfo->syn_lefthand, syn_level) ||
325  !bms_is_subset(sjinfo->syn_righthand, syn_level))
326  continue;
327 
328  /* do we reference any nullable rels of this OJ? */
329  if (bms_overlap(eval_at, sjinfo->min_righthand) ||
330  (sjinfo->jointype == JOIN_FULL &&
331  bms_overlap(eval_at, sjinfo->min_lefthand)))
332  {
333  /* yes; have we included all its rels in eval_at? */
334  if (!bms_is_subset(sjinfo->min_lefthand, eval_at) ||
335  !bms_is_subset(sjinfo->min_righthand, eval_at))
336  {
337  /* no, so add them in */
338  eval_at = bms_add_members(eval_at,
339  sjinfo->min_lefthand);
340  eval_at = bms_add_members(eval_at,
341  sjinfo->min_righthand);
342  /* we'll need another iteration */
343  found_some = true;
344  }
345  }
346  }
347  } while (found_some);
348 
349  /* Can't move the PHV's eval_at level to above its syntactic level */
350  Assert(bms_is_subset(eval_at, syn_level));
351 
352  phinfo->ph_eval_at = eval_at;
353  }
354 }
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:495
@ JOIN_FULL
Definition: nodes.h:295
List * join_info_list
Definition: pathnodes.h:333
Relids syn_lefthand
Definition: pathnodes.h:2705
Relids min_righthand
Definition: pathnodes.h:2704
JoinType jointype
Definition: pathnodes.h:2707
Relids min_lefthand
Definition: pathnodes.h:2703
Relids syn_righthand
Definition: pathnodes.h:2706

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, PlannerInfo::placeholder_list, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by deconstruct_recurse().