Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * paramassign.c
4 : * Functions for assigning PARAM_EXEC slots during planning.
5 : *
6 : * This module is responsible for managing three planner data structures:
7 : *
8 : * root->glob->paramExecTypes: records actual assignments of PARAM_EXEC slots.
9 : * The i'th list element holds the data type OID of the i'th parameter slot.
10 : * (Elements can be InvalidOid if they represent slots that are needed for
11 : * chgParam signaling, but will never hold a value at runtime.) This list is
12 : * global to the whole plan since the executor has only one PARAM_EXEC array.
13 : * Assignments are permanent for the plan: we never remove entries once added.
14 : *
15 : * root->plan_params: a list of PlannerParamItem nodes, recording Vars and
16 : * PlaceHolderVars that the root's query level needs to supply to lower-level
17 : * subqueries, along with the PARAM_EXEC number to use for each such value.
18 : * Elements are added to this list while planning a subquery, and the list
19 : * is reset to empty after completion of each subquery.
20 : *
21 : * root->curOuterParams: a list of NestLoopParam nodes, recording Vars and
22 : * PlaceHolderVars that some outer level of nestloop needs to pass down to
23 : * a lower-level plan node in its righthand side. Elements are added to this
24 : * list as createplan.c creates lower Plan nodes that need such Params, and
25 : * are removed when it creates a NestLoop Plan node that will supply those
26 : * values.
27 : *
28 : * The latter two data structures are used to prevent creating multiple
29 : * PARAM_EXEC slots (each requiring work to fill) when the same upper
30 : * SubPlan or NestLoop supplies a value that is referenced in more than
31 : * one place in its child plan nodes. However, when the same Var has to
32 : * be supplied to different subplan trees by different SubPlan or NestLoop
33 : * parent nodes, we don't recognize any commonality; a fresh plan_params or
34 : * curOuterParams entry will be made (since the old one has been removed
35 : * when we finished processing the earlier SubPlan or NestLoop) and a fresh
36 : * PARAM_EXEC number will be assigned. At one time we tried to avoid
37 : * allocating duplicate PARAM_EXEC numbers in such cases, but it's harder
38 : * than it seems to avoid bugs due to overlapping Param lifetimes, so we
39 : * don't risk that anymore. Minimizing the number of PARAM_EXEC slots
40 : * doesn't really save much executor work anyway.
41 : *
42 : *
43 : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
44 : * Portions Copyright (c) 1994, Regents of the University of California
45 : *
46 : * IDENTIFICATION
47 : * src/backend/optimizer/util/paramassign.c
48 : *
49 : *-------------------------------------------------------------------------
50 : */
51 : #include "postgres.h"
52 :
53 : #include "nodes/nodeFuncs.h"
54 : #include "nodes/plannodes.h"
55 : #include "optimizer/paramassign.h"
56 : #include "optimizer/placeholder.h"
57 : #include "rewrite/rewriteManip.h"
58 :
59 :
60 : /*
61 : * Select a PARAM_EXEC number to identify the given Var as a parameter for
62 : * the current subquery. (It might already have one.)
63 : * Record the need for the Var in the proper upper-level root->plan_params.
64 : */
65 : static int
66 48032 : assign_param_for_var(PlannerInfo *root, Var *var)
67 : {
68 : ListCell *ppl;
69 : PlannerParamItem *pitem;
70 : Index levelsup;
71 :
72 : /* Find the query level the Var belongs to */
73 96494 : for (levelsup = var->varlevelsup; levelsup > 0; levelsup--)
74 48462 : root = root->parent_root;
75 :
76 : /* If there's already a matching PlannerParamItem there, just use it */
77 70968 : foreach(ppl, root->plan_params)
78 : {
79 30302 : pitem = (PlannerParamItem *) lfirst(ppl);
80 30302 : if (IsA(pitem->item, Var))
81 : {
82 30302 : Var *pvar = (Var *) pitem->item;
83 :
84 : /*
85 : * This comparison must match _equalVar(), except for ignoring
86 : * varlevelsup. Note that _equalVar() ignores varnosyn,
87 : * varattnosyn, and location, so this does too.
88 : */
89 30302 : if (pvar->varno == var->varno &&
90 28226 : pvar->varattno == var->varattno &&
91 7366 : pvar->vartype == var->vartype &&
92 7366 : pvar->vartypmod == var->vartypmod &&
93 14732 : pvar->varcollid == var->varcollid &&
94 7366 : bms_equal(pvar->varnullingrels, var->varnullingrels))
95 7366 : return pitem->paramId;
96 : }
97 : }
98 :
99 : /* Nope, so make a new one */
100 40666 : var = copyObject(var);
101 40666 : var->varlevelsup = 0;
102 :
103 40666 : pitem = makeNode(PlannerParamItem);
104 40666 : pitem->item = (Node *) var;
105 40666 : pitem->paramId = list_length(root->glob->paramExecTypes);
106 40666 : root->glob->paramExecTypes = lappend_oid(root->glob->paramExecTypes,
107 : var->vartype);
108 :
109 40666 : root->plan_params = lappend(root->plan_params, pitem);
110 :
111 40666 : return pitem->paramId;
112 : }
113 :
114 : /*
115 : * Generate a Param node to replace the given Var,
116 : * which is expected to have varlevelsup > 0 (ie, it is not local).
117 : * Record the need for the Var in the proper upper-level root->plan_params.
118 : */
119 : Param *
120 48032 : replace_outer_var(PlannerInfo *root, Var *var)
121 : {
122 : Param *retval;
123 : int i;
124 :
125 : Assert(var->varlevelsup > 0 && var->varlevelsup < root->query_level);
126 :
127 : /* Find the Var in the appropriate plan_params, or add it if not present */
128 48032 : i = assign_param_for_var(root, var);
129 :
130 48032 : retval = makeNode(Param);
131 48032 : retval->paramkind = PARAM_EXEC;
132 48032 : retval->paramid = i;
133 48032 : retval->paramtype = var->vartype;
134 48032 : retval->paramtypmod = var->vartypmod;
135 48032 : retval->paramcollid = var->varcollid;
136 48032 : retval->location = var->location;
137 :
138 48032 : return retval;
139 : }
140 :
141 : /*
142 : * Select a PARAM_EXEC number to identify the given PlaceHolderVar as a
143 : * parameter for the current subquery. (It might already have one.)
144 : * Record the need for the PHV in the proper upper-level root->plan_params.
145 : *
146 : * This is just like assign_param_for_var, except for PlaceHolderVars.
147 : */
148 : static int
149 42 : assign_param_for_placeholdervar(PlannerInfo *root, PlaceHolderVar *phv)
150 : {
151 : ListCell *ppl;
152 : PlannerParamItem *pitem;
153 : Index levelsup;
154 :
155 : /* Find the query level the PHV belongs to */
156 84 : for (levelsup = phv->phlevelsup; levelsup > 0; levelsup--)
157 42 : root = root->parent_root;
158 :
159 : /* If there's already a matching PlannerParamItem there, just use it */
160 102 : foreach(ppl, root->plan_params)
161 : {
162 60 : pitem = (PlannerParamItem *) lfirst(ppl);
163 60 : if (IsA(pitem->item, PlaceHolderVar))
164 : {
165 0 : PlaceHolderVar *pphv = (PlaceHolderVar *) pitem->item;
166 :
167 : /* We assume comparing the PHIDs is sufficient */
168 0 : if (pphv->phid == phv->phid)
169 0 : return pitem->paramId;
170 : }
171 : }
172 :
173 : /* Nope, so make a new one */
174 42 : phv = copyObject(phv);
175 42 : IncrementVarSublevelsUp((Node *) phv, -((int) phv->phlevelsup), 0);
176 : Assert(phv->phlevelsup == 0);
177 :
178 42 : pitem = makeNode(PlannerParamItem);
179 42 : pitem->item = (Node *) phv;
180 42 : pitem->paramId = list_length(root->glob->paramExecTypes);
181 42 : root->glob->paramExecTypes = lappend_oid(root->glob->paramExecTypes,
182 42 : exprType((Node *) phv->phexpr));
183 :
184 42 : root->plan_params = lappend(root->plan_params, pitem);
185 :
186 42 : return pitem->paramId;
187 : }
188 :
189 : /*
190 : * Generate a Param node to replace the given PlaceHolderVar,
191 : * which is expected to have phlevelsup > 0 (ie, it is not local).
192 : * Record the need for the PHV in the proper upper-level root->plan_params.
193 : *
194 : * This is just like replace_outer_var, except for PlaceHolderVars.
195 : */
196 : Param *
197 42 : replace_outer_placeholdervar(PlannerInfo *root, PlaceHolderVar *phv)
198 : {
199 : Param *retval;
200 : int i;
201 :
202 : Assert(phv->phlevelsup > 0 && phv->phlevelsup < root->query_level);
203 :
204 : /* Find the PHV in the appropriate plan_params, or add it if not present */
205 42 : i = assign_param_for_placeholdervar(root, phv);
206 :
207 42 : retval = makeNode(Param);
208 42 : retval->paramkind = PARAM_EXEC;
209 42 : retval->paramid = i;
210 42 : retval->paramtype = exprType((Node *) phv->phexpr);
211 42 : retval->paramtypmod = exprTypmod((Node *) phv->phexpr);
212 42 : retval->paramcollid = exprCollation((Node *) phv->phexpr);
213 42 : retval->location = -1;
214 :
215 42 : return retval;
216 : }
217 :
218 : /*
219 : * Generate a Param node to replace the given Aggref
220 : * which is expected to have agglevelsup > 0 (ie, it is not local).
221 : * Record the need for the Aggref in the proper upper-level root->plan_params.
222 : */
223 : Param *
224 52 : replace_outer_agg(PlannerInfo *root, Aggref *agg)
225 : {
226 : Param *retval;
227 : PlannerParamItem *pitem;
228 : Index levelsup;
229 :
230 : Assert(agg->agglevelsup > 0 && agg->agglevelsup < root->query_level);
231 :
232 : /* Find the query level the Aggref belongs to */
233 104 : for (levelsup = agg->agglevelsup; levelsup > 0; levelsup--)
234 52 : root = root->parent_root;
235 :
236 : /*
237 : * It does not seem worthwhile to try to de-duplicate references to outer
238 : * aggs. Just make a new slot every time.
239 : */
240 52 : agg = copyObject(agg);
241 52 : IncrementVarSublevelsUp((Node *) agg, -((int) agg->agglevelsup), 0);
242 : Assert(agg->agglevelsup == 0);
243 :
244 52 : pitem = makeNode(PlannerParamItem);
245 52 : pitem->item = (Node *) agg;
246 52 : pitem->paramId = list_length(root->glob->paramExecTypes);
247 52 : root->glob->paramExecTypes = lappend_oid(root->glob->paramExecTypes,
248 : agg->aggtype);
249 :
250 52 : root->plan_params = lappend(root->plan_params, pitem);
251 :
252 52 : retval = makeNode(Param);
253 52 : retval->paramkind = PARAM_EXEC;
254 52 : retval->paramid = pitem->paramId;
255 52 : retval->paramtype = agg->aggtype;
256 52 : retval->paramtypmod = -1;
257 52 : retval->paramcollid = agg->aggcollid;
258 52 : retval->location = agg->location;
259 :
260 52 : return retval;
261 : }
262 :
263 : /*
264 : * Generate a Param node to replace the given GroupingFunc expression which is
265 : * expected to have agglevelsup > 0 (ie, it is not local).
266 : * Record the need for the GroupingFunc in the proper upper-level
267 : * root->plan_params.
268 : */
269 : Param *
270 64 : replace_outer_grouping(PlannerInfo *root, GroupingFunc *grp)
271 : {
272 : Param *retval;
273 : PlannerParamItem *pitem;
274 : Index levelsup;
275 64 : Oid ptype = exprType((Node *) grp);
276 :
277 : Assert(grp->agglevelsup > 0 && grp->agglevelsup < root->query_level);
278 :
279 : /* Find the query level the GroupingFunc belongs to */
280 136 : for (levelsup = grp->agglevelsup; levelsup > 0; levelsup--)
281 72 : root = root->parent_root;
282 :
283 : /*
284 : * It does not seem worthwhile to try to de-duplicate references to outer
285 : * aggs. Just make a new slot every time.
286 : */
287 64 : grp = copyObject(grp);
288 64 : IncrementVarSublevelsUp((Node *) grp, -((int) grp->agglevelsup), 0);
289 : Assert(grp->agglevelsup == 0);
290 :
291 64 : pitem = makeNode(PlannerParamItem);
292 64 : pitem->item = (Node *) grp;
293 64 : pitem->paramId = list_length(root->glob->paramExecTypes);
294 64 : root->glob->paramExecTypes = lappend_oid(root->glob->paramExecTypes,
295 : ptype);
296 :
297 64 : root->plan_params = lappend(root->plan_params, pitem);
298 :
299 64 : retval = makeNode(Param);
300 64 : retval->paramkind = PARAM_EXEC;
301 64 : retval->paramid = pitem->paramId;
302 64 : retval->paramtype = ptype;
303 64 : retval->paramtypmod = -1;
304 64 : retval->paramcollid = InvalidOid;
305 64 : retval->location = grp->location;
306 :
307 64 : return retval;
308 : }
309 :
310 : /*
311 : * Generate a Param node to replace the given MergeSupportFunc expression
312 : * which is expected to be in the RETURNING list of an upper-level MERGE
313 : * query. Record the need for the MergeSupportFunc in the proper upper-level
314 : * root->plan_params.
315 : */
316 : Param *
317 6 : replace_outer_merge_support(PlannerInfo *root, MergeSupportFunc *msf)
318 : {
319 : Param *retval;
320 : PlannerParamItem *pitem;
321 6 : Oid ptype = exprType((Node *) msf);
322 :
323 : Assert(root->parse->commandType != CMD_MERGE);
324 :
325 : /*
326 : * The parser should have ensured that the MergeSupportFunc is in the
327 : * RETURNING list of an upper-level MERGE query, so find that query.
328 : */
329 : do
330 : {
331 6 : root = root->parent_root;
332 6 : if (root == NULL)
333 0 : elog(ERROR, "MergeSupportFunc found outside MERGE");
334 6 : } while (root->parse->commandType != CMD_MERGE);
335 :
336 : /*
337 : * It does not seem worthwhile to try to de-duplicate references to outer
338 : * MergeSupportFunc expressions. Just make a new slot every time.
339 : */
340 6 : msf = copyObject(msf);
341 :
342 6 : pitem = makeNode(PlannerParamItem);
343 6 : pitem->item = (Node *) msf;
344 6 : pitem->paramId = list_length(root->glob->paramExecTypes);
345 6 : root->glob->paramExecTypes = lappend_oid(root->glob->paramExecTypes,
346 : ptype);
347 :
348 6 : root->plan_params = lappend(root->plan_params, pitem);
349 :
350 6 : retval = makeNode(Param);
351 6 : retval->paramkind = PARAM_EXEC;
352 6 : retval->paramid = pitem->paramId;
353 6 : retval->paramtype = ptype;
354 6 : retval->paramtypmod = -1;
355 6 : retval->paramcollid = InvalidOid;
356 6 : retval->location = msf->location;
357 :
358 6 : return retval;
359 : }
360 :
361 : /*
362 : * Generate a Param node to replace the given Var,
363 : * which is expected to come from some upper NestLoop plan node.
364 : * Record the need for the Var in root->curOuterParams.
365 : */
366 : Param *
367 83394 : replace_nestloop_param_var(PlannerInfo *root, Var *var)
368 : {
369 : Param *param;
370 : NestLoopParam *nlp;
371 : ListCell *lc;
372 :
373 : /* Is this Var already listed in root->curOuterParams? */
374 92078 : foreach(lc, root->curOuterParams)
375 : {
376 46530 : nlp = (NestLoopParam *) lfirst(lc);
377 46530 : if (equal(var, nlp->paramval))
378 : {
379 : /* Yes, so just make a Param referencing this NLP's slot */
380 37846 : param = makeNode(Param);
381 37846 : param->paramkind = PARAM_EXEC;
382 37846 : param->paramid = nlp->paramno;
383 37846 : param->paramtype = var->vartype;
384 37846 : param->paramtypmod = var->vartypmod;
385 37846 : param->paramcollid = var->varcollid;
386 37846 : param->location = var->location;
387 37846 : return param;
388 : }
389 : }
390 :
391 : /* No, so assign a PARAM_EXEC slot for a new NLP */
392 45548 : param = generate_new_exec_param(root,
393 : var->vartype,
394 : var->vartypmod,
395 : var->varcollid);
396 45548 : param->location = var->location;
397 :
398 : /* Add it to the list of required NLPs */
399 45548 : nlp = makeNode(NestLoopParam);
400 45548 : nlp->paramno = param->paramid;
401 45548 : nlp->paramval = copyObject(var);
402 45548 : root->curOuterParams = lappend(root->curOuterParams, nlp);
403 :
404 : /* And return the replacement Param */
405 45548 : return param;
406 : }
407 :
408 : /*
409 : * Generate a Param node to replace the given PlaceHolderVar,
410 : * which is expected to come from some upper NestLoop plan node.
411 : * Record the need for the PHV in root->curOuterParams.
412 : *
413 : * This is just like replace_nestloop_param_var, except for PlaceHolderVars.
414 : */
415 : Param *
416 246 : replace_nestloop_param_placeholdervar(PlannerInfo *root, PlaceHolderVar *phv)
417 : {
418 : Param *param;
419 : NestLoopParam *nlp;
420 : ListCell *lc;
421 :
422 : /* Is this PHV already listed in root->curOuterParams? */
423 324 : foreach(lc, root->curOuterParams)
424 : {
425 180 : nlp = (NestLoopParam *) lfirst(lc);
426 180 : if (equal(phv, nlp->paramval))
427 : {
428 : /* Yes, so just make a Param referencing this NLP's slot */
429 102 : param = makeNode(Param);
430 102 : param->paramkind = PARAM_EXEC;
431 102 : param->paramid = nlp->paramno;
432 102 : param->paramtype = exprType((Node *) phv->phexpr);
433 102 : param->paramtypmod = exprTypmod((Node *) phv->phexpr);
434 102 : param->paramcollid = exprCollation((Node *) phv->phexpr);
435 102 : param->location = -1;
436 102 : return param;
437 : }
438 : }
439 :
440 : /* No, so assign a PARAM_EXEC slot for a new NLP */
441 144 : param = generate_new_exec_param(root,
442 144 : exprType((Node *) phv->phexpr),
443 144 : exprTypmod((Node *) phv->phexpr),
444 144 : exprCollation((Node *) phv->phexpr));
445 :
446 : /* Add it to the list of required NLPs */
447 144 : nlp = makeNode(NestLoopParam);
448 144 : nlp->paramno = param->paramid;
449 144 : nlp->paramval = (Var *) copyObject(phv);
450 144 : root->curOuterParams = lappend(root->curOuterParams, nlp);
451 :
452 : /* And return the replacement Param */
453 144 : return param;
454 : }
455 :
456 : /*
457 : * process_subquery_nestloop_params
458 : * Handle params of a parameterized subquery that need to be fed
459 : * from an outer nestloop.
460 : *
461 : * Currently, that would be *all* params that a subquery in FROM has demanded
462 : * from the current query level, since they must be LATERAL references.
463 : *
464 : * subplan_params is a list of PlannerParamItems that we intend to pass to
465 : * a subquery-in-FROM. (This was constructed in root->plan_params while
466 : * planning the subquery, but isn't there anymore when this is called.)
467 : *
468 : * The subplan's references to the outer variables are already represented
469 : * as PARAM_EXEC Params, since that conversion was done by the routines above
470 : * while planning the subquery. So we need not modify the subplan or the
471 : * PlannerParamItems here. What we do need to do is add entries to
472 : * root->curOuterParams to signal the parent nestloop plan node that it must
473 : * provide these values. This differs from replace_nestloop_param_var in
474 : * that the PARAM_EXEC slots to use have already been determined.
475 : *
476 : * Note that we also use root->curOuterRels as an implicit parameter for
477 : * sanity checks.
478 : */
479 : void
480 454 : process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
481 : {
482 : ListCell *lc;
483 :
484 1016 : foreach(lc, subplan_params)
485 : {
486 562 : PlannerParamItem *pitem = lfirst_node(PlannerParamItem, lc);
487 :
488 562 : if (IsA(pitem->item, Var))
489 : {
490 532 : Var *var = (Var *) pitem->item;
491 : NestLoopParam *nlp;
492 : ListCell *lc2;
493 :
494 : /* If not from a nestloop outer rel, complain */
495 532 : if (!bms_is_member(var->varno, root->curOuterRels))
496 0 : elog(ERROR, "non-LATERAL parameter required by subquery");
497 :
498 : /* Is this param already listed in root->curOuterParams? */
499 724 : foreach(lc2, root->curOuterParams)
500 : {
501 192 : nlp = (NestLoopParam *) lfirst(lc2);
502 192 : if (nlp->paramno == pitem->paramId)
503 : {
504 : Assert(equal(var, nlp->paramval));
505 : /* Present, so nothing to do */
506 0 : break;
507 : }
508 : }
509 532 : if (lc2 == NULL)
510 : {
511 : /* No, so add it */
512 532 : nlp = makeNode(NestLoopParam);
513 532 : nlp->paramno = pitem->paramId;
514 532 : nlp->paramval = copyObject(var);
515 532 : root->curOuterParams = lappend(root->curOuterParams, nlp);
516 : }
517 : }
518 30 : else if (IsA(pitem->item, PlaceHolderVar))
519 : {
520 30 : PlaceHolderVar *phv = (PlaceHolderVar *) pitem->item;
521 : NestLoopParam *nlp;
522 : ListCell *lc2;
523 :
524 : /* If not from a nestloop outer rel, complain */
525 30 : if (!bms_is_subset(find_placeholder_info(root, phv)->ph_eval_at,
526 30 : root->curOuterRels))
527 0 : elog(ERROR, "non-LATERAL parameter required by subquery");
528 :
529 : /* Is this param already listed in root->curOuterParams? */
530 90 : foreach(lc2, root->curOuterParams)
531 : {
532 60 : nlp = (NestLoopParam *) lfirst(lc2);
533 60 : if (nlp->paramno == pitem->paramId)
534 : {
535 : Assert(equal(phv, nlp->paramval));
536 : /* Present, so nothing to do */
537 0 : break;
538 : }
539 : }
540 30 : if (lc2 == NULL)
541 : {
542 : /* No, so add it */
543 30 : nlp = makeNode(NestLoopParam);
544 30 : nlp->paramno = pitem->paramId;
545 30 : nlp->paramval = (Var *) copyObject(phv);
546 30 : root->curOuterParams = lappend(root->curOuterParams, nlp);
547 : }
548 : }
549 : else
550 0 : elog(ERROR, "unexpected type of subquery parameter");
551 : }
552 454 : }
553 :
554 : /*
555 : * Identify any NestLoopParams that should be supplied by a NestLoop plan
556 : * node with the specified lefthand rels. Remove them from the active
557 : * root->curOuterParams list and return them as the result list.
558 : *
559 : * XXX Here we also hack up the returned Vars and PHVs so that they do not
560 : * contain nullingrel sets exceeding what is available from the outer side.
561 : * This is needed if we have applied outer join identity 3,
562 : * (A leftjoin B on (Pab)) leftjoin C on (Pb*c)
563 : * = A leftjoin (B leftjoin C on (Pbc)) on (Pab)
564 : * and C contains lateral references to B. It's still safe to apply the
565 : * identity, but the parser will have created those references in the form
566 : * "b*" (i.e., with varnullingrels listing the A/B join), while what we will
567 : * have available from the nestloop's outer side is just "b". We deal with
568 : * that here by stripping the nullingrels down to what is available from the
569 : * outer side according to leftrelids.
570 : *
571 : * That fixes matters for the case of forward application of identity 3.
572 : * If the identity was applied in the reverse direction, we will have
573 : * parameter Vars containing too few nullingrel bits rather than too many.
574 : * Currently, that causes no problems because setrefs.c applies only a
575 : * subset check to nullingrels in NestLoopParams, but we'd have to work
576 : * harder if we ever want to tighten that check. This is all pretty annoying
577 : * because it greatly weakens setrefs.c's cross-check, but the alternative
578 : * seems to be to generate multiple versions of each laterally-parameterized
579 : * subquery, which'd be unduly expensive.
580 : */
581 : List *
582 81712 : identify_current_nestloop_params(PlannerInfo *root, Relids leftrelids)
583 : {
584 : List *result;
585 : ListCell *cell;
586 :
587 81712 : result = NIL;
588 128918 : foreach(cell, root->curOuterParams)
589 : {
590 47206 : NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
591 :
592 : /*
593 : * We are looking for Vars and PHVs that can be supplied by the
594 : * lefthand rels. When we find one, it's okay to modify it in-place
595 : * because all the routines above make a fresh copy to put into
596 : * curOuterParams.
597 : */
598 94238 : if (IsA(nlp->paramval, Var) &&
599 47032 : bms_is_member(nlp->paramval->varno, leftrelids))
600 46080 : {
601 46080 : Var *var = (Var *) nlp->paramval;
602 :
603 46080 : root->curOuterParams = foreach_delete_current(root->curOuterParams,
604 : cell);
605 46080 : var->varnullingrels = bms_intersect(var->varnullingrels,
606 : leftrelids);
607 46080 : result = lappend(result, nlp);
608 : }
609 1300 : else if (IsA(nlp->paramval, PlaceHolderVar) &&
610 348 : bms_is_subset(find_placeholder_info(root,
611 174 : (PlaceHolderVar *) nlp->paramval)->ph_eval_at,
612 : leftrelids))
613 : {
614 174 : PlaceHolderVar *phv = (PlaceHolderVar *) nlp->paramval;
615 :
616 174 : root->curOuterParams = foreach_delete_current(root->curOuterParams,
617 : cell);
618 174 : phv->phnullingrels = bms_intersect(phv->phnullingrels,
619 : leftrelids);
620 174 : result = lappend(result, nlp);
621 : }
622 : }
623 81712 : return result;
624 : }
625 :
626 : /*
627 : * Generate a new Param node that will not conflict with any other.
628 : *
629 : * This is used to create Params representing subplan outputs or
630 : * NestLoop parameters.
631 : *
632 : * We don't need to build a PlannerParamItem for such a Param, but we do
633 : * need to make sure we record the type in paramExecTypes (otherwise,
634 : * there won't be a slot allocated for it).
635 : */
636 : Param *
637 58252 : generate_new_exec_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod,
638 : Oid paramcollation)
639 : {
640 : Param *retval;
641 :
642 58252 : retval = makeNode(Param);
643 58252 : retval->paramkind = PARAM_EXEC;
644 58252 : retval->paramid = list_length(root->glob->paramExecTypes);
645 58252 : root->glob->paramExecTypes = lappend_oid(root->glob->paramExecTypes,
646 : paramtype);
647 58252 : retval->paramtype = paramtype;
648 58252 : retval->paramtypmod = paramtypmod;
649 58252 : retval->paramcollid = paramcollation;
650 58252 : retval->location = -1;
651 :
652 58252 : return retval;
653 : }
654 :
655 : /*
656 : * Assign a (nonnegative) PARAM_EXEC ID for a special parameter (one that
657 : * is not actually used to carry a value at runtime). Such parameters are
658 : * used for special runtime signaling purposes, such as connecting a
659 : * recursive union node to its worktable scan node or forcing plan
660 : * re-evaluation within the EvalPlanQual mechanism. No actual Param node
661 : * exists with this ID, however.
662 : */
663 : int
664 102630 : assign_special_exec_param(PlannerInfo *root)
665 : {
666 102630 : int paramId = list_length(root->glob->paramExecTypes);
667 :
668 102630 : root->glob->paramExecTypes = lappend_oid(root->glob->paramExecTypes,
669 : InvalidOid);
670 102630 : return paramId;
671 : }
|