]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/setrefs.c
6b8faf52b0a231449b559aaa15a1685f19eef71f
[postgresql] / src / backend / optimizer / plan / setrefs.c
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-2013, 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/pathnode.h"
23 #include "optimizer/planmain.h"
24 #include "optimizer/tlist.h"
25 #include "tcop/utility.h"
26 #include "utils/lsyscache.h"
27 #include "utils/syscache.h"
28
29
30 typedef struct
31 {
32         Index           varno;                  /* RT index of Var */
33         AttrNumber      varattno;               /* attr number of Var */
34         AttrNumber      resno;                  /* TLE position of Var */
35 } tlist_vinfo;
36
37 typedef struct
38 {
39         List       *tlist;                      /* underlying target list */
40         int                     num_vars;               /* number of plain Var tlist entries */
41         bool            has_ph_vars;    /* are there PlaceHolderVar entries? */
42         bool            has_non_vars;   /* are there other entries? */
43         /* array of num_vars entries: */
44         tlist_vinfo vars[1];            /* VARIABLE LENGTH ARRAY */
45 } indexed_tlist;                                /* VARIABLE LENGTH STRUCT */
46
47 typedef struct
48 {
49         PlannerInfo *root;
50         int                     rtoffset;
51 } fix_scan_expr_context;
52
53 typedef struct
54 {
55         PlannerInfo *root;
56         indexed_tlist *outer_itlist;
57         indexed_tlist *inner_itlist;
58         Index           acceptable_rel;
59         int                     rtoffset;
60 } fix_join_expr_context;
61
62 typedef struct
63 {
64         PlannerInfo *root;
65         indexed_tlist *subplan_itlist;
66         Index           newvarno;
67         int                     rtoffset;
68 } fix_upper_expr_context;
69
70 /*
71  * Check if a Const node is a regclass value.  We accept plain OID too,
72  * since a regclass Const will get folded to that type if it's an argument
73  * to oideq or similar operators.  (This might result in some extraneous
74  * values in a plan's list of relation dependencies, but the worst result
75  * would be occasional useless replans.)
76  */
77 #define ISREGCLASSCONST(con) \
78         (((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \
79          !(con)->constisnull)
80
81 #define fix_scan_list(root, lst, rtoffset) \
82         ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset))
83
84 static Plan *set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset);
85 static Plan *set_indexonlyscan_references(PlannerInfo *root,
86                                                          IndexOnlyScan *plan,
87                                                          int rtoffset);
88 static Plan *set_subqueryscan_references(PlannerInfo *root,
89                                                         SubqueryScan *plan,
90                                                         int rtoffset);
91 static bool trivial_subqueryscan(SubqueryScan *plan);
92 static Node *fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset);
93 static Node *fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context);
94 static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context);
95 static void set_join_references(PlannerInfo *root, Join *join, int rtoffset);
96 static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset);
97 static void set_dummy_tlist_references(Plan *plan, int rtoffset);
98 static indexed_tlist *build_tlist_index(List *tlist);
99 static Var *search_indexed_tlist_for_var(Var *var,
100                                                          indexed_tlist *itlist,
101                                                          Index newvarno,
102                                                          int rtoffset);
103 static Var *search_indexed_tlist_for_non_var(Node *node,
104                                                                  indexed_tlist *itlist,
105                                                                  Index newvarno);
106 static Var *search_indexed_tlist_for_sortgroupref(Node *node,
107                                                                           Index sortgroupref,
108                                                                           indexed_tlist *itlist,
109                                                                           Index newvarno);
110 static List *fix_join_expr(PlannerInfo *root,
111                           List *clauses,
112                           indexed_tlist *outer_itlist,
113                           indexed_tlist *inner_itlist,
114                           Index acceptable_rel, int rtoffset);
115 static Node *fix_join_expr_mutator(Node *node,
116                                           fix_join_expr_context *context);
117 static Node *fix_upper_expr(PlannerInfo *root,
118                            Node *node,
119                            indexed_tlist *subplan_itlist,
120                            Index newvarno,
121                            int rtoffset);
122 static Node *fix_upper_expr_mutator(Node *node,
123                                            fix_upper_expr_context *context);
124 static List *set_returning_clause_references(PlannerInfo *root,
125                                                                 List *rlist,
126                                                                 Plan *topplan,
127                                                                 Index resultRelation,
128                                                                 int rtoffset);
129 static bool fix_opfuncids_walker(Node *node, void *context);
130 static bool extract_query_dependencies_walker(Node *node,
131                                                                   PlannerInfo *context);
132
133
134 /*****************************************************************************
135  *
136  *              SUBPLAN REFERENCES
137  *
138  *****************************************************************************/
139
140 /*
141  * set_plan_references
142  *
143  * This is the final processing pass of the planner/optimizer.  The plan
144  * tree is complete; we just have to adjust some representational details
145  * for the convenience of the executor:
146  *
147  * 1. We flatten the various subquery rangetables into a single list, and
148  * zero out RangeTblEntry fields that are not useful to the executor.
149  *
150  * 2. We adjust Vars in scan nodes to be consistent with the flat rangetable.
151  *
152  * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
153  * subplans.
154  *
155  * 4. We compute regproc OIDs for operators (ie, we look up the function
156  * that implements each op).
157  *
158  * 5. We create lists of specific objects that the plan depends on.
159  * This will be used by plancache.c to drive invalidation of cached plans.
160  * Relation dependencies are represented by OIDs, and everything else by
161  * PlanInvalItems (this distinction is motivated by the shared-inval APIs).
162  * Currently, relations and user-defined functions are the only types of
163  * objects that are explicitly tracked this way.
164  *
165  * We also perform one final optimization step, which is to delete
166  * SubqueryScan plan nodes that aren't doing anything useful (ie, have
167  * no qual and a no-op targetlist).  The reason for doing this last is that
168  * it can't readily be done before set_plan_references, because it would
169  * break set_upper_references: the Vars in the subquery's top tlist
170  * wouldn't match up with the Vars in the outer plan tree.  The SubqueryScan
171  * serves a necessary function as a buffer between outer query and subquery
172  * variable numbering ... but after we've flattened the rangetable this is
173  * no longer a problem, since then there's only one rtindex namespace.
174  *
175  * set_plan_references recursively traverses the whole plan tree.
176  *
177  * The return value is normally the same Plan node passed in, but can be
178  * different when the passed-in Plan is a SubqueryScan we decide isn't needed.
179  *
180  * The flattened rangetable entries are appended to root->glob->finalrtable.
181  * Also, rowmarks entries are appended to root->glob->finalrowmarks, and the
182  * RT indexes of ModifyTable result relations to root->glob->resultRelations.
183  * Plan dependencies are appended to root->glob->relationOids (for relations)
184  * and root->glob->invalItems (for everything else).
185  *
186  * Notice that we modify Plan nodes in-place, but use expression_tree_mutator
187  * to process targetlist and qual expressions.  We can assume that the Plan
188  * nodes were just built by the planner and are not multiply referenced, but
189  * it's not so safe to assume that for expression tree nodes.
190  */
191 Plan *
192 set_plan_references(PlannerInfo *root, Plan *plan)
193 {
194         PlannerGlobal *glob = root->glob;
195         int                     rtoffset = list_length(glob->finalrtable);
196         ListCell   *lc;
197
198         /*
199          * In the flat rangetable, we zero out substructure pointers that are not
200          * needed by the executor; this reduces the storage space and copying cost
201          * for cached plans.  We keep only the alias and eref Alias fields, which
202          * are needed by EXPLAIN, and the selectedCols and modifiedCols bitmaps,
203          * which are needed for executor-startup permissions checking and for
204          * trigger event checking.
205          */
206         foreach(lc, root->parse->rtable)
207         {
208                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
209                 RangeTblEntry *newrte;
210
211                 /* flat copy to duplicate all the scalar fields */
212                 newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
213                 memcpy(newrte, rte, sizeof(RangeTblEntry));
214
215                 /* zap unneeded sub-structure */
216                 newrte->subquery = NULL;
217                 newrte->joinaliasvars = NIL;
218                 newrte->funcexpr = NULL;
219                 newrte->funccoltypes = NIL;
220                 newrte->funccoltypmods = NIL;
221                 newrte->funccolcollations = NIL;
222                 newrte->values_lists = NIL;
223                 newrte->values_collations = NIL;
224                 newrte->ctecoltypes = NIL;
225                 newrte->ctecoltypmods = NIL;
226                 newrte->ctecolcollations = NIL;
227
228                 glob->finalrtable = lappend(glob->finalrtable, newrte);
229
230                 /*
231                  * If it's a plain relation RTE, add the table to relationOids.
232                  *
233                  * We do this even though the RTE might be unreferenced in the plan
234                  * tree; this would correspond to cases such as views that were
235                  * expanded, child tables that were eliminated by constraint
236                  * exclusion, etc.      Schema invalidation on such a rel must still force
237                  * rebuilding of the plan.
238                  *
239                  * Note we don't bother to avoid duplicate list entries.  We could,
240                  * but it would probably cost more cycles than it would save.
241                  */
242                 if (newrte->rtekind == RTE_RELATION)
243                         glob->relationOids = lappend_oid(glob->relationOids,
244                                                                                          newrte->relid);
245         }
246
247         /*
248          * Check for RT index overflow; it's very unlikely, but if it did happen,
249          * the executor would get confused by varnos that match the special varno
250          * values.
251          */
252         if (IS_SPECIAL_VARNO(list_length(glob->finalrtable)))
253                 ereport(ERROR,
254                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
255                                  errmsg("too many range table entries")));
256
257         /*
258          * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
259          */
260         foreach(lc, root->rowMarks)
261         {
262                 PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
263                 PlanRowMark *newrc;
264
265                 Assert(IsA(rc, PlanRowMark));
266
267                 /* flat copy is enough since all fields are scalars */
268                 newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark));
269                 memcpy(newrc, rc, sizeof(PlanRowMark));
270
271                 /* adjust indexes ... but *not* the rowmarkId */
272                 newrc->rti += rtoffset;
273                 newrc->prti += rtoffset;
274
275                 glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
276         }
277
278         /* Now fix the Plan tree */
279         return set_plan_refs(root, plan, rtoffset);
280 }
281
282 /*
283  * set_plan_refs: recurse through the Plan nodes of a single subquery level
284  */
285 static Plan *
286 set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
287 {
288         ListCell   *l;
289
290         if (plan == NULL)
291                 return NULL;
292
293         /*
294          * Plan-type-specific fixes
295          */
296         switch (nodeTag(plan))
297         {
298                 case T_SeqScan:
299                         {
300                                 SeqScan    *splan = (SeqScan *) plan;
301
302                                 splan->scanrelid += rtoffset;
303                                 splan->plan.targetlist =
304                                         fix_scan_list(root, splan->plan.targetlist, rtoffset);
305                                 splan->plan.qual =
306                                         fix_scan_list(root, splan->plan.qual, rtoffset);
307                         }
308                         break;
309                 case T_IndexScan:
310                         {
311                                 IndexScan  *splan = (IndexScan *) plan;
312
313                                 splan->scan.scanrelid += rtoffset;
314                                 splan->scan.plan.targetlist =
315                                         fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
316                                 splan->scan.plan.qual =
317                                         fix_scan_list(root, splan->scan.plan.qual, rtoffset);
318                                 splan->indexqual =
319                                         fix_scan_list(root, splan->indexqual, rtoffset);
320                                 splan->indexqualorig =
321                                         fix_scan_list(root, splan->indexqualorig, rtoffset);
322                                 splan->indexorderby =
323                                         fix_scan_list(root, splan->indexorderby, rtoffset);
324                                 splan->indexorderbyorig =
325                                         fix_scan_list(root, splan->indexorderbyorig, rtoffset);
326                         }
327                         break;
328                 case T_IndexOnlyScan:
329                         {
330                                 IndexOnlyScan *splan = (IndexOnlyScan *) plan;
331
332                                 return set_indexonlyscan_references(root, splan, rtoffset);
333                         }
334                         break;
335                 case T_BitmapIndexScan:
336                         {
337                                 BitmapIndexScan *splan = (BitmapIndexScan *) plan;
338
339                                 splan->scan.scanrelid += rtoffset;
340                                 /* no need to fix targetlist and qual */
341                                 Assert(splan->scan.plan.targetlist == NIL);
342                                 Assert(splan->scan.plan.qual == NIL);
343                                 splan->indexqual =
344                                         fix_scan_list(root, splan->indexqual, rtoffset);
345                                 splan->indexqualorig =
346                                         fix_scan_list(root, splan->indexqualorig, rtoffset);
347                         }
348                         break;
349                 case T_BitmapHeapScan:
350                         {
351                                 BitmapHeapScan *splan = (BitmapHeapScan *) plan;
352
353                                 splan->scan.scanrelid += rtoffset;
354                                 splan->scan.plan.targetlist =
355                                         fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
356                                 splan->scan.plan.qual =
357                                         fix_scan_list(root, splan->scan.plan.qual, rtoffset);
358                                 splan->bitmapqualorig =
359                                         fix_scan_list(root, splan->bitmapqualorig, rtoffset);
360                         }
361                         break;
362                 case T_TidScan:
363                         {
364                                 TidScan    *splan = (TidScan *) plan;
365
366                                 splan->scan.scanrelid += rtoffset;
367                                 splan->scan.plan.targetlist =
368                                         fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
369                                 splan->scan.plan.qual =
370                                         fix_scan_list(root, splan->scan.plan.qual, rtoffset);
371                                 splan->tidquals =
372                                         fix_scan_list(root, splan->tidquals, rtoffset);
373                         }
374                         break;
375                 case T_SubqueryScan:
376                         /* Needs special treatment, see comments below */
377                         return set_subqueryscan_references(root,
378                                                                                            (SubqueryScan *) plan,
379                                                                                            rtoffset);
380                 case T_FunctionScan:
381                         {
382                                 FunctionScan *splan = (FunctionScan *) plan;
383
384                                 splan->scan.scanrelid += rtoffset;
385                                 splan->scan.plan.targetlist =
386                                         fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
387                                 splan->scan.plan.qual =
388                                         fix_scan_list(root, splan->scan.plan.qual, rtoffset);
389                                 splan->funcexpr =
390                                         fix_scan_expr(root, splan->funcexpr, rtoffset);
391                         }
392                         break;
393                 case T_ValuesScan:
394                         {
395                                 ValuesScan *splan = (ValuesScan *) plan;
396
397                                 splan->scan.scanrelid += rtoffset;
398                                 splan->scan.plan.targetlist =
399                                         fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
400                                 splan->scan.plan.qual =
401                                         fix_scan_list(root, splan->scan.plan.qual, rtoffset);
402                                 splan->values_lists =
403                                         fix_scan_list(root, splan->values_lists, rtoffset);
404                         }
405                         break;
406                 case T_CteScan:
407                         {
408                                 CteScan    *splan = (CteScan *) plan;
409
410                                 splan->scan.scanrelid += rtoffset;
411                                 splan->scan.plan.targetlist =
412                                         fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
413                                 splan->scan.plan.qual =
414                                         fix_scan_list(root, splan->scan.plan.qual, rtoffset);
415                         }
416                         break;
417                 case T_WorkTableScan:
418                         {
419                                 WorkTableScan *splan = (WorkTableScan *) plan;
420
421                                 splan->scan.scanrelid += rtoffset;
422                                 splan->scan.plan.targetlist =
423                                         fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
424                                 splan->scan.plan.qual =
425                                         fix_scan_list(root, splan->scan.plan.qual, rtoffset);
426                         }
427                         break;
428                 case T_ForeignScan:
429                         {
430                                 ForeignScan *splan = (ForeignScan *) plan;
431
432                                 splan->scan.scanrelid += rtoffset;
433                                 splan->scan.plan.targetlist =
434                                         fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
435                                 splan->scan.plan.qual =
436                                         fix_scan_list(root, splan->scan.plan.qual, rtoffset);
437                                 splan->fdw_exprs =
438                                         fix_scan_list(root, splan->fdw_exprs, rtoffset);
439                         }
440                         break;
441
442                 case T_NestLoop:
443                 case T_MergeJoin:
444                 case T_HashJoin:
445                         set_join_references(root, (Join *) plan, rtoffset);
446                         break;
447
448                 case T_Hash:
449                 case T_Material:
450                 case T_Sort:
451                 case T_Unique:
452                 case T_SetOp:
453
454                         /*
455                          * These plan types don't actually bother to evaluate their
456                          * targetlists, because they just return their unmodified input
457                          * tuples.      Even though the targetlist won't be used by the
458                          * executor, we fix it up for possible use by EXPLAIN (not to
459                          * mention ease of debugging --- wrong varnos are very confusing).
460                          */
461                         set_dummy_tlist_references(plan, rtoffset);
462
463                         /*
464                          * Since these plan types don't check quals either, we should not
465                          * find any qual expression attached to them.
466                          */
467                         Assert(plan->qual == NIL);
468                         break;
469                 case T_LockRows:
470                         {
471                                 LockRows   *splan = (LockRows *) plan;
472
473                                 /*
474                                  * Like the plan types above, LockRows doesn't evaluate its
475                                  * tlist or quals.      But we have to fix up the RT indexes in
476                                  * its rowmarks.
477                                  */
478                                 set_dummy_tlist_references(plan, rtoffset);
479                                 Assert(splan->plan.qual == NIL);
480
481                                 foreach(l, splan->rowMarks)
482                                 {
483                                         PlanRowMark *rc = (PlanRowMark *) lfirst(l);
484
485                                         rc->rti += rtoffset;
486                                         rc->prti += rtoffset;
487                                 }
488                         }
489                         break;
490                 case T_Limit:
491                         {
492                                 Limit      *splan = (Limit *) plan;
493
494                                 /*
495                                  * Like the plan types above, Limit doesn't evaluate its tlist
496                                  * or quals.  It does have live expressions for limit/offset,
497                                  * however; and those cannot contain subplan variable refs, so
498                                  * fix_scan_expr works for them.
499                                  */
500                                 set_dummy_tlist_references(plan, rtoffset);
501                                 Assert(splan->plan.qual == NIL);
502
503                                 splan->limitOffset =
504                                         fix_scan_expr(root, splan->limitOffset, rtoffset);
505                                 splan->limitCount =
506                                         fix_scan_expr(root, splan->limitCount, rtoffset);
507                         }
508                         break;
509                 case T_Agg:
510                 case T_Group:
511                         set_upper_references(root, plan, rtoffset);
512                         break;
513                 case T_WindowAgg:
514                         {
515                                 WindowAgg  *wplan = (WindowAgg *) plan;
516
517                                 set_upper_references(root, plan, rtoffset);
518
519                                 /*
520                                  * Like Limit node limit/offset expressions, WindowAgg has
521                                  * frame offset expressions, which cannot contain subplan
522                                  * variable refs, so fix_scan_expr works for them.
523                                  */
524                                 wplan->startOffset =
525                                         fix_scan_expr(root, wplan->startOffset, rtoffset);
526                                 wplan->endOffset =
527                                         fix_scan_expr(root, wplan->endOffset, rtoffset);
528                         }
529                         break;
530                 case T_Result:
531                         {
532                                 Result     *splan = (Result *) plan;
533
534                                 /*
535                                  * Result may or may not have a subplan; if not, it's more
536                                  * like a scan node than an upper node.
537                                  */
538                                 if (splan->plan.lefttree != NULL)
539                                         set_upper_references(root, plan, rtoffset);
540                                 else
541                                 {
542                                         splan->plan.targetlist =
543                                                 fix_scan_list(root, splan->plan.targetlist, rtoffset);
544                                         splan->plan.qual =
545                                                 fix_scan_list(root, splan->plan.qual, rtoffset);
546                                 }
547                                 /* resconstantqual can't contain any subplan variable refs */
548                                 splan->resconstantqual =
549                                         fix_scan_expr(root, splan->resconstantqual, rtoffset);
550                         }
551                         break;
552                 case T_ModifyTable:
553                         {
554                                 ModifyTable *splan = (ModifyTable *) plan;
555
556                                 Assert(splan->plan.targetlist == NIL);
557                                 Assert(splan->plan.qual == NIL);
558
559                                 if (splan->returningLists)
560                                 {
561                                         List       *newRL = NIL;
562                                         ListCell   *lcrl,
563                                                            *lcrr,
564                                                            *lcp;
565
566                                         /*
567                                          * Pass each per-subplan returningList through
568                                          * set_returning_clause_references().
569                                          */
570                                         Assert(list_length(splan->returningLists) == list_length(splan->resultRelations));
571                                         Assert(list_length(splan->returningLists) == list_length(splan->plans));
572                                         forthree(lcrl, splan->returningLists,
573                                                          lcrr, splan->resultRelations,
574                                                          lcp, splan->plans)
575                                         {
576                                                 List       *rlist = (List *) lfirst(lcrl);
577                                                 Index           resultrel = lfirst_int(lcrr);
578                                                 Plan       *subplan = (Plan *) lfirst(lcp);
579
580                                                 rlist = set_returning_clause_references(root,
581                                                                                                                                 rlist,
582                                                                                                                                 subplan,
583                                                                                                                                 resultrel,
584                                                                                                                                 rtoffset);
585                                                 newRL = lappend(newRL, rlist);
586                                         }
587                                         splan->returningLists = newRL;
588
589                                         /*
590                                          * Set up the visible plan targetlist as being the same as
591                                          * the first RETURNING list. This is for the use of
592                                          * EXPLAIN; the executor won't pay any attention to the
593                                          * targetlist.  We postpone this step until here so that
594                                          * we don't have to do set_returning_clause_references()
595                                          * twice on identical targetlists.
596                                          */
597                                         splan->plan.targetlist = copyObject(linitial(newRL));
598                                 }
599
600                                 foreach(l, splan->resultRelations)
601                                 {
602                                         lfirst_int(l) += rtoffset;
603                                 }
604                                 foreach(l, splan->rowMarks)
605                                 {
606                                         PlanRowMark *rc = (PlanRowMark *) lfirst(l);
607
608                                         rc->rti += rtoffset;
609                                         rc->prti += rtoffset;
610                                 }
611                                 foreach(l, splan->plans)
612                                 {
613                                         lfirst(l) = set_plan_refs(root,
614                                                                                           (Plan *) lfirst(l),
615                                                                                           rtoffset);
616                                 }
617
618                                 /*
619                                  * Append this ModifyTable node's final result relation RT
620                                  * index(es) to the global list for the plan, and set its
621                                  * resultRelIndex to reflect their starting position in the
622                                  * global list.
623                                  */
624                                 splan->resultRelIndex = list_length(root->glob->resultRelations);
625                                 root->glob->resultRelations =
626                                         list_concat(root->glob->resultRelations,
627                                                                 list_copy(splan->resultRelations));
628                         }
629                         break;
630                 case T_Append:
631                         {
632                                 Append     *splan = (Append *) plan;
633
634                                 /*
635                                  * Append, like Sort et al, doesn't actually evaluate its
636                                  * targetlist or check quals.
637                                  */
638                                 set_dummy_tlist_references(plan, rtoffset);
639                                 Assert(splan->plan.qual == NIL);
640                                 foreach(l, splan->appendplans)
641                                 {
642                                         lfirst(l) = set_plan_refs(root,
643                                                                                           (Plan *) lfirst(l),
644                                                                                           rtoffset);
645                                 }
646                         }
647                         break;
648                 case T_MergeAppend:
649                         {
650                                 MergeAppend *splan = (MergeAppend *) plan;
651
652                                 /*
653                                  * MergeAppend, like Sort et al, doesn't actually evaluate its
654                                  * targetlist or check quals.
655                                  */
656                                 set_dummy_tlist_references(plan, rtoffset);
657                                 Assert(splan->plan.qual == NIL);
658                                 foreach(l, splan->mergeplans)
659                                 {
660                                         lfirst(l) = set_plan_refs(root,
661                                                                                           (Plan *) lfirst(l),
662                                                                                           rtoffset);
663                                 }
664                         }
665                         break;
666                 case T_RecursiveUnion:
667                         /* This doesn't evaluate targetlist or check quals either */
668                         set_dummy_tlist_references(plan, rtoffset);
669                         Assert(plan->qual == NIL);
670                         break;
671                 case T_BitmapAnd:
672                         {
673                                 BitmapAnd  *splan = (BitmapAnd *) plan;
674
675                                 /* BitmapAnd works like Append, but has no tlist */
676                                 Assert(splan->plan.targetlist == NIL);
677                                 Assert(splan->plan.qual == NIL);
678                                 foreach(l, splan->bitmapplans)
679                                 {
680                                         lfirst(l) = set_plan_refs(root,
681                                                                                           (Plan *) lfirst(l),
682                                                                                           rtoffset);
683                                 }
684                         }
685                         break;
686                 case T_BitmapOr:
687                         {
688                                 BitmapOr   *splan = (BitmapOr *) plan;
689
690                                 /* BitmapOr works like Append, but has no tlist */
691                                 Assert(splan->plan.targetlist == NIL);
692                                 Assert(splan->plan.qual == NIL);
693                                 foreach(l, splan->bitmapplans)
694                                 {
695                                         lfirst(l) = set_plan_refs(root,
696                                                                                           (Plan *) lfirst(l),
697                                                                                           rtoffset);
698                                 }
699                         }
700                         break;
701                 default:
702                         elog(ERROR, "unrecognized node type: %d",
703                                  (int) nodeTag(plan));
704                         break;
705         }
706
707         /*
708          * Now recurse into child plans, if any
709          *
710          * NOTE: it is essential that we recurse into child plans AFTER we set
711          * subplan references in this plan's tlist and quals.  If we did the
712          * reference-adjustments bottom-up, then we would fail to match this
713          * plan's var nodes against the already-modified nodes of the children.
714          */
715         plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
716         plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
717
718         return plan;
719 }
720
721 /*
722  * set_indexonlyscan_references
723  *              Do set_plan_references processing on an IndexOnlyScan
724  *
725  * This is unlike the handling of a plain IndexScan because we have to
726  * convert Vars referencing the heap into Vars referencing the index.
727  * We can use the fix_upper_expr machinery for that, by working from a
728  * targetlist describing the index columns.
729  */
730 static Plan *
731 set_indexonlyscan_references(PlannerInfo *root,
732                                                          IndexOnlyScan *plan,
733                                                          int rtoffset)
734 {
735         indexed_tlist *index_itlist;
736
737         index_itlist = build_tlist_index(plan->indextlist);
738
739         plan->scan.scanrelid += rtoffset;
740         plan->scan.plan.targetlist = (List *)
741                 fix_upper_expr(root,
742                                            (Node *) plan->scan.plan.targetlist,
743                                            index_itlist,
744                                            INDEX_VAR,
745                                            rtoffset);
746         plan->scan.plan.qual = (List *)
747                 fix_upper_expr(root,
748                                            (Node *) plan->scan.plan.qual,
749                                            index_itlist,
750                                            INDEX_VAR,
751                                            rtoffset);
752         /* indexqual is already transformed to reference index columns */
753         plan->indexqual = fix_scan_list(root, plan->indexqual, rtoffset);
754         /* indexorderby is already transformed to reference index columns */
755         plan->indexorderby = fix_scan_list(root, plan->indexorderby, rtoffset);
756         /* indextlist must NOT be transformed to reference index columns */
757         plan->indextlist = fix_scan_list(root, plan->indextlist, rtoffset);
758
759         pfree(index_itlist);
760
761         return (Plan *) plan;
762 }
763
764 /*
765  * set_subqueryscan_references
766  *              Do set_plan_references processing on a SubqueryScan
767  *
768  * We try to strip out the SubqueryScan entirely; if we can't, we have
769  * to do the normal processing on it.
770  */
771 static Plan *
772 set_subqueryscan_references(PlannerInfo *root,
773                                                         SubqueryScan *plan,
774                                                         int rtoffset)
775 {
776         RelOptInfo *rel;
777         Plan       *result;
778
779         /* Need to look up the subquery's RelOptInfo, since we need its subroot */
780         rel = find_base_rel(root, plan->scan.scanrelid);
781         Assert(rel->subplan == plan->subplan);
782
783         /* Recursively process the subplan */
784         plan->subplan = set_plan_references(rel->subroot, plan->subplan);
785
786         if (trivial_subqueryscan(plan))
787         {
788                 /*
789                  * We can omit the SubqueryScan node and just pull up the subplan.
790                  */
791                 ListCell   *lp,
792                                    *lc;
793
794                 result = plan->subplan;
795
796                 /* We have to be sure we don't lose any initplans */
797                 result->initPlan = list_concat(plan->scan.plan.initPlan,
798                                                                            result->initPlan);
799
800                 /*
801                  * We also have to transfer the SubqueryScan's result-column names
802                  * into the subplan, else columns sent to client will be improperly
803                  * labeled if this is the topmost plan level.  Copy the "source
804                  * column" information too.
805                  */
806                 forboth(lp, plan->scan.plan.targetlist, lc, result->targetlist)
807                 {
808                         TargetEntry *ptle = (TargetEntry *) lfirst(lp);
809                         TargetEntry *ctle = (TargetEntry *) lfirst(lc);
810
811                         ctle->resname = ptle->resname;
812                         ctle->resorigtbl = ptle->resorigtbl;
813                         ctle->resorigcol = ptle->resorigcol;
814                 }
815         }
816         else
817         {
818                 /*
819                  * Keep the SubqueryScan node.  We have to do the processing that
820                  * set_plan_references would otherwise have done on it.  Notice we do
821                  * not do set_upper_references() here, because a SubqueryScan will
822                  * always have been created with correct references to its subplan's
823                  * outputs to begin with.
824                  */
825                 plan->scan.scanrelid += rtoffset;
826                 plan->scan.plan.targetlist =
827                         fix_scan_list(root, plan->scan.plan.targetlist, rtoffset);
828                 plan->scan.plan.qual =
829                         fix_scan_list(root, plan->scan.plan.qual, rtoffset);
830
831                 result = (Plan *) plan;
832         }
833
834         return result;
835 }
836
837 /*
838  * trivial_subqueryscan
839  *              Detect whether a SubqueryScan can be deleted from the plan tree.
840  *
841  * We can delete it if it has no qual to check and the targetlist just
842  * regurgitates the output of the child plan.
843  */
844 static bool
845 trivial_subqueryscan(SubqueryScan *plan)
846 {
847         int                     attrno;
848         ListCell   *lp,
849                            *lc;
850
851         if (plan->scan.plan.qual != NIL)
852                 return false;
853
854         if (list_length(plan->scan.plan.targetlist) !=
855                 list_length(plan->subplan->targetlist))
856                 return false;                   /* tlists not same length */
857
858         attrno = 1;
859         forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
860         {
861                 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
862                 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
863
864                 if (ptle->resjunk != ctle->resjunk)
865                         return false;           /* tlist doesn't match junk status */
866
867                 /*
868                  * We accept either a Var referencing the corresponding element of the
869                  * subplan tlist, or a Const equaling the subplan element. See
870                  * generate_setop_tlist() for motivation.
871                  */
872                 if (ptle->expr && IsA(ptle->expr, Var))
873                 {
874                         Var                *var = (Var *) ptle->expr;
875
876                         Assert(var->varno == plan->scan.scanrelid);
877                         Assert(var->varlevelsup == 0);
878                         if (var->varattno != attrno)
879                                 return false;   /* out of order */
880                 }
881                 else if (ptle->expr && IsA(ptle->expr, Const))
882                 {
883                         if (!equal(ptle->expr, ctle->expr))
884                                 return false;
885                 }
886                 else
887                         return false;
888
889                 attrno++;
890         }
891
892         return true;
893 }
894
895 /*
896  * copyVar
897  *              Copy a Var node.
898  *
899  * fix_scan_expr and friends do this enough times that it's worth having
900  * a bespoke routine instead of using the generic copyObject() function.
901  */
902 static inline Var *
903 copyVar(Var *var)
904 {
905         Var                *newvar = (Var *) palloc(sizeof(Var));
906
907         *newvar = *var;
908         return newvar;
909 }
910
911 /*
912  * fix_expr_common
913  *              Do generic set_plan_references processing on an expression node
914  *
915  * This is code that is common to all variants of expression-fixing.
916  * We must look up operator opcode info for OpExpr and related nodes,
917  * add OIDs from regclass Const nodes into root->glob->relationOids, and
918  * add catalog TIDs for user-defined functions into root->glob->invalItems.
919  *
920  * We assume it's okay to update opcode info in-place.  So this could possibly
921  * scribble on the planner's input data structures, but it's OK.
922  */
923 static void
924 fix_expr_common(PlannerInfo *root, Node *node)
925 {
926         /* We assume callers won't call us on a NULL pointer */
927         if (IsA(node, Aggref))
928         {
929                 record_plan_function_dependency(root,
930                                                                                 ((Aggref *) node)->aggfnoid);
931         }
932         else if (IsA(node, WindowFunc))
933         {
934                 record_plan_function_dependency(root,
935                                                                                 ((WindowFunc *) node)->winfnoid);
936         }
937         else if (IsA(node, FuncExpr))
938         {
939                 record_plan_function_dependency(root,
940                                                                                 ((FuncExpr *) node)->funcid);
941         }
942         else if (IsA(node, OpExpr))
943         {
944                 set_opfuncid((OpExpr *) node);
945                 record_plan_function_dependency(root,
946                                                                                 ((OpExpr *) node)->opfuncid);
947         }
948         else if (IsA(node, DistinctExpr))
949         {
950                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
951                 record_plan_function_dependency(root,
952                                                                                 ((DistinctExpr *) node)->opfuncid);
953         }
954         else if (IsA(node, NullIfExpr))
955         {
956                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
957                 record_plan_function_dependency(root,
958                                                                                 ((NullIfExpr *) node)->opfuncid);
959         }
960         else if (IsA(node, ScalarArrayOpExpr))
961         {
962                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
963                 record_plan_function_dependency(root,
964                                                                          ((ScalarArrayOpExpr *) node)->opfuncid);
965         }
966         else if (IsA(node, ArrayCoerceExpr))
967         {
968                 if (OidIsValid(((ArrayCoerceExpr *) node)->elemfuncid))
969                         record_plan_function_dependency(root,
970                                                                          ((ArrayCoerceExpr *) node)->elemfuncid);
971         }
972         else if (IsA(node, Const))
973         {
974                 Const      *con = (Const *) node;
975
976                 /* Check for regclass reference */
977                 if (ISREGCLASSCONST(con))
978                         root->glob->relationOids =
979                                 lappend_oid(root->glob->relationOids,
980                                                         DatumGetObjectId(con->constvalue));
981         }
982 }
983
984 /*
985  * fix_scan_expr
986  *              Do set_plan_references processing on a scan-level expression
987  *
988  * This consists of incrementing all Vars' varnos by rtoffset,
989  * looking up operator opcode info for OpExpr and related nodes,
990  * and adding OIDs from regclass Const nodes into root->glob->relationOids.
991  */
992 static Node *
993 fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset)
994 {
995         fix_scan_expr_context context;
996
997         context.root = root;
998         context.rtoffset = rtoffset;
999
1000         if (rtoffset != 0 || root->glob->lastPHId != 0)
1001         {
1002                 return fix_scan_expr_mutator(node, &context);
1003         }
1004         else
1005         {
1006                 /*
1007                  * If rtoffset == 0, we don't need to change any Vars, and if there
1008                  * are no placeholders anywhere we won't need to remove them.  Then
1009                  * it's OK to just scribble on the input node tree instead of copying
1010                  * (since the only change, filling in any unset opfuncid fields, is
1011                  * harmless).  This saves just enough cycles to be noticeable on
1012                  * trivial queries.
1013                  */
1014                 (void) fix_scan_expr_walker(node, &context);
1015                 return node;
1016         }
1017 }
1018
1019 static Node *
1020 fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
1021 {
1022         if (node == NULL)
1023                 return NULL;
1024         if (IsA(node, Var))
1025         {
1026                 Var                *var = copyVar((Var *) node);
1027
1028                 Assert(var->varlevelsup == 0);
1029
1030                 /*
1031                  * We should not see any Vars marked INNER_VAR or OUTER_VAR.  But an
1032                  * indexqual expression could contain INDEX_VAR Vars.
1033                  */
1034                 Assert(var->varno != INNER_VAR);
1035                 Assert(var->varno != OUTER_VAR);
1036                 if (!IS_SPECIAL_VARNO(var->varno))
1037                         var->varno += context->rtoffset;
1038                 if (var->varnoold > 0)
1039                         var->varnoold += context->rtoffset;
1040                 return (Node *) var;
1041         }
1042         if (IsA(node, CurrentOfExpr))
1043         {
1044                 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
1045
1046                 Assert(cexpr->cvarno != INNER_VAR);
1047                 Assert(cexpr->cvarno != OUTER_VAR);
1048                 if (!IS_SPECIAL_VARNO(cexpr->cvarno))
1049                         cexpr->cvarno += context->rtoffset;
1050                 return (Node *) cexpr;
1051         }
1052         if (IsA(node, PlaceHolderVar))
1053         {
1054                 /* At scan level, we should always just evaluate the contained expr */
1055                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1056
1057                 return fix_scan_expr_mutator((Node *) phv->phexpr, context);
1058         }
1059         fix_expr_common(context->root, node);
1060         return expression_tree_mutator(node, fix_scan_expr_mutator,
1061                                                                    (void *) context);
1062 }
1063
1064 static bool
1065 fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
1066 {
1067         if (node == NULL)
1068                 return false;
1069         Assert(!IsA(node, PlaceHolderVar));
1070         fix_expr_common(context->root, node);
1071         return expression_tree_walker(node, fix_scan_expr_walker,
1072                                                                   (void *) context);
1073 }
1074
1075 /*
1076  * set_join_references
1077  *        Modify the target list and quals of a join node to reference its
1078  *        subplans, by setting the varnos to OUTER_VAR or INNER_VAR and setting
1079  *        attno values to the result domain number of either the corresponding
1080  *        outer or inner join tuple item.  Also perform opcode lookup for these
1081  *        expressions. and add regclass OIDs to root->glob->relationOids.
1082  */
1083 static void
1084 set_join_references(PlannerInfo *root, Join *join, int rtoffset)
1085 {
1086         Plan       *outer_plan = join->plan.lefttree;
1087         Plan       *inner_plan = join->plan.righttree;
1088         indexed_tlist *outer_itlist;
1089         indexed_tlist *inner_itlist;
1090
1091         outer_itlist = build_tlist_index(outer_plan->targetlist);
1092         inner_itlist = build_tlist_index(inner_plan->targetlist);
1093
1094         /* All join plans have tlist, qual, and joinqual */
1095         join->plan.targetlist = fix_join_expr(root,
1096                                                                                   join->plan.targetlist,
1097                                                                                   outer_itlist,
1098                                                                                   inner_itlist,
1099                                                                                   (Index) 0,
1100                                                                                   rtoffset);
1101         join->plan.qual = fix_join_expr(root,
1102                                                                         join->plan.qual,
1103                                                                         outer_itlist,
1104                                                                         inner_itlist,
1105                                                                         (Index) 0,
1106                                                                         rtoffset);
1107         join->joinqual = fix_join_expr(root,
1108                                                                    join->joinqual,
1109                                                                    outer_itlist,
1110                                                                    inner_itlist,
1111                                                                    (Index) 0,
1112                                                                    rtoffset);
1113
1114         /* Now do join-type-specific stuff */
1115         if (IsA(join, NestLoop))
1116         {
1117                 NestLoop   *nl = (NestLoop *) join;
1118                 ListCell   *lc;
1119
1120                 foreach(lc, nl->nestParams)
1121                 {
1122                         NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
1123
1124                         nlp->paramval = (Var *) fix_upper_expr(root,
1125                                                                                                    (Node *) nlp->paramval,
1126                                                                                                    outer_itlist,
1127                                                                                                    OUTER_VAR,
1128                                                                                                    rtoffset);
1129                         /* Check we replaced any PlaceHolderVar with simple Var */
1130                         if (!(IsA(nlp->paramval, Var) &&
1131                                   nlp->paramval->varno == OUTER_VAR))
1132                                 elog(ERROR, "NestLoopParam was not reduced to a simple Var");
1133                 }
1134         }
1135         else if (IsA(join, MergeJoin))
1136         {
1137                 MergeJoin  *mj = (MergeJoin *) join;
1138
1139                 mj->mergeclauses = fix_join_expr(root,
1140                                                                                  mj->mergeclauses,
1141                                                                                  outer_itlist,
1142                                                                                  inner_itlist,
1143                                                                                  (Index) 0,
1144                                                                                  rtoffset);
1145         }
1146         else if (IsA(join, HashJoin))
1147         {
1148                 HashJoin   *hj = (HashJoin *) join;
1149
1150                 hj->hashclauses = fix_join_expr(root,
1151                                                                                 hj->hashclauses,
1152                                                                                 outer_itlist,
1153                                                                                 inner_itlist,
1154                                                                                 (Index) 0,
1155                                                                                 rtoffset);
1156         }
1157
1158         pfree(outer_itlist);
1159         pfree(inner_itlist);
1160 }
1161
1162 /*
1163  * set_upper_references
1164  *        Update the targetlist and quals of an upper-level plan node
1165  *        to refer to the tuples returned by its lefttree subplan.
1166  *        Also perform opcode lookup for these expressions, and
1167  *        add regclass OIDs to root->glob->relationOids.
1168  *
1169  * This is used for single-input plan types like Agg, Group, Result.
1170  *
1171  * In most cases, we have to match up individual Vars in the tlist and
1172  * qual expressions with elements of the subplan's tlist (which was
1173  * generated by flatten_tlist() from these selfsame expressions, so it
1174  * should have all the required variables).  There is an important exception,
1175  * however: GROUP BY and ORDER BY expressions will have been pushed into the
1176  * subplan tlist unflattened.  If these values are also needed in the output
1177  * then we want to reference the subplan tlist element rather than recomputing
1178  * the expression.
1179  */
1180 static void
1181 set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
1182 {
1183         Plan       *subplan = plan->lefttree;
1184         indexed_tlist *subplan_itlist;
1185         List       *output_targetlist;
1186         ListCell   *l;
1187
1188         subplan_itlist = build_tlist_index(subplan->targetlist);
1189
1190         output_targetlist = NIL;
1191         foreach(l, plan->targetlist)
1192         {
1193                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1194                 Node       *newexpr;
1195
1196                 /* If it's a non-Var sort/group item, first try to match by sortref */
1197                 if (tle->ressortgroupref != 0 && !IsA(tle->expr, Var))
1198                 {
1199                         newexpr = (Node *)
1200                                 search_indexed_tlist_for_sortgroupref((Node *) tle->expr,
1201                                                                                                           tle->ressortgroupref,
1202                                                                                                           subplan_itlist,
1203                                                                                                           OUTER_VAR);
1204                         if (!newexpr)
1205                                 newexpr = fix_upper_expr(root,
1206                                                                                  (Node *) tle->expr,
1207                                                                                  subplan_itlist,
1208                                                                                  OUTER_VAR,
1209                                                                                  rtoffset);
1210                 }
1211                 else
1212                         newexpr = fix_upper_expr(root,
1213                                                                          (Node *) tle->expr,
1214                                                                          subplan_itlist,
1215                                                                          OUTER_VAR,
1216                                                                          rtoffset);
1217                 tle = flatCopyTargetEntry(tle);
1218                 tle->expr = (Expr *) newexpr;
1219                 output_targetlist = lappend(output_targetlist, tle);
1220         }
1221         plan->targetlist = output_targetlist;
1222
1223         plan->qual = (List *)
1224                 fix_upper_expr(root,
1225                                            (Node *) plan->qual,
1226                                            subplan_itlist,
1227                                            OUTER_VAR,
1228                                            rtoffset);
1229
1230         pfree(subplan_itlist);
1231 }
1232
1233 /*
1234  * set_dummy_tlist_references
1235  *        Replace the targetlist of an upper-level plan node with a simple
1236  *        list of OUTER_VAR references to its child.
1237  *
1238  * This is used for plan types like Sort and Append that don't evaluate
1239  * their targetlists.  Although the executor doesn't care at all what's in
1240  * the tlist, EXPLAIN needs it to be realistic.
1241  *
1242  * Note: we could almost use set_upper_references() here, but it fails for
1243  * Append for lack of a lefttree subplan.  Single-purpose code is faster
1244  * anyway.
1245  */
1246 static void
1247 set_dummy_tlist_references(Plan *plan, int rtoffset)
1248 {
1249         List       *output_targetlist;
1250         ListCell   *l;
1251
1252         output_targetlist = NIL;
1253         foreach(l, plan->targetlist)
1254         {
1255                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1256                 Var                *oldvar = (Var *) tle->expr;
1257                 Var                *newvar;
1258
1259                 newvar = makeVar(OUTER_VAR,
1260                                                  tle->resno,
1261                                                  exprType((Node *) oldvar),
1262                                                  exprTypmod((Node *) oldvar),
1263                                                  exprCollation((Node *) oldvar),
1264                                                  0);
1265                 if (IsA(oldvar, Var))
1266                 {
1267                         newvar->varnoold = oldvar->varno + rtoffset;
1268                         newvar->varoattno = oldvar->varattno;
1269                 }
1270                 else
1271                 {
1272                         newvar->varnoold = 0;           /* wasn't ever a plain Var */
1273                         newvar->varoattno = 0;
1274                 }
1275
1276                 tle = flatCopyTargetEntry(tle);
1277                 tle->expr = (Expr *) newvar;
1278                 output_targetlist = lappend(output_targetlist, tle);
1279         }
1280         plan->targetlist = output_targetlist;
1281
1282         /* We don't touch plan->qual here */
1283 }
1284
1285
1286 /*
1287  * build_tlist_index --- build an index data structure for a child tlist
1288  *
1289  * In most cases, subplan tlists will be "flat" tlists with only Vars,
1290  * so we try to optimize that case by extracting information about Vars
1291  * in advance.  Matching a parent tlist to a child is still an O(N^2)
1292  * operation, but at least with a much smaller constant factor than plain
1293  * tlist_member() searches.
1294  *
1295  * The result of this function is an indexed_tlist struct to pass to
1296  * search_indexed_tlist_for_var() or search_indexed_tlist_for_non_var().
1297  * When done, the indexed_tlist may be freed with a single pfree().
1298  */
1299 static indexed_tlist *
1300 build_tlist_index(List *tlist)
1301 {
1302         indexed_tlist *itlist;
1303         tlist_vinfo *vinfo;
1304         ListCell   *l;
1305
1306         /* Create data structure with enough slots for all tlist entries */
1307         itlist = (indexed_tlist *)
1308                 palloc(offsetof(indexed_tlist, vars) +
1309                            list_length(tlist) * sizeof(tlist_vinfo));
1310
1311         itlist->tlist = tlist;
1312         itlist->has_ph_vars = false;
1313         itlist->has_non_vars = false;
1314
1315         /* Find the Vars and fill in the index array */
1316         vinfo = itlist->vars;
1317         foreach(l, tlist)
1318         {
1319                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1320
1321                 if (tle->expr && IsA(tle->expr, Var))
1322                 {
1323                         Var                *var = (Var *) tle->expr;
1324
1325                         vinfo->varno = var->varno;
1326                         vinfo->varattno = var->varattno;
1327                         vinfo->resno = tle->resno;
1328                         vinfo++;
1329                 }
1330                 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
1331                         itlist->has_ph_vars = true;
1332                 else
1333                         itlist->has_non_vars = true;
1334         }
1335
1336         itlist->num_vars = (vinfo - itlist->vars);
1337
1338         return itlist;
1339 }
1340
1341 /*
1342  * build_tlist_index_other_vars --- build a restricted tlist index
1343  *
1344  * This is like build_tlist_index, but we only index tlist entries that
1345  * are Vars belonging to some rel other than the one specified.  We will set
1346  * has_ph_vars (allowing PlaceHolderVars to be matched), but not has_non_vars
1347  * (so nothing other than Vars and PlaceHolderVars can be matched).
1348  */
1349 static indexed_tlist *
1350 build_tlist_index_other_vars(List *tlist, Index ignore_rel)
1351 {
1352         indexed_tlist *itlist;
1353         tlist_vinfo *vinfo;
1354         ListCell   *l;
1355
1356         /* Create data structure with enough slots for all tlist entries */
1357         itlist = (indexed_tlist *)
1358                 palloc(offsetof(indexed_tlist, vars) +
1359                            list_length(tlist) * sizeof(tlist_vinfo));
1360
1361         itlist->tlist = tlist;
1362         itlist->has_ph_vars = false;
1363         itlist->has_non_vars = false;
1364
1365         /* Find the desired Vars and fill in the index array */
1366         vinfo = itlist->vars;
1367         foreach(l, tlist)
1368         {
1369                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1370
1371                 if (tle->expr && IsA(tle->expr, Var))
1372                 {
1373                         Var                *var = (Var *) tle->expr;
1374
1375                         if (var->varno != ignore_rel)
1376                         {
1377                                 vinfo->varno = var->varno;
1378                                 vinfo->varattno = var->varattno;
1379                                 vinfo->resno = tle->resno;
1380                                 vinfo++;
1381                         }
1382                 }
1383                 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
1384                         itlist->has_ph_vars = true;
1385         }
1386
1387         itlist->num_vars = (vinfo - itlist->vars);
1388
1389         return itlist;
1390 }
1391
1392 /*
1393  * search_indexed_tlist_for_var --- find a Var in an indexed tlist
1394  *
1395  * If a match is found, return a copy of the given Var with suitably
1396  * modified varno/varattno (to wit, newvarno and the resno of the TLE entry).
1397  * Also ensure that varnoold is incremented by rtoffset.
1398  * If no match, return NULL.
1399  */
1400 static Var *
1401 search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
1402                                                          Index newvarno, int rtoffset)
1403 {
1404         Index           varno = var->varno;
1405         AttrNumber      varattno = var->varattno;
1406         tlist_vinfo *vinfo;
1407         int                     i;
1408
1409         vinfo = itlist->vars;
1410         i = itlist->num_vars;
1411         while (i-- > 0)
1412         {
1413                 if (vinfo->varno == varno && vinfo->varattno == varattno)
1414                 {
1415                         /* Found a match */
1416                         Var                *newvar = copyVar(var);
1417
1418                         newvar->varno = newvarno;
1419                         newvar->varattno = vinfo->resno;
1420                         if (newvar->varnoold > 0)
1421                                 newvar->varnoold += rtoffset;
1422                         return newvar;
1423                 }
1424                 vinfo++;
1425         }
1426         return NULL;                            /* no match */
1427 }
1428
1429 /*
1430  * search_indexed_tlist_for_non_var --- find a non-Var in an indexed tlist
1431  *
1432  * If a match is found, return a Var constructed to reference the tlist item.
1433  * If no match, return NULL.
1434  *
1435  * NOTE: it is a waste of time to call this unless itlist->has_ph_vars or
1436  * itlist->has_non_vars
1437  */
1438 static Var *
1439 search_indexed_tlist_for_non_var(Node *node,
1440                                                                  indexed_tlist *itlist, Index newvarno)
1441 {
1442         TargetEntry *tle;
1443
1444         tle = tlist_member(node, itlist->tlist);
1445         if (tle)
1446         {
1447                 /* Found a matching subplan output expression */
1448                 Var                *newvar;
1449
1450                 newvar = makeVarFromTargetEntry(newvarno, tle);
1451                 newvar->varnoold = 0;   /* wasn't ever a plain Var */
1452                 newvar->varoattno = 0;
1453                 return newvar;
1454         }
1455         return NULL;                            /* no match */
1456 }
1457
1458 /*
1459  * search_indexed_tlist_for_sortgroupref --- find a sort/group expression
1460  *              (which is assumed not to be just a Var)
1461  *
1462  * If a match is found, return a Var constructed to reference the tlist item.
1463  * If no match, return NULL.
1464  *
1465  * This is needed to ensure that we select the right subplan TLE in cases
1466  * where there are multiple textually-equal()-but-volatile sort expressions.
1467  * And it's also faster than search_indexed_tlist_for_non_var.
1468  */
1469 static Var *
1470 search_indexed_tlist_for_sortgroupref(Node *node,
1471                                                                           Index sortgroupref,
1472                                                                           indexed_tlist *itlist,
1473                                                                           Index newvarno)
1474 {
1475         ListCell   *lc;
1476
1477         foreach(lc, itlist->tlist)
1478         {
1479                 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1480
1481                 /* The equal() check should be redundant, but let's be paranoid */
1482                 if (tle->ressortgroupref == sortgroupref &&
1483                         equal(node, tle->expr))
1484                 {
1485                         /* Found a matching subplan output expression */
1486                         Var                *newvar;
1487
1488                         newvar = makeVarFromTargetEntry(newvarno, tle);
1489                         newvar->varnoold = 0;           /* wasn't ever a plain Var */
1490                         newvar->varoattno = 0;
1491                         return newvar;
1492                 }
1493         }
1494         return NULL;                            /* no match */
1495 }
1496
1497 /*
1498  * fix_join_expr
1499  *         Create a new set of targetlist entries or join qual clauses by
1500  *         changing the varno/varattno values of variables in the clauses
1501  *         to reference target list values from the outer and inner join
1502  *         relation target lists.  Also perform opcode lookup and add
1503  *         regclass OIDs to root->glob->relationOids.
1504  *
1505  * This is used in two different scenarios: a normal join clause, where all
1506  * the Vars in the clause *must* be replaced by OUTER_VAR or INNER_VAR
1507  * references; and a RETURNING clause, which may contain both Vars of the
1508  * target relation and Vars of other relations.  In the latter case we want
1509  * to replace the other-relation Vars by OUTER_VAR references, while leaving
1510  * target Vars alone.
1511  *
1512  * For a normal join, acceptable_rel should be zero so that any failure to
1513  * match a Var will be reported as an error.  For the RETURNING case, pass
1514  * inner_itlist = NULL and acceptable_rel = the ID of the target relation.
1515  *
1516  * 'clauses' is the targetlist or list of join clauses
1517  * 'outer_itlist' is the indexed target list of the outer join relation
1518  * 'inner_itlist' is the indexed target list of the inner join relation,
1519  *              or NULL
1520  * 'acceptable_rel' is either zero or the rangetable index of a relation
1521  *              whose Vars may appear in the clause without provoking an error
1522  * 'rtoffset': how much to increment varnoold by
1523  *
1524  * Returns the new expression tree.  The original clause structure is
1525  * not modified.
1526  */
1527 static List *
1528 fix_join_expr(PlannerInfo *root,
1529                           List *clauses,
1530                           indexed_tlist *outer_itlist,
1531                           indexed_tlist *inner_itlist,
1532                           Index acceptable_rel,
1533                           int rtoffset)
1534 {
1535         fix_join_expr_context context;
1536
1537         context.root = root;
1538         context.outer_itlist = outer_itlist;
1539         context.inner_itlist = inner_itlist;
1540         context.acceptable_rel = acceptable_rel;
1541         context.rtoffset = rtoffset;
1542         return (List *) fix_join_expr_mutator((Node *) clauses, &context);
1543 }
1544
1545 static Node *
1546 fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
1547 {
1548         Var                *newvar;
1549
1550         if (node == NULL)
1551                 return NULL;
1552         if (IsA(node, Var))
1553         {
1554                 Var                *var = (Var *) node;
1555
1556                 /* First look for the var in the input tlists */
1557                 newvar = search_indexed_tlist_for_var(var,
1558                                                                                           context->outer_itlist,
1559                                                                                           OUTER_VAR,
1560                                                                                           context->rtoffset);
1561                 if (newvar)
1562                         return (Node *) newvar;
1563                 if (context->inner_itlist)
1564                 {
1565                         newvar = search_indexed_tlist_for_var(var,
1566                                                                                                   context->inner_itlist,
1567                                                                                                   INNER_VAR,
1568                                                                                                   context->rtoffset);
1569                         if (newvar)
1570                                 return (Node *) newvar;
1571                 }
1572
1573                 /* If it's for acceptable_rel, adjust and return it */
1574                 if (var->varno == context->acceptable_rel)
1575                 {
1576                         var = copyVar(var);
1577                         var->varno += context->rtoffset;
1578                         if (var->varnoold > 0)
1579                                 var->varnoold += context->rtoffset;
1580                         return (Node *) var;
1581                 }
1582
1583                 /* No referent found for Var */
1584                 elog(ERROR, "variable not found in subplan target lists");
1585         }
1586         if (IsA(node, PlaceHolderVar))
1587         {
1588                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1589
1590                 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
1591                 if (context->outer_itlist->has_ph_vars)
1592                 {
1593                         newvar = search_indexed_tlist_for_non_var((Node *) phv,
1594                                                                                                           context->outer_itlist,
1595                                                                                                           OUTER_VAR);
1596                         if (newvar)
1597                                 return (Node *) newvar;
1598                 }
1599                 if (context->inner_itlist && context->inner_itlist->has_ph_vars)
1600                 {
1601                         newvar = search_indexed_tlist_for_non_var((Node *) phv,
1602                                                                                                           context->inner_itlist,
1603                                                                                                           INNER_VAR);
1604                         if (newvar)
1605                                 return (Node *) newvar;
1606                 }
1607
1608                 /* If not supplied by input plans, evaluate the contained expr */
1609                 return fix_join_expr_mutator((Node *) phv->phexpr, context);
1610         }
1611         /* Try matching more complex expressions too, if tlists have any */
1612         if (context->outer_itlist->has_non_vars)
1613         {
1614                 newvar = search_indexed_tlist_for_non_var(node,
1615                                                                                                   context->outer_itlist,
1616                                                                                                   OUTER_VAR);
1617                 if (newvar)
1618                         return (Node *) newvar;
1619         }
1620         if (context->inner_itlist && context->inner_itlist->has_non_vars)
1621         {
1622                 newvar = search_indexed_tlist_for_non_var(node,
1623                                                                                                   context->inner_itlist,
1624                                                                                                   INNER_VAR);
1625                 if (newvar)
1626                         return (Node *) newvar;
1627         }
1628         fix_expr_common(context->root, node);
1629         return expression_tree_mutator(node,
1630                                                                    fix_join_expr_mutator,
1631                                                                    (void *) context);
1632 }
1633
1634 /*
1635  * fix_upper_expr
1636  *              Modifies an expression tree so that all Var nodes reference outputs
1637  *              of a subplan.  Also performs opcode lookup, and adds regclass OIDs to
1638  *              root->glob->relationOids.
1639  *
1640  * This is used to fix up target and qual expressions of non-join upper-level
1641  * plan nodes, as well as index-only scan nodes.
1642  *
1643  * An error is raised if no matching var can be found in the subplan tlist
1644  * --- so this routine should only be applied to nodes whose subplans'
1645  * targetlists were generated via flatten_tlist() or some such method.
1646  *
1647  * If itlist->has_non_vars is true, then we try to match whole subexpressions
1648  * against elements of the subplan tlist, so that we can avoid recomputing
1649  * expressions that were already computed by the subplan.  (This is relatively
1650  * expensive, so we don't want to try it in the common case where the
1651  * subplan tlist is just a flattened list of Vars.)
1652  *
1653  * 'node': the tree to be fixed (a target item or qual)
1654  * 'subplan_itlist': indexed target list for subplan (or index)
1655  * 'newvarno': varno to use for Vars referencing tlist elements
1656  * 'rtoffset': how much to increment varnoold by
1657  *
1658  * The resulting tree is a copy of the original in which all Var nodes have
1659  * varno = newvarno, varattno = resno of corresponding targetlist element.
1660  * The original tree is not modified.
1661  */
1662 static Node *
1663 fix_upper_expr(PlannerInfo *root,
1664                            Node *node,
1665                            indexed_tlist *subplan_itlist,
1666                            Index newvarno,
1667                            int rtoffset)
1668 {
1669         fix_upper_expr_context context;
1670
1671         context.root = root;
1672         context.subplan_itlist = subplan_itlist;
1673         context.newvarno = newvarno;
1674         context.rtoffset = rtoffset;
1675         return fix_upper_expr_mutator(node, &context);
1676 }
1677
1678 static Node *
1679 fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
1680 {
1681         Var                *newvar;
1682
1683         if (node == NULL)
1684                 return NULL;
1685         if (IsA(node, Var))
1686         {
1687                 Var                *var = (Var *) node;
1688
1689                 newvar = search_indexed_tlist_for_var(var,
1690                                                                                           context->subplan_itlist,
1691                                                                                           context->newvarno,
1692                                                                                           context->rtoffset);
1693                 if (!newvar)
1694                         elog(ERROR, "variable not found in subplan target list");
1695                 return (Node *) newvar;
1696         }
1697         if (IsA(node, PlaceHolderVar))
1698         {
1699                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1700
1701                 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
1702                 if (context->subplan_itlist->has_ph_vars)
1703                 {
1704                         newvar = search_indexed_tlist_for_non_var((Node *) phv,
1705                                                                                                           context->subplan_itlist,
1706                                                                                                           context->newvarno);
1707                         if (newvar)
1708                                 return (Node *) newvar;
1709                 }
1710                 /* If not supplied by input plan, evaluate the contained expr */
1711                 return fix_upper_expr_mutator((Node *) phv->phexpr, context);
1712         }
1713         /* Try matching more complex expressions too, if tlist has any */
1714         if (context->subplan_itlist->has_non_vars)
1715         {
1716                 newvar = search_indexed_tlist_for_non_var(node,
1717                                                                                                   context->subplan_itlist,
1718                                                                                                   context->newvarno);
1719                 if (newvar)
1720                         return (Node *) newvar;
1721         }
1722         fix_expr_common(context->root, node);
1723         return expression_tree_mutator(node,
1724                                                                    fix_upper_expr_mutator,
1725                                                                    (void *) context);
1726 }
1727
1728 /*
1729  * set_returning_clause_references
1730  *              Perform setrefs.c's work on a RETURNING targetlist
1731  *
1732  * If the query involves more than just the result table, we have to
1733  * adjust any Vars that refer to other tables to reference junk tlist
1734  * entries in the top subplan's targetlist.  Vars referencing the result
1735  * table should be left alone, however (the executor will evaluate them
1736  * using the actual heap tuple, after firing triggers if any).  In the
1737  * adjusted RETURNING list, result-table Vars will have their original
1738  * varno (plus rtoffset), but Vars for other rels will have varno OUTER_VAR.
1739  *
1740  * We also must perform opcode lookup and add regclass OIDs to
1741  * root->glob->relationOids.
1742  *
1743  * 'rlist': the RETURNING targetlist to be fixed
1744  * 'topplan': the top subplan node that will be just below the ModifyTable
1745  *              node (note it's not yet passed through set_plan_refs)
1746  * 'resultRelation': RT index of the associated result relation
1747  * 'rtoffset': how much to increment varnos by
1748  *
1749  * Note: the given 'root' is for the parent query level, not the 'topplan'.
1750  * This does not matter currently since we only access the dependency-item
1751  * lists in root->glob, but it would need some hacking if we wanted a root
1752  * that actually matches the subplan.
1753  *
1754  * Note: resultRelation is not yet adjusted by rtoffset.
1755  */
1756 static List *
1757 set_returning_clause_references(PlannerInfo *root,
1758                                                                 List *rlist,
1759                                                                 Plan *topplan,
1760                                                                 Index resultRelation,
1761                                                                 int rtoffset)
1762 {
1763         indexed_tlist *itlist;
1764
1765         /*
1766          * We can perform the desired Var fixup by abusing the fix_join_expr
1767          * machinery that formerly handled inner indexscan fixup.  We search the
1768          * top plan's targetlist for Vars of non-result relations, and use
1769          * fix_join_expr to convert RETURNING Vars into references to those tlist
1770          * entries, while leaving result-rel Vars as-is.
1771          *
1772          * PlaceHolderVars will also be sought in the targetlist, but no
1773          * more-complex expressions will be.  Note that it is not possible for a
1774          * PlaceHolderVar to refer to the result relation, since the result is
1775          * never below an outer join.  If that case could happen, we'd have to be
1776          * prepared to pick apart the PlaceHolderVar and evaluate its contained
1777          * expression instead.
1778          */
1779         itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
1780
1781         rlist = fix_join_expr(root,
1782                                                   rlist,
1783                                                   itlist,
1784                                                   NULL,
1785                                                   resultRelation,
1786                                                   rtoffset);
1787
1788         pfree(itlist);
1789
1790         return rlist;
1791 }
1792
1793 /*****************************************************************************
1794  *                                      OPERATOR REGPROC LOOKUP
1795  *****************************************************************************/
1796
1797 /*
1798  * fix_opfuncids
1799  *        Calculate opfuncid field from opno for each OpExpr node in given tree.
1800  *        The given tree can be anything expression_tree_walker handles.
1801  *
1802  * The argument is modified in-place.  (This is OK since we'd want the
1803  * same change for any node, even if it gets visited more than once due to
1804  * shared structure.)
1805  */
1806 void
1807 fix_opfuncids(Node *node)
1808 {
1809         /* This tree walk requires no special setup, so away we go... */
1810         fix_opfuncids_walker(node, NULL);
1811 }
1812
1813 static bool
1814 fix_opfuncids_walker(Node *node, void *context)
1815 {
1816         if (node == NULL)
1817                 return false;
1818         if (IsA(node, OpExpr))
1819                 set_opfuncid((OpExpr *) node);
1820         else if (IsA(node, DistinctExpr))
1821                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1822         else if (IsA(node, NullIfExpr))
1823                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1824         else if (IsA(node, ScalarArrayOpExpr))
1825                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1826         return expression_tree_walker(node, fix_opfuncids_walker, context);
1827 }
1828
1829 /*
1830  * set_opfuncid
1831  *              Set the opfuncid (procedure OID) in an OpExpr node,
1832  *              if it hasn't been set already.
1833  *
1834  * Because of struct equivalence, this can also be used for
1835  * DistinctExpr and NullIfExpr nodes.
1836  */
1837 void
1838 set_opfuncid(OpExpr *opexpr)
1839 {
1840         if (opexpr->opfuncid == InvalidOid)
1841                 opexpr->opfuncid = get_opcode(opexpr->opno);
1842 }
1843
1844 /*
1845  * set_sa_opfuncid
1846  *              As above, for ScalarArrayOpExpr nodes.
1847  */
1848 void
1849 set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
1850 {
1851         if (opexpr->opfuncid == InvalidOid)
1852                 opexpr->opfuncid = get_opcode(opexpr->opno);
1853 }
1854
1855 /*****************************************************************************
1856  *                                      QUERY DEPENDENCY MANAGEMENT
1857  *****************************************************************************/
1858
1859 /*
1860  * record_plan_function_dependency
1861  *              Mark the current plan as depending on a particular function.
1862  *
1863  * This is exported so that the function-inlining code can record a
1864  * dependency on a function that it's removed from the plan tree.
1865  */
1866 void
1867 record_plan_function_dependency(PlannerInfo *root, Oid funcid)
1868 {
1869         /*
1870          * For performance reasons, we don't bother to track built-in functions;
1871          * we just assume they'll never change (or at least not in ways that'd
1872          * invalidate plans using them).  For this purpose we can consider a
1873          * built-in function to be one with OID less than FirstBootstrapObjectId.
1874          * Note that the OID generator guarantees never to generate such an OID
1875          * after startup, even at OID wraparound.
1876          */
1877         if (funcid >= (Oid) FirstBootstrapObjectId)
1878         {
1879                 PlanInvalItem *inval_item = makeNode(PlanInvalItem);
1880
1881                 /*
1882                  * It would work to use any syscache on pg_proc, but the easiest is
1883                  * PROCOID since we already have the function's OID at hand.  Note
1884                  * that plancache.c knows we use PROCOID.
1885                  */
1886                 inval_item->cacheId = PROCOID;
1887                 inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
1888                                                                                                    ObjectIdGetDatum(funcid));
1889
1890                 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
1891         }
1892 }
1893
1894 /*
1895  * extract_query_dependencies
1896  *              Given a not-yet-planned query or queries (i.e. a Query node or list
1897  *              of Query nodes), extract dependencies just as set_plan_references
1898  *              would do.
1899  *
1900  * This is needed by plancache.c to handle invalidation of cached unplanned
1901  * queries.
1902  */
1903 void
1904 extract_query_dependencies(Node *query,
1905                                                    List **relationOids,
1906                                                    List **invalItems)
1907 {
1908         PlannerGlobal glob;
1909         PlannerInfo root;
1910
1911         /* Make up dummy planner state so we can use this module's machinery */
1912         MemSet(&glob, 0, sizeof(glob));
1913         glob.type = T_PlannerGlobal;
1914         glob.relationOids = NIL;
1915         glob.invalItems = NIL;
1916
1917         MemSet(&root, 0, sizeof(root));
1918         root.type = T_PlannerInfo;
1919         root.glob = &glob;
1920
1921         (void) extract_query_dependencies_walker(query, &root);
1922
1923         *relationOids = glob.relationOids;
1924         *invalItems = glob.invalItems;
1925 }
1926
1927 static bool
1928 extract_query_dependencies_walker(Node *node, PlannerInfo *context)
1929 {
1930         if (node == NULL)
1931                 return false;
1932         Assert(!IsA(node, PlaceHolderVar));
1933         /* Extract function dependencies and check for regclass Consts */
1934         fix_expr_common(context, node);
1935         if (IsA(node, Query))
1936         {
1937                 Query      *query = (Query *) node;
1938                 ListCell   *lc;
1939
1940                 if (query->commandType == CMD_UTILITY)
1941                 {
1942                         /*
1943                          * Ignore utility statements, except those (such as EXPLAIN) that
1944                          * contain a parsed-but-not-planned query.
1945                          */
1946                         query = UtilityContainsQuery(query->utilityStmt);
1947                         if (query == NULL)
1948                                 return false;
1949                 }
1950
1951                 /* Collect relation OIDs in this Query's rtable */
1952                 foreach(lc, query->rtable)
1953                 {
1954                         RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
1955
1956                         if (rte->rtekind == RTE_RELATION)
1957                                 context->glob->relationOids =
1958                                         lappend_oid(context->glob->relationOids, rte->relid);
1959                 }
1960
1961                 /* And recurse into the query's subexpressions */
1962                 return query_tree_walker(query, extract_query_dependencies_walker,
1963                                                                  (void *) context, 0);
1964         }
1965         return expression_tree_walker(node, extract_query_dependencies_walker,
1966                                                                   (void *) context);
1967 }