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.

Data Structures

struct  contain_placeholder_references_context
 

Typedefs

typedef struct contain_placeholder_references_context contain_placeholder_references_context
 

Functions

static void find_placeholders_recurse (PlannerInfo *root, Node *jtnode)
 
static void find_placeholders_in_expr (PlannerInfo *root, Node *expr)
 
static bool contain_placeholder_references_walker (Node *node, contain_placeholder_references_context *context)
 
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 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)
 

Typedef Documentation

◆ contain_placeholder_references_context

Function Documentation

◆ add_placeholders_to_base_rels()

void add_placeholders_to_base_rels ( PlannerInfo root)

Definition at line 331 of file placeholder.c.

332 {
333  ListCell *lc;
334 
335  foreach(lc, root->placeholder_list)
336  {
337  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
338  Relids eval_at = phinfo->ph_eval_at;
339  int varno;
340 
341  if (bms_get_singleton_member(eval_at, &varno) &&
342  bms_nonempty_difference(phinfo->ph_needed, eval_at))
343  {
344  RelOptInfo *rel = find_base_rel(root, varno);
345 
346  /*
347  * As in add_vars_to_targetlist(), a value computed at scan level
348  * has not yet been nulled by any outer join, so its phnullingrels
349  * should be empty.
350  */
351  Assert(phinfo->ph_var->phnullingrels == NULL);
352 
353  /* Copying the PHV might be unnecessary here, but be safe */
354  rel->reltarget->exprs = lappend(rel->reltarget->exprs,
355  copyObject(phinfo->ph_var));
356  /* reltarget's cost and width fields will be updated later */
357  }
358  }
359 }
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
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
#define copyObject(obj)
Definition: nodes.h:244
#define lfirst(lc)
Definition: pg_list.h:172
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:393
List * exprs
Definition: pathnodes.h:1511
Relids ph_needed
Definition: pathnodes.h:3028
Relids ph_eval_at
Definition: pathnodes.h:3022
PlaceHolderVar * ph_var
Definition: pathnodes.h:3019
Relids phnullingrels
Definition: pathnodes.h:2736
List * placeholder_list
Definition: pathnodes.h:374
struct PathTarget * reltarget
Definition: pathnodes.h:888

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, 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,
SpecialJoinInfo sjinfo 
)

Definition at line 375 of file placeholder.c.

378 {
379  Relids relids = joinrel->relids;
380  ListCell *lc;
381 
382  foreach(lc, root->placeholder_list)
383  {
384  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
385 
386  /* Is it computable here? */
387  if (bms_is_subset(phinfo->ph_eval_at, relids))
388  {
389  /* Is it still needed above this joinrel? */
390  if (bms_nonempty_difference(phinfo->ph_needed, relids))
391  {
392  /*
393  * Yes, but only add to tlist if it wasn't computed in either
394  * input; otherwise it should be there already. Also, we
395  * charge the cost of evaluating the contained expression if
396  * the PHV can be computed here but not in either input. This
397  * is a bit bogus because we make the decision based on the
398  * first pair of possible input relations considered for the
399  * joinrel. With other pairs, it might be possible to compute
400  * the PHV in one input or the other, and then we'd be double
401  * charging the PHV's cost for some join paths. For now, live
402  * with that; but we might want to improve it later by
403  * refiguring the reltarget costs for each pair of inputs.
404  */
405  if (!bms_is_subset(phinfo->ph_eval_at, outer_rel->relids) &&
406  !bms_is_subset(phinfo->ph_eval_at, inner_rel->relids))
407  {
408  /* Copying might be unnecessary here, but be safe */
409  PlaceHolderVar *phv = copyObject(phinfo->ph_var);
410  QualCost cost;
411 
412  /*
413  * It'll start out not nulled by anything. Joins above
414  * this one might add to its phnullingrels later, in much
415  * the same way as for Vars.
416  */
417  Assert(phv->phnullingrels == NULL);
418 
419  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
420  phv);
421  cost_qual_eval_node(&cost, (Node *) phv->phexpr, root);
422  joinrel->reltarget->cost.startup += cost.startup;
423  joinrel->reltarget->cost.per_tuple += cost.per_tuple;
424  joinrel->reltarget->width += phinfo->ph_width;
425  }
426  }
427 
428  /*
429  * Also adjust joinrel's direct_lateral_relids to include the
430  * PHV's source rel(s). We must do this even if we're not
431  * actually going to emit the PHV, otherwise join_is_legal() will
432  * reject valid join orderings. (In principle maybe we could
433  * instead remove the joinrel's lateral_relids dependency; but
434  * that's complicated to get right, and cases where we're not
435  * going to emit the PHV are too rare to justify the work.)
436  *
437  * In principle we should only do this if the join doesn't yet
438  * include the PHV's source rel(s). But our caller
439  * build_join_rel() will clean things up by removing the join's
440  * own relids from its direct_lateral_relids, so we needn't
441  * account for that here.
442  */
443  joinrel->direct_lateral_relids =
445  phinfo->ph_lateral);
446  }
447  }
448 }
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:4393
Definition: nodes.h:129
QualCost cost
Definition: pathnodes.h:1517
Relids ph_lateral
Definition: pathnodes.h:3025
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
Relids relids
Definition: pathnodes.h:866
Relids direct_lateral_relids
Definition: pathnodes.h:906

