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