Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * setrefs.c
4 : : * Post-processing of a completed plan tree: fix references to subplan
5 : : * vars, compute regproc values for operators, etc
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : *
11 : : * IDENTIFICATION
12 : : * src/backend/optimizer/plan/setrefs.c
13 : : *
14 : : *-------------------------------------------------------------------------
15 : : */
16 : : #include "postgres.h"
17 : :
18 : : #include "access/transam.h"
19 : : #include "catalog/pg_type.h"
20 : : #include "nodes/makefuncs.h"
21 : : #include "nodes/nodeFuncs.h"
22 : : #include "optimizer/optimizer.h"
23 : : #include "optimizer/pathnode.h"
24 : : #include "optimizer/planmain.h"
25 : : #include "optimizer/planner.h"
26 : : #include "optimizer/subselect.h"
27 : : #include "optimizer/tlist.h"
28 : : #include "parser/parse_relation.h"
29 : : #include "rewrite/rewriteManip.h"
30 : : #include "tcop/utility.h"
31 : : #include "utils/syscache.h"
32 : :
33 : :
34 : : typedef enum
35 : : {
36 : : NRM_EQUAL, /* expect exact match of nullingrels */
37 : : NRM_SUBSET, /* actual Var may have a subset of input */
38 : : NRM_SUPERSET, /* actual Var may have a superset of input */
39 : : } NullingRelsMatch;
40 : :
41 : : typedef struct
42 : : {
43 : : int varno; /* RT index of Var */
44 : : AttrNumber varattno; /* attr number of Var */
45 : : AttrNumber resno; /* TLE position of Var */
46 : : Bitmapset *varnullingrels; /* Var's varnullingrels */
47 : : } tlist_vinfo;
48 : :
49 : : typedef struct
50 : : {
51 : : List *tlist; /* underlying target list */
52 : : int num_vars; /* number of plain Var tlist entries */
53 : : bool has_ph_vars; /* are there PlaceHolderVar entries? */
54 : : bool has_non_vars; /* are there other entries? */
55 : : tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]; /* has num_vars entries */
56 : : } indexed_tlist;
57 : :
58 : : typedef struct
59 : : {
60 : : PlannerInfo *root;
61 : : int rtoffset;
62 : : double num_exec;
63 : : } fix_scan_expr_context;
64 : :
65 : : typedef struct
66 : : {
67 : : PlannerInfo *root;
68 : : indexed_tlist *outer_itlist;
69 : : indexed_tlist *inner_itlist;
70 : : Index acceptable_rel;
71 : : int rtoffset;
72 : : NullingRelsMatch nrm_match;
73 : : double num_exec;
74 : : } fix_join_expr_context;
75 : :
76 : : typedef struct
77 : : {
78 : : PlannerInfo *root;
79 : : indexed_tlist *subplan_itlist;
80 : : int newvarno;
81 : : int rtoffset;
82 : : NullingRelsMatch nrm_match;
83 : : double num_exec;
84 : : } fix_upper_expr_context;
85 : :
86 : : typedef struct
87 : : {
88 : : PlannerInfo *root;
89 : : indexed_tlist *subplan_itlist;
90 : : int newvarno;
91 : : } fix_windowagg_cond_context;
92 : :
93 : : /* Context info for flatten_rtes_walker() */
94 : : typedef struct
95 : : {
96 : : PlannerGlobal *glob;
97 : : Query *query;
98 : : } flatten_rtes_walker_context;
99 : :
100 : : /*
101 : : * Selecting the best alternative in an AlternativeSubPlan expression requires
102 : : * estimating how many times that expression will be evaluated. For an
103 : : * expression in a plan node's targetlist, the plan's estimated number of
104 : : * output rows is clearly what to use, but for an expression in a qual it's
105 : : * far less clear. Since AlternativeSubPlans aren't heavily used, we don't
106 : : * want to expend a lot of cycles making such estimates. What we use is twice
107 : : * the number of output rows. That's not entirely unfounded: we know that
108 : : * clause_selectivity() would fall back to a default selectivity estimate
109 : : * of 0.5 for any SubPlan, so if the qual containing the SubPlan is the last
110 : : * to be applied (which it likely would be, thanks to order_qual_clauses()),
111 : : * this matches what we could have estimated in a far more laborious fashion.
112 : : * Obviously there are many other scenarios, but it's probably not worth the
113 : : * trouble to try to improve on this estimate, especially not when we don't
114 : : * have a better estimate for the selectivity of the SubPlan qual itself.
115 : : */
116 : : #define NUM_EXEC_TLIST(parentplan) ((parentplan)->plan_rows)
117 : : #define NUM_EXEC_QUAL(parentplan) ((parentplan)->plan_rows * 2.0)
118 : :
119 : : /*
120 : : * Check if a Const node is a regclass value. We accept plain OID too,
121 : : * since a regclass Const will get folded to that type if it's an argument
122 : : * to oideq or similar operators. (This might result in some extraneous
123 : : * values in a plan's list of relation dependencies, but the worst result
124 : : * would be occasional useless replans.)
125 : : */
126 : : #define ISREGCLASSCONST(con) \
127 : : (((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \
128 : : !(con)->constisnull)
129 : :
130 : : #define fix_scan_list(root, lst, rtoffset, num_exec) \
131 : : ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset, num_exec))
132 : :
133 : : static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing);
134 : : static void flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte);
135 : : static bool flatten_rtes_walker(Node *node, flatten_rtes_walker_context *cxt);
136 : : static void add_rte_to_flat_rtable(PlannerGlobal *glob, List *rteperminfos,
137 : : RangeTblEntry *rte);
138 : : static Plan *set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset);
139 : : static Plan *set_indexonlyscan_references(PlannerInfo *root,
140 : : IndexOnlyScan *plan,
141 : : int rtoffset);
142 : : static Plan *set_subqueryscan_references(PlannerInfo *root,
143 : : SubqueryScan *plan,
144 : : int rtoffset);
145 : : static Plan *clean_up_removed_plan_level(Plan *parent, Plan *child);
146 : : static void set_foreignscan_references(PlannerInfo *root,
147 : : ForeignScan *fscan,
148 : : int rtoffset);
149 : : static void set_customscan_references(PlannerInfo *root,
150 : : CustomScan *cscan,
151 : : int rtoffset);
152 : : static Plan *set_append_references(PlannerInfo *root,
153 : : Append *aplan,
154 : : int rtoffset);
155 : : static Plan *set_mergeappend_references(PlannerInfo *root,
156 : : MergeAppend *mplan,
157 : : int rtoffset);
158 : : static void set_hash_references(PlannerInfo *root, Plan *plan, int rtoffset);
159 : : static Relids offset_relid_set(Relids relids, int rtoffset);
160 : : static Node *fix_scan_expr(PlannerInfo *root, Node *node,
161 : : int rtoffset, double num_exec);
162 : : static Node *fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context);
163 : : static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context);
164 : : static void set_join_references(PlannerInfo *root, Join *join, int rtoffset);
165 : : static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset);
166 : : static void set_param_references(PlannerInfo *root, Plan *plan);
167 : : static Node *convert_combining_aggrefs(Node *node, void *context);
168 : : static void set_dummy_tlist_references(Plan *plan, int rtoffset);
169 : : static indexed_tlist *build_tlist_index(List *tlist);
170 : : static Var *search_indexed_tlist_for_var(Var *var,
171 : : indexed_tlist *itlist,
172 : : int newvarno,
173 : : int rtoffset,
174 : : NullingRelsMatch nrm_match);
175 : : static Var *search_indexed_tlist_for_phv(PlaceHolderVar *phv,
176 : : indexed_tlist *itlist,
177 : : int newvarno,
178 : : NullingRelsMatch nrm_match);
179 : : static Var *search_indexed_tlist_for_non_var(Expr *node,
180 : : indexed_tlist *itlist,
181 : : int newvarno);
182 : : static Var *search_indexed_tlist_for_sortgroupref(Expr *node,
183 : : Index sortgroupref,
184 : : indexed_tlist *itlist,
185 : : int newvarno);
186 : : static List *fix_join_expr(PlannerInfo *root,
187 : : List *clauses,
188 : : indexed_tlist *outer_itlist,
189 : : indexed_tlist *inner_itlist,
190 : : Index acceptable_rel,
191 : : int rtoffset,
192 : : NullingRelsMatch nrm_match,
193 : : double num_exec);
194 : : static Node *fix_join_expr_mutator(Node *node,
195 : : fix_join_expr_context *context);
196 : : static Node *fix_upper_expr(PlannerInfo *root,
197 : : Node *node,
198 : : indexed_tlist *subplan_itlist,
199 : : int newvarno,
200 : : int rtoffset,
201 : : NullingRelsMatch nrm_match,
202 : : double num_exec);
203 : : static Node *fix_upper_expr_mutator(Node *node,
204 : : fix_upper_expr_context *context);
205 : : static List *set_returning_clause_references(PlannerInfo *root,
206 : : List *rlist,
207 : : Plan *topplan,
208 : : Index resultRelation,
209 : : int rtoffset);
210 : : static List *set_windowagg_runcondition_references(PlannerInfo *root,
211 : : List *runcondition,
212 : : Plan *plan);
213 : :
214 : : static void record_elided_node(PlannerGlobal *glob, int plan_node_id,
215 : : NodeTag elided_type, Bitmapset *relids);
216 : :
217 : :
218 : : /*****************************************************************************
219 : : *
220 : : * SUBPLAN REFERENCES
221 : : *
222 : : *****************************************************************************/
223 : :
224 : : /*
225 : : * set_plan_references
226 : : *
227 : : * This is the final processing pass of the planner/optimizer. The plan
228 : : * tree is complete; we just have to adjust some representational details
229 : : * for the convenience of the executor:
230 : : *
231 : : * 1. We flatten the various subquery rangetables into a single list, and
232 : : * zero out RangeTblEntry fields that are not useful to the executor.
233 : : *
234 : : * 2. We adjust Vars in scan nodes to be consistent with the flat rangetable.
235 : : *
236 : : * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
237 : : * subplans.
238 : : *
239 : : * 4. Aggrefs in Agg plan nodes need to be adjusted in some cases involving
240 : : * partial aggregation or minmax aggregate optimization.
241 : : *
242 : : * 5. PARAM_MULTIEXPR Params are replaced by regular PARAM_EXEC Params,
243 : : * now that we have finished planning all MULTIEXPR subplans.
244 : : *
245 : : * 6. AlternativeSubPlan expressions are replaced by just one of their
246 : : * alternatives, using an estimate of how many times they'll be executed.
247 : : *
248 : : * 7. We compute regproc OIDs for operators (ie, we look up the function
249 : : * that implements each op).
250 : : *
251 : : * 8. We create lists of specific objects that the plan depends on.
252 : : * This will be used by plancache.c to drive invalidation of cached plans.
253 : : * Relation dependencies are represented by OIDs, and everything else by
254 : : * PlanInvalItems (this distinction is motivated by the shared-inval APIs).
255 : : * Currently, relations, user-defined functions, and domains are the only
256 : : * types of objects that are explicitly tracked this way.
257 : : *
258 : : * 9. We assign every plan node in the tree a unique ID.
259 : : *
260 : : * We also perform one final optimization step, which is to delete
261 : : * SubqueryScan, Append, and MergeAppend plan nodes that aren't doing
262 : : * anything useful. The reason for doing this last is that
263 : : * it can't readily be done before set_plan_references, because it would
264 : : * break set_upper_references: the Vars in the child plan's top tlist
265 : : * wouldn't match up with the Vars in the outer plan tree. A SubqueryScan
266 : : * serves a necessary function as a buffer between outer query and subquery
267 : : * variable numbering ... but after we've flattened the rangetable this is
268 : : * no longer a problem, since then there's only one rtindex namespace.
269 : : * Likewise, Append and MergeAppend buffer between the parent and child vars
270 : : * of an appendrel, but we don't need to worry about that once we've done
271 : : * set_plan_references.
272 : : *
273 : : * set_plan_references recursively traverses the whole plan tree.
274 : : *
275 : : * The return value is normally the same Plan node passed in, but can be
276 : : * different when the passed-in Plan is a node we decide isn't needed.
277 : : *
278 : : * The flattened rangetable entries are appended to root->glob->finalrtable.
279 : : * Also, rowmarks entries are appended to root->glob->finalrowmarks, and the
280 : : * RT indexes of ModifyTable result relations to root->glob->resultRelations,
281 : : * and flattened AppendRelInfos are appended to root->glob->appendRelations.
282 : : * Plan dependencies are appended to root->glob->relationOids (for relations)
283 : : * and root->glob->invalItems (for everything else).
284 : : *
285 : : * Notice that we modify Plan nodes in-place, but use expression_tree_mutator
286 : : * to process targetlist and qual expressions. We can assume that the Plan
287 : : * nodes were just built by the planner and are not multiply referenced, but
288 : : * it's not so safe to assume that for expression tree nodes.
289 : : */
290 : : Plan *
291 : 51588 : set_plan_references(PlannerInfo *root, Plan *plan)
292 : : {
293 : 51588 : Plan *result;
294 : 51588 : PlannerGlobal *glob = root->glob;
295 : 51588 : int rtoffset = list_length(glob->finalrtable);
296 : 51588 : ListCell *lc;
297 : :
298 : : /*
299 : : * Add all the query's RTEs to the flattened rangetable. The live ones
300 : : * will have their rangetable indexes increased by rtoffset. (Additional
301 : : * RTEs, not referenced by the Plan tree, might get added after those.)
302 : : */
303 : 51588 : add_rtes_to_flat_rtable(root, false);
304 : :
305 : : /*
306 : : * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
307 : : */
308 [ + + + + : 53121 : foreach(lc, root->rowMarks)
+ + ]
309 : : {
310 : 1533 : PlanRowMark *rc = lfirst_node(PlanRowMark, lc);
311 : 1533 : PlanRowMark *newrc;
312 : :
313 : : /* sanity check on existing row marks */
314 [ + - ]: 1533 : Assert(root->simple_rel_array[rc->rti] != NULL &&
315 : : root->simple_rte_array[rc->rti] != NULL);
316 : :
317 : : /* flat copy is enough since all fields are scalars */
318 : 1533 : newrc = palloc_object(PlanRowMark);
319 : 1533 : memcpy(newrc, rc, sizeof(PlanRowMark));
320 : :
321 : : /* adjust indexes ... but *not* the rowmarkId */
322 : 1533 : newrc->rti += rtoffset;
323 : 1533 : newrc->prti += rtoffset;
324 : :
325 : 1533 : glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
326 : 1533 : }
327 : :
328 : : /*
329 : : * Adjust RT indexes of AppendRelInfos and add to final appendrels list.
330 : : * We assume the AppendRelInfos were built during planning and don't need
331 : : * to be copied.
332 : : */
333 [ + + + + : 59344 : foreach(lc, root->append_rel_list)
+ + ]
334 : : {
335 : 7756 : AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
336 : :
337 : : /* adjust RT indexes */
338 : 7756 : appinfo->parent_relid += rtoffset;
339 : 7756 : appinfo->child_relid += rtoffset;
340 : :
341 : : /*
342 : : * Rather than adjust the translated_vars entries, just drop 'em.
343 : : * Neither the executor nor EXPLAIN currently need that data.
344 : : */
345 : 7756 : appinfo->translated_vars = NIL;
346 : :
347 : 7756 : glob->appendRelations = lappend(glob->appendRelations, appinfo);
348 : 7756 : }
349 : :
350 : : /* If needed, create workspace for processing AlternativeSubPlans */
351 [ + + ]: 51588 : if (root->hasAlternativeSubPlans)
352 : : {
353 : 108 : root->isAltSubplan = (bool *)
354 : 108 : palloc0(list_length(glob->subplans) * sizeof(bool));
355 : 108 : root->isUsedSubplan = (bool *)
356 : 108 : palloc0(list_length(glob->subplans) * sizeof(bool));
357 : 108 : }
358 : :
359 : : /* Now fix the Plan tree */
360 : 51588 : result = set_plan_refs(root, plan, rtoffset);
361 : :
362 : : /*
363 : : * If we have AlternativeSubPlans, it is likely that we now have some
364 : : * unreferenced subplans in glob->subplans. To avoid expending cycles on
365 : : * those subplans later, get rid of them by setting those list entries to
366 : : * NULL. (Note: we can't do this immediately upon processing an
367 : : * AlternativeSubPlan, because there may be multiple copies of the
368 : : * AlternativeSubPlan, and they can get resolved differently.)
369 : : */
370 [ + + ]: 51588 : if (root->hasAlternativeSubPlans)
371 : : {
372 [ + - + + : 500 : foreach(lc, glob->subplans)
+ + ]
373 : : {
374 : 392 : int ndx = foreach_current_index(lc);
375 : :
376 : : /*
377 : : * If it was used by some AlternativeSubPlan in this query level,
378 : : * but wasn't selected as best by any AlternativeSubPlan, then we
379 : : * don't need it. Do not touch subplans that aren't parts of
380 : : * AlternativeSubPlans.
381 : : */
382 [ + - + + ]: 392 : if (root->isAltSubplan[ndx] && !root->isUsedSubplan[ndx])
383 : 193 : lfirst(lc) = NULL;
384 : 392 : }
385 : 108 : }
386 : :
387 : 103176 : return result;
388 : 51588 : }
389 : :
390 : : /*
391 : : * Extract RangeTblEntries from the plan's rangetable, and add to flat rtable
392 : : *
393 : : * This can recurse into subquery plans; "recursing" is true if so.
394 : : *
395 : : * This also seems like a good place to add the query's RTEPermissionInfos to
396 : : * the flat rteperminfos.
397 : : */
398 : : static void
399 : 51629 : add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
400 : : {
401 : 51629 : PlannerGlobal *glob = root->glob;
402 : 51629 : Index rti;
403 : 51629 : ListCell *lc;
404 : :
405 : : /*
406 : : * Record enough information to make it possible for code that looks at
407 : : * the final range table to understand how it was constructed. (If
408 : : * finalrtable is still NIL, then this is the very topmost PlannerInfo,
409 : : * which will always have plan_name == NULL and rtoffset == 0; we omit the
410 : : * degenerate list entry.)
411 : : */
412 [ + + ]: 51629 : if (root->glob->finalrtable != NIL)
413 : : {
414 : 7996 : SubPlanRTInfo *rtinfo = makeNode(SubPlanRTInfo);
415 : :
416 : 7996 : rtinfo->plan_name = root->plan_name;
417 : 7996 : rtinfo->rtoffset = list_length(root->glob->finalrtable);
418 : :
419 : : /* When recursing = true, it's an unplanned or dummy subquery. */
420 : 7996 : rtinfo->dummy = recursing;
421 : :
422 : 7996 : root->glob->subrtinfos = lappend(root->glob->subrtinfos, rtinfo);
423 : 7996 : }
424 : :
425 : : /*
426 : : * Add the query's own RTEs to the flattened rangetable.
427 : : *
428 : : * At top level, we must add all RTEs so that their indexes in the
429 : : * flattened rangetable match up with their original indexes. When
430 : : * recursing, we only care about extracting relation RTEs (and subquery
431 : : * RTEs that were once relation RTEs).
432 : : */
433 [ + - + + : 146255 : foreach(lc, root->parse->rtable)
+ + ]
434 : : {
435 : 94626 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
436 : :
437 [ + + + + : 94659 : if (!recursing || rte->rtekind == RTE_RELATION ||
- + ]
438 [ + + ]: 41 : (rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)))
439 : 94585 : add_rte_to_flat_rtable(glob, root->parse->rteperminfos, rte);
440 : 94626 : }
441 : :
442 : : /*
443 : : * If there are any dead subqueries, they are not referenced in the Plan
444 : : * tree, so we must add RTEs contained in them to the flattened rtable
445 : : * separately. (If we failed to do this, the executor would not perform
446 : : * expected permission checks for tables mentioned in such subqueries.)
447 : : *
448 : : * Note: this pass over the rangetable can't be combined with the previous
449 : : * one, because that would mess up the numbering of the live RTEs in the
450 : : * flattened rangetable.
451 : : */
452 : 51629 : rti = 1;
453 [ + - + + : 146255 : foreach(lc, root->parse->rtable)
+ + ]
454 : : {
455 : 94626 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
456 : :
457 : : /*
458 : : * We should ignore inheritance-parent RTEs: their contents have been
459 : : * pulled up into our rangetable already. Also ignore any subquery
460 : : * RTEs without matching RelOptInfos, as they likewise have been
461 : : * pulled up.
462 : : */
463 [ + + + + : 94626 : if (rte->rtekind == RTE_SUBQUERY && !rte->inh &&
- + ]
464 : 7874 : rti < root->simple_rel_array_size)
465 : : {
466 : 7874 : RelOptInfo *rel = root->simple_rel_array[rti];
467 : :
468 [ + + ]: 7874 : if (rel != NULL)
469 : : {
470 [ - + ]: 3623 : Assert(rel->relid == rti); /* sanity check on array */
471 : :
472 : : /*
473 : : * The subquery might never have been planned at all, if it
474 : : * was excluded on the basis of self-contradictory constraints
475 : : * in our query level. In this case apply
476 : : * flatten_unplanned_rtes.
477 : : *
478 : : * If it was planned but the result rel is dummy, we assume
479 : : * that it has been omitted from our plan tree (see
480 : : * set_subquery_pathlist), and recurse to pull up its RTEs.
481 : : *
482 : : * Otherwise, it should be represented by a SubqueryScan node
483 : : * somewhere in our plan tree, and we'll pull up its RTEs when
484 : : * we process that plan node.
485 : : *
486 : : * However, if we're recursing, then we should pull up RTEs
487 : : * whether the subquery is dummy or not, because we've found
488 : : * that some upper query level is treating this one as dummy,
489 : : * and so we won't scan this level's plan tree at all.
490 : : */
491 [ + + ]: 3623 : if (rel->subroot == NULL)
492 : 4 : flatten_unplanned_rtes(glob, rte);
493 [ + + + + ]: 3619 : else if (recursing ||
494 : 3611 : IS_DUMMY_REL(fetch_upper_rel(rel->subroot,
495 : : UPPERREL_FINAL, NULL)))
496 : 41 : add_rtes_to_flat_rtable(rel->subroot, true);
497 : 3623 : }
498 : 7874 : }
499 : 94626 : rti++;
500 : 94626 : }
501 : 51629 : }
502 : :
503 : : /*
504 : : * Extract RangeTblEntries from a subquery that was never planned at all
505 : : */
506 : :
507 : : static void
508 : 4 : flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte)
509 : : {
510 : 4 : flatten_rtes_walker_context cxt = {glob, rte->subquery};
511 : :
512 : : /* Use query_tree_walker to find all RTEs in the parse tree */
513 : 4 : (void) query_tree_walker(rte->subquery,
514 : : flatten_rtes_walker,
515 : : &cxt,
516 : : QTW_EXAMINE_RTES_BEFORE);
517 : 4 : }
518 : :
519 : : static bool
520 : 100 : flatten_rtes_walker(Node *node, flatten_rtes_walker_context *cxt)
521 : : {
522 [ + + ]: 100 : if (node == NULL)
523 : 58 : return false;
524 [ + + ]: 42 : if (IsA(node, RangeTblEntry))
525 : : {
526 : 3 : RangeTblEntry *rte = (RangeTblEntry *) node;
527 : :
528 : : /* As above, we need only save relation RTEs and former relations */
529 [ - + # # ]: 3 : if (rte->rtekind == RTE_RELATION ||
530 [ # # ]: 0 : (rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)))
531 : 3 : add_rte_to_flat_rtable(cxt->glob, cxt->query->rteperminfos, rte);
532 : 3 : return false;
533 : 3 : }
534 [ + + ]: 39 : if (IsA(node, Query))
535 : : {
536 : : /*
537 : : * Recurse into subselects. Must update cxt->query to this query so
538 : : * that the rtable and rteperminfos correspond with each other.
539 : : */
540 : 1 : Query *save_query = cxt->query;
541 : 1 : bool result;
542 : :
543 : 1 : cxt->query = (Query *) node;
544 : 1 : result = query_tree_walker((Query *) node,
545 : : flatten_rtes_walker,
546 : : cxt,
547 : : QTW_EXAMINE_RTES_BEFORE);
548 : 1 : cxt->query = save_query;
549 : 1 : return result;
550 : 1 : }
551 : 38 : return expression_tree_walker(node, flatten_rtes_walker, cxt);
552 : 100 : }
553 : :
554 : : /*
555 : : * Add (a copy of) the given RTE to the final rangetable and also the
556 : : * corresponding RTEPermissionInfo, if any, to final rteperminfos.
557 : : *
558 : : * In the flat rangetable, we zero out substructure pointers that are not
559 : : * needed by the executor; this reduces the storage space and copying cost
560 : : * for cached plans. We keep only the ctename, alias, eref Alias fields,
561 : : * which are needed by EXPLAIN, and perminfoindex which is needed by the
562 : : * executor to fetch the RTE's RTEPermissionInfo.
563 : : */
564 : : static void
565 : 94588 : add_rte_to_flat_rtable(PlannerGlobal *glob, List *rteperminfos,
566 : : RangeTblEntry *rte)
567 : : {
568 : 94588 : RangeTblEntry *newrte;
569 : :
570 : : /* flat copy to duplicate all the scalar fields */
571 : 94588 : newrte = palloc_object(RangeTblEntry);
572 : 94588 : memcpy(newrte, rte, sizeof(RangeTblEntry));
573 : :
574 : : /* zap unneeded sub-structure */
575 : 94588 : newrte->tablesample = NULL;
576 : 94588 : newrte->subquery = NULL;
577 : 94588 : newrte->joinaliasvars = NIL;
578 : 94588 : newrte->joinleftcols = NIL;
579 : 94588 : newrte->joinrightcols = NIL;
580 : 94588 : newrte->join_using_alias = NULL;
581 : 94588 : newrte->functions = NIL;
582 : 94588 : newrte->tablefunc = NULL;
583 : 94588 : newrte->values_lists = NIL;
584 : 94588 : newrte->coltypes = NIL;
585 : 94588 : newrte->coltypmods = NIL;
586 : 94588 : newrte->colcollations = NIL;
587 : 94588 : newrte->groupexprs = NIL;
588 : 94588 : newrte->securityQuals = NIL;
589 : :
590 : 94588 : glob->finalrtable = lappend(glob->finalrtable, newrte);
591 : :
592 : : /*
593 : : * If it's a plain relation RTE (or a subquery that was once a view
594 : : * reference), add the relation OID to relationOids. Also add its new RT
595 : : * index to the set of relations to be potentially accessed during
596 : : * execution.
597 : : *
598 : : * We do this even though the RTE might be unreferenced in the plan tree;
599 : : * this would correspond to cases such as views that were expanded, child
600 : : * tables that were eliminated by constraint exclusion, etc. Schema
601 : : * invalidation on such a rel must still force rebuilding of the plan.
602 : : *
603 : : * Note we don't bother to avoid making duplicate list entries. We could,
604 : : * but it would probably cost more cycles than it would save.
605 : : */
606 [ + + + + ]: 103111 : if (newrte->rtekind == RTE_RELATION ||
607 [ + + ]: 40834 : (newrte->rtekind == RTE_SUBQUERY && OidIsValid(newrte->relid)))
608 : : {
609 : 55355 : glob->relationOids = lappend_oid(glob->relationOids, newrte->relid);
610 : 110710 : glob->allRelids = bms_add_member(glob->allRelids,
611 : 55355 : list_length(glob->finalrtable));
612 : 55355 : }
613 : :
614 : : /*
615 : : * Add a copy of the RTEPermissionInfo, if any, corresponding to this RTE
616 : : * to the flattened global list.
617 : : */
618 [ + + ]: 94588 : if (rte->perminfoindex > 0)
619 : : {
620 : 48977 : RTEPermissionInfo *perminfo;
621 : 48977 : RTEPermissionInfo *newperminfo;
622 : :
623 : : /* Get the existing one from this query's rteperminfos. */
624 : 48977 : perminfo = getRTEPermissionInfo(rteperminfos, newrte);
625 : :
626 : : /*
627 : : * Add a new one to finalrteperminfos and copy the contents of the
628 : : * existing one into it. Note that addRTEPermissionInfo() also
629 : : * updates newrte->perminfoindex to point to newperminfo in
630 : : * finalrteperminfos.
631 : : */
632 : 48977 : newrte->perminfoindex = 0; /* expected by addRTEPermissionInfo() */
633 : 48977 : newperminfo = addRTEPermissionInfo(&glob->finalrteperminfos, newrte);
634 : 48977 : memcpy(newperminfo, perminfo, sizeof(RTEPermissionInfo));
635 : 48977 : }
636 : 94588 : }
637 : :
638 : : /*
639 : : * set_plan_refs: recurse through the Plan nodes of a single subquery level
640 : : */
641 : : static Plan *
642 : 284379 : set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
643 : : {
644 : 284379 : ListCell *l;
645 : :
646 [ + + ]: 284379 : if (plan == NULL)
647 : 163436 : return NULL;
648 : :
649 : : /* Assign this node a unique ID. */
650 : 120943 : plan->plan_node_id = root->glob->lastPlanNodeId++;
651 : :
652 : : /*
653 : : * Plan-type-specific fixes
654 : : */
655 [ + + + + : 120943 : switch (nodeTag(plan))
+ + + + +
+ + + + +
+ + + + +
- - + + +
+ + + + +
+ + + + +
+ - ]
656 : : {
657 : : case T_SeqScan:
658 : : {
659 : 25976 : SeqScan *splan = (SeqScan *) plan;
660 : :
661 : 25976 : splan->scan.scanrelid += rtoffset;
662 : 25976 : splan->scan.plan.targetlist =
663 : 25976 : fix_scan_list(root, splan->scan.plan.targetlist,
664 : : rtoffset, NUM_EXEC_TLIST(plan));
665 : 25976 : splan->scan.plan.qual =
666 : 25976 : fix_scan_list(root, splan->scan.plan.qual,
667 : : rtoffset, NUM_EXEC_QUAL(plan));
668 : 25976 : }
669 : 25976 : break;
670 : : case T_SampleScan:
671 : : {
672 : 45 : SampleScan *splan = (SampleScan *) plan;
673 : :
674 : 45 : splan->scan.scanrelid += rtoffset;
675 : 45 : splan->scan.plan.targetlist =
676 : 45 : fix_scan_list(root, splan->scan.plan.targetlist,
677 : : rtoffset, NUM_EXEC_TLIST(plan));
678 : 45 : splan->scan.plan.qual =
679 : 45 : fix_scan_list(root, splan->scan.plan.qual,
680 : : rtoffset, NUM_EXEC_QUAL(plan));
681 : 45 : splan->tablesample = (TableSampleClause *)
682 : 90 : fix_scan_expr(root, (Node *) splan->tablesample,
683 : 45 : rtoffset, 1);
684 : 45 : }
685 : 45 : break;
686 : : case T_IndexScan:
687 : : {
688 : 11690 : IndexScan *splan = (IndexScan *) plan;
689 : :
690 : 11690 : splan->scan.scanrelid += rtoffset;
691 : 11690 : splan->scan.plan.targetlist =
692 : 11690 : fix_scan_list(root, splan->scan.plan.targetlist,
693 : : rtoffset, NUM_EXEC_TLIST(plan));
694 : 11690 : splan->scan.plan.qual =
695 : 11690 : fix_scan_list(root, splan->scan.plan.qual,
696 : : rtoffset, NUM_EXEC_QUAL(plan));
697 : 11690 : splan->indexqual =
698 : 11690 : fix_scan_list(root, splan->indexqual,
699 : : rtoffset, 1);
700 : 11690 : splan->indexqualorig =
701 : 11690 : fix_scan_list(root, splan->indexqualorig,
702 : : rtoffset, NUM_EXEC_QUAL(plan));
703 : 11690 : splan->indexorderby =
704 : 11690 : fix_scan_list(root, splan->indexorderby,
705 : : rtoffset, 1);
706 : 11690 : splan->indexorderbyorig =
707 : 11690 : fix_scan_list(root, splan->indexorderbyorig,
708 : : rtoffset, NUM_EXEC_QUAL(plan));
709 : 11690 : }
710 : 11690 : break;
711 : : case T_IndexOnlyScan:
712 : : {
713 : 1841 : IndexOnlyScan *splan = (IndexOnlyScan *) plan;
714 : :
715 : 1841 : return set_indexonlyscan_references(root, splan, rtoffset);
716 : 1841 : }
717 : : break;
718 : : case T_BitmapIndexScan:
719 : : {
720 : 2717 : BitmapIndexScan *splan = (BitmapIndexScan *) plan;
721 : :
722 : 2717 : splan->scan.scanrelid += rtoffset;
723 : : /* no need to fix targetlist and qual */
724 [ + - ]: 2717 : Assert(splan->scan.plan.targetlist == NIL);
725 [ + - ]: 2717 : Assert(splan->scan.plan.qual == NIL);
726 : 2717 : splan->indexqual =
727 : 2717 : fix_scan_list(root, splan->indexqual, rtoffset, 1);
728 : 2717 : splan->indexqualorig =
729 : 2717 : fix_scan_list(root, splan->indexqualorig,
730 : : rtoffset, NUM_EXEC_QUAL(plan));
731 : 2717 : }
732 : 2717 : break;
733 : : case T_BitmapHeapScan:
734 : : {
735 : 2666 : BitmapHeapScan *splan = (BitmapHeapScan *) plan;
736 : :
737 : 2666 : splan->scan.scanrelid += rtoffset;
738 : 2666 : splan->scan.plan.targetlist =
739 : 2666 : fix_scan_list(root, splan->scan.plan.targetlist,
740 : : rtoffset, NUM_EXEC_TLIST(plan));
741 : 2666 : splan->scan.plan.qual =
742 : 2666 : fix_scan_list(root, splan->scan.plan.qual,
743 : : rtoffset, NUM_EXEC_QUAL(plan));
744 : 2666 : splan->bitmapqualorig =
745 : 2666 : fix_scan_list(root, splan->bitmapqualorig,
746 : : rtoffset, NUM_EXEC_QUAL(plan));
747 : 2666 : }
748 : 2666 : break;
749 : : case T_TidScan:
750 : : {
751 : 87 : TidScan *splan = (TidScan *) plan;
752 : :
753 : 87 : splan->scan.scanrelid += rtoffset;
754 : 87 : splan->scan.plan.targetlist =
755 : 87 : fix_scan_list(root, splan->scan.plan.targetlist,
756 : : rtoffset, NUM_EXEC_TLIST(plan));
757 : 87 : splan->scan.plan.qual =
758 : 87 : fix_scan_list(root, splan->scan.plan.qual,
759 : : rtoffset, NUM_EXEC_QUAL(plan));
760 : 87 : splan->tidquals =
761 : 87 : fix_scan_list(root, splan->tidquals,
762 : : rtoffset, 1);
763 : 87 : }
764 : 87 : break;
765 : : case T_TidRangeScan:
766 : : {
767 : 334 : TidRangeScan *splan = (TidRangeScan *) plan;
768 : :
769 : 334 : splan->scan.scanrelid += rtoffset;
770 : 334 : splan->scan.plan.targetlist =
771 : 334 : fix_scan_list(root, splan->scan.plan.targetlist,
772 : : rtoffset, NUM_EXEC_TLIST(plan));
773 : 334 : splan->scan.plan.qual =
774 : 334 : fix_scan_list(root, splan->scan.plan.qual,
775 : : rtoffset, NUM_EXEC_QUAL(plan));
776 : 334 : splan->tidrangequals =
777 : 334 : fix_scan_list(root, splan->tidrangequals,
778 : : rtoffset, 1);
779 : 334 : }
780 : 334 : break;
781 : : case T_SubqueryScan:
782 : : /* Needs special treatment, see comments below */
783 : 7144 : return set_subqueryscan_references(root,
784 : 3572 : (SubqueryScan *) plan,
785 : 3572 : rtoffset);
786 : : case T_FunctionScan:
787 : : {
788 : 3642 : FunctionScan *splan = (FunctionScan *) plan;
789 : :
790 : 3642 : splan->scan.scanrelid += rtoffset;
791 : 3642 : splan->scan.plan.targetlist =
792 : 3642 : fix_scan_list(root, splan->scan.plan.targetlist,
793 : : rtoffset, NUM_EXEC_TLIST(plan));
794 : 3642 : splan->scan.plan.qual =
795 : 3642 : fix_scan_list(root, splan->scan.plan.qual,
796 : : rtoffset, NUM_EXEC_QUAL(plan));
797 : 3642 : splan->functions =
798 : 3642 : fix_scan_list(root, splan->functions, rtoffset, 1);
799 : 3642 : }
800 : 3642 : break;
801 : : case T_TableFuncScan:
802 : : {
803 : 103 : TableFuncScan *splan = (TableFuncScan *) plan;
804 : :
805 : 103 : splan->scan.scanrelid += rtoffset;
806 : 103 : splan->scan.plan.targetlist =
807 : 103 : fix_scan_list(root, splan->scan.plan.targetlist,
808 : : rtoffset, NUM_EXEC_TLIST(plan));
809 : 103 : splan->scan.plan.qual =
810 : 103 : fix_scan_list(root, splan->scan.plan.qual,
811 : : rtoffset, NUM_EXEC_QUAL(plan));
812 : 103 : splan->tablefunc = (TableFunc *)
813 : 206 : fix_scan_expr(root, (Node *) splan->tablefunc,
814 : 103 : rtoffset, 1);
815 : 103 : }
816 : 103 : break;
817 : : case T_ValuesScan:
818 : : {
819 : 1114 : ValuesScan *splan = (ValuesScan *) plan;
820 : :
821 : 1114 : splan->scan.scanrelid += rtoffset;
822 : 1114 : splan->scan.plan.targetlist =
823 : 1114 : fix_scan_list(root, splan->scan.plan.targetlist,
824 : : rtoffset, NUM_EXEC_TLIST(plan));
825 : 1114 : splan->scan.plan.qual =
826 : 1114 : fix_scan_list(root, splan->scan.plan.qual,
827 : : rtoffset, NUM_EXEC_QUAL(plan));
828 : 1114 : splan->values_lists =
829 : 1114 : fix_scan_list(root, splan->values_lists,
830 : : rtoffset, 1);
831 : 1114 : }
832 : 1114 : break;
833 : : case T_CteScan:
834 : : {
835 : 212 : CteScan *splan = (CteScan *) plan;
836 : :
837 : 212 : splan->scan.scanrelid += rtoffset;
838 : 212 : splan->scan.plan.targetlist =
839 : 212 : fix_scan_list(root, splan->scan.plan.targetlist,
840 : : rtoffset, NUM_EXEC_TLIST(plan));
841 : 212 : splan->scan.plan.qual =
842 : 212 : fix_scan_list(root, splan->scan.plan.qual,
843 : : rtoffset, NUM_EXEC_QUAL(plan));
844 : 212 : }
845 : 212 : break;
846 : : case T_NamedTuplestoreScan:
847 : : {
848 : 77 : NamedTuplestoreScan *splan = (NamedTuplestoreScan *) plan;
849 : :
850 : 77 : splan->scan.scanrelid += rtoffset;
851 : 77 : splan->scan.plan.targetlist =
852 : 77 : fix_scan_list(root, splan->scan.plan.targetlist,
853 : : rtoffset, NUM_EXEC_TLIST(plan));
854 : 77 : splan->scan.plan.qual =
855 : 77 : fix_scan_list(root, splan->scan.plan.qual,
856 : : rtoffset, NUM_EXEC_QUAL(plan));
857 : 77 : }
858 : 77 : break;
859 : : case T_WorkTableScan:
860 : : {
861 : 73 : WorkTableScan *splan = (WorkTableScan *) plan;
862 : :
863 : 73 : splan->scan.scanrelid += rtoffset;
864 : 73 : splan->scan.plan.targetlist =
865 : 73 : fix_scan_list(root, splan->scan.plan.targetlist,
866 : : rtoffset, NUM_EXEC_TLIST(plan));
867 : 73 : splan->scan.plan.qual =
868 : 73 : fix_scan_list(root, splan->scan.plan.qual,
869 : : rtoffset, NUM_EXEC_QUAL(plan));
870 : 73 : }
871 : 73 : break;
872 : : case T_ForeignScan:
873 : 0 : set_foreignscan_references(root, (ForeignScan *) plan, rtoffset);
874 : 0 : break;
875 : : case T_CustomScan:
876 : 0 : set_customscan_references(root, (CustomScan *) plan, rtoffset);
877 : 0 : break;
878 : :
879 : : case T_NestLoop:
880 : : case T_MergeJoin:
881 : : case T_HashJoin:
882 : 12986 : set_join_references(root, (Join *) plan, rtoffset);
883 : 12986 : break;
884 : :
885 : : case T_Gather:
886 : : case T_GatherMerge:
887 : : {
888 : 260 : set_upper_references(root, plan, rtoffset);
889 : 260 : set_param_references(root, plan);
890 : : }
891 : 260 : break;
892 : :
893 : : case T_Hash:
894 : 3335 : set_hash_references(root, plan, rtoffset);
895 : 3335 : break;
896 : :
897 : : case T_Memoize:
898 : : {
899 : 215 : Memoize *mplan = (Memoize *) plan;
900 : :
901 : : /*
902 : : * Memoize does not evaluate its targetlist. It just uses the
903 : : * same targetlist from its outer subnode.
904 : : */
905 : 215 : set_dummy_tlist_references(plan, rtoffset);
906 : :
907 : 215 : mplan->param_exprs = fix_scan_list(root, mplan->param_exprs,
908 : : rtoffset,
909 : : NUM_EXEC_TLIST(plan));
910 : : break;
911 : 215 : }
912 : :
913 : : case T_Material:
914 : : case T_Sort:
915 : : case T_IncrementalSort:
916 : : case T_Unique:
917 : : case T_SetOp:
918 : :
919 : : /*
920 : : * These plan types don't actually bother to evaluate their
921 : : * targetlists, because they just return their unmodified input
922 : : * tuples. Even though the targetlist won't be used by the
923 : : * executor, we fix it up for possible use by EXPLAIN (not to
924 : : * mention ease of debugging --- wrong varnos are very confusing).
925 : : */
926 : 10829 : set_dummy_tlist_references(plan, rtoffset);
927 : :
928 : : /*
929 : : * Since these plan types don't check quals either, we should not
930 : : * find any qual expression attached to them.
931 : : */
932 [ + - ]: 10829 : Assert(plan->qual == NIL);
933 : 10829 : break;
934 : : case T_LockRows:
935 : : {
936 : 801 : LockRows *splan = (LockRows *) plan;
937 : :
938 : : /*
939 : : * Like the plan types above, LockRows doesn't evaluate its
940 : : * tlist or quals. But we have to fix up the RT indexes in
941 : : * its rowmarks.
942 : : */
943 : 801 : set_dummy_tlist_references(plan, rtoffset);
944 [ + - ]: 801 : Assert(splan->plan.qual == NIL);
945 : :
946 [ + - + + : 1891 : foreach(l, splan->rowMarks)
+ + ]
947 : : {
948 : 1090 : PlanRowMark *rc = (PlanRowMark *) lfirst(l);
949 : :
950 : 1090 : rc->rti += rtoffset;
951 : 1090 : rc->prti += rtoffset;
952 : 1090 : }
953 : 801 : }
954 : 801 : break;
955 : : case T_Limit:
956 : : {
957 : 491 : Limit *splan = (Limit *) plan;
958 : :
959 : : /*
960 : : * Like the plan types above, Limit doesn't evaluate its tlist
961 : : * or quals. It does have live expressions for limit/offset,
962 : : * however; and those cannot contain subplan variable refs, so
963 : : * fix_scan_expr works for them.
964 : : */
965 : 491 : set_dummy_tlist_references(plan, rtoffset);
966 [ + - ]: 491 : Assert(splan->plan.qual == NIL);
967 : :
968 : 491 : splan->limitOffset =
969 : 491 : fix_scan_expr(root, splan->limitOffset, rtoffset, 1);
970 : 491 : splan->limitCount =
971 : 491 : fix_scan_expr(root, splan->limitCount, rtoffset, 1);
972 : 491 : }
973 : 491 : break;
974 : : case T_Agg:
975 : : {
976 : 5512 : Agg *agg = (Agg *) plan;
977 : :
978 : : /*
979 : : * If this node is combining partial-aggregation results, we
980 : : * must convert its Aggrefs to contain references to the
981 : : * partial-aggregate subexpressions that will be available
982 : : * from the child plan node.
983 : : */
984 [ + + ]: 5512 : if (DO_AGGSPLIT_COMBINE(agg->aggsplit))
985 : : {
986 : 231 : plan->targetlist = (List *)
987 : 231 : convert_combining_aggrefs((Node *) plan->targetlist,
988 : : NULL);
989 : 231 : plan->qual = (List *)
990 : 231 : convert_combining_aggrefs((Node *) plan->qual,
991 : : NULL);
992 : 231 : }
993 : :
994 : 5512 : set_upper_references(root, plan, rtoffset);
995 : 5512 : }
996 : 5512 : break;
997 : : case T_Group:
998 : 39 : set_upper_references(root, plan, rtoffset);
999 : 39 : break;
1000 : : case T_WindowAgg:
1001 : : {
1002 : 457 : WindowAgg *wplan = (WindowAgg *) plan;
1003 : :
1004 : : /*
1005 : : * Adjust the WindowAgg's run conditions by swapping the
1006 : : * WindowFuncs references out to instead reference the Var in
1007 : : * the scan slot so that when the executor evaluates the
1008 : : * runCondition, it receives the WindowFunc's value from the
1009 : : * slot that the result has just been stored into rather than
1010 : : * evaluating the WindowFunc all over again.
1011 : : */
1012 : 914 : wplan->runCondition = set_windowagg_runcondition_references(root,
1013 : 457 : wplan->runCondition,
1014 : 457 : (Plan *) wplan);
1015 : :
1016 : 457 : set_upper_references(root, plan, rtoffset);
1017 : :
1018 : : /*
1019 : : * Like Limit node limit/offset expressions, WindowAgg has
1020 : : * frame offset expressions, which cannot contain subplan
1021 : : * variable refs, so fix_scan_expr works for them.
1022 : : */
1023 : 457 : wplan->startOffset =
1024 : 457 : fix_scan_expr(root, wplan->startOffset, rtoffset, 1);
1025 : 457 : wplan->endOffset =
1026 : 457 : fix_scan_expr(root, wplan->endOffset, rtoffset, 1);
1027 : 457 : wplan->runCondition = fix_scan_list(root,
1028 : : wplan->runCondition,
1029 : : rtoffset,
1030 : : NUM_EXEC_TLIST(plan));
1031 : 457 : wplan->runConditionOrig = fix_scan_list(root,
1032 : : wplan->runConditionOrig,
1033 : : rtoffset,
1034 : : NUM_EXEC_TLIST(plan));
1035 : 457 : }
1036 : 457 : break;
1037 : : case T_Result:
1038 : : {
1039 : 19754 : Result *splan = (Result *) plan;
1040 : :
1041 : : /*
1042 : : * Result may or may not have a subplan; if not, it's more
1043 : : * like a scan node than an upper node.
1044 : : */
1045 [ + + ]: 19754 : if (splan->plan.lefttree != NULL)
1046 : 1816 : set_upper_references(root, plan, rtoffset);
1047 : : else
1048 : : {
1049 : : /*
1050 : : * The tlist of a childless Result could contain
1051 : : * unresolved ROWID_VAR Vars, in case it's representing a
1052 : : * target relation which is completely empty because of
1053 : : * constraint exclusion. Replace any such Vars by null
1054 : : * constants, as though they'd been resolved for a leaf
1055 : : * scan node that doesn't support them. We could have
1056 : : * fix_scan_expr do this, but since the case is only
1057 : : * expected to occur here, it seems safer to special-case
1058 : : * it here and keep the assertions that ROWID_VARs
1059 : : * shouldn't be seen by fix_scan_expr.
1060 : : *
1061 : : * We also must handle the case where set operations have
1062 : : * been short-circuited resulting in a dummy Result node.
1063 : : * prepunion.c uses varno==0 for the set op targetlist.
1064 : : * See generate_setop_tlist() and generate_setop_tlist().
1065 : : * Here we rewrite these to use varno==1, which is the
1066 : : * varno of the first set-op child. Without this, EXPLAIN
1067 : : * will have trouble displaying targetlists of dummy set
1068 : : * operations.
1069 : : */
1070 [ + + + + : 41906 : foreach(l, splan->plan.targetlist)
+ + ]
1071 : : {
1072 : 23968 : TargetEntry *tle = (TargetEntry *) lfirst(l);
1073 : 23968 : Var *var = (Var *) tle->expr;
1074 : :
1075 [ + - + + ]: 23968 : if (var && IsA(var, Var))
1076 : : {
1077 [ + + ]: 276 : if (var->varno == ROWID_VAR)
1078 : 24 : tle->expr = (Expr *) makeNullConst(var->vartype,
1079 : 12 : var->vartypmod,
1080 : 12 : var->varcollid);
1081 [ + + ]: 264 : else if (var->varno == 0)
1082 : 5 : tle->expr = (Expr *) makeVar(1,
1083 : 5 : var->varattno,
1084 : 5 : var->vartype,
1085 : 5 : var->vartypmod,
1086 : 5 : var->varcollid,
1087 : 5 : var->varlevelsup);
1088 : 276 : }
1089 : 23968 : }
1090 : :
1091 : 17938 : splan->plan.targetlist =
1092 : 17938 : fix_scan_list(root, splan->plan.targetlist,
1093 : : rtoffset, NUM_EXEC_TLIST(plan));
1094 : 17938 : splan->plan.qual =
1095 : 17938 : fix_scan_list(root, splan->plan.qual,
1096 : : rtoffset, NUM_EXEC_QUAL(plan));
1097 : : }
1098 : : /* resconstantqual can't contain any subplan variable refs */
1099 : 19754 : splan->resconstantqual =
1100 : 19754 : fix_scan_expr(root, splan->resconstantqual, rtoffset, 1);
1101 : : /* adjust the relids set */
1102 : 19754 : splan->relids = offset_relid_set(splan->relids, rtoffset);
1103 : 19754 : }
1104 : 19754 : break;
1105 : : case T_ProjectSet:
1106 : 1646 : set_upper_references(root, plan, rtoffset);
1107 : 1646 : break;
1108 : : case T_ModifyTable:
1109 : : {
1110 : 7170 : ModifyTable *splan = (ModifyTable *) plan;
1111 : 7170 : Plan *subplan = outerPlan(splan);
1112 : :
1113 [ + - ]: 7170 : Assert(splan->plan.targetlist == NIL);
1114 [ + - ]: 7170 : Assert(splan->plan.qual == NIL);
1115 : :
1116 : 7170 : splan->withCheckOptionLists =
1117 : 7170 : fix_scan_list(root, splan->withCheckOptionLists,
1118 : : rtoffset, 1);
1119 : :
1120 [ + + ]: 7170 : if (splan->returningLists)
1121 : : {
1122 : 337 : List *newRL = NIL;
1123 : 337 : ListCell *lcrl,
1124 : : *lcrr;
1125 : :
1126 : : /*
1127 : : * Pass each per-resultrel returningList through
1128 : : * set_returning_clause_references().
1129 : : */
1130 [ + - ]: 337 : Assert(list_length(splan->returningLists) == list_length(splan->resultRelations));
1131 [ + - + + : 723 : forboth(lcrl, splan->returningLists,
+ - + + +
+ + + ]
1132 : : lcrr, splan->resultRelations)
1133 : : {
1134 : 386 : List *rlist = (List *) lfirst(lcrl);
1135 : 386 : Index resultrel = lfirst_int(lcrr);
1136 : :
1137 : 772 : rlist = set_returning_clause_references(root,
1138 : 386 : rlist,
1139 : 386 : subplan,
1140 : 386 : resultrel,
1141 : 386 : rtoffset);
1142 : 386 : newRL = lappend(newRL, rlist);
1143 : 386 : }
1144 : 337 : splan->returningLists = newRL;
1145 : :
1146 : : /*
1147 : : * Set up the visible plan targetlist as being the same as
1148 : : * the first RETURNING list. This is mostly for the use
1149 : : * of EXPLAIN; the executor won't execute that targetlist,
1150 : : * although it does use it to prepare the node's result
1151 : : * tuple slot. We postpone this step until here so that
1152 : : * we don't have to do set_returning_clause_references()
1153 : : * twice on identical targetlists.
1154 : : */
1155 : 337 : splan->plan.targetlist = copyObject(linitial(newRL));
1156 : 337 : }
1157 : :
1158 : : /*
1159 : : * We treat ModifyTable with ON CONFLICT as a form of 'pseudo
1160 : : * join', where the inner side is the EXCLUDED tuple.
1161 : : * Therefore use fix_join_expr to setup the relevant variables
1162 : : * to INNER_VAR. We explicitly don't create any OUTER_VARs as
1163 : : * those are already used by RETURNING and it seems better to
1164 : : * be non-conflicting.
1165 : : */
1166 [ + + ]: 7170 : if (splan->onConflictSet)
1167 : : {
1168 : 156 : indexed_tlist *itlist;
1169 : :
1170 : 156 : itlist = build_tlist_index(splan->exclRelTlist);
1171 : :
1172 : 156 : splan->onConflictSet =
1173 : 312 : fix_join_expr(root, splan->onConflictSet,
1174 : 156 : NULL, itlist,
1175 : 156 : linitial_int(splan->resultRelations),
1176 : 156 : rtoffset, NRM_EQUAL, NUM_EXEC_QUAL(plan));
1177 : :
1178 : 156 : splan->onConflictWhere = (Node *)
1179 : 312 : fix_join_expr(root, (List *) splan->onConflictWhere,
1180 : 156 : NULL, itlist,
1181 : 156 : linitial_int(splan->resultRelations),
1182 : 156 : rtoffset, NRM_EQUAL, NUM_EXEC_QUAL(plan));
1183 : :
1184 : 156 : pfree(itlist);
1185 : :
1186 : 156 : splan->exclRelTlist =
1187 : 156 : fix_scan_list(root, splan->exclRelTlist, rtoffset, 1);
1188 : 156 : }
1189 : :
1190 : : /*
1191 : : * The MERGE statement produces the target rows by performing
1192 : : * a right join between the target relation and the source
1193 : : * relation (which could be a plain relation or a subquery).
1194 : : * The INSERT and UPDATE actions of the MERGE statement
1195 : : * require access to the columns from the source relation. We
1196 : : * arrange things so that the source relation attributes are
1197 : : * available as INNER_VAR and the target relation attributes
1198 : : * are available from the scan tuple.
1199 : : */
1200 [ + + ]: 7170 : if (splan->mergeActionLists != NIL)
1201 : : {
1202 : 273 : List *newMJC = NIL;
1203 : 273 : ListCell *lca,
1204 : : *lcj,
1205 : : *lcr;
1206 : :
1207 : : /*
1208 : : * Fix the targetList of individual action nodes so that
1209 : : * the so-called "source relation" Vars are referenced as
1210 : : * INNER_VAR. Note that for this to work correctly during
1211 : : * execution, the ecxt_innertuple must be set to the tuple
1212 : : * obtained by executing the subplan, which is what
1213 : : * constitutes the "source relation".
1214 : : *
1215 : : * We leave the Vars from the result relation (i.e. the
1216 : : * target relation) unchanged i.e. those Vars would be
1217 : : * picked from the scan slot. So during execution, we must
1218 : : * ensure that ecxt_scantuple is setup correctly to refer
1219 : : * to the tuple from the target relation.
1220 : : */
1221 : 273 : indexed_tlist *itlist;
1222 : :
1223 : 273 : itlist = build_tlist_index(subplan->targetlist);
1224 : :
1225 [ + - + + : 589 : forthree(lca, splan->mergeActionLists,
+ - + + +
- + + + +
- + + + ]
1226 : : lcj, splan->mergeJoinConditions,
1227 : : lcr, splan->resultRelations)
1228 : : {
1229 : 316 : List *mergeActionList = lfirst(lca);
1230 : 316 : Node *mergeJoinCondition = lfirst(lcj);
1231 : 316 : Index resultrel = lfirst_int(lcr);
1232 : :
1233 [ + - + + : 824 : foreach(l, mergeActionList)
+ + ]
1234 : : {
1235 : 508 : MergeAction *action = (MergeAction *) lfirst(l);
1236 : :
1237 : : /* Fix targetList of each action. */
1238 : 1016 : action->targetList = fix_join_expr(root,
1239 : 508 : action->targetList,
1240 : 508 : NULL, itlist,
1241 : 508 : resultrel,
1242 : 508 : rtoffset,
1243 : : NRM_EQUAL,
1244 : 508 : NUM_EXEC_TLIST(plan));
1245 : :
1246 : : /* Fix quals too. */
1247 : 1016 : action->qual = (Node *) fix_join_expr(root,
1248 : 508 : (List *) action->qual,
1249 : 508 : NULL, itlist,
1250 : 508 : resultrel,
1251 : 508 : rtoffset,
1252 : : NRM_EQUAL,
1253 : 508 : NUM_EXEC_QUAL(plan));
1254 : 508 : }
1255 : :
1256 : : /* Fix join condition too. */
1257 : 316 : mergeJoinCondition = (Node *)
1258 : 632 : fix_join_expr(root,
1259 : 316 : (List *) mergeJoinCondition,
1260 : 316 : NULL, itlist,
1261 : 316 : resultrel,
1262 : 316 : rtoffset,
1263 : : NRM_EQUAL,
1264 : 316 : NUM_EXEC_QUAL(plan));
1265 : 316 : newMJC = lappend(newMJC, mergeJoinCondition);
1266 : 316 : }
1267 : 273 : splan->mergeJoinConditions = newMJC;
1268 : 273 : }
1269 : :
1270 : 7170 : splan->nominalRelation += rtoffset;
1271 [ + + ]: 7170 : if (splan->rootRelation)
1272 : 370 : splan->rootRelation += rtoffset;
1273 : 7170 : splan->exclRelRTI += rtoffset;
1274 : :
1275 [ + - + + : 14709 : foreach(l, splan->resultRelations)
+ + ]
1276 : : {
1277 : 7539 : lfirst_int(l) += rtoffset;
1278 : 7539 : }
1279 [ + + + + : 7613 : foreach(l, splan->rowMarks)
+ + ]
1280 : : {
1281 : 443 : PlanRowMark *rc = (PlanRowMark *) lfirst(l);
1282 : :
1283 : 443 : rc->rti += rtoffset;
1284 : 443 : rc->prti += rtoffset;
1285 : 443 : }
1286 : :
1287 : : /*
1288 : : * Append this ModifyTable node's final result relation RT
1289 : : * index(es) to the global list for the plan.
1290 : : */
1291 : 7170 : root->glob->resultRelations =
1292 : 14340 : list_concat(root->glob->resultRelations,
1293 : 7170 : splan->resultRelations);
1294 [ + + ]: 7170 : if (splan->rootRelation)
1295 : : {
1296 : 370 : root->glob->resultRelations =
1297 : 740 : lappend_int(root->glob->resultRelations,
1298 : 370 : splan->rootRelation);
1299 : 370 : }
1300 : 7170 : }
1301 : 7170 : break;
1302 : : case T_Append:
1303 : : /* Needs special treatment, see comments below */
1304 : 6170 : return set_append_references(root,
1305 : 3085 : (Append *) plan,
1306 : 3085 : rtoffset);
1307 : : case T_MergeAppend:
1308 : : /* Needs special treatment, see comments below */
1309 : 182 : return set_mergeappend_references(root,
1310 : 91 : (MergeAppend *) plan,
1311 : 91 : rtoffset);
1312 : : case T_RecursiveUnion:
1313 : : /* This doesn't evaluate targetlist or check quals either */
1314 : 73 : set_dummy_tlist_references(plan, rtoffset);
1315 [ + - ]: 73 : Assert(plan->qual == NIL);
1316 : 73 : break;
1317 : : case T_BitmapAnd:
1318 : : {
1319 : 17 : BitmapAnd *splan = (BitmapAnd *) plan;
1320 : :
1321 : : /* BitmapAnd works like Append, but has no tlist */
1322 [ + - ]: 17 : Assert(splan->plan.targetlist == NIL);
1323 [ + - ]: 17 : Assert(splan->plan.qual == NIL);
1324 [ + - + + : 51 : foreach(l, splan->bitmapplans)
+ + ]
1325 : : {
1326 : 68 : lfirst(l) = set_plan_refs(root,
1327 : 34 : (Plan *) lfirst(l),
1328 : 34 : rtoffset);
1329 : 34 : }
1330 : 17 : }
1331 : 17 : break;
1332 : : case T_BitmapOr:
1333 : : {
1334 : 33 : BitmapOr *splan = (BitmapOr *) plan;
1335 : :
1336 : : /* BitmapOr works like Append, but has no tlist */
1337 [ + - ]: 33 : Assert(splan->plan.targetlist == NIL);
1338 [ + - ]: 33 : Assert(splan->plan.qual == NIL);
1339 [ + - + + : 100 : foreach(l, splan->bitmapplans)
+ + ]
1340 : : {
1341 : 134 : lfirst(l) = set_plan_refs(root,
1342 : 67 : (Plan *) lfirst(l),
1343 : 67 : rtoffset);
1344 : 67 : }
1345 : 33 : }
1346 : 33 : break;
1347 : : default:
1348 [ # # # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
1349 : : (int) nodeTag(plan));
1350 : 0 : break;
1351 : : }
1352 : :
1353 : : /*
1354 : : * Now recurse into child plans, if any
1355 : : *
1356 : : * NOTE: it is essential that we recurse into child plans AFTER we set
1357 : : * subplan references in this plan's tlist and quals. If we did the
1358 : : * reference-adjustments bottom-up, then we would fail to match this
1359 : : * plan's var nodes against the already-modified nodes of the children.
1360 : : */
1361 : 112354 : plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
1362 : 112354 : plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
1363 : :
1364 : 112354 : return plan;
1365 : 284379 : }
1366 : :
1367 : : /*
1368 : : * set_indexonlyscan_references
1369 : : * Do set_plan_references processing on an IndexOnlyScan
1370 : : *
1371 : : * This is unlike the handling of a plain IndexScan because we have to
1372 : : * convert Vars referencing the heap into Vars referencing the index.
1373 : : * We can use the fix_upper_expr machinery for that, by working from a
1374 : : * targetlist describing the index columns.
1375 : : */
1376 : : static Plan *
1377 : 1841 : set_indexonlyscan_references(PlannerInfo *root,
1378 : : IndexOnlyScan *plan,
1379 : : int rtoffset)
1380 : : {
1381 : 1841 : indexed_tlist *index_itlist;
1382 : 1841 : List *stripped_indextlist;
1383 : 1841 : ListCell *lc;
1384 : :
1385 : : /*
1386 : : * Vars in the plan node's targetlist, qual, and recheckqual must only
1387 : : * reference columns that the index AM can actually return. To ensure
1388 : : * this, remove non-returnable columns (which are marked as resjunk) from
1389 : : * the indexed tlist. We can just drop them because the indexed_tlist
1390 : : * machinery pays attention to TLE resnos, not physical list position.
1391 : : */
1392 : 1841 : stripped_indextlist = NIL;
1393 [ + - + + : 4011 : foreach(lc, plan->indextlist)
+ + ]
1394 : : {
1395 : 2170 : TargetEntry *indextle = (TargetEntry *) lfirst(lc);
1396 : :
1397 [ + + ]: 2170 : if (!indextle->resjunk)
1398 : 2162 : stripped_indextlist = lappend(stripped_indextlist, indextle);
1399 : 2170 : }
1400 : :
1401 : 1841 : index_itlist = build_tlist_index(stripped_indextlist);
1402 : :
1403 : 1841 : plan->scan.scanrelid += rtoffset;
1404 : 1841 : plan->scan.plan.targetlist = (List *)
1405 : 3682 : fix_upper_expr(root,
1406 : 1841 : (Node *) plan->scan.plan.targetlist,
1407 : 1841 : index_itlist,
1408 : : INDEX_VAR,
1409 : 1841 : rtoffset,
1410 : : NRM_EQUAL,
1411 : 1841 : NUM_EXEC_TLIST((Plan *) plan));
1412 : 1841 : plan->scan.plan.qual = (List *)
1413 : 3682 : fix_upper_expr(root,
1414 : 1841 : (Node *) plan->scan.plan.qual,
1415 : 1841 : index_itlist,
1416 : : INDEX_VAR,
1417 : 1841 : rtoffset,
1418 : : NRM_EQUAL,
1419 : 1841 : NUM_EXEC_QUAL((Plan *) plan));
1420 : 1841 : plan->recheckqual = (List *)
1421 : 3682 : fix_upper_expr(root,
1422 : 1841 : (Node *) plan->recheckqual,
1423 : 1841 : index_itlist,
1424 : : INDEX_VAR,
1425 : 1841 : rtoffset,
1426 : : NRM_EQUAL,
1427 : 1841 : NUM_EXEC_QUAL((Plan *) plan));
1428 : : /* indexqual is already transformed to reference index columns */
1429 : 1841 : plan->indexqual = fix_scan_list(root, plan->indexqual,
1430 : : rtoffset, 1);
1431 : : /* indexorderby is already transformed to reference index columns */
1432 : 1841 : plan->indexorderby = fix_scan_list(root, plan->indexorderby,
1433 : : rtoffset, 1);
1434 : : /* indextlist must NOT be transformed to reference index columns */
1435 : 1841 : plan->indextlist = fix_scan_list(root, plan->indextlist,
1436 : : rtoffset, NUM_EXEC_TLIST((Plan *) plan));
1437 : :
1438 : 1841 : pfree(index_itlist);
1439 : :
1440 : 3682 : return (Plan *) plan;
1441 : 1841 : }
1442 : :
1443 : : /*
1444 : : * set_subqueryscan_references
1445 : : * Do set_plan_references processing on a SubqueryScan
1446 : : *
1447 : : * We try to strip out the SubqueryScan entirely; if we can't, we have
1448 : : * to do the normal processing on it.
1449 : : */
1450 : : static Plan *
1451 : 3572 : set_subqueryscan_references(PlannerInfo *root,
1452 : : SubqueryScan *plan,
1453 : : int rtoffset)
1454 : : {
1455 : 3572 : RelOptInfo *rel;
1456 : 3572 : Plan *result;
1457 : :
1458 : : /* Need to look up the subquery's RelOptInfo, since we need its subroot */
1459 : 3572 : rel = find_base_rel(root, plan->scan.scanrelid);
1460 : :
1461 : : /* Recursively process the subplan */
1462 : 3572 : plan->subplan = set_plan_references(rel->subroot, plan->subplan);
1463 : :
1464 [ + + ]: 3572 : if (trivial_subqueryscan(plan))
1465 : : {
1466 : 2076 : Index scanrelid;
1467 : :
1468 : : /*
1469 : : * We can omit the SubqueryScan node and just pull up the subplan.
1470 : : */
1471 : 2076 : result = clean_up_removed_plan_level((Plan *) plan, plan->subplan);
1472 : :
1473 : : /* Remember that we removed a SubqueryScan */
1474 : 2076 : scanrelid = plan->scan.scanrelid + rtoffset;
1475 : 4152 : record_elided_node(root->glob, plan->subplan->plan_node_id,
1476 : 2076 : T_SubqueryScan, bms_make_singleton(scanrelid));
1477 : 2076 : }
1478 : : else
1479 : : {
1480 : : /*
1481 : : * Keep the SubqueryScan node. We have to do the processing that
1482 : : * set_plan_references would otherwise have done on it. Notice we do
1483 : : * not do set_upper_references() here, because a SubqueryScan will
1484 : : * always have been created with correct references to its subplan's
1485 : : * outputs to begin with.
1486 : : */
1487 : 1496 : plan->scan.scanrelid += rtoffset;
1488 : 1496 : plan->scan.plan.targetlist =
1489 : 1496 : fix_scan_list(root, plan->scan.plan.targetlist,
1490 : : rtoffset, NUM_EXEC_TLIST((Plan *) plan));
1491 : 1496 : plan->scan.plan.qual =
1492 : 1496 : fix_scan_list(root, plan->scan.plan.qual,
1493 : : rtoffset, NUM_EXEC_QUAL((Plan *) plan));
1494 : :
1495 : 1496 : result = (Plan *) plan;
1496 : : }
1497 : :
1498 : 7144 : return result;
1499 : 3572 : }
1500 : :
1501 : : /*
1502 : : * trivial_subqueryscan
1503 : : * Detect whether a SubqueryScan can be deleted from the plan tree.
1504 : : *
1505 : : * We can delete it if it has no qual to check and the targetlist just
1506 : : * regurgitates the output of the child plan.
1507 : : *
1508 : : * This can be called from mark_async_capable_plan(), a helper function for
1509 : : * create_append_plan(), before set_subqueryscan_references(), to determine
1510 : : * triviality of a SubqueryScan that is a child of an Append node. So we
1511 : : * cache the result in the SubqueryScan node to avoid repeated computation.
1512 : : *
1513 : : * Note: when called from mark_async_capable_plan(), we determine the result
1514 : : * before running finalize_plan() on the SubqueryScan node (if needed) and
1515 : : * set_plan_references() on the subplan tree, but this would be safe, because
1516 : : * 1) finalize_plan() doesn't modify the tlist or quals for the SubqueryScan
1517 : : * node (or that for any plan node in the subplan tree), and
1518 : : * 2) set_plan_references() modifies the tlist for every plan node in the
1519 : : * subplan tree, but keeps const/resjunk columns as const/resjunk ones and
1520 : : * preserves the length and order of the tlist, and
1521 : : * 3) set_plan_references() might delete the topmost plan node like an Append
1522 : : * or MergeAppend from the subplan tree and pull up the child plan node,
1523 : : * but in that case, the tlist for the child plan node exactly matches the
1524 : : * parent.
1525 : : */
1526 : : bool
1527 : 5140 : trivial_subqueryscan(SubqueryScan *plan)
1528 : : {
1529 : 5140 : int attrno;
1530 : 5140 : ListCell *lp,
1531 : : *lc;
1532 : :
1533 : : /* We might have detected this already; in which case reuse the result */
1534 [ + + ]: 5140 : if (plan->scanstatus == SUBQUERY_SCAN_TRIVIAL)
1535 : 549 : return true;
1536 [ + + ]: 4591 : if (plan->scanstatus == SUBQUERY_SCAN_NONTRIVIAL)
1537 : 1019 : return false;
1538 [ + - ]: 3572 : Assert(plan->scanstatus == SUBQUERY_SCAN_UNKNOWN);
1539 : : /* Initially, mark the SubqueryScan as non-deletable from the plan tree */
1540 : 3572 : plan->scanstatus = SUBQUERY_SCAN_NONTRIVIAL;
1541 : :
1542 [ + + ]: 3572 : if (plan->scan.plan.qual != NIL)
1543 : 119 : return false;
1544 : :
1545 [ + + + + ]: 6906 : if (list_length(plan->scan.plan.targetlist) !=
1546 : 3453 : list_length(plan->subplan->targetlist))
1547 : 276 : return false; /* tlists not same length */
1548 : :
1549 : 3177 : attrno = 1;
1550 [ + + + + : 10336 : forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
+ + + + +
+ + + +
+ ]
1551 : : {
1552 : 7159 : TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1553 : 7159 : TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1554 : :
1555 [ + + ]: 7159 : if (ptle->resjunk != ctle->resjunk)
1556 : 4 : return false; /* tlist doesn't match junk status */
1557 : :
1558 : : /*
1559 : : * We accept either a Var referencing the corresponding element of the
1560 : : * subplan tlist, or a Const equaling the subplan element. See
1561 : : * generate_setop_tlist() for motivation.
1562 : : */
1563 [ + - + + ]: 7155 : if (ptle->expr && IsA(ptle->expr, Var))
1564 : : {
1565 : 5878 : Var *var = (Var *) ptle->expr;
1566 : :
1567 [ - + ]: 5878 : Assert(var->varno == plan->scan.scanrelid);
1568 [ - + ]: 5878 : Assert(var->varlevelsup == 0);
1569 [ + + ]: 5878 : if (var->varattno != attrno)
1570 : 32 : return false; /* out of order */
1571 [ + + ]: 5878 : }
1572 [ + - + + ]: 1277 : else if (ptle->expr && IsA(ptle->expr, Const))
1573 : : {
1574 [ + + ]: 1223 : if (!equal(ptle->expr, ctle->expr))
1575 : 1011 : return false;
1576 : 212 : }
1577 : : else
1578 : 54 : return false;
1579 : :
1580 : 6058 : attrno++;
1581 [ + + ]: 7159 : }
1582 : :
1583 : : /* Re-mark the SubqueryScan as deletable from the plan tree */
1584 : 2076 : plan->scanstatus = SUBQUERY_SCAN_TRIVIAL;
1585 : :
1586 : 2076 : return true;
1587 : 5140 : }
1588 : :
1589 : : /*
1590 : : * clean_up_removed_plan_level
1591 : : * Do necessary cleanup when we strip out a SubqueryScan, Append, etc
1592 : : *
1593 : : * We are dropping the "parent" plan in favor of returning just its "child".
1594 : : * A few small tweaks are needed.
1595 : : */
1596 : : static Plan *
1597 : 2648 : clean_up_removed_plan_level(Plan *parent, Plan *child)
1598 : : {
1599 : : /*
1600 : : * We have to be sure we don't lose any initplans, so move any that were
1601 : : * attached to the parent plan to the child. If any are parallel-unsafe,
1602 : : * the child is no longer parallel-safe. As a cosmetic matter, also add
1603 : : * the initplans' run costs to the child's costs.
1604 : : */
1605 [ + + ]: 2648 : if (parent->initPlan)
1606 : : {
1607 : 7 : Cost initplan_cost;
1608 : 7 : bool unsafe_initplans;
1609 : :
1610 : 7 : SS_compute_initplan_cost(parent->initPlan,
1611 : : &initplan_cost, &unsafe_initplans);
1612 : 7 : child->startup_cost += initplan_cost;
1613 : 7 : child->total_cost += initplan_cost;
1614 [ + + ]: 7 : if (unsafe_initplans)
1615 : 3 : child->parallel_safe = false;
1616 : :
1617 : : /*
1618 : : * Attach plans this way so that parent's initplans are processed
1619 : : * before any pre-existing initplans of the child. Probably doesn't
1620 : : * matter, but let's preserve the ordering just in case.
1621 : : */
1622 : 14 : child->initPlan = list_concat(parent->initPlan,
1623 : 7 : child->initPlan);
1624 : 7 : }
1625 : :
1626 : : /*
1627 : : * We also have to transfer the parent's column labeling info into the
1628 : : * child, else columns sent to client will be improperly labeled if this
1629 : : * is the topmost plan level. resjunk and so on may be important too.
1630 : : */
1631 : 2648 : apply_tlist_labeling(child->targetlist, parent->targetlist);
1632 : :
1633 : 2648 : return child;
1634 : : }
1635 : :
1636 : : /*
1637 : : * set_foreignscan_references
1638 : : * Do set_plan_references processing on a ForeignScan
1639 : : */
1640 : : static void
1641 : 0 : set_foreignscan_references(PlannerInfo *root,
1642 : : ForeignScan *fscan,
1643 : : int rtoffset)
1644 : : {
1645 : : /* Adjust scanrelid if it's valid */
1646 [ # # ]: 0 : if (fscan->scan.scanrelid > 0)
1647 : 0 : fscan->scan.scanrelid += rtoffset;
1648 : :
1649 [ # # # # ]: 0 : if (fscan->fdw_scan_tlist != NIL || fscan->scan.scanrelid == 0)
1650 : : {
1651 : : /*
1652 : : * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals to reference
1653 : : * foreign scan tuple
1654 : : */
1655 : 0 : indexed_tlist *itlist = build_tlist_index(fscan->fdw_scan_tlist);
1656 : :
1657 : 0 : fscan->scan.plan.targetlist = (List *)
1658 : 0 : fix_upper_expr(root,
1659 : 0 : (Node *) fscan->scan.plan.targetlist,
1660 : 0 : itlist,
1661 : : INDEX_VAR,
1662 : 0 : rtoffset,
1663 : : NRM_EQUAL,
1664 : 0 : NUM_EXEC_TLIST((Plan *) fscan));
1665 : 0 : fscan->scan.plan.qual = (List *)
1666 : 0 : fix_upper_expr(root,
1667 : 0 : (Node *) fscan->scan.plan.qual,
1668 : 0 : itlist,
1669 : : INDEX_VAR,
1670 : 0 : rtoffset,
1671 : : NRM_EQUAL,
1672 : 0 : NUM_EXEC_QUAL((Plan *) fscan));
1673 : 0 : fscan->fdw_exprs = (List *)
1674 : 0 : fix_upper_expr(root,
1675 : 0 : (Node *) fscan->fdw_exprs,
1676 : 0 : itlist,
1677 : : INDEX_VAR,
1678 : 0 : rtoffset,
1679 : : NRM_EQUAL,
1680 : 0 : NUM_EXEC_QUAL((Plan *) fscan));
1681 : 0 : fscan->fdw_recheck_quals = (List *)
1682 : 0 : fix_upper_expr(root,
1683 : 0 : (Node *) fscan->fdw_recheck_quals,
1684 : 0 : itlist,
1685 : : INDEX_VAR,
1686 : 0 : rtoffset,
1687 : : NRM_EQUAL,
1688 : 0 : NUM_EXEC_QUAL((Plan *) fscan));
1689 : 0 : pfree(itlist);
1690 : : /* fdw_scan_tlist itself just needs fix_scan_list() adjustments */
1691 : 0 : fscan->fdw_scan_tlist =
1692 : 0 : fix_scan_list(root, fscan->fdw_scan_tlist,
1693 : : rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1694 : 0 : }
1695 : : else
1696 : : {
1697 : : /*
1698 : : * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals in the standard
1699 : : * way
1700 : : */
1701 : 0 : fscan->scan.plan.targetlist =
1702 : 0 : fix_scan_list(root, fscan->scan.plan.targetlist,
1703 : : rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1704 : 0 : fscan->scan.plan.qual =
1705 : 0 : fix_scan_list(root, fscan->scan.plan.qual,
1706 : : rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1707 : 0 : fscan->fdw_exprs =
1708 : 0 : fix_scan_list(root, fscan->fdw_exprs,
1709 : : rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1710 : 0 : fscan->fdw_recheck_quals =
1711 : 0 : fix_scan_list(root, fscan->fdw_recheck_quals,
1712 : : rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1713 : : }
1714 : :
1715 : 0 : fscan->fs_relids = offset_relid_set(fscan->fs_relids, rtoffset);
1716 : 0 : fscan->fs_base_relids = offset_relid_set(fscan->fs_base_relids, rtoffset);
1717 : :
1718 : : /* Adjust resultRelation if it's valid */
1719 [ # # ]: 0 : if (fscan->resultRelation > 0)
1720 : 0 : fscan->resultRelation += rtoffset;
1721 : 0 : }
1722 : :
1723 : : /*
1724 : : * set_customscan_references
1725 : : * Do set_plan_references processing on a CustomScan
1726 : : */
1727 : : static void
1728 : 0 : set_customscan_references(PlannerInfo *root,
1729 : : CustomScan *cscan,
1730 : : int rtoffset)
1731 : : {
1732 : 0 : ListCell *lc;
1733 : :
1734 : : /* Adjust scanrelid if it's valid */
1735 [ # # ]: 0 : if (cscan->scan.scanrelid > 0)
1736 : 0 : cscan->scan.scanrelid += rtoffset;
1737 : :
1738 [ # # # # ]: 0 : if (cscan->custom_scan_tlist != NIL || cscan->scan.scanrelid == 0)
1739 : : {
1740 : : /* Adjust tlist, qual, custom_exprs to reference custom scan tuple */
1741 : 0 : indexed_tlist *itlist = build_tlist_index(cscan->custom_scan_tlist);
1742 : :
1743 : 0 : cscan->scan.plan.targetlist = (List *)
1744 : 0 : fix_upper_expr(root,
1745 : 0 : (Node *) cscan->scan.plan.targetlist,
1746 : 0 : itlist,
1747 : : INDEX_VAR,
1748 : 0 : rtoffset,
1749 : : NRM_EQUAL,
1750 : 0 : NUM_EXEC_TLIST((Plan *) cscan));
1751 : 0 : cscan->scan.plan.qual = (List *)
1752 : 0 : fix_upper_expr(root,
1753 : 0 : (Node *) cscan->scan.plan.qual,
1754 : 0 : itlist,
1755 : : INDEX_VAR,
1756 : 0 : rtoffset,
1757 : : NRM_EQUAL,
1758 : 0 : NUM_EXEC_QUAL((Plan *) cscan));
1759 : 0 : cscan->custom_exprs = (List *)
1760 : 0 : fix_upper_expr(root,
1761 : 0 : (Node *) cscan->custom_exprs,
1762 : 0 : itlist,
1763 : : INDEX_VAR,
1764 : 0 : rtoffset,
1765 : : NRM_EQUAL,
1766 : 0 : NUM_EXEC_QUAL((Plan *) cscan));
1767 : 0 : pfree(itlist);
1768 : : /* custom_scan_tlist itself just needs fix_scan_list() adjustments */
1769 : 0 : cscan->custom_scan_tlist =
1770 : 0 : fix_scan_list(root, cscan->custom_scan_tlist,
1771 : : rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1772 : 0 : }
1773 : : else
1774 : : {
1775 : : /* Adjust tlist, qual, custom_exprs in the standard way */
1776 : 0 : cscan->scan.plan.targetlist =
1777 : 0 : fix_scan_list(root, cscan->scan.plan.targetlist,
1778 : : rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1779 : 0 : cscan->scan.plan.qual =
1780 : 0 : fix_scan_list(root, cscan->scan.plan.qual,
1781 : : rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1782 : 0 : cscan->custom_exprs =
1783 : 0 : fix_scan_list(root, cscan->custom_exprs,
1784 : : rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1785 : : }
1786 : :
1787 : : /* Adjust child plan-nodes recursively, if needed */
1788 [ # # # # : 0 : foreach(lc, cscan->custom_plans)
# # ]
1789 : : {
1790 : 0 : lfirst(lc) = set_plan_refs(root, (Plan *) lfirst(lc), rtoffset);
1791 : 0 : }
1792 : :
1793 : 0 : cscan->custom_relids = offset_relid_set(cscan->custom_relids, rtoffset);
1794 : 0 : }
1795 : :
1796 : : /*
1797 : : * register_partpruneinfo
1798 : : * Subroutine for set_append_references and set_mergeappend_references
1799 : : *
1800 : : * Add the PartitionPruneInfo from root->partPruneInfos at the given index
1801 : : * into PlannerGlobal->partPruneInfos and return its index there.
1802 : : *
1803 : : * Also update the RT indexes present in PartitionedRelPruneInfos to add the
1804 : : * offset.
1805 : : *
1806 : : * Finally, if there are initial pruning steps, add the RT indexes of the
1807 : : * leaf partitions to the set of relations that are prunable at execution
1808 : : * startup time.
1809 : : */
1810 : : static int
1811 : 91 : register_partpruneinfo(PlannerInfo *root, int part_prune_index, int rtoffset)
1812 : : {
1813 : 91 : PlannerGlobal *glob = root->glob;
1814 : 91 : PartitionPruneInfo *pinfo;
1815 : 91 : ListCell *l;
1816 : :
1817 [ + - ]: 91 : Assert(part_prune_index >= 0 &&
1818 : : part_prune_index < list_length(root->partPruneInfos));
1819 : 91 : pinfo = list_nth_node(PartitionPruneInfo, root->partPruneInfos,
1820 : : part_prune_index);
1821 : :
1822 : 91 : pinfo->relids = offset_relid_set(pinfo->relids, rtoffset);
1823 [ + - + + : 184 : foreach(l, pinfo->prune_infos)
+ + ]
1824 : : {
1825 : 93 : List *prune_infos = lfirst(l);
1826 : 93 : ListCell *l2;
1827 : :
1828 [ + - + + : 258 : foreach(l2, prune_infos)
+ + ]
1829 : : {
1830 : 165 : PartitionedRelPruneInfo *prelinfo = lfirst(l2);
1831 : 165 : int i;
1832 : :
1833 : 165 : prelinfo->rtindex += rtoffset;
1834 : 165 : prelinfo->initial_pruning_steps =
1835 : 165 : fix_scan_list(root, prelinfo->initial_pruning_steps,
1836 : : rtoffset, 1);
1837 : 165 : prelinfo->exec_pruning_steps =
1838 : 165 : fix_scan_list(root, prelinfo->exec_pruning_steps,
1839 : : rtoffset, 1);
1840 : :
1841 [ + + ]: 652 : for (i = 0; i < prelinfo->nparts; i++)
1842 : : {
1843 : : /*
1844 : : * Non-leaf partitions and partitions that do not have a
1845 : : * subplan are not included in this map as mentioned in
1846 : : * make_partitionedrel_pruneinfo().
1847 : : */
1848 [ + + ]: 487 : if (prelinfo->leafpart_rti_map[i])
1849 : : {
1850 : 393 : prelinfo->leafpart_rti_map[i] += rtoffset;
1851 [ + + ]: 393 : if (prelinfo->initial_pruning_steps)
1852 : 230 : glob->prunableRelids = bms_add_member(glob->prunableRelids,
1853 : 115 : prelinfo->leafpart_rti_map[i]);
1854 : 393 : }
1855 : 487 : }
1856 : 165 : }
1857 : 93 : }
1858 : :
1859 : 91 : glob->partPruneInfos = lappend(glob->partPruneInfos, pinfo);
1860 : :
1861 : 182 : return list_length(glob->partPruneInfos) - 1;
1862 : 91 : }
1863 : :
1864 : : /*
1865 : : * set_append_references
1866 : : * Do set_plan_references processing on an Append
1867 : : *
1868 : : * We try to strip out the Append entirely; if we can't, we have
1869 : : * to do the normal processing on it.
1870 : : */
1871 : : static Plan *
1872 : 3085 : set_append_references(PlannerInfo *root,
1873 : : Append *aplan,
1874 : : int rtoffset)
1875 : : {
1876 : 3085 : ListCell *l;
1877 : :
1878 : : /*
1879 : : * Append, like Sort et al, doesn't actually evaluate its targetlist or
1880 : : * check quals. If it's got exactly one child plan, then it's not doing
1881 : : * anything useful at all, and we can strip it out.
1882 : : */
1883 [ + - ]: 3085 : Assert(aplan->plan.qual == NIL);
1884 : :
1885 : : /* First, we gotta recurse on the children */
1886 [ + - + + : 10809 : foreach(l, aplan->appendplans)
+ + ]
1887 : : {
1888 : 7724 : lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1889 : 7724 : }
1890 : :
1891 : : /*
1892 : : * See if it's safe to get rid of the Append entirely. For this to be
1893 : : * safe, there must be only one child plan and that child plan's parallel
1894 : : * awareness must match the Append's. The reason for the latter is that
1895 : : * if the Append is parallel aware and the child is not, then the calling
1896 : : * plan may execute the non-parallel aware child multiple times. (If you
1897 : : * change these rules, update create_append_path to match.)
1898 : : */
1899 [ + + ]: 3085 : if (list_length(aplan->appendplans) == 1)
1900 : : {
1901 : 572 : Plan *p = (Plan *) linitial(aplan->appendplans);
1902 : :
1903 [ + - ]: 572 : if (p->parallel_aware == aplan->plan.parallel_aware)
1904 : : {
1905 : 572 : Plan *result;
1906 : :
1907 : 572 : result = clean_up_removed_plan_level((Plan *) aplan, p);
1908 : :
1909 : : /* Remember that we removed an Append */
1910 : 1144 : record_elided_node(root->glob, p->plan_node_id, T_Append,
1911 : 572 : offset_relid_set(aplan->apprelids, rtoffset));
1912 : :
1913 : 572 : return result;
1914 : 572 : }
1915 [ + - ]: 572 : }
1916 : :
1917 : : /*
1918 : : * Otherwise, clean up the Append as needed. It's okay to do this after
1919 : : * recursing to the children, because set_dummy_tlist_references doesn't
1920 : : * look at those.
1921 : : */
1922 : 2513 : set_dummy_tlist_references((Plan *) aplan, rtoffset);
1923 : :
1924 : 2513 : aplan->apprelids = offset_relid_set(aplan->apprelids, rtoffset);
1925 : :
1926 : : /*
1927 : : * Add PartitionPruneInfo, if any, to PlannerGlobal and update the index.
1928 : : * Also update the RT indexes present in it to add the offset.
1929 : : */
1930 [ + + ]: 2513 : if (aplan->part_prune_index >= 0)
1931 : 85 : aplan->part_prune_index =
1932 : 85 : register_partpruneinfo(root, aplan->part_prune_index, rtoffset);
1933 : :
1934 : : /* We don't need to recurse to lefttree or righttree ... */
1935 [ + - ]: 2513 : Assert(aplan->plan.lefttree == NULL);
1936 [ + - ]: 2513 : Assert(aplan->plan.righttree == NULL);
1937 : :
1938 : 2513 : return (Plan *) aplan;
1939 : 3085 : }
1940 : :
1941 : : /*
1942 : : * set_mergeappend_references
1943 : : * Do set_plan_references processing on a MergeAppend
1944 : : *
1945 : : * We try to strip out the MergeAppend entirely; if we can't, we have
1946 : : * to do the normal processing on it.
1947 : : */
1948 : : static Plan *
1949 : 91 : set_mergeappend_references(PlannerInfo *root,
1950 : : MergeAppend *mplan,
1951 : : int rtoffset)
1952 : : {
1953 : 91 : ListCell *l;
1954 : :
1955 : : /*
1956 : : * MergeAppend, like Sort et al, doesn't actually evaluate its targetlist
1957 : : * or check quals. If it's got exactly one child plan, then it's not
1958 : : * doing anything useful at all, and we can strip it out.
1959 : : */
1960 [ + - ]: 91 : Assert(mplan->plan.qual == NIL);
1961 : :
1962 : : /* First, we gotta recurse on the children */
1963 [ + - + + : 349 : foreach(l, mplan->mergeplans)
+ + ]
1964 : : {
1965 : 258 : lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1966 : 258 : }
1967 : :
1968 : : /*
1969 : : * See if it's safe to get rid of the MergeAppend entirely. For this to
1970 : : * be safe, there must be only one child plan and that child plan's
1971 : : * parallel awareness must match the MergeAppend's. The reason for the
1972 : : * latter is that if the MergeAppend is parallel aware and the child is
1973 : : * not, then the calling plan may execute the non-parallel aware child
1974 : : * multiple times. (If you change these rules, update
1975 : : * create_merge_append_path to match.)
1976 : : */
1977 [ + - ]: 91 : if (list_length(mplan->mergeplans) == 1)
1978 : : {
1979 : 0 : Plan *p = (Plan *) linitial(mplan->mergeplans);
1980 : :
1981 [ # # ]: 0 : if (p->parallel_aware == mplan->plan.parallel_aware)
1982 : : {
1983 : 0 : Plan *result;
1984 : :
1985 : 0 : result = clean_up_removed_plan_level((Plan *) mplan, p);
1986 : :
1987 : : /* Remember that we removed a MergeAppend */
1988 : 0 : record_elided_node(root->glob, p->plan_node_id, T_MergeAppend,
1989 : 0 : offset_relid_set(mplan->apprelids, rtoffset));
1990 : :
1991 : 0 : return result;
1992 : 0 : }
1993 [ # # ]: 0 : }
1994 : :
1995 : : /*
1996 : : * Otherwise, clean up the MergeAppend as needed. It's okay to do this
1997 : : * after recursing to the children, because set_dummy_tlist_references
1998 : : * doesn't look at those.
1999 : : */
2000 : 91 : set_dummy_tlist_references((Plan *) mplan, rtoffset);
2001 : :
2002 : 91 : mplan->apprelids = offset_relid_set(mplan->apprelids, rtoffset);
2003 : :
2004 : : /*
2005 : : * Add PartitionPruneInfo, if any, to PlannerGlobal and update the index.
2006 : : * Also update the RT indexes present in it to add the offset.
2007 : : */
2008 [ + + ]: 91 : if (mplan->part_prune_index >= 0)
2009 : 6 : mplan->part_prune_index =
2010 : 6 : register_partpruneinfo(root, mplan->part_prune_index, rtoffset);
2011 : :
2012 : : /* We don't need to recurse to lefttree or righttree ... */
2013 [ + - ]: 91 : Assert(mplan->plan.lefttree == NULL);
2014 [ + - ]: 91 : Assert(mplan->plan.righttree == NULL);
2015 : :
2016 : 91 : return (Plan *) mplan;
2017 : 91 : }
2018 : :
2019 : : /*
2020 : : * set_hash_references
2021 : : * Do set_plan_references processing on a Hash node
2022 : : */
2023 : : static void
2024 : 3335 : set_hash_references(PlannerInfo *root, Plan *plan, int rtoffset)
2025 : : {
2026 : 3335 : Hash *hplan = (Hash *) plan;
2027 : 3335 : Plan *outer_plan = plan->lefttree;
2028 : 3335 : indexed_tlist *outer_itlist;
2029 : :
2030 : : /*
2031 : : * Hash's hashkeys are used when feeding tuples into the hashtable,
2032 : : * therefore have them reference Hash's outer plan (which itself is the
2033 : : * inner plan of the HashJoin).
2034 : : */
2035 : 3335 : outer_itlist = build_tlist_index(outer_plan->targetlist);
2036 : 3335 : hplan->hashkeys = (List *)
2037 : 6670 : fix_upper_expr(root,
2038 : 3335 : (Node *) hplan->hashkeys,
2039 : 3335 : outer_itlist,
2040 : : OUTER_VAR,
2041 : 3335 : rtoffset,
2042 : : NRM_EQUAL,
2043 : 3335 : NUM_EXEC_QUAL(plan));
2044 : :
2045 : : /* Hash doesn't project */
2046 : 3335 : set_dummy_tlist_references(plan, rtoffset);
2047 : :
2048 : : /* Hash nodes don't have their own quals */
2049 [ + - ]: 3335 : Assert(plan->qual == NIL);
2050 : 3335 : }
2051 : :
2052 : : /*
2053 : : * offset_relid_set
2054 : : * Apply rtoffset to the members of a Relids set.
2055 : : */
2056 : : static Relids
2057 : 23021 : offset_relid_set(Relids relids, int rtoffset)
2058 : : {
2059 : 23021 : Relids result = NULL;
2060 : 23021 : int rtindex;
2061 : :
2062 : : /* If there's no offset to apply, we needn't recompute the value */
2063 [ + + ]: 23021 : if (rtoffset == 0)
2064 : 20193 : return relids;
2065 : 2828 : rtindex = -1;
2066 [ + + ]: 4057 : while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
2067 : 1229 : result = bms_add_member(result, rtindex + rtoffset);
2068 : 2828 : return result;
2069 : 23021 : }
2070 : :
2071 : : /*
2072 : : * copyVar
2073 : : * Copy a Var node.
2074 : : *
2075 : : * fix_scan_expr and friends do this enough times that it's worth having
2076 : : * a bespoke routine instead of using the generic copyObject() function.
2077 : : */
2078 : : static inline Var *
2079 : 204895 : copyVar(Var *var)
2080 : : {
2081 : 204895 : Var *newvar = palloc_object(Var);
2082 : :
2083 : 204895 : *newvar = *var;
2084 : 409790 : return newvar;
2085 : 204895 : }
2086 : :
2087 : : /*
2088 : : * fix_expr_common
2089 : : * Do generic set_plan_references processing on an expression node
2090 : : *
2091 : : * This is code that is common to all variants of expression-fixing.
2092 : : * We must look up operator opcode info for OpExpr and related nodes,
2093 : : * add OIDs from regclass Const nodes into root->glob->relationOids, and
2094 : : * add PlanInvalItems for user-defined functions into root->glob->invalItems.
2095 : : * We also fill in column index lists for GROUPING() expressions.
2096 : : *
2097 : : * We assume it's okay to update opcode info in-place. So this could possibly
2098 : : * scribble on the planner's input data structures, but it's OK.
2099 : : */
2100 : : static void
2101 : 1329226 : fix_expr_common(PlannerInfo *root, Node *node)
2102 : : {
2103 : : /* We assume callers won't call us on a NULL pointer */
2104 [ + + ]: 1329226 : if (IsA(node, Aggref))
2105 : : {
2106 : 15094 : record_plan_function_dependency(root,
2107 : 7547 : ((Aggref *) node)->aggfnoid);
2108 : 7547 : }
2109 [ + + ]: 1321679 : else if (IsA(node, WindowFunc))
2110 : : {
2111 : 1292 : record_plan_function_dependency(root,
2112 : 646 : ((WindowFunc *) node)->winfnoid);
2113 : 646 : }
2114 [ + + ]: 1321033 : else if (IsA(node, FuncExpr))
2115 : : {
2116 : 56410 : record_plan_function_dependency(root,
2117 : 28205 : ((FuncExpr *) node)->funcid);
2118 : 28205 : }
2119 [ + + ]: 1292828 : else if (IsA(node, OpExpr))
2120 : : {
2121 : 84385 : set_opfuncid((OpExpr *) node);
2122 : 168770 : record_plan_function_dependency(root,
2123 : 84385 : ((OpExpr *) node)->opfuncid);
2124 : 84385 : }
2125 [ + + ]: 1208443 : else if (IsA(node, DistinctExpr))
2126 : : {
2127 : 38 : set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
2128 : 76 : record_plan_function_dependency(root,
2129 : 38 : ((DistinctExpr *) node)->opfuncid);
2130 : 38 : }
2131 [ + + ]: 1208405 : else if (IsA(node, NullIfExpr))
2132 : : {
2133 : 21 : set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
2134 : 42 : record_plan_function_dependency(root,
2135 : 21 : ((NullIfExpr *) node)->opfuncid);
2136 : 21 : }
2137 [ + + ]: 1208384 : else if (IsA(node, ScalarArrayOpExpr))
2138 : : {
2139 : 4413 : ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
2140 : :
2141 : 4413 : set_sa_opfuncid(saop);
2142 : 4413 : record_plan_function_dependency(root, saop->opfuncid);
2143 : :
2144 [ + + ]: 4413 : if (OidIsValid(saop->hashfuncid))
2145 : 25 : record_plan_function_dependency(root, saop->hashfuncid);
2146 : :
2147 [ + + ]: 4413 : if (OidIsValid(saop->negfuncid))
2148 : 11 : record_plan_function_dependency(root, saop->negfuncid);
2149 : 4413 : }
2150 [ + + ]: 1203971 : else if (IsA(node, Const))
2151 : : {
2152 : 135432 : Const *con = (Const *) node;
2153 : :
2154 : : /* Check for regclass reference */
2155 [ + + + + ]: 135432 : if (ISREGCLASSCONST(con))
2156 : 20093 : root->glob->relationOids =
2157 : 40186 : lappend_oid(root->glob->relationOids,
2158 : 20093 : DatumGetObjectId(con->constvalue));
2159 : 135432 : }
2160 [ + + ]: 1068539 : else if (IsA(node, GroupingFunc))
2161 : : {
2162 : 57 : GroupingFunc *g = (GroupingFunc *) node;
2163 : 57 : AttrNumber *grouping_map = root->grouping_map;
2164 : :
2165 : : /* If there are no grouping sets, we don't need this. */
2166 : :
2167 [ + + + - ]: 57 : Assert(grouping_map || g->cols == NIL);
2168 : :
2169 [ + + ]: 57 : if (grouping_map)
2170 : : {
2171 : 43 : ListCell *lc;
2172 : 43 : List *cols = NIL;
2173 : :
2174 [ + - + + : 113 : foreach(lc, g->refs)
+ + ]
2175 : : {
2176 : 70 : cols = lappend_int(cols, grouping_map[lfirst_int(lc)]);
2177 : 70 : }
2178 : :
2179 [ - + # # ]: 43 : Assert(!g->cols || equal(cols, g->cols));
2180 : :
2181 [ - + ]: 43 : if (!g->cols)
2182 : 43 : g->cols = cols;
2183 : 43 : }
2184 : 57 : }
2185 : 1329226 : }
2186 : :
2187 : : /*
2188 : : * fix_param_node
2189 : : * Do set_plan_references processing on a Param
2190 : : *
2191 : : * If it's a PARAM_MULTIEXPR, replace it with the appropriate Param from
2192 : : * root->multiexpr_params; otherwise no change is needed.
2193 : : * Just for paranoia's sake, we make a copy of the node in either case.
2194 : : */
2195 : : static Node *
2196 : 12987 : fix_param_node(PlannerInfo *root, Param *p)
2197 : : {
2198 [ + + ]: 12987 : if (p->paramkind == PARAM_MULTIEXPR)
2199 : : {
2200 : 46 : int subqueryid = p->paramid >> 16;
2201 : 46 : int colno = p->paramid & 0xFFFF;
2202 : 46 : List *params;
2203 : :
2204 [ + - ]: 46 : if (subqueryid <= 0 ||
2205 : 46 : subqueryid > list_length(root->multiexpr_params))
2206 [ # # # # ]: 0 : elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
2207 : 46 : params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
2208 [ + - ]: 46 : if (colno <= 0 || colno > list_length(params))
2209 [ # # # # ]: 0 : elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
2210 : 46 : return copyObject(list_nth(params, colno - 1));
2211 : 46 : }
2212 : 12941 : return (Node *) copyObject(p);
2213 : 12987 : }
2214 : :
2215 : : /*
2216 : : * fix_alternative_subplan
2217 : : * Do set_plan_references processing on an AlternativeSubPlan
2218 : : *
2219 : : * Choose one of the alternative implementations and return just that one,
2220 : : * discarding the rest of the AlternativeSubPlan structure.
2221 : : * Note: caller must still recurse into the result!
2222 : : *
2223 : : * We don't make any attempt to fix up cost estimates in the parent plan
2224 : : * node or higher-level nodes.
2225 : : */
2226 : : static Node *
2227 : 217 : fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan,
2228 : : double num_exec)
2229 : : {
2230 : 217 : SubPlan *bestplan = NULL;
2231 : 217 : Cost bestcost = 0;
2232 : 217 : ListCell *lc;
2233 : :
2234 : : /*
2235 : : * Compute the estimated cost of each subplan assuming num_exec
2236 : : * executions, and keep the cheapest one. In event of exact equality of
2237 : : * estimates, we prefer the later plan; this is a bit arbitrary, but in
2238 : : * current usage it biases us to break ties against fast-start subplans.
2239 : : */
2240 [ + - ]: 217 : Assert(asplan->subplans != NIL);
2241 : :
2242 [ + - + + : 651 : foreach(lc, asplan->subplans)
+ + ]
2243 : : {
2244 : 434 : SubPlan *curplan = (SubPlan *) lfirst(lc);
2245 : 434 : Cost curcost;
2246 : :
2247 : 434 : curcost = curplan->startup_cost + num_exec * curplan->per_call_cost;
2248 [ + + + + ]: 434 : if (bestplan == NULL || curcost <= bestcost)
2249 : : {
2250 : 253 : bestplan = curplan;
2251 : 253 : bestcost = curcost;
2252 : 253 : }
2253 : :
2254 : : /* Also mark all subplans that are in AlternativeSubPlans */
2255 : 434 : root->isAltSubplan[curplan->plan_id - 1] = true;
2256 : 434 : }
2257 : :
2258 : : /* Mark the subplan we selected */
2259 : 217 : root->isUsedSubplan[bestplan->plan_id - 1] = true;
2260 : :
2261 : 434 : return (Node *) bestplan;
2262 : 217 : }
2263 : :
2264 : : /*
2265 : : * fix_scan_expr
2266 : : * Do set_plan_references processing on a scan-level expression
2267 : : *
2268 : : * This consists of incrementing all Vars' varnos by rtoffset,
2269 : : * replacing PARAM_MULTIEXPR Params, expanding PlaceHolderVars,
2270 : : * replacing Aggref nodes that should be replaced by initplan output Params,
2271 : : * choosing the best implementation for AlternativeSubPlans,
2272 : : * looking up operator opcode info for OpExpr and related nodes,
2273 : : * and adding OIDs from regclass Const nodes into root->glob->relationOids.
2274 : : *
2275 : : * 'node': the expression to be modified
2276 : : * 'rtoffset': how much to increment varnos by
2277 : : * 'num_exec': estimated number of executions of expression
2278 : : *
2279 : : * The expression tree is either copied-and-modified, or modified in-place
2280 : : * if that seems safe.
2281 : : */
2282 : : static Node *
2283 : 227049 : fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset, double num_exec)
2284 : : {
2285 : 227049 : fix_scan_expr_context context;
2286 : :
2287 : 227049 : context.root = root;
2288 : 227049 : context.rtoffset = rtoffset;
2289 : 227049 : context.num_exec = num_exec;
2290 : :
2291 [ + + ]: 227049 : if (rtoffset != 0 ||
2292 [ + + ]: 183232 : root->multiexpr_params != NIL ||
2293 [ + + ]: 183140 : root->glob->lastPHId != 0 ||
2294 [ + + + + ]: 181439 : root->minmax_aggs != NIL ||
2295 : 181310 : root->hasAlternativeSubPlans)
2296 : : {
2297 : 47352 : return fix_scan_expr_mutator(node, &context);
2298 : : }
2299 : : else
2300 : : {
2301 : : /*
2302 : : * If rtoffset == 0, we don't need to change any Vars, and if there
2303 : : * are no MULTIEXPR subqueries then we don't need to replace
2304 : : * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
2305 : : * we won't need to remove them, and if there are no minmax Aggrefs we
2306 : : * won't need to replace them, and if there are no AlternativeSubPlans
2307 : : * we won't need to remove them. Then it's OK to just scribble on the
2308 : : * input node tree instead of copying (since the only change, filling
2309 : : * in any unset opfuncid fields, is harmless). This saves just enough
2310 : : * cycles to be noticeable on trivial queries.
2311 : : */
2312 : 179697 : (void) fix_scan_expr_walker(node, &context);
2313 : 179697 : return node;
2314 : : }
2315 : 227049 : }
2316 : :
2317 : : static Node *
2318 : 292391 : fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
2319 : : {
2320 [ + + ]: 292391 : if (node == NULL)
2321 : 19871 : return NULL;
2322 [ + + ]: 272520 : if (IsA(node, Var))
2323 : : {
2324 : 95464 : Var *var = copyVar((Var *) node);
2325 : :
2326 [ + - ]: 95464 : Assert(var->varlevelsup == 0);
2327 : :
2328 : : /*
2329 : : * We should not see Vars marked INNER_VAR, OUTER_VAR, or ROWID_VAR.
2330 : : * But an indexqual expression could contain INDEX_VAR Vars.
2331 : : */
2332 [ + - ]: 95464 : Assert(var->varno != INNER_VAR);
2333 [ + - ]: 95464 : Assert(var->varno != OUTER_VAR);
2334 [ + - ]: 95464 : Assert(var->varno != ROWID_VAR);
2335 [ + + ]: 95464 : if (!IS_SPECIAL_VARNO(var->varno))
2336 : 89914 : var->varno += context->rtoffset;
2337 [ + + ]: 95464 : if (var->varnosyn > 0)
2338 : 95322 : var->varnosyn += context->rtoffset;
2339 : 95464 : return (Node *) var;
2340 : 95464 : }
2341 [ + + ]: 177056 : if (IsA(node, Param))
2342 : 11295 : return fix_param_node(context->root, (Param *) node);
2343 [ + + ]: 165761 : if (IsA(node, Aggref))
2344 : : {
2345 : 62 : Aggref *aggref = (Aggref *) node;
2346 : 62 : Param *aggparam;
2347 : :
2348 : : /* See if the Aggref should be replaced by a Param */
2349 : 62 : aggparam = find_minmax_agg_replacement_param(context->root, aggref);
2350 [ + - ]: 62 : if (aggparam != NULL)
2351 : : {
2352 : : /* Make a copy of the Param for paranoia's sake */
2353 : 62 : return (Node *) copyObject(aggparam);
2354 : : }
2355 : : /* If no match, just fall through to process it normally */
2356 [ - + - ]: 62 : }
2357 [ - + ]: 165699 : if (IsA(node, CurrentOfExpr))
2358 : : {
2359 : 0 : CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
2360 : :
2361 [ # # ]: 0 : Assert(!IS_SPECIAL_VARNO(cexpr->cvarno));
2362 : 0 : cexpr->cvarno += context->rtoffset;
2363 : 0 : return (Node *) cexpr;
2364 : 0 : }
2365 [ + + ]: 165699 : if (IsA(node, PlaceHolderVar))
2366 : : {
2367 : : /* At scan level, we should always just evaluate the contained expr */
2368 : 417 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
2369 : :
2370 : : /* XXX can we assert something about phnullingrels? */
2371 : 417 : return fix_scan_expr_mutator((Node *) phv->phexpr, context);
2372 : 417 : }
2373 [ + + ]: 165282 : if (IsA(node, AlternativeSubPlan))
2374 : 147 : return fix_scan_expr_mutator(fix_alternative_subplan(context->root,
2375 : 49 : (AlternativeSubPlan *) node,
2376 : 49 : context->num_exec),
2377 : 49 : context);
2378 : 165233 : fix_expr_common(context->root, node);
2379 : 165233 : return expression_tree_mutator(node, fix_scan_expr_mutator, context);
2380 : 292391 : }
2381 : :
2382 : : static bool
2383 : 972440 : fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
2384 : : {
2385 [ + + ]: 972440 : if (node == NULL)
2386 : 90682 : return false;
2387 [ + + + - ]: 881758 : Assert(!(IsA(node, Var) && ((Var *) node)->varno == ROWID_VAR));
2388 [ + - ]: 881758 : Assert(!IsA(node, PlaceHolderVar));
2389 [ + - ]: 881758 : Assert(!IsA(node, AlternativeSubPlan));
2390 : 881758 : fix_expr_common(context->root, node);
2391 : 881758 : return expression_tree_walker(node, fix_scan_expr_walker, context);
2392 : 972440 : }
2393 : :
2394 : : /*
2395 : : * set_join_references
2396 : : * Modify the target list and quals of a join node to reference its
2397 : : * subplans, by setting the varnos to OUTER_VAR or INNER_VAR and setting
2398 : : * attno values to the result domain number of either the corresponding
2399 : : * outer or inner join tuple item. Also perform opcode lookup for these
2400 : : * expressions, and add regclass OIDs to root->glob->relationOids.
2401 : : */
2402 : : static void
2403 : 12986 : set_join_references(PlannerInfo *root, Join *join, int rtoffset)
2404 : : {
2405 : 12986 : Plan *outer_plan = join->plan.lefttree;
2406 : 12986 : Plan *inner_plan = join->plan.righttree;
2407 : 12986 : indexed_tlist *outer_itlist;
2408 : 12986 : indexed_tlist *inner_itlist;
2409 : :
2410 : 12986 : outer_itlist = build_tlist_index(outer_plan->targetlist);
2411 : 12986 : inner_itlist = build_tlist_index(inner_plan->targetlist);
2412 : :
2413 : : /*
2414 : : * First process the joinquals (including merge or hash clauses). These
2415 : : * are logically below the join so they can always use all values
2416 : : * available from the input tlists. It's okay to also handle
2417 : : * NestLoopParams now, because those couldn't refer to nullable
2418 : : * subexpressions.
2419 : : */
2420 : 25972 : join->joinqual = fix_join_expr(root,
2421 : 12986 : join->joinqual,
2422 : 12986 : outer_itlist,
2423 : 12986 : inner_itlist,
2424 : : (Index) 0,
2425 : 12986 : rtoffset,
2426 : : NRM_EQUAL,
2427 : 12986 : NUM_EXEC_QUAL((Plan *) join));
2428 : :
2429 : : /* Now do join-type-specific stuff */
2430 [ + + ]: 12986 : if (IsA(join, NestLoop))
2431 : : {
2432 : 9143 : NestLoop *nl = (NestLoop *) join;
2433 : 9143 : ListCell *lc;
2434 : :
2435 [ + + + + : 13385 : foreach(lc, nl->nestParams)
+ + ]
2436 : : {
2437 : 4242 : NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
2438 : :
2439 : : /*
2440 : : * Because we don't reparameterize parameterized paths to match
2441 : : * the outer-join level at which they are used, Vars seen in the
2442 : : * NestLoopParam expression may have nullingrels that are just a
2443 : : * subset of those in the Vars actually available from the outer
2444 : : * side. (Lateral references can also cause this, as explained in
2445 : : * the comments for identify_current_nestloop_params.) Not
2446 : : * checking this exactly is a bit grotty, but the work needed to
2447 : : * make things match up perfectly seems well out of proportion to
2448 : : * the value.
2449 : : */
2450 : 8484 : nlp->paramval = (Var *) fix_upper_expr(root,
2451 : 4242 : (Node *) nlp->paramval,
2452 : 4242 : outer_itlist,
2453 : : OUTER_VAR,
2454 : 4242 : rtoffset,
2455 : : NRM_SUBSET,
2456 : 4242 : NUM_EXEC_TLIST(outer_plan));
2457 : : /* Check we replaced any PlaceHolderVar with simple Var */
2458 [ + - ]: 4242 : if (!(IsA(nlp->paramval, Var) &&
2459 : 4242 : nlp->paramval->varno == OUTER_VAR))
2460 [ # # # # ]: 0 : elog(ERROR, "NestLoopParam was not reduced to a simple Var");
2461 : 4242 : }
2462 : 9143 : }
2463 [ + + ]: 3843 : else if (IsA(join, MergeJoin))
2464 : : {
2465 : 508 : MergeJoin *mj = (MergeJoin *) join;
2466 : :
2467 : 1016 : mj->mergeclauses = fix_join_expr(root,
2468 : 508 : mj->mergeclauses,
2469 : 508 : outer_itlist,
2470 : 508 : inner_itlist,
2471 : : (Index) 0,
2472 : 508 : rtoffset,
2473 : : NRM_EQUAL,
2474 : 508 : NUM_EXEC_QUAL((Plan *) join));
2475 : 508 : }
2476 [ - + ]: 3335 : else if (IsA(join, HashJoin))
2477 : : {
2478 : 3335 : HashJoin *hj = (HashJoin *) join;
2479 : :
2480 : 6670 : hj->hashclauses = fix_join_expr(root,
2481 : 3335 : hj->hashclauses,
2482 : 3335 : outer_itlist,
2483 : 3335 : inner_itlist,
2484 : : (Index) 0,
2485 : 3335 : rtoffset,
2486 : : NRM_EQUAL,
2487 : 3335 : NUM_EXEC_QUAL((Plan *) join));
2488 : :
2489 : : /*
2490 : : * HashJoin's hashkeys are used to look for matching tuples from its
2491 : : * outer plan (not the Hash node!) in the hashtable.
2492 : : */
2493 : 6670 : hj->hashkeys = (List *) fix_upper_expr(root,
2494 : 3335 : (Node *) hj->hashkeys,
2495 : 3335 : outer_itlist,
2496 : : OUTER_VAR,
2497 : 3335 : rtoffset,
2498 : : NRM_EQUAL,
2499 : 3335 : NUM_EXEC_QUAL((Plan *) join));
2500 : 3335 : }
2501 : :
2502 : : /*
2503 : : * Now we need to fix up the targetlist and qpqual, which are logically
2504 : : * above the join. This means that, if it's not an inner join, any Vars
2505 : : * and PHVs appearing here should have nullingrels that include the
2506 : : * effects of the outer join, ie they will have nullingrels equal to the
2507 : : * input Vars' nullingrels plus the bit added by the outer join. We don't
2508 : : * currently have enough info available here to identify what that should
2509 : : * be, so we just tell fix_join_expr to accept superset nullingrels
2510 : : * matches instead of exact ones.
2511 : : */
2512 : 25972 : join->plan.targetlist = fix_join_expr(root,
2513 : 12986 : join->plan.targetlist,
2514 : 12986 : outer_itlist,
2515 : 12986 : inner_itlist,
2516 : : (Index) 0,
2517 : 12986 : rtoffset,
2518 : 12986 : (join->jointype == JOIN_INNER ? NRM_EQUAL : NRM_SUPERSET),
2519 : 12986 : NUM_EXEC_TLIST((Plan *) join));
2520 : 25972 : join->plan.qual = fix_join_expr(root,
2521 : 12986 : join->plan.qual,
2522 : 12986 : outer_itlist,
2523 : 12986 : inner_itlist,
2524 : : (Index) 0,
2525 : 12986 : rtoffset,
2526 : 12986 : (join->jointype == JOIN_INNER ? NRM_EQUAL : NRM_SUPERSET),
2527 : 12986 : NUM_EXEC_QUAL((Plan *) join));
2528 : :
2529 : 12986 : pfree(outer_itlist);
2530 : 12986 : pfree(inner_itlist);
2531 : 12986 : }
2532 : :
2533 : : /*
2534 : : * set_upper_references
2535 : : * Update the targetlist and quals of an upper-level plan node
2536 : : * to refer to the tuples returned by its lefttree subplan.
2537 : : * Also perform opcode lookup for these expressions, and
2538 : : * add regclass OIDs to root->glob->relationOids.
2539 : : *
2540 : : * This is used for single-input plan types like Agg, Group, Result.
2541 : : *
2542 : : * In most cases, we have to match up individual Vars in the tlist and
2543 : : * qual expressions with elements of the subplan's tlist (which was
2544 : : * generated by flattening these selfsame expressions, so it should have all
2545 : : * the required variables). There is an important exception, however:
2546 : : * depending on where we are in the plan tree, sort/group columns may have
2547 : : * been pushed into the subplan tlist unflattened. If these values are also
2548 : : * needed in the output then we want to reference the subplan tlist element
2549 : : * rather than recomputing the expression.
2550 : : */
2551 : : static void
2552 : 9730 : set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
2553 : : {
2554 : 9730 : Plan *subplan = plan->lefttree;
2555 : 9730 : indexed_tlist *subplan_itlist;
2556 : 9730 : List *output_targetlist;
2557 : 9730 : ListCell *l;
2558 : :
2559 : 9730 : subplan_itlist = build_tlist_index(subplan->targetlist);
2560 : :
2561 : : /*
2562 : : * If it's a grouping node with grouping sets, any Vars and PHVs appearing
2563 : : * in the targetlist and quals should have nullingrels that include the
2564 : : * effects of the grouping step, ie they will have nullingrels equal to
2565 : : * the input Vars/PHVs' nullingrels plus the RT index of the grouping
2566 : : * step. In order to perform exact nullingrels matches, we remove the RT
2567 : : * index of the grouping step first.
2568 : : */
2569 [ + + ]: 9730 : if (IsA(plan, Agg) &&
2570 [ + + + + ]: 5512 : root->group_rtindex > 0 &&
2571 : 1062 : ((Agg *) plan)->groupingSets)
2572 : : {
2573 : 151 : plan->targetlist = (List *)
2574 : 302 : remove_nulling_relids((Node *) plan->targetlist,
2575 : 151 : bms_make_singleton(root->group_rtindex),
2576 : : NULL);
2577 : 151 : plan->qual = (List *)
2578 : 302 : remove_nulling_relids((Node *) plan->qual,
2579 : 151 : bms_make_singleton(root->group_rtindex),
2580 : : NULL);
2581 : 151 : }
2582 : :
2583 : 9730 : output_targetlist = NIL;
2584 [ + + + + : 27361 : foreach(l, plan->targetlist)
+ + ]
2585 : : {
2586 : 17631 : TargetEntry *tle = (TargetEntry *) lfirst(l);
2587 : 17631 : Node *newexpr;
2588 : :
2589 : : /* If it's a sort/group item, first try to match by sortref */
2590 [ + + ]: 17631 : if (tle->ressortgroupref != 0)
2591 : : {
2592 : 6009 : newexpr = (Node *)
2593 : 12018 : search_indexed_tlist_for_sortgroupref(tle->expr,
2594 : 6009 : tle->ressortgroupref,
2595 : 6009 : subplan_itlist,
2596 : : OUTER_VAR);
2597 [ + + ]: 6009 : if (!newexpr)
2598 : 7186 : newexpr = fix_upper_expr(root,
2599 : 3593 : (Node *) tle->expr,
2600 : 3593 : subplan_itlist,
2601 : : OUTER_VAR,
2602 : 3593 : rtoffset,
2603 : : NRM_EQUAL,
2604 : 3593 : NUM_EXEC_TLIST(plan));
2605 : 6009 : }
2606 : : else
2607 : 23244 : newexpr = fix_upper_expr(root,
2608 : 11622 : (Node *) tle->expr,
2609 : 11622 : subplan_itlist,
2610 : : OUTER_VAR,
2611 : 11622 : rtoffset,
2612 : : NRM_EQUAL,
2613 : 11622 : NUM_EXEC_TLIST(plan));
2614 : 17631 : tle = flatCopyTargetEntry(tle);
2615 : 17631 : tle->expr = (Expr *) newexpr;
2616 : 17631 : output_targetlist = lappend(output_targetlist, tle);
2617 : 17631 : }
2618 : 9730 : plan->targetlist = output_targetlist;
2619 : :
2620 : 9730 : plan->qual = (List *)
2621 : 19460 : fix_upper_expr(root,
2622 : 9730 : (Node *) plan->qual,
2623 : 9730 : subplan_itlist,
2624 : : OUTER_VAR,
2625 : 9730 : rtoffset,
2626 : : NRM_EQUAL,
2627 : 9730 : NUM_EXEC_QUAL(plan));
2628 : :
2629 : 9730 : pfree(subplan_itlist);
2630 : 9730 : }
2631 : :
2632 : : /*
2633 : : * set_param_references
2634 : : * Initialize the initParam list in Gather or Gather merge node such that
2635 : : * it contains reference of all the params that needs to be evaluated
2636 : : * before execution of the node. It contains the initplan params that are
2637 : : * being passed to the plan nodes below it.
2638 : : */
2639 : : static void
2640 : 260 : set_param_references(PlannerInfo *root, Plan *plan)
2641 : : {
2642 [ + + + - ]: 260 : Assert(IsA(plan, Gather) || IsA(plan, GatherMerge));
2643 : :
2644 [ + + ]: 260 : if (plan->lefttree->extParam)
2645 : : {
2646 : 240 : PlannerInfo *proot;
2647 : 240 : Bitmapset *initSetParam = NULL;
2648 : 240 : ListCell *l;
2649 : :
2650 [ + + ]: 509 : for (proot = root; proot != NULL; proot = proot->parent_root)
2651 : : {
2652 [ + + + + : 282 : foreach(l, proot->init_plans)
+ + ]
2653 : : {
2654 : 13 : SubPlan *initsubplan = (SubPlan *) lfirst(l);
2655 : 13 : ListCell *l2;
2656 : :
2657 [ + - + + : 26 : foreach(l2, initsubplan->setParam)
+ + ]
2658 : : {
2659 : 13 : initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
2660 : 13 : }
2661 : 13 : }
2662 : 269 : }
2663 : :
2664 : : /*
2665 : : * Remember the list of all external initplan params that are used by
2666 : : * the children of Gather or Gather merge node.
2667 : : */
2668 [ + + ]: 240 : if (IsA(plan, Gather))
2669 : 173 : ((Gather *) plan)->initParam =
2670 : 173 : bms_intersect(plan->lefttree->extParam, initSetParam);
2671 : : else
2672 : 67 : ((GatherMerge *) plan)->initParam =
2673 : 67 : bms_intersect(plan->lefttree->extParam, initSetParam);
2674 : 240 : }
2675 : 260 : }
2676 : :
2677 : : /*
2678 : : * Recursively scan an expression tree and convert Aggrefs to the proper
2679 : : * intermediate form for combining aggregates. This means (1) replacing each
2680 : : * one's argument list with a single argument that is the original Aggref
2681 : : * modified to show partial aggregation and (2) changing the upper Aggref to
2682 : : * show combining aggregation.
2683 : : *
2684 : : * After this step, set_upper_references will replace the partial Aggrefs
2685 : : * with Vars referencing the lower Agg plan node's outputs, so that the final
2686 : : * form seen by the executor is a combining Aggref with a Var as input.
2687 : : *
2688 : : * It's rather messy to postpone this step until setrefs.c; ideally it'd be
2689 : : * done in createplan.c. The difficulty is that once we modify the Aggref
2690 : : * expressions, they will no longer be equal() to their original form and
2691 : : * so cross-plan-node-level matches will fail. So this has to happen after
2692 : : * the plan node above the Agg has resolved its subplan references.
2693 : : */
2694 : : static Node *
2695 : 1681 : convert_combining_aggrefs(Node *node, void *context)
2696 : : {
2697 [ + + ]: 1681 : if (node == NULL)
2698 : 200 : return NULL;
2699 [ + + ]: 1481 : if (IsA(node, Aggref))
2700 : : {
2701 : 380 : Aggref *orig_agg = (Aggref *) node;
2702 : 380 : Aggref *child_agg;
2703 : 380 : Aggref *parent_agg;
2704 : :
2705 : : /* Assert we've not chosen to partial-ize any unsupported cases */
2706 [ + - ]: 380 : Assert(orig_agg->aggorder == NIL);
2707 [ + - ]: 380 : Assert(orig_agg->aggdistinct == NIL);
2708 : :
2709 : : /*
2710 : : * Since aggregate calls can't be nested, we needn't recurse into the
2711 : : * arguments. But for safety, flat-copy the Aggref node itself rather
2712 : : * than modifying it in-place.
2713 : : */
2714 : 380 : child_agg = makeNode(Aggref);
2715 : 380 : memcpy(child_agg, orig_agg, sizeof(Aggref));
2716 : :
2717 : : /*
2718 : : * For the parent Aggref, we want to copy all the fields of the
2719 : : * original aggregate *except* the args list, which we'll replace
2720 : : * below, and the aggfilter expression, which should be applied only
2721 : : * by the child not the parent. Rather than explicitly knowing about
2722 : : * all the other fields here, we can momentarily modify child_agg to
2723 : : * provide a suitable source for copyObject.
2724 : : */
2725 : 380 : child_agg->args = NIL;
2726 : 380 : child_agg->aggfilter = NULL;
2727 : 380 : parent_agg = copyObject(child_agg);
2728 : 380 : child_agg->args = orig_agg->args;
2729 : 380 : child_agg->aggfilter = orig_agg->aggfilter;
2730 : :
2731 : : /*
2732 : : * Now, set up child_agg to represent the first phase of partial
2733 : : * aggregation. For now, assume serialization is required.
2734 : : */
2735 : 380 : mark_partial_aggref(child_agg, AGGSPLIT_INITIAL_SERIAL);
2736 : :
2737 : : /*
2738 : : * And set up parent_agg to represent the second phase.
2739 : : */
2740 : 380 : parent_agg->args = list_make1(makeTargetEntry((Expr *) child_agg,
2741 : : 1, NULL, false));
2742 : 380 : mark_partial_aggref(parent_agg, AGGSPLIT_FINAL_DESERIAL);
2743 : :
2744 : 380 : return (Node *) parent_agg;
2745 : 380 : }
2746 : 1101 : return expression_tree_mutator(node, convert_combining_aggrefs, context);
2747 : 1681 : }
2748 : :
2749 : : /*
2750 : : * set_dummy_tlist_references
2751 : : * Replace the targetlist of an upper-level plan node with a simple
2752 : : * list of OUTER_VAR references to its child.
2753 : : *
2754 : : * This is used for plan types like Sort and Append that don't evaluate
2755 : : * their targetlists. Although the executor doesn't care at all what's in
2756 : : * the tlist, EXPLAIN needs it to be realistic.
2757 : : *
2758 : : * Note: we could almost use set_upper_references() here, but it fails for
2759 : : * Append for lack of a lefttree subplan. Single-purpose code is faster
2760 : : * anyway.
2761 : : */
2762 : : static void
2763 : 18348 : set_dummy_tlist_references(Plan *plan, int rtoffset)
2764 : : {
2765 : 18348 : List *output_targetlist;
2766 : 18348 : ListCell *l;
2767 : :
2768 : 18348 : output_targetlist = NIL;
2769 [ + + + + : 76577 : foreach(l, plan->targetlist)
+ + ]
2770 : : {
2771 : 58229 : TargetEntry *tle = (TargetEntry *) lfirst(l);
2772 : 58229 : Var *oldvar = (Var *) tle->expr;
2773 : 58229 : Var *newvar;
2774 : :
2775 : : /*
2776 : : * As in search_indexed_tlist_for_non_var(), we prefer to keep Consts
2777 : : * as Consts, not Vars referencing Consts. Here, there's no speed
2778 : : * advantage to be had, but it makes EXPLAIN output look cleaner, and
2779 : : * again it avoids confusing the executor.
2780 : : */
2781 [ + + ]: 58229 : if (IsA(oldvar, Const))
2782 : : {
2783 : : /* just reuse the existing TLE node */
2784 : 1323 : output_targetlist = lappend(output_targetlist, tle);
2785 : 1323 : continue;
2786 : : }
2787 : :
2788 : 56906 : newvar = makeVar(OUTER_VAR,
2789 : 56906 : tle->resno,
2790 : 56906 : exprType((Node *) oldvar),
2791 : 56906 : exprTypmod((Node *) oldvar),
2792 : 56906 : exprCollation((Node *) oldvar),
2793 : : 0);
2794 [ + + + + ]: 56906 : if (IsA(oldvar, Var) &&
2795 : 42149 : oldvar->varnosyn > 0)
2796 : : {
2797 : 36647 : newvar->varnosyn = oldvar->varnosyn + rtoffset;
2798 : 36647 : newvar->varattnosyn = oldvar->varattnosyn;
2799 : 36647 : }
2800 : : else
2801 : : {
2802 : 20259 : newvar->varnosyn = 0; /* wasn't ever a plain Var */
2803 : 20259 : newvar->varattnosyn = 0;
2804 : : }
2805 : :
2806 : 56906 : tle = flatCopyTargetEntry(tle);
2807 : 56906 : tle->expr = (Expr *) newvar;
2808 : 56906 : output_targetlist = lappend(output_targetlist, tle);
2809 [ - + + ]: 58229 : }
2810 : 18348 : plan->targetlist = output_targetlist;
2811 : :
2812 : : /* We don't touch plan->qual here */
2813 : 18348 : }
2814 : :
2815 : :
2816 : : /*
2817 : : * build_tlist_index --- build an index data structure for a child tlist
2818 : : *
2819 : : * In most cases, subplan tlists will be "flat" tlists with only Vars,
2820 : : * so we try to optimize that case by extracting information about Vars
2821 : : * in advance. Matching a parent tlist to a child is still an O(N^2)
2822 : : * operation, but at least with a much smaller constant factor than plain
2823 : : * tlist_member() searches.
2824 : : *
2825 : : * The result of this function is an indexed_tlist struct to pass to
2826 : : * search_indexed_tlist_for_var() and siblings.
2827 : : * When done, the indexed_tlist may be freed with a single pfree().
2828 : : */
2829 : : static indexed_tlist *
2830 : 41764 : build_tlist_index(List *tlist)
2831 : : {
2832 : 41764 : indexed_tlist *itlist;
2833 : 41764 : tlist_vinfo *vinfo;
2834 : 41764 : ListCell *l;
2835 : :
2836 : : /* Create data structure with enough slots for all tlist entries */
2837 : 41764 : itlist = (indexed_tlist *)
2838 : 41764 : palloc(offsetof(indexed_tlist, vars) +
2839 : 41764 : list_length(tlist) * sizeof(tlist_vinfo));
2840 : :
2841 : 41764 : itlist->tlist = tlist;
2842 : 41764 : itlist->has_ph_vars = false;
2843 : 41764 : itlist->has_non_vars = false;
2844 : :
2845 : : /* Find the Vars and fill in the index array */
2846 : 41764 : vinfo = itlist->vars;
2847 [ + + + + : 355488 : foreach(l, tlist)
+ + ]
2848 : : {
2849 : 313724 : TargetEntry *tle = (TargetEntry *) lfirst(l);
2850 : :
2851 [ + - + + ]: 313724 : if (tle->expr && IsA(tle->expr, Var))
2852 : : {
2853 : 310806 : Var *var = (Var *) tle->expr;
2854 : :
2855 : 310806 : vinfo->varno = var->varno;
2856 : 310806 : vinfo->varattno = var->varattno;
2857 : 310806 : vinfo->resno = tle->resno;
2858 : 310806 : vinfo->varnullingrels = var->varnullingrels;
2859 : 310806 : vinfo++;
2860 : 310806 : }
2861 [ + - + + ]: 2918 : else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2862 : 540 : itlist->has_ph_vars = true;
2863 : : else
2864 : 2378 : itlist->has_non_vars = true;
2865 : 313724 : }
2866 : :
2867 : 41764 : itlist->num_vars = (vinfo - itlist->vars);
2868 : :
2869 : 83528 : return itlist;
2870 : 41764 : }
2871 : :
2872 : : /*
2873 : : * build_tlist_index_other_vars --- build a restricted tlist index
2874 : : *
2875 : : * This is like build_tlist_index, but we only index tlist entries that
2876 : : * are Vars belonging to some rel other than the one specified. We will set
2877 : : * has_ph_vars (allowing PlaceHolderVars to be matched), but not has_non_vars
2878 : : * (so nothing other than Vars and PlaceHolderVars can be matched).
2879 : : */
2880 : : static indexed_tlist *
2881 : 386 : build_tlist_index_other_vars(List *tlist, int ignore_rel)
2882 : : {
2883 : 386 : indexed_tlist *itlist;
2884 : 386 : tlist_vinfo *vinfo;
2885 : 386 : ListCell *l;
2886 : :
2887 : : /* Create data structure with enough slots for all tlist entries */
2888 : 386 : itlist = (indexed_tlist *)
2889 : 386 : palloc(offsetof(indexed_tlist, vars) +
2890 : 386 : list_length(tlist) * sizeof(tlist_vinfo));
2891 : :
2892 : 386 : itlist->tlist = tlist;
2893 : 386 : itlist->has_ph_vars = false;
2894 : 386 : itlist->has_non_vars = false;
2895 : :
2896 : : /* Find the desired Vars and fill in the index array */
2897 : 386 : vinfo = itlist->vars;
2898 [ + + + + : 1547 : foreach(l, tlist)
+ + ]
2899 : : {
2900 : 1161 : TargetEntry *tle = (TargetEntry *) lfirst(l);
2901 : :
2902 [ + - + + ]: 1161 : if (tle->expr && IsA(tle->expr, Var))
2903 : : {
2904 : 703 : Var *var = (Var *) tle->expr;
2905 : :
2906 [ + + ]: 703 : if (var->varno != ignore_rel)
2907 : : {
2908 : 585 : vinfo->varno = var->varno;
2909 : 585 : vinfo->varattno = var->varattno;
2910 : 585 : vinfo->resno = tle->resno;
2911 : 585 : vinfo->varnullingrels = var->varnullingrels;
2912 : 585 : vinfo++;
2913 : 585 : }
2914 : 703 : }
2915 [ + - + + ]: 458 : else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2916 : 3 : itlist->has_ph_vars = true;
2917 : 1161 : }
2918 : :
2919 : 386 : itlist->num_vars = (vinfo - itlist->vars);
2920 : :
2921 : 772 : return itlist;
2922 : 386 : }
2923 : :
2924 : : /*
2925 : : * search_indexed_tlist_for_var --- find a Var in an indexed tlist
2926 : : *
2927 : : * If a match is found, return a copy of the given Var with suitably
2928 : : * modified varno/varattno (to wit, newvarno and the resno of the TLE entry).
2929 : : * Also ensure that varnosyn is incremented by rtoffset.
2930 : : * If no match, return NULL.
2931 : : *
2932 : : * We cross-check the varnullingrels of the subplan output Var based on
2933 : : * nrm_match. Most call sites should pass NRM_EQUAL indicating we expect
2934 : : * an exact match. However, there are places where we haven't cleaned
2935 : : * things up completely, and we have to settle for allowing subset or
2936 : : * superset matches.
2937 : : */
2938 : : static Var *
2939 : 140027 : search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
2940 : : int newvarno, int rtoffset,
2941 : : NullingRelsMatch nrm_match)
2942 : : {
2943 : 140027 : int varno = var->varno;
2944 : 140027 : AttrNumber varattno = var->varattno;
2945 : 140027 : tlist_vinfo *vinfo;
2946 : 140027 : int i;
2947 : :
2948 : 140027 : vinfo = itlist->vars;
2949 : 140027 : i = itlist->num_vars;
2950 [ + + ]: 927556 : while (i-- > 0)
2951 : : {
2952 [ + + + + ]: 895160 : if (vinfo->varno == varno && vinfo->varattno == varattno)
2953 : : {
2954 : : /* Found a match */
2955 : 107631 : Var *newvar = copyVar(var);
2956 : :
2957 : : /*
2958 : : * Verify that we kept all the nullingrels machinations straight.
2959 : : *
2960 : : * XXX we skip the check for system columns and whole-row Vars.
2961 : : * That's because such Vars might be row identity Vars, which are
2962 : : * generated without any varnullingrels. It'd be hard to do
2963 : : * otherwise, since they're normally made very early in planning,
2964 : : * when we haven't looked at the jointree yet and don't know which
2965 : : * joins might null such Vars. Doesn't seem worth the expense to
2966 : : * make them fully valid. (While it's slightly annoying that we
2967 : : * thereby lose checking for user-written references to such
2968 : : * columns, it seems unlikely that a bug in nullingrels logic
2969 : : * would affect only system columns.)
2970 : : */
2971 [ + + + + : 107631 : if (!(varattno <= 0 ||
+ + ]
2972 [ + - ]: 104393 : (nrm_match == NRM_SUBSET ?
2973 : 4143 : bms_is_subset(var->varnullingrels, vinfo->varnullingrels) :
2974 : 100250 : nrm_match == NRM_SUPERSET ?
2975 : 28086 : bms_is_subset(vinfo->varnullingrels, var->varnullingrels) :
2976 : 72164 : bms_equal(vinfo->varnullingrels, var->varnullingrels))))
2977 [ # # # # ]: 0 : elog(ERROR, "wrong varnullingrels %s (expected %s) for Var %d/%d",
2978 : : bmsToString(var->varnullingrels),
2979 : : bmsToString(vinfo->varnullingrels),
2980 : : varno, varattno);
2981 : :
2982 : 107631 : newvar->varno = newvarno;
2983 : 107631 : newvar->varattno = vinfo->resno;
2984 [ + + ]: 107631 : if (newvar->varnosyn > 0)
2985 : 107600 : newvar->varnosyn += rtoffset;
2986 : 107631 : return newvar;
2987 : 107631 : }
2988 : 787529 : vinfo++;
2989 : : }
2990 : 32396 : return NULL; /* no match */
2991 : 140027 : }
2992 : :
2993 : : /*
2994 : : * search_indexed_tlist_for_phv --- find a PlaceHolderVar in an indexed tlist
2995 : : *
2996 : : * If a match is found, return a Var constructed to reference the tlist item.
2997 : : * If no match, return NULL.
2998 : : *
2999 : : * Cross-check phnullingrels as in search_indexed_tlist_for_var.
3000 : : *
3001 : : * NOTE: it is a waste of time to call this unless itlist->has_ph_vars.
3002 : : */
3003 : : static Var *
3004 : 520 : search_indexed_tlist_for_phv(PlaceHolderVar *phv,
3005 : : indexed_tlist *itlist, int newvarno,
3006 : : NullingRelsMatch nrm_match)
3007 : : {
3008 : 520 : ListCell *lc;
3009 : :
3010 [ + - + + : 1780 : foreach(lc, itlist->tlist)
+ + + + ]
3011 : : {
3012 : 1260 : TargetEntry *tle = (TargetEntry *) lfirst(lc);
3013 : :
3014 [ + - + + ]: 1260 : if (tle->expr && IsA(tle->expr, PlaceHolderVar))
3015 : : {
3016 : 664 : PlaceHolderVar *subphv = (PlaceHolderVar *) tle->expr;
3017 : 664 : Var *newvar;
3018 : :
3019 : : /*
3020 : : * Analogously to search_indexed_tlist_for_var, we match on phid
3021 : : * only. We don't use equal(), partially for speed but mostly
3022 : : * because phnullingrels might not be exactly equal.
3023 : : */
3024 [ + + ]: 664 : if (phv->phid != subphv->phid)
3025 : 197 : continue;
3026 : :
3027 : : /* Verify that we kept all the nullingrels machinations straight */
3028 [ + + + - : 467 : if (!(nrm_match == NRM_SUBSET ?
+ + ]
3029 : 42 : bms_is_subset(phv->phnullingrels, subphv->phnullingrels) :
3030 : 425 : nrm_match == NRM_SUPERSET ?
3031 : 234 : bms_is_subset(subphv->phnullingrels, phv->phnullingrels) :
3032 : 191 : bms_equal(subphv->phnullingrels, phv->phnullingrels)))
3033 [ # # # # ]: 0 : elog(ERROR, "wrong phnullingrels %s (expected %s) for PlaceHolderVar %d",
3034 : : bmsToString(phv->phnullingrels),
3035 : : bmsToString(subphv->phnullingrels),
3036 : : phv->phid);
3037 : :
3038 : : /* Found a matching subplan output expression */
3039 : 467 : newvar = makeVarFromTargetEntry(newvarno, tle);
3040 : 467 : newvar->varnosyn = 0; /* wasn't ever a plain Var */
3041 : 467 : newvar->varattnosyn = 0;
3042 : 467 : return newvar;
3043 : 664 : }
3044 [ + + + ]: 1260 : }
3045 : 53 : return NULL; /* no match */
3046 : 520 : }
3047 : :
3048 : : /*
3049 : : * search_indexed_tlist_for_non_var --- find a non-Var/PHV in an indexed tlist
3050 : : *
3051 : : * If a match is found, return a Var constructed to reference the tlist item.
3052 : : * If no match, return NULL.
3053 : : *
3054 : : * NOTE: it is a waste of time to call this unless itlist->has_non_vars.
3055 : : */
3056 : : static Var *
3057 : 5092 : search_indexed_tlist_for_non_var(Expr *node,
3058 : : indexed_tlist *itlist, int newvarno)
3059 : : {
3060 : 5092 : TargetEntry *tle;
3061 : :
3062 : : /*
3063 : : * If it's a simple Const, replacing it with a Var is silly, even if there
3064 : : * happens to be an identical Const below; a Var is more expensive to
3065 : : * execute than a Const. What's more, replacing it could confuse some
3066 : : * places in the executor that expect to see simple Consts for, eg,
3067 : : * dropped columns.
3068 : : */
3069 [ + + ]: 5092 : if (IsA(node, Const))
3070 : 228 : return NULL;
3071 : :
3072 : 4864 : tle = tlist_member(node, itlist->tlist);
3073 [ + + ]: 4864 : if (tle)
3074 : : {
3075 : : /* Found a matching subplan output expression */
3076 : 1150 : Var *newvar;
3077 : :
3078 : 1150 : newvar = makeVarFromTargetEntry(newvarno, tle);
3079 : 1150 : newvar->varnosyn = 0; /* wasn't ever a plain Var */
3080 : 1150 : newvar->varattnosyn = 0;
3081 : 1150 : return newvar;
3082 : 1150 : }
3083 : 3714 : return NULL; /* no match */
3084 : 5092 : }
3085 : :
3086 : : /*
3087 : : * search_indexed_tlist_for_sortgroupref --- find a sort/group expression
3088 : : *
3089 : : * If a match is found, return a Var constructed to reference the tlist item.
3090 : : * If no match, return NULL.
3091 : : *
3092 : : * This is needed to ensure that we select the right subplan TLE in cases
3093 : : * where there are multiple textually-equal()-but-volatile sort expressions.
3094 : : * And it's also faster than search_indexed_tlist_for_non_var.
3095 : : */
3096 : : static Var *
3097 : 6009 : search_indexed_tlist_for_sortgroupref(Expr *node,
3098 : : Index sortgroupref,
3099 : : indexed_tlist *itlist,
3100 : : int newvarno)
3101 : : {
3102 : 6009 : ListCell *lc;
3103 : :
3104 [ + + + + : 30586 : foreach(lc, itlist->tlist)
+ + + + ]
3105 : : {
3106 : 24577 : TargetEntry *tle = (TargetEntry *) lfirst(lc);
3107 : :
3108 : : /*
3109 : : * Usually the equal() check is redundant, but in setop plans it may
3110 : : * not be, since prepunion.c assigns ressortgroupref equal to the
3111 : : * column resno without regard to whether that matches the topmost
3112 : : * level's sortgrouprefs and without regard to whether any implicit
3113 : : * coercions are added in the setop tree. We might have to clean that
3114 : : * up someday; but for now, just ignore any false matches.
3115 : : */
3116 [ + + + + ]: 24577 : if (tle->ressortgroupref == sortgroupref &&
3117 : 2438 : equal(node, tle->expr))
3118 : : {
3119 : : /* Found a matching subplan output expression */
3120 : 2416 : Var *newvar;
3121 : :
3122 : 2416 : newvar = makeVarFromTargetEntry(newvarno, tle);
3123 : 2416 : newvar->varnosyn = 0; /* wasn't ever a plain Var */
3124 : 2416 : newvar->varattnosyn = 0;
3125 : 2416 : return newvar;
3126 : 2416 : }
3127 [ + + ]: 24577 : }
3128 : 3593 : return NULL; /* no match */
3129 : 6009 : }
3130 : :
3131 : : /*
3132 : : * fix_join_expr
3133 : : * Create a new set of targetlist entries or join qual clauses by
3134 : : * changing the varno/varattno values of variables in the clauses
3135 : : * to reference target list values from the outer and inner join
3136 : : * relation target lists. Also perform opcode lookup and add
3137 : : * regclass OIDs to root->glob->relationOids.
3138 : : *
3139 : : * This is used in four different scenarios:
3140 : : * 1) a normal join clause, where all the Vars in the clause *must* be
3141 : : * replaced by OUTER_VAR or INNER_VAR references. In this case
3142 : : * acceptable_rel should be zero so that any failure to match a Var will be
3143 : : * reported as an error.
3144 : : * 2) RETURNING clauses, which may contain both Vars of the target relation
3145 : : * and Vars of other relations. In this case we want to replace the
3146 : : * other-relation Vars by OUTER_VAR references, while leaving target Vars
3147 : : * alone. Thus inner_itlist = NULL and acceptable_rel = the ID of the
3148 : : * target relation should be passed.
3149 : : * 3) ON CONFLICT UPDATE SET/WHERE clauses. Here references to EXCLUDED are
3150 : : * to be replaced with INNER_VAR references, while leaving target Vars (the
3151 : : * to-be-updated relation) alone. Correspondingly inner_itlist is to be
3152 : : * EXCLUDED elements, outer_itlist = NULL and acceptable_rel the target
3153 : : * relation.
3154 : : * 4) MERGE. In this case, references to the source relation are to be
3155 : : * replaced with INNER_VAR references, leaving Vars of the target
3156 : : * relation (the to-be-modified relation) alone. So inner_itlist is to be
3157 : : * the source relation elements, outer_itlist = NULL and acceptable_rel
3158 : : * the target relation.
3159 : : *
3160 : : * 'clauses' is the targetlist or list of join clauses
3161 : : * 'outer_itlist' is the indexed target list of the outer join relation,
3162 : : * or NULL
3163 : : * 'inner_itlist' is the indexed target list of the inner join relation,
3164 : : * or NULL
3165 : : * 'acceptable_rel' is either zero or the rangetable index of a relation
3166 : : * whose Vars may appear in the clause without provoking an error
3167 : : * 'rtoffset': how much to increment varnos by
3168 : : * 'nrm_match': as for search_indexed_tlist_for_var()
3169 : : * 'num_exec': estimated number of executions of expression
3170 : : *
3171 : : * Returns the new expression tree. The original clause structure is
3172 : : * not modified.
3173 : : */
3174 : : static List *
3175 : 44831 : fix_join_expr(PlannerInfo *root,
3176 : : List *clauses,
3177 : : indexed_tlist *outer_itlist,
3178 : : indexed_tlist *inner_itlist,
3179 : : Index acceptable_rel,
3180 : : int rtoffset,
3181 : : NullingRelsMatch nrm_match,
3182 : : double num_exec)
3183 : : {
3184 : 44831 : fix_join_expr_context context;
3185 : :
3186 : 44831 : context.root = root;
3187 : 44831 : context.outer_itlist = outer_itlist;
3188 : 44831 : context.inner_itlist = inner_itlist;
3189 : 44831 : context.acceptable_rel = acceptable_rel;
3190 : 44831 : context.rtoffset = rtoffset;
3191 : 44831 : context.nrm_match = nrm_match;
3192 : 44831 : context.num_exec = num_exec;
3193 : 89662 : return (List *) fix_join_expr_mutator((Node *) clauses, &context);
3194 : 44831 : }
3195 : :
3196 : : static Node *
3197 : 270608 : fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
3198 : : {
3199 : 270608 : Var *newvar;
3200 : :
3201 [ + + ]: 270608 : if (node == NULL)
3202 : 28324 : return NULL;
3203 [ + + ]: 242284 : if (IsA(node, Var))
3204 : : {
3205 : 85154 : Var *var = (Var *) node;
3206 : :
3207 : : /*
3208 : : * Verify that Vars with non-default varreturningtype only appear in
3209 : : * the RETURNING list, and refer to the target relation.
3210 : : */
3211 [ + + ]: 85154 : if (var->varreturningtype != VAR_RETURNING_DEFAULT)
3212 : : {
3213 [ + - ]: 421 : if (context->inner_itlist != NULL ||
3214 : 421 : context->outer_itlist == NULL ||
3215 : 421 : context->acceptable_rel == 0)
3216 [ # # # # ]: 0 : elog(ERROR, "variable returning old/new found outside RETURNING list");
3217 [ + - ]: 421 : if (var->varno != context->acceptable_rel)
3218 [ # # # # ]: 0 : elog(ERROR, "wrong varno %d (expected %d) for variable returning old/new",
3219 : : var->varno, context->acceptable_rel);
3220 : 421 : }
3221 : :
3222 : : /* Look for the var in the input tlists, first in the outer */
3223 [ + + ]: 85154 : if (context->outer_itlist)
3224 : : {
3225 : 168208 : newvar = search_indexed_tlist_for_var(var,
3226 : 84104 : context->outer_itlist,
3227 : : OUTER_VAR,
3228 : 84104 : context->rtoffset,
3229 : 84104 : context->nrm_match);
3230 [ + + ]: 84104 : if (newvar)
3231 : 52140 : return (Node *) newvar;
3232 : 31964 : }
3233 : :
3234 : : /* then in the inner. */
3235 [ + + ]: 33014 : if (context->inner_itlist)
3236 : : {
3237 : 63292 : newvar = search_indexed_tlist_for_var(var,
3238 : 31646 : context->inner_itlist,
3239 : : INNER_VAR,
3240 : 31646 : context->rtoffset,
3241 : 31646 : context->nrm_match);
3242 [ + + ]: 31646 : if (newvar)
3243 : 31214 : return (Node *) newvar;
3244 : 432 : }
3245 : :
3246 : : /* If it's for acceptable_rel, adjust and return it */
3247 [ + - ]: 1800 : if (var->varno == context->acceptable_rel)
3248 : : {
3249 : 1800 : var = copyVar(var);
3250 : 1800 : var->varno += context->rtoffset;
3251 [ + + ]: 1800 : if (var->varnosyn > 0)
3252 : 1697 : var->varnosyn += context->rtoffset;
3253 : 1800 : return (Node *) var;
3254 : : }
3255 : :
3256 : : /* No referent found for Var */
3257 [ # # # # ]: 0 : elog(ERROR, "variable not found in subplan target lists");
3258 [ + - ]: 85154 : }
3259 [ + + ]: 157130 : if (IsA(node, PlaceHolderVar))
3260 : : {
3261 : 366 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
3262 : :
3263 : : /* See if the PlaceHolderVar has bubbled up from a lower plan node */
3264 [ + + + + ]: 366 : if (context->outer_itlist && context->outer_itlist->has_ph_vars)
3265 : : {
3266 : 302 : newvar = search_indexed_tlist_for_phv(phv,
3267 : 151 : context->outer_itlist,
3268 : : OUTER_VAR,
3269 : 151 : context->nrm_match);
3270 [ + + ]: 151 : if (newvar)
3271 : 108 : return (Node *) newvar;
3272 : 43 : }
3273 [ + - + + ]: 258 : if (context->inner_itlist && context->inner_itlist->has_ph_vars)
3274 : : {
3275 : 418 : newvar = search_indexed_tlist_for_phv(phv,
3276 : 209 : context->inner_itlist,
3277 : : INNER_VAR,
3278 : 209 : context->nrm_match);
3279 [ + + ]: 209 : if (newvar)
3280 : 199 : return (Node *) newvar;
3281 : 10 : }
3282 : :
3283 : : /* If not supplied by input plans, evaluate the contained expr */
3284 : : /* XXX can we assert something about phnullingrels? */
3285 : 59 : return fix_join_expr_mutator((Node *) phv->phexpr, context);
3286 : 366 : }
3287 : : /* Try matching more complex expressions too, if tlists have any */
3288 [ + + + + ]: 156764 : if (context->outer_itlist && context->outer_itlist->has_non_vars)
3289 : : {
3290 : 534 : newvar = search_indexed_tlist_for_non_var((Expr *) node,
3291 : 267 : context->outer_itlist,
3292 : : OUTER_VAR);
3293 [ + + ]: 267 : if (newvar)
3294 : 25 : return (Node *) newvar;
3295 : 242 : }
3296 [ + + + + ]: 156739 : if (context->inner_itlist && context->inner_itlist->has_non_vars)
3297 : : {
3298 : 2272 : newvar = search_indexed_tlist_for_non_var((Expr *) node,
3299 : 1136 : context->inner_itlist,
3300 : : INNER_VAR);
3301 [ + + ]: 1136 : if (newvar)
3302 : 202 : return (Node *) newvar;
3303 : 934 : }
3304 : : /* Special cases (apply only AFTER failing to match to lower tlist) */
3305 [ + + ]: 156537 : if (IsA(node, Param))
3306 : 687 : return fix_param_node(context->root, (Param *) node);
3307 [ + + ]: 155850 : if (IsA(node, AlternativeSubPlan))
3308 : 486 : return fix_join_expr_mutator(fix_alternative_subplan(context->root,
3309 : 162 : (AlternativeSubPlan *) node,
3310 : 162 : context->num_exec),
3311 : 162 : context);
3312 : 155688 : fix_expr_common(context->root, node);
3313 : 155688 : return expression_tree_mutator(node, fix_join_expr_mutator, context);
3314 : 270608 : }
3315 : :
3316 : : /*
3317 : : * fix_upper_expr
3318 : : * Modifies an expression tree so that all Var nodes reference outputs
3319 : : * of a subplan. Also looks for Aggref nodes that should be replaced
3320 : : * by initplan output Params. Also performs opcode lookup, and adds
3321 : : * regclass OIDs to root->glob->relationOids.
3322 : : *
3323 : : * This is used to fix up target and qual expressions of non-join upper-level
3324 : : * plan nodes, as well as index-only scan nodes.
3325 : : *
3326 : : * An error is raised if no matching var can be found in the subplan tlist
3327 : : * --- so this routine should only be applied to nodes whose subplans'
3328 : : * targetlists were generated by flattening the expressions used in the
3329 : : * parent node.
3330 : : *
3331 : : * If itlist->has_non_vars is true, then we try to match whole subexpressions
3332 : : * against elements of the subplan tlist, so that we can avoid recomputing
3333 : : * expressions that were already computed by the subplan. (This is relatively
3334 : : * expensive, so we don't want to try it in the common case where the
3335 : : * subplan tlist is just a flattened list of Vars.)
3336 : : *
3337 : : * 'node': the tree to be fixed (a target item or qual)
3338 : : * 'subplan_itlist': indexed target list for subplan (or index)
3339 : : * 'newvarno': varno to use for Vars referencing tlist elements
3340 : : * 'rtoffset': how much to increment varnos by
3341 : : * 'nrm_match': as for search_indexed_tlist_for_var()
3342 : : * 'num_exec': estimated number of executions of expression
3343 : : *
3344 : : * The resulting tree is a copy of the original in which all Var nodes have
3345 : : * varno = newvarno, varattno = resno of corresponding targetlist element.
3346 : : * The original tree is not modified.
3347 : : */
3348 : : static Node *
3349 : 41380 : fix_upper_expr(PlannerInfo *root,
3350 : : Node *node,
3351 : : indexed_tlist *subplan_itlist,
3352 : : int newvarno,
3353 : : int rtoffset,
3354 : : NullingRelsMatch nrm_match,
3355 : : double num_exec)
3356 : : {
3357 : 41380 : fix_upper_expr_context context;
3358 : :
3359 : 41380 : context.root = root;
3360 : 41380 : context.subplan_itlist = subplan_itlist;
3361 : 41380 : context.newvarno = newvarno;
3362 : 41380 : context.rtoffset = rtoffset;
3363 : 41380 : context.nrm_match = nrm_match;
3364 : 41380 : context.num_exec = num_exec;
3365 : 82760 : return fix_upper_expr_mutator(node, &context);
3366 : 41380 : }
3367 : :
3368 : : static Node *
3369 : 123520 : fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
3370 : : {
3371 : 123520 : Var *newvar;
3372 : :
3373 [ + + ]: 123520 : if (node == NULL)
3374 : 41008 : return NULL;
3375 [ + + ]: 82512 : if (IsA(node, Var))
3376 : : {
3377 : 24277 : Var *var = (Var *) node;
3378 : :
3379 : 48554 : newvar = search_indexed_tlist_for_var(var,
3380 : 24277 : context->subplan_itlist,
3381 : 24277 : context->newvarno,
3382 : 24277 : context->rtoffset,
3383 : 24277 : context->nrm_match);
3384 [ + - ]: 24277 : if (!newvar)
3385 [ # # # # ]: 0 : elog(ERROR, "variable not found in subplan target list");
3386 : 24277 : return (Node *) newvar;
3387 : 24277 : }
3388 [ + + ]: 58235 : if (IsA(node, PlaceHolderVar))
3389 : : {
3390 : 182 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
3391 : :
3392 : : /* See if the PlaceHolderVar has bubbled up from a lower plan node */
3393 [ + + ]: 182 : if (context->subplan_itlist->has_ph_vars)
3394 : : {
3395 : 320 : newvar = search_indexed_tlist_for_phv(phv,
3396 : 160 : context->subplan_itlist,
3397 : 160 : context->newvarno,
3398 : 160 : context->nrm_match);
3399 [ + - ]: 160 : if (newvar)
3400 : 160 : return (Node *) newvar;
3401 : 0 : }
3402 : : /* If not supplied by input plan, evaluate the contained expr */
3403 : : /* XXX can we assert something about phnullingrels? */
3404 : 22 : return fix_upper_expr_mutator((Node *) phv->phexpr, context);
3405 : 182 : }
3406 : : /* Try matching more complex expressions too, if tlist has any */
3407 [ + + ]: 58053 : if (context->subplan_itlist->has_non_vars)
3408 : : {
3409 : 7318 : newvar = search_indexed_tlist_for_non_var((Expr *) node,
3410 : 3659 : context->subplan_itlist,
3411 : 3659 : context->newvarno);
3412 [ + + ]: 3659 : if (newvar)
3413 : 893 : return (Node *) newvar;
3414 : 2766 : }
3415 : : /* Special cases (apply only AFTER failing to match to lower tlist) */
3416 [ + + ]: 57160 : if (IsA(node, Param))
3417 : 1005 : return fix_param_node(context->root, (Param *) node);
3418 [ + + ]: 56155 : if (IsA(node, Aggref))
3419 : : {
3420 : 6421 : Aggref *aggref = (Aggref *) node;
3421 : 6421 : Param *aggparam;
3422 : :
3423 : : /* See if the Aggref should be replaced by a Param */
3424 : 6421 : aggparam = find_minmax_agg_replacement_param(context->root, aggref);
3425 [ - + ]: 6421 : if (aggparam != NULL)
3426 : : {
3427 : : /* Make a copy of the Param for paranoia's sake */
3428 : 0 : return (Node *) copyObject(aggparam);
3429 : : }
3430 : : /* If no match, just fall through to process it normally */
3431 [ - + ]: 6421 : }
3432 [ + + ]: 56155 : if (IsA(node, AlternativeSubPlan))
3433 : 18 : return fix_upper_expr_mutator(fix_alternative_subplan(context->root,
3434 : 6 : (AlternativeSubPlan *) node,
3435 : 6 : context->num_exec),
3436 : 6 : context);
3437 : 56149 : fix_expr_common(context->root, node);
3438 : 56149 : return expression_tree_mutator(node, fix_upper_expr_mutator, context);
3439 : 123520 : }
3440 : :
3441 : : /*
3442 : : * set_returning_clause_references
3443 : : * Perform setrefs.c's work on a RETURNING targetlist
3444 : : *
3445 : : * If the query involves more than just the result table, we have to
3446 : : * adjust any Vars that refer to other tables to reference junk tlist
3447 : : * entries in the top subplan's targetlist. Vars referencing the result
3448 : : * table should be left alone, however (the executor will evaluate them
3449 : : * using the actual heap tuple, after firing triggers if any). In the
3450 : : * adjusted RETURNING list, result-table Vars will have their original
3451 : : * varno (plus rtoffset), but Vars for other rels will have varno OUTER_VAR.
3452 : : *
3453 : : * We also must perform opcode lookup and add regclass OIDs to
3454 : : * root->glob->relationOids.
3455 : : *
3456 : : * 'rlist': the RETURNING targetlist to be fixed
3457 : : * 'topplan': the top subplan node that will be just below the ModifyTable
3458 : : * node (note it's not yet passed through set_plan_refs)
3459 : : * 'resultRelation': RT index of the associated result relation
3460 : : * 'rtoffset': how much to increment varnos by
3461 : : *
3462 : : * Note: the given 'root' is for the parent query level, not the 'topplan'.
3463 : : * This does not matter currently since we only access the dependency-item
3464 : : * lists in root->glob, but it would need some hacking if we wanted a root
3465 : : * that actually matches the subplan.
3466 : : *
3467 : : * Note: resultRelation is not yet adjusted by rtoffset.
3468 : : */
3469 : : static List *
3470 : 386 : set_returning_clause_references(PlannerInfo *root,
3471 : : List *rlist,
3472 : : Plan *topplan,
3473 : : Index resultRelation,
3474 : : int rtoffset)
3475 : : {
3476 : 386 : indexed_tlist *itlist;
3477 : :
3478 : : /*
3479 : : * We can perform the desired Var fixup by abusing the fix_join_expr
3480 : : * machinery that formerly handled inner indexscan fixup. We search the
3481 : : * top plan's targetlist for Vars of non-result relations, and use
3482 : : * fix_join_expr to convert RETURNING Vars into references to those tlist
3483 : : * entries, while leaving result-rel Vars as-is.
3484 : : *
3485 : : * PlaceHolderVars will also be sought in the targetlist, but no
3486 : : * more-complex expressions will be. Note that it is not possible for a
3487 : : * PlaceHolderVar to refer to the result relation, since the result is
3488 : : * never below an outer join. If that case could happen, we'd have to be
3489 : : * prepared to pick apart the PlaceHolderVar and evaluate its contained
3490 : : * expression instead.
3491 : : */
3492 : 386 : itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
3493 : :
3494 : 772 : rlist = fix_join_expr(root,
3495 : 386 : rlist,
3496 : 386 : itlist,
3497 : : NULL,
3498 : 386 : resultRelation,
3499 : 386 : rtoffset,
3500 : : NRM_EQUAL,
3501 : 386 : NUM_EXEC_TLIST(topplan));
3502 : :
3503 : 386 : pfree(itlist);
3504 : :
3505 : 772 : return rlist;
3506 : 386 : }
3507 : :
3508 : : /*
3509 : : * fix_windowagg_condition_expr_mutator
3510 : : * Mutator function for replacing WindowFuncs with the corresponding Var
3511 : : * in the targetlist which references that WindowFunc.
3512 : : */
3513 : : static Node *
3514 : 577 : fix_windowagg_condition_expr_mutator(Node *node,
3515 : : fix_windowagg_cond_context *context)
3516 : : {
3517 [ + + ]: 577 : if (node == NULL)
3518 : 429 : return NULL;
3519 : :
3520 [ + + ]: 148 : if (IsA(node, WindowFunc))
3521 : : {
3522 : 30 : Var *newvar;
3523 : :
3524 : 60 : newvar = search_indexed_tlist_for_non_var((Expr *) node,
3525 : 30 : context->subplan_itlist,
3526 : 30 : context->newvarno);
3527 [ + - ]: 30 : if (newvar)
3528 : 30 : return (Node *) newvar;
3529 [ # # # # ]: 0 : elog(ERROR, "WindowFunc not found in subplan target lists");
3530 [ - + - ]: 30 : }
3531 : :
3532 : 118 : return expression_tree_mutator(node,
3533 : : fix_windowagg_condition_expr_mutator,
3534 : : context);
3535 : 577 : }
3536 : :
3537 : : /*
3538 : : * fix_windowagg_condition_expr
3539 : : * Converts references in 'runcondition' so that any WindowFunc
3540 : : * references are swapped out for a Var which references the matching
3541 : : * WindowFunc in 'subplan_itlist'.
3542 : : */
3543 : : static List *
3544 : 457 : fix_windowagg_condition_expr(PlannerInfo *root,
3545 : : List *runcondition,
3546 : : indexed_tlist *subplan_itlist)
3547 : : {
3548 : 457 : fix_windowagg_cond_context context;
3549 : :
3550 : 457 : context.root = root;
3551 : 457 : context.subplan_itlist = subplan_itlist;
3552 : 457 : context.newvarno = 0;
3553 : :
3554 : 914 : return (List *) fix_windowagg_condition_expr_mutator((Node *) runcondition,
3555 : : &context);
3556 : 457 : }
3557 : :
3558 : : /*
3559 : : * set_windowagg_runcondition_references
3560 : : * Converts references in 'runcondition' so that any WindowFunc
3561 : : * references are swapped out for a Var which references the matching
3562 : : * WindowFunc in 'plan' targetlist.
3563 : : */
3564 : : static List *
3565 : 457 : set_windowagg_runcondition_references(PlannerInfo *root,
3566 : : List *runcondition,
3567 : : Plan *plan)
3568 : : {
3569 : 457 : List *newlist;
3570 : 457 : indexed_tlist *itlist;
3571 : :
3572 : 457 : itlist = build_tlist_index(plan->targetlist);
3573 : :
3574 : 457 : newlist = fix_windowagg_condition_expr(root, runcondition, itlist);
3575 : :
3576 : 457 : pfree(itlist);
3577 : :
3578 : 914 : return newlist;
3579 : 457 : }
3580 : :
3581 : : /*
3582 : : * find_minmax_agg_replacement_param
3583 : : * If the given Aggref is one that we are optimizing into a subquery
3584 : : * (cf. planagg.c), then return the Param that should replace it.
3585 : : * Else return NULL.
3586 : : *
3587 : : * This is exported so that SS_finalize_plan can use it before setrefs.c runs.
3588 : : * Note that it will not find anything until we have built a Plan from a
3589 : : * MinMaxAggPath, as root->minmax_aggs will never be filled otherwise.
3590 : : */
3591 : : Param *
3592 : 8414 : find_minmax_agg_replacement_param(PlannerInfo *root, Aggref *aggref)
3593 : : {
3594 [ + + - + ]: 8414 : if (root->minmax_aggs != NIL &&
3595 : 148 : list_length(aggref->args) == 1)
3596 : : {
3597 : 148 : TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
3598 : 148 : ListCell *lc;
3599 : :
3600 [ + - - + : 312 : foreach(lc, root->minmax_aggs)
+ - + - ]
3601 : : {
3602 : 164 : MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
3603 : :
3604 [ + + - + ]: 164 : if (mminfo->aggfnoid == aggref->aggfnoid &&
3605 : 148 : equal(mminfo->target, curTarget->expr))
3606 : 148 : return mminfo->param;
3607 [ + + ]: 164 : }
3608 [ - + - ]: 148 : }
3609 : 8266 : return NULL;
3610 : 8414 : }
3611 : :
3612 : :
3613 : : /*****************************************************************************
3614 : : * QUERY DEPENDENCY MANAGEMENT
3615 : : *****************************************************************************/
3616 : :
3617 : : /*
3618 : : * record_plan_function_dependency
3619 : : * Mark the current plan as depending on a particular function.
3620 : : *
3621 : : * This is exported so that the function-inlining code can record a
3622 : : * dependency on a function that it's removed from the plan tree.
3623 : : */
3624 : : void
3625 : 125749 : record_plan_function_dependency(PlannerInfo *root, Oid funcid)
3626 : : {
3627 : : /*
3628 : : * For performance reasons, we don't bother to track built-in functions;
3629 : : * we just assume they'll never change (or at least not in ways that'd
3630 : : * invalidate plans using them). For this purpose we can consider a
3631 : : * built-in function to be one with OID less than FirstUnpinnedObjectId.
3632 : : * Note that the OID generator guarantees never to generate such an OID
3633 : : * after startup, even at OID wraparound.
3634 : : */
3635 [ + + ]: 125749 : if (funcid >= (Oid) FirstUnpinnedObjectId)
3636 : : {
3637 : 2701 : PlanInvalItem *inval_item = makeNode(PlanInvalItem);
3638 : :
3639 : : /*
3640 : : * It would work to use any syscache on pg_proc, but the easiest is
3641 : : * PROCOID since we already have the function's OID at hand. Note
3642 : : * that plancache.c knows we use PROCOID.
3643 : : */
3644 : 2701 : inval_item->cacheId = PROCOID;
3645 : 2701 : inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
3646 : : ObjectIdGetDatum(funcid));
3647 : :
3648 : 2701 : root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3649 : 2701 : }
3650 : 125749 : }
3651 : :
3652 : : /*
3653 : : * record_plan_type_dependency
3654 : : * Mark the current plan as depending on a particular type.
3655 : : *
3656 : : * This is exported so that eval_const_expressions can record a
3657 : : * dependency on a domain that it's removed a CoerceToDomain node for.
3658 : : *
3659 : : * We don't currently need to record dependencies on domains that the
3660 : : * plan contains CoerceToDomain nodes for, though that might change in
3661 : : * future. Hence, this isn't actually called in this module, though
3662 : : * someday fix_expr_common might call it.
3663 : : */
3664 : : void
3665 : 906 : record_plan_type_dependency(PlannerInfo *root, Oid typid)
3666 : : {
3667 : : /*
3668 : : * As in record_plan_function_dependency, ignore the possibility that
3669 : : * someone would change a built-in domain.
3670 : : */
3671 [ - + ]: 906 : if (typid >= (Oid) FirstUnpinnedObjectId)
3672 : : {
3673 : 906 : PlanInvalItem *inval_item = makeNode(PlanInvalItem);
3674 : :
3675 : : /*
3676 : : * It would work to use any syscache on pg_type, but the easiest is
3677 : : * TYPEOID since we already have the type's OID at hand. Note that
3678 : : * plancache.c knows we use TYPEOID.
3679 : : */
3680 : 906 : inval_item->cacheId = TYPEOID;
3681 : 906 : inval_item->hashValue = GetSysCacheHashValue1(TYPEOID,
3682 : : ObjectIdGetDatum(typid));
3683 : :
3684 : 906 : root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3685 : 906 : }
3686 : 906 : }
3687 : :
3688 : : /*
3689 : : * extract_query_dependencies
3690 : : * Given a rewritten, but not yet planned, query or queries
3691 : : * (i.e. a Query node or list of Query nodes), extract dependencies
3692 : : * just as set_plan_references would do. Also detect whether any
3693 : : * rewrite steps were affected by RLS.
3694 : : *
3695 : : * This is needed by plancache.c to handle invalidation of cached unplanned
3696 : : * queries.
3697 : : *
3698 : : * Note: this does not go through eval_const_expressions, and hence doesn't
3699 : : * reflect its additions of inlined functions and elided CoerceToDomain nodes
3700 : : * to the invalItems list. This is obviously OK for functions, since we'll
3701 : : * see them in the original query tree anyway. For domains, it's OK because
3702 : : * we don't care about domains unless they get elided. That is, a plan might
3703 : : * have domain dependencies that the query tree doesn't.
3704 : : */
3705 : : void
3706 : 6037 : extract_query_dependencies(Node *query,
3707 : : List **relationOids,
3708 : : List **invalItems,
3709 : : bool *hasRowSecurity)
3710 : : {
3711 : 6037 : PlannerGlobal glob;
3712 : 6037 : PlannerInfo root;
3713 : :
3714 : : /* Make up dummy planner state so we can use this module's machinery */
3715 [ + - + - : 175073 : MemSet(&glob, 0, sizeof(glob));
+ - - + +
+ ]
3716 : 6037 : glob.type = T_PlannerGlobal;
3717 : 6037 : glob.relationOids = NIL;
3718 : 6037 : glob.invalItems = NIL;
3719 : : /* Hack: we use glob.dependsOnRole to collect hasRowSecurity flags */
3720 : 6037 : glob.dependsOnRole = false;
3721 : :
3722 [ + - + - : 561441 : MemSet(&root, 0, sizeof(root));
+ - - + +
+ ]
3723 : 6037 : root.type = T_PlannerInfo;
3724 : 6037 : root.glob = &glob;
3725 : :
3726 : 6037 : (void) extract_query_dependencies_walker(query, &root);
3727 : :
3728 : 6037 : *relationOids = glob.relationOids;
3729 : 6037 : *invalItems = glob.invalItems;
3730 : 6037 : *hasRowSecurity = glob.dependsOnRole;
3731 : 6037 : }
3732 : :
3733 : : /*
3734 : : * Tree walker for extract_query_dependencies.
3735 : : *
3736 : : * This is exported so that expression_planner_with_deps can call it on
3737 : : * simple expressions (post-planning, not before planning, in that case).
3738 : : * In that usage, glob.dependsOnRole isn't meaningful, but the relationOids
3739 : : * and invalItems lists are added to as needed.
3740 : : */
3741 : : bool
3742 : 154956 : extract_query_dependencies_walker(Node *node, PlannerInfo *context)
3743 : : {
3744 [ + + ]: 154956 : if (node == NULL)
3745 : 78015 : return false;
3746 [ + - ]: 76941 : Assert(!IsA(node, PlaceHolderVar));
3747 [ + + ]: 76941 : if (IsA(node, Query))
3748 : : {
3749 : 6543 : Query *query = (Query *) node;
3750 : 6543 : ListCell *lc;
3751 : :
3752 [ + + ]: 6543 : if (query->commandType == CMD_UTILITY)
3753 : : {
3754 : : /*
3755 : : * This logic must handle any utility command for which parse
3756 : : * analysis was nontrivial (cf. stmt_requires_parse_analysis).
3757 : : *
3758 : : * Notably, CALL requires its own processing.
3759 : : */
3760 [ + + ]: 1357 : if (IsA(query->utilityStmt, CallStmt))
3761 : : {
3762 : 2 : CallStmt *callstmt = (CallStmt *) query->utilityStmt;
3763 : :
3764 : : /* We need not examine funccall, just the transformed exprs */
3765 : 4 : (void) extract_query_dependencies_walker((Node *) callstmt->funcexpr,
3766 : 2 : context);
3767 : 4 : (void) extract_query_dependencies_walker((Node *) callstmt->outargs,
3768 : 2 : context);
3769 : 2 : return false;
3770 : 2 : }
3771 : :
3772 : : /*
3773 : : * Ignore other utility statements, except those (such as EXPLAIN)
3774 : : * that contain a parsed-but-not-planned query. For those, we
3775 : : * just need to transfer our attention to the contained query.
3776 : : */
3777 : 1355 : query = UtilityContainsQuery(query->utilityStmt);
3778 [ + + ]: 1355 : if (query == NULL)
3779 : 6 : return false;
3780 : 1349 : }
3781 : :
3782 : : /* Remember if any Query has RLS quals applied by rewriter */
3783 [ + + ]: 6535 : if (query->hasRowSecurity)
3784 : 37 : context->glob->dependsOnRole = true;
3785 : :
3786 : : /* Collect relation OIDs in this Query's rtable */
3787 [ + + + + : 10290 : foreach(lc, query->rtable)
+ + ]
3788 : : {
3789 : 3755 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3790 : :
3791 [ + + ]: 3755 : if (rte->rtekind == RTE_RELATION ||
3792 [ + + + + ]: 739 : (rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)) ||
3793 [ + + ]: 643 : (rte->rtekind == RTE_NAMEDTUPLESTORE && OidIsValid(rte->relid)))
3794 : 3189 : context->glob->relationOids =
3795 : 3189 : lappend_oid(context->glob->relationOids, rte->relid);
3796 : 3755 : }
3797 : :
3798 : : /* And recurse into the query's subexpressions */
3799 : 6535 : return query_tree_walker(query, extract_query_dependencies_walker,
3800 : : context, 0);
3801 : 6543 : }
3802 : : /* Extract function dependencies and check for regclass Consts */
3803 : 70398 : fix_expr_common(context, node);
3804 : 70398 : return expression_tree_walker(node, extract_query_dependencies_walker,
3805 : : context);
3806 : 154956 : }
3807 : :
3808 : : /*
3809 : : * Record some details about a node removed from the plan during setrefs
3810 : : * processing, for the benefit of code trying to reconstruct planner decisions
3811 : : * from examination of the final plan tree.
3812 : : */
3813 : : static void
3814 : 2648 : record_elided_node(PlannerGlobal *glob, int plan_node_id,
3815 : : NodeTag elided_type, Bitmapset *relids)
3816 : : {
3817 : 2648 : ElidedNode *n = makeNode(ElidedNode);
3818 : :
3819 : 2648 : n->plan_node_id = plan_node_id;
3820 : 2648 : n->elided_type = elided_type;
3821 : 2648 : n->relids = relids;
3822 : :
3823 : 2648 : glob->elidedNodes = lappend(glob->elidedNodes, n);
3824 : 2648 : }
|