References Assert(), bms_add_members(), bms_is_subset(), bms_nonempty_difference(), 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, PlannerInfo::placeholder_list, RelOptInfo::relids, RelOptInfo::reltarget, 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 463 of file placeholder.c.

465 {
467 
468  /* We can answer quickly in the common case that there's no PHVs at all */
469  if (root->glob->lastPHId == 0)
470  return false;
471  /* Else run the recursive search */
472  context.relid = relid;
473  context.sublevels_up = 0;
474  return contain_placeholder_references_walker(clause, &context);
475 }
static bool contain_placeholder_references_walker(Node *node, contain_placeholder_references_context *context)
Definition: placeholder.c:478
Index lastPHId
Definition: pathnodes.h:141
PlannerGlobal * glob
Definition: pathnodes.h:205

References contain_placeholder_references_walker(), PlannerInfo::glob, PlannerGlobal::lastPHId, contain_placeholder_references_context::relid, and contain_placeholder_references_context::sublevels_up.

Referenced by make_outerjoininfo().

◆ contain_placeholder_references_walker()

static bool contain_placeholder_references_walker ( Node node,
contain_placeholder_references_context context 
)
static

Definition at line 478 of file placeholder.c.

480 {
481  if (node == NULL)
482  return false;
483  if (IsA(node, PlaceHolderVar))
484  {
485  PlaceHolderVar *phv = (PlaceHolderVar *) node;
486 
487  /* We should just look through PHVs of other query levels */
488  if (phv->phlevelsup == context->sublevels_up)
489  {
490  /* If phrels matches, we found what we came for */
491  if (bms_is_member(context->relid, phv->phrels))
492  return true;
493 
494  /*
495  * We should not examine phnullingrels: what we are looking for is
496  * references in the contained expression, not OJs that might null
497  * the result afterwards. Also, we don't need to recurse into the
498  * contained expression, because phrels should adequately
499  * summarize what's in there. So we're done here.
500  */
501  return false;
502  }
503  }
504  else if (IsA(node, Query))
505  {
506  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
507  bool result;
508 
509  context->sublevels_up++;
510  result = query_tree_walker((Query *) node,
512  context,
513  0);
514  context->sublevels_up--;
515  return result;
516  }
518  context);
519 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:428
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:156
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
Index phlevelsup
Definition: pathnodes.h:2742

References bms_is_member(), expression_tree_walker, IsA, PlaceHolderVar::phlevelsup, query_tree_walker, contain_placeholder_references_context::relid, and contain_placeholder_references_context::sublevels_up.

Referenced by contain_placeholder_references_to().

◆ 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  if (bms_is_empty(phinfo->ph_lateral))
129  phinfo->ph_lateral = NULL; /* make it exactly NULL if empty */
130  phinfo->ph_eval_at = bms_int_members(rels_used, phv->phrels);
131  /* If no contained vars, force evaluation at syntactic location */
132  if (bms_is_empty(phinfo->ph_eval_at))
133  {
134  phinfo->ph_eval_at = bms_copy(phv->phrels);
135  Assert(!bms_is_empty(phinfo->ph_eval_at));
136  }
137  phinfo->ph_needed = NULL; /* initially it's unused */
138  /* for the moment, estimate width using just the datatype info */
139  phinfo->ph_width = get_typavgwidth(exprType((Node *) phv->phexpr),
140  exprTypmod((Node *) phv->phexpr));
141 
142  /*
143  * Add to both placeholder_list and placeholder_array. Note: because we
144  * store pointers to the PlaceHolderInfos in two data structures, it'd be
145  * unsafe to pass the whole placeholder_list structure through
146  * expression_tree_mutator or the like --- or at least, you'd have to
147  * rebuild the placeholder_array afterwards.
148  */
149  root->placeholder_list = lappend(root->placeholder_list, phinfo);
150 
151  if (phinfo->phid >= root->placeholder_array_size)
152  {
153  /* Must allocate or enlarge placeholder_array */
154  int new_size;
155 
156  new_size = root->placeholder_array_size ? root->placeholder_array_size * 2 : 8;
157  while (phinfo->phid >= new_size)
158  new_size *= 2;
159  if (root->placeholder_array)
160  root->placeholder_array =
161  repalloc0_array(root->placeholder_array, PlaceHolderInfo *, root->placeholder_array_size, new_size);
162  else
163  root->placeholder_array =
164  palloc0_array(PlaceHolderInfo *, new_size);
165  root->placeholder_array_size = new_size;
166  }
167  root->placeholder_array[phinfo->phid] = phinfo;
168 
169  /*
170  * The PHV's contained expression may contain other, lower-level PHVs. We
171  * now know we need to get those into the PlaceHolderInfo list, too, so we
172  * may as well do that immediately.
173  */
174  find_placeholders_in_expr(root, (Node *) phinfo->ph_var->phexpr);
175 
176  return phinfo;
177 }
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:39
#define palloc0_array(type, count)
Definition: fe_memutils.h:65
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:176
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:110
static void find_placeholders_in_expr(PlannerInfo *root, Node *expr)
Definition: placeholder.c:259
bool placeholdersFrozen
Definition: pathnodes.h:502
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:108

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, 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_expr()

