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