static void find_placeholders_in_expr ( PlannerInfo root,
Node expr 
)
static

Definition at line 259 of file placeholder.c.

260 {
261  List *vars;
262  ListCell *vl;
263 
264  /*
265  * pull_var_clause does more than we need here, but it'll do and it's
266  * convenient to use.
267  */
268  vars = pull_var_clause(expr,
272  foreach(vl, vars)
273  {
274  PlaceHolderVar *phv = (PlaceHolderVar *) lfirst(vl);
275 
276  /* Ignore any plain Vars */
277  if (!IsA(phv, PlaceHolderVar))
278  continue;
279 
280  /* Create a PlaceHolderInfo entry if there's not one already */
281  (void) find_placeholder_info(root, phv);
282  }
283  list_free(vars);
284 }
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
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition: placeholder.c:83
Definition: pg_list.h:54
Definition: regcomp.c:282
List * pull_var_clause(Node *node, int flags)
Definition: var.c:607

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

◆ find_placeholders_in_jointree()

void find_placeholders_in_jointree ( PlannerInfo root)

Definition at line 187 of file placeholder.c.

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

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

Referenced by query_planner().

◆ find_placeholders_recurse()

static void find_placeholders_recurse ( PlannerInfo root,
Node jtnode 
)
static

Definition at line 209 of file placeholder.c.

210 {
211  if (jtnode == NULL)
212  return;
213  if (IsA(jtnode, RangeTblRef))
214  {
215  /* No quals to deal with here */
216  }
217  else if (IsA(jtnode, FromExpr))
218  {
219  FromExpr *f = (FromExpr *) jtnode;
220  ListCell *l;
221 
222  /*
223  * First, recurse to handle child joins.
224  */
225  foreach(l, f->fromlist)
226  {
228  }
229 
230  /*
231  * Now process the top-level quals.
232  */
234  }
235  else if (IsA(jtnode, JoinExpr))
236  {
237  JoinExpr *j = (JoinExpr *) jtnode;
238 
239  /*
240  * First, recurse to handle child joins.
241  */
242  find_placeholders_recurse(root, j->larg);
243  find_placeholders_recurse(root, j->rarg);
244 
245  /* Process the qual clauses */
246  find_placeholders_in_expr(root, j->quals);
247  }
248  else
249  elog(ERROR, "unrecognized node type: %d",
250  (int) nodeTag(jtnode));
251 }
int j
Definition: isn.c:74
#define nodeTag(nodeptr)
Definition: nodes.h:133
Node * quals
Definition: primnodes.h:1841
List * fromlist
Definition: primnodes.h:1840

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

Referenced by find_placeholders_in_jointree().

◆ fix_placeholder_input_needed_levels()

void fix_placeholder_input_needed_levels ( PlannerInfo root)

Definition at line 302 of file placeholder.c.

303 {
304  ListCell *lc;
305 
306  foreach(lc, root->placeholder_list)
307  {
308  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
309  List *vars = pull_var_clause((Node *) phinfo->ph_var->phexpr,
313 
314  add_vars_to_targetlist(root, vars, phinfo->ph_eval_at);
315  list_free(vars);
316  }
317 }
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
Definition: initsplan.c:277

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 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 PlannerInfo::glob, PlannerGlobal::lastPHId, makeNode, PlaceHolderVar::phid, PlaceHolderVar::phlevelsup, and PlaceHolderVar::phnullingrels.

Referenced by add_nullingrels_if_needed(), and pullup_replace_vars_callback().