]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/setrefs.c
Fix the plan-invalidation mechanism to treat regclass constants that refer to
[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-2007, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  *        $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.137 2007/10/11 18:05:27 tgl Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17
18 #include "catalog/pg_type.h"
19 #include "nodes/makefuncs.h"
20 #include "optimizer/clauses.h"
21 #include "optimizer/planmain.h"
22 #include "optimizer/tlist.h"
23 #include "parser/parse_expr.h"
24 #include "parser/parsetree.h"
25 #include "utils/lsyscache.h"
26
27
28 typedef struct
29 {
30         Index           varno;                  /* RT index of Var */
31         AttrNumber      varattno;               /* attr number of Var */
32         AttrNumber      resno;                  /* TLE position of Var */
33 } tlist_vinfo;
34
35 typedef struct
36 {
37         List       *tlist;                      /* underlying target list */
38         int                     num_vars;               /* number of plain Var tlist entries */
39         bool            has_non_vars;   /* are there non-plain-Var entries? */
40         /* array of num_vars entries: */
41         tlist_vinfo vars[1];            /* VARIABLE LENGTH ARRAY */
42 } indexed_tlist;                                /* VARIABLE LENGTH STRUCT */
43
44 typedef struct
45 {
46         PlannerGlobal *glob;
47         int                     rtoffset;
48 } fix_scan_expr_context;
49
50 typedef struct
51 {
52         PlannerGlobal *glob;
53         indexed_tlist *outer_itlist;
54         indexed_tlist *inner_itlist;
55         Index           acceptable_rel;
56         int                     rtoffset;
57 } fix_join_expr_context;
58
59 typedef struct
60 {
61         PlannerGlobal *glob;
62         indexed_tlist *subplan_itlist;
63         int                     rtoffset;
64 } fix_upper_expr_context;
65
66 #define fix_scan_list(glob, lst, rtoffset) \
67         ((List *) fix_scan_expr(glob, (Node *) (lst), rtoffset))
68
69 static Plan *set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset);
70 static Plan *set_subqueryscan_references(PlannerGlobal *glob,
71                                                                                  SubqueryScan *plan,
72                                                                                  int rtoffset);
73 static bool trivial_subqueryscan(SubqueryScan *plan);
74 static Node *fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset);
75 static Node *fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context);
76 static void set_join_references(PlannerGlobal *glob, Join *join, int rtoffset);
77 static void set_inner_join_references(PlannerGlobal *glob, Plan *inner_plan,
78                                                   indexed_tlist *outer_itlist);
79 static void set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset);
80 static void set_dummy_tlist_references(Plan *plan, int rtoffset);
81 static indexed_tlist *build_tlist_index(List *tlist);
82 static Var *search_indexed_tlist_for_var(Var *var,
83                                                          indexed_tlist *itlist,
84                                                          Index newvarno,
85                                                          int rtoffset);
86 static Var *search_indexed_tlist_for_non_var(Node *node,
87                                                                  indexed_tlist *itlist,
88                                                                  Index newvarno);
89 static List *fix_join_expr(PlannerGlobal *glob,
90                                                    List *clauses,
91                                                    indexed_tlist *outer_itlist,
92                                                    indexed_tlist *inner_itlist,
93                                                    Index acceptable_rel, int rtoffset);
94 static Node *fix_join_expr_mutator(Node *node,
95                                                                    fix_join_expr_context *context);
96 static Node *fix_upper_expr(PlannerGlobal *glob,
97                                                         Node *node,
98                                                         indexed_tlist *subplan_itlist,
99                                                         int rtoffset);
100 static Node *fix_upper_expr_mutator(Node *node,
101                                                                         fix_upper_expr_context *context);
102 static bool fix_opfuncids_walker(Node *node, void *context);
103
104
105 /*****************************************************************************
106  *
107  *              SUBPLAN REFERENCES
108  *
109  *****************************************************************************/
110
111 /*
112  * set_plan_references
113  *
114  * This is the final processing pass of the planner/optimizer.  The plan
115  * tree is complete; we just have to adjust some representational details
116  * for the convenience of the executor:
117  *
118  * 1. We flatten the various subquery rangetables into a single list, and
119  * zero out RangeTblEntry fields that are not useful to the executor.
120  *
121  * 2. We adjust Vars in scan nodes to be consistent with the flat rangetable.
122  *
123  * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
124  * subplans.
125  *
126  * 4. We compute regproc OIDs for operators (ie, we look up the function
127  * that implements each op).
128  *
129  * 5. We create a list of OIDs of relations that the plan depends on.
130  * This will be used by plancache.c to drive invalidation of cached plans.
131  * (Someday we might want to generalize this to include other types of
132  * objects, but for now tracking relations seems to solve most problems.)
133  *
134  * We also perform one final optimization step, which is to delete
135  * SubqueryScan plan nodes that aren't doing anything useful (ie, have
136  * no qual and a no-op targetlist).  The reason for doing this last is that
137  * it can't readily be done before set_plan_references, because it would
138  * break set_upper_references: the Vars in the subquery's top tlist
139  * wouldn't match up with the Vars in the outer plan tree.  The SubqueryScan
140  * serves a necessary function as a buffer between outer query and subquery
141  * variable numbering ... but after we've flattened the rangetable this is
142  * no longer a problem, since then there's only one rtindex namespace.
143  *
144  * set_plan_references recursively traverses the whole plan tree.
145  *
146  * Inputs:
147  *      glob: global data for planner run
148  *      plan: the topmost node of the plan
149  *      rtable: the rangetable for the current subquery
150  *
151  * The return value is normally the same Plan node passed in, but can be
152  * different when the passed-in Plan is a SubqueryScan we decide isn't needed.
153  *
154  * The flattened rangetable entries are appended to glob->finalrtable, and
155  * the list of relation OIDs is appended to glob->relationOids.
156  *
157  * Notice that we modify Plan nodes in-place, but use expression_tree_mutator
158  * to process targetlist and qual expressions.  We can assume that the Plan
159  * nodes were just built by the planner and are not multiply referenced, but
160  * it's not so safe to assume that for expression tree nodes.
161  */
162 Plan *
163 set_plan_references(PlannerGlobal *glob, Plan *plan, List *rtable)
164 {
165         int                     rtoffset = list_length(glob->finalrtable);
166         ListCell   *lc;
167
168         /*
169          * In the flat rangetable, we zero out substructure pointers that are
170          * not needed by the executor; this reduces the storage space and
171          * copying cost for cached plans.  We keep only the alias and eref
172          * Alias fields, which are needed by EXPLAIN.
173          */
174         foreach(lc, rtable)
175         {
176                 RangeTblEntry  *rte = (RangeTblEntry *) lfirst(lc);
177                 RangeTblEntry  *newrte;
178
179                 /* flat copy to duplicate all the scalar fields */
180                 newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
181                 memcpy(newrte, rte, sizeof(RangeTblEntry));
182
183                 /* zap unneeded sub-structure */
184                 newrte->subquery = NULL;
185                 newrte->funcexpr = NULL;
186                 newrte->funccoltypes = NIL;
187                 newrte->funccoltypmods = NIL;
188                 newrte->values_lists = NIL;
189                 newrte->joinaliasvars = NIL;
190
191                 glob->finalrtable = lappend(glob->finalrtable, newrte);
192
193                 /*
194                  * If it's a plain relation RTE, add the table to relationOids.
195                  *
196                  * We do this even though the RTE might be unreferenced in the
197                  * plan tree; this would correspond to cases such as views that
198                  * were expanded, child tables that were eliminated by constraint
199                  * exclusion, etc.  Schema invalidation on such a rel must still
200                  * force rebuilding of the plan.
201                  *
202                  * Note we don't bother to avoid duplicate list entries.  We could,
203                  * but it would probably cost more cycles than it would save.
204                  */
205                 if (newrte->rtekind == RTE_RELATION)
206                         glob->relationOids = lappend_oid(glob->relationOids,
207                                                                                          newrte->relid);
208         }
209
210         /* Now fix the Plan tree */
211         return set_plan_refs(glob, plan, rtoffset);
212 }
213
214 /*
215  * set_plan_refs: recurse through the Plan nodes of a single subquery level
216  */
217 static Plan *
218 set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
219 {
220         ListCell   *l;
221
222         if (plan == NULL)
223                 return NULL;
224
225         /*
226          * Plan-type-specific fixes
227          */
228         switch (nodeTag(plan))
229         {
230                 case T_SeqScan:
231                         {
232                                 SeqScan *splan = (SeqScan *) plan;
233
234                                 splan->scanrelid += rtoffset;
235                                 splan->plan.targetlist =
236                                         fix_scan_list(glob, splan->plan.targetlist, rtoffset);
237                                 splan->plan.qual =
238                                         fix_scan_list(glob, splan->plan.qual, rtoffset);
239                         }
240                         break;
241                 case T_IndexScan:
242                         {
243                                 IndexScan *splan = (IndexScan *) plan;
244
245                                 splan->scan.scanrelid += rtoffset;
246                                 splan->scan.plan.targetlist =
247                                         fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
248                                 splan->scan.plan.qual =
249                                         fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
250                                 splan->indexqual =
251                                         fix_scan_list(glob, splan->indexqual, rtoffset);
252                                 splan->indexqualorig =
253                                         fix_scan_list(glob, splan->indexqualorig, rtoffset);
254                         }
255                         break;
256                 case T_BitmapIndexScan:
257                         {
258                                 BitmapIndexScan *splan = (BitmapIndexScan *) plan;
259
260                                 splan->scan.scanrelid += rtoffset;
261                                 /* no need to fix targetlist and qual */
262                                 Assert(splan->scan.plan.targetlist == NIL);
263                                 Assert(splan->scan.plan.qual == NIL);
264                                 splan->indexqual =
265                                         fix_scan_list(glob, splan->indexqual, rtoffset);
266                                 splan->indexqualorig =
267                                         fix_scan_list(glob, splan->indexqualorig, rtoffset);
268                         }
269                         break;
270                 case T_BitmapHeapScan:
271                         {
272                                 BitmapHeapScan *splan = (BitmapHeapScan *) plan;
273
274                                 splan->scan.scanrelid += rtoffset;
275                                 splan->scan.plan.targetlist =
276                                         fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
277                                 splan->scan.plan.qual =
278                                         fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
279                                 splan->bitmapqualorig =
280                                         fix_scan_list(glob, splan->bitmapqualorig, rtoffset);
281                         }
282                         break;
283                 case T_TidScan:
284                         {
285                                 TidScan *splan = (TidScan *) plan;
286
287                                 splan->scan.scanrelid += rtoffset;
288                                 splan->scan.plan.targetlist =
289                                         fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
290                                 splan->scan.plan.qual =
291                                         fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
292                                 splan->tidquals =
293                                         fix_scan_list(glob, splan->tidquals, rtoffset);
294                         }
295                         break;
296                 case T_SubqueryScan:
297                         /* Needs special treatment, see comments below */
298                         return set_subqueryscan_references(glob,
299                                                                                            (SubqueryScan *) plan,
300                                                                                            rtoffset);
301                 case T_FunctionScan:
302                         {
303                                 FunctionScan *splan = (FunctionScan *) plan;
304
305                                 splan->scan.scanrelid += rtoffset;
306                                 splan->scan.plan.targetlist =
307                                         fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
308                                 splan->scan.plan.qual =
309                                         fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
310                                 splan->funcexpr =
311                                         fix_scan_expr(glob, splan->funcexpr, rtoffset);
312                         }
313                         break;
314                 case T_ValuesScan:
315                         {
316                                 ValuesScan *splan = (ValuesScan *) plan;
317
318                                 splan->scan.scanrelid += rtoffset;
319                                 splan->scan.plan.targetlist =
320                                         fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
321                                 splan->scan.plan.qual =
322                                         fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
323                                 splan->values_lists =
324                                         fix_scan_list(glob, splan->values_lists, rtoffset);
325                         }
326                         break;
327
328                 case T_NestLoop:
329                 case T_MergeJoin:
330                 case T_HashJoin:
331                         set_join_references(glob, (Join *) plan, rtoffset);
332                         break;
333
334                 case T_Hash:
335                 case T_Material:
336                 case T_Sort:
337                 case T_Unique:
338                 case T_SetOp:
339
340                         /*
341                          * These plan types don't actually bother to evaluate their
342                          * targetlists, because they just return their unmodified input
343                          * tuples.  Even though the targetlist won't be used by the
344                          * executor, we fix it up for possible use by EXPLAIN (not to
345                          * mention ease of debugging --- wrong varnos are very confusing).
346                          */
347                         set_dummy_tlist_references(plan, rtoffset);
348                         /*
349                          * Since these plan types don't check quals either, we should not
350                          * find any qual expression attached to them.
351                          */
352                         Assert(plan->qual == NIL);
353                         break;
354                 case T_Limit:
355                         {
356                                 Limit *splan = (Limit *) plan;
357
358                                 /*
359                                  * Like the plan types above, Limit doesn't evaluate its tlist
360                                  * or quals.  It does have live expressions for limit/offset,
361                                  * however; and those cannot contain subplan variable refs,
362                                  * so fix_scan_expr works for them.
363                                  */
364                                 set_dummy_tlist_references(plan, rtoffset);
365                                 Assert(splan->plan.qual == NIL);
366
367                                 splan->limitOffset =
368                                         fix_scan_expr(glob, splan->limitOffset, rtoffset);
369                                 splan->limitCount =
370                                         fix_scan_expr(glob, splan->limitCount, rtoffset);
371                         }
372                         break;
373                 case T_Agg:
374                 case T_Group:
375                         set_upper_references(glob, plan, rtoffset);
376                         break;
377                 case T_Result:
378                         {
379                                 Result *splan = (Result *) plan;
380
381                                 /*
382                                  * Result may or may not have a subplan; if not, it's more
383                                  * like a scan node than an upper node.
384                                  */
385                                 if (splan->plan.lefttree != NULL)
386                                         set_upper_references(glob, plan, rtoffset);
387                                 else
388                                 {
389                                         splan->plan.targetlist =
390                                                 fix_scan_list(glob, splan->plan.targetlist, rtoffset);
391                                         splan->plan.qual =
392                                                 fix_scan_list(glob, splan->plan.qual, rtoffset);
393                                 }
394                                 /* resconstantqual can't contain any subplan variable refs */
395                                 splan->resconstantqual =
396                                         fix_scan_expr(glob, splan->resconstantqual, rtoffset);
397                         }
398                         break;
399                 case T_Append:
400                         {
401                                 Append *splan = (Append *) plan;
402
403                                 /*
404                                  * Append, like Sort et al, doesn't actually evaluate its
405                                  * targetlist or check quals.
406                                  */
407                                 set_dummy_tlist_references(plan, rtoffset);
408                                 Assert(splan->plan.qual == NIL);
409                                 foreach(l, splan->appendplans)
410                                 {
411                                         lfirst(l) = set_plan_refs(glob,
412                                                                                           (Plan *) lfirst(l),
413                                                                                           rtoffset);
414                                 }
415                         }
416                         break;
417                 case T_BitmapAnd:
418                         {
419                                 BitmapAnd *splan = (BitmapAnd *) plan;
420
421                                 /* BitmapAnd works like Append, but has no tlist */
422                                 Assert(splan->plan.targetlist == NIL);
423                                 Assert(splan->plan.qual == NIL);
424                                 foreach(l, splan->bitmapplans)
425                                 {
426                                         lfirst(l) = set_plan_refs(glob,
427                                                                                           (Plan *) lfirst(l),
428                                                                                           rtoffset);
429                                 }
430                         }
431                         break;
432                 case T_BitmapOr:
433                         {
434                                 BitmapOr *splan = (BitmapOr *) plan;
435
436                                 /* BitmapOr works like Append, but has no tlist */
437                                 Assert(splan->plan.targetlist == NIL);
438                                 Assert(splan->plan.qual == NIL);
439                                 foreach(l, splan->bitmapplans)
440                                 {
441                                         lfirst(l) = set_plan_refs(glob,
442                                                                                           (Plan *) lfirst(l),
443                                                                                           rtoffset);
444                                 }
445                         }
446                         break;
447                 default:
448                         elog(ERROR, "unrecognized node type: %d",
449                                  (int) nodeTag(plan));
450                         break;
451         }
452
453         /*
454          * Now recurse into child plans, if any
455          *
456          * NOTE: it is essential that we recurse into child plans AFTER we set
457          * subplan references in this plan's tlist and quals.  If we did the
458          * reference-adjustments bottom-up, then we would fail to match this
459          * plan's var nodes against the already-modified nodes of the children.
460          */
461         plan->lefttree = set_plan_refs(glob, plan->lefttree, rtoffset);
462         plan->righttree = set_plan_refs(glob, plan->righttree, rtoffset);
463
464         return plan;
465 }
466
467 /*
468  * set_subqueryscan_references
469  *              Do set_plan_references processing on a SubqueryScan
470  *
471  * We try to strip out the SubqueryScan entirely; if we can't, we have
472  * to do the normal processing on it.
473  */
474 static Plan *
475 set_subqueryscan_references(PlannerGlobal *glob,
476                                                         SubqueryScan *plan,
477                                                         int rtoffset)
478 {
479         Plan       *result;
480
481         /* First, recursively process the subplan */
482         plan->subplan = set_plan_references(glob, plan->subplan, plan->subrtable);
483
484         /* subrtable is no longer needed in the plan tree */
485         plan->subrtable = NIL;
486
487         if (trivial_subqueryscan(plan))
488         {
489                 /*
490                  * We can omit the SubqueryScan node and just pull up the subplan.
491                  */
492                 ListCell   *lp,
493                                    *lc;
494
495                 result = plan->subplan;
496
497                 /* We have to be sure we don't lose any initplans */
498                 result->initPlan = list_concat(plan->scan.plan.initPlan,
499                                                                            result->initPlan);
500
501                 /*
502                  * We also have to transfer the SubqueryScan's result-column names
503                  * into the subplan, else columns sent to client will be improperly
504                  * labeled if this is the topmost plan level.  Copy the "source
505                  * column" information too.
506                  */
507                 forboth(lp, plan->scan.plan.targetlist, lc, result->targetlist)
508                 {
509                         TargetEntry *ptle = (TargetEntry *) lfirst(lp);
510                         TargetEntry *ctle = (TargetEntry *) lfirst(lc);
511
512                         ctle->resname = ptle->resname;
513                         ctle->resorigtbl = ptle->resorigtbl;
514                         ctle->resorigcol = ptle->resorigcol;
515                 }
516         }
517         else
518         {
519                 /*
520                  * Keep the SubqueryScan node.  We have to do the processing that
521                  * set_plan_references would otherwise have done on it.  Notice we do
522                  * not do set_upper_references() here, because a SubqueryScan will
523                  * always have been created with correct references to its subplan's
524                  * outputs to begin with.
525                  */
526                 plan->scan.scanrelid += rtoffset;
527                 plan->scan.plan.targetlist =
528                         fix_scan_list(glob, plan->scan.plan.targetlist, rtoffset);
529                 plan->scan.plan.qual =
530                         fix_scan_list(glob, plan->scan.plan.qual, rtoffset);
531
532                 result = (Plan *) plan;
533         }
534
535         return result;
536 }
537
538 /*
539  * trivial_subqueryscan
540  *              Detect whether a SubqueryScan can be deleted from the plan tree.
541  *
542  * We can delete it if it has no qual to check and the targetlist just
543  * regurgitates the output of the child plan.
544  */
545 static bool
546 trivial_subqueryscan(SubqueryScan *plan)
547 {
548         int                     attrno;
549         ListCell   *lp,
550                            *lc;
551
552         if (plan->scan.plan.qual != NIL)
553                 return false;
554
555         if (list_length(plan->scan.plan.targetlist) !=
556                 list_length(plan->subplan->targetlist))
557                 return false;                   /* tlists not same length */
558
559         attrno = 1;
560         forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
561         {
562                 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
563                 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
564
565                 if (ptle->resjunk != ctle->resjunk)
566                         return false;           /* tlist doesn't match junk status */
567
568                 /*
569                  * We accept either a Var referencing the corresponding element of the
570                  * subplan tlist, or a Const equaling the subplan element. See
571                  * generate_setop_tlist() for motivation.
572                  */
573                 if (ptle->expr && IsA(ptle->expr, Var))
574                 {
575                         Var                *var = (Var *) ptle->expr;
576
577                         Assert(var->varno == plan->scan.scanrelid);
578                         Assert(var->varlevelsup == 0);
579                         if (var->varattno != attrno)
580                                 return false;   /* out of order */
581                 }
582                 else if (ptle->expr && IsA(ptle->expr, Const))
583                 {
584                         if (!equal(ptle->expr, ctle->expr))
585                                 return false;
586                 }
587                 else
588                         return false;
589
590                 attrno++;
591         }
592
593         return true;
594 }
595
596 /*
597  * copyVar
598  *              Copy a Var node.
599  *
600  * fix_scan_expr and friends do this enough times that it's worth having
601  * a bespoke routine instead of using the generic copyObject() function.
602  */
603 static inline Var *
604 copyVar(Var *var)
605 {
606         Var                *newvar = (Var *) palloc(sizeof(Var));
607
608         *newvar = *var;
609         return newvar;
610 }
611
612 /*
613  * fix_scan_expr
614  *              Do set_plan_references processing on a scan-level expression
615  *
616  * This consists of incrementing all Vars' varnos by rtoffset,
617  * looking up operator opcode info for OpExpr and related nodes,
618  * and adding OIDs from regclass Const nodes into glob->relationOids.
619  */
620 static Node *
621 fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset)
622 {
623         fix_scan_expr_context context;
624
625         context.glob = glob;
626         context.rtoffset = rtoffset;
627         return fix_scan_expr_mutator(node, &context);
628 }
629
630 static Node *
631 fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
632 {
633         if (node == NULL)
634                 return NULL;
635         if (IsA(node, Var))
636         {
637                 Var                *var = copyVar((Var *) node);
638
639                 Assert(var->varlevelsup == 0);
640                 /*
641                  * We should not see any Vars marked INNER, but in a nestloop inner
642                  * scan there could be OUTER Vars.  Leave them alone.
643                  */
644                 Assert(var->varno != INNER);
645                 if (var->varno > 0 && var->varno != OUTER)
646                         var->varno += context->rtoffset;
647                 if (var->varnoold > 0)
648                         var->varnoold += context->rtoffset;
649                 return (Node *) var;
650         }
651         if (IsA(node, CurrentOfExpr))
652         {
653                 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
654
655                 Assert(cexpr->cvarno != INNER);
656                 Assert(cexpr->cvarno != OUTER);
657                 cexpr->cvarno += context->rtoffset;
658                 return (Node *) cexpr;
659         }
660         /*
661          * Since we update opcode info in-place, this part could possibly
662          * scribble on the planner's input data structures, but it's OK.
663          */
664         if (IsA(node, OpExpr))
665                 set_opfuncid((OpExpr *) node);
666         else if (IsA(node, DistinctExpr))
667                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
668         else if (IsA(node, NullIfExpr))
669                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
670         else if (IsA(node, ScalarArrayOpExpr))
671                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
672         else if (IsA(node, Const))
673         {
674                 Const      *con = (Const *) node;
675
676                 /* Check for regclass reference */
677                 if (con->consttype == REGCLASSOID && !con->constisnull)
678                         context->glob->relationOids =
679                                 lappend_oid(context->glob->relationOids,
680                                                         DatumGetObjectId(con->constvalue));
681                 /* Fall through to let expression_tree_mutator copy it */
682         }
683         return expression_tree_mutator(node, fix_scan_expr_mutator,
684                                                                    (void *) context);
685 }
686
687 /*
688  * set_join_references
689  *        Modify the target list and quals of a join node to reference its
690  *        subplans, by setting the varnos to OUTER or INNER and setting attno
691  *        values to the result domain number of either the corresponding outer
692  *        or inner join tuple item.  Also perform opcode lookup for these
693  *        expressions. and add regclass OIDs to glob->relationOids.
694  *
695  * In the case of a nestloop with inner indexscan, we will also need to
696  * apply the same transformation to any outer vars appearing in the
697  * quals of the child indexscan.  set_inner_join_references does that.
698  */
699 static void
700 set_join_references(PlannerGlobal *glob, Join *join, int rtoffset)
701 {
702         Plan       *outer_plan = join->plan.lefttree;
703         Plan       *inner_plan = join->plan.righttree;
704         indexed_tlist *outer_itlist;
705         indexed_tlist *inner_itlist;
706
707         outer_itlist = build_tlist_index(outer_plan->targetlist);
708         inner_itlist = build_tlist_index(inner_plan->targetlist);
709
710         /* All join plans have tlist, qual, and joinqual */
711         join->plan.targetlist = fix_join_expr(glob,
712                                                                                   join->plan.targetlist,
713                                                                                   outer_itlist,
714                                                                                   inner_itlist,
715                                                                                   (Index) 0,
716                                                                                   rtoffset);
717         join->plan.qual = fix_join_expr(glob,
718                                                                         join->plan.qual,
719                                                                         outer_itlist,
720                                                                         inner_itlist,
721                                                                         (Index) 0,
722                                                                         rtoffset);
723         join->joinqual = fix_join_expr(glob,
724                                                                    join->joinqual,
725                                                                    outer_itlist,
726                                                                    inner_itlist,
727                                                                    (Index) 0,
728                                                                    rtoffset);
729
730         /* Now do join-type-specific stuff */
731         if (IsA(join, NestLoop))
732         {
733                 /* This processing is split out to handle possible recursion */
734                 set_inner_join_references(glob, inner_plan, outer_itlist);
735         }
736         else if (IsA(join, MergeJoin))
737         {
738                 MergeJoin  *mj = (MergeJoin *) join;
739
740                 mj->mergeclauses = fix_join_expr(glob,
741                                                                                  mj->mergeclauses,
742                                                                                  outer_itlist,
743                                                                                  inner_itlist,
744                                                                                  (Index) 0,
745                                                                                  rtoffset);
746         }
747         else if (IsA(join, HashJoin))
748         {
749                 HashJoin   *hj = (HashJoin *) join;
750
751                 hj->hashclauses = fix_join_expr(glob,
752                                                                                 hj->hashclauses,
753                                                                                 outer_itlist,
754                                                                                 inner_itlist,
755                                                                                 (Index) 0,
756                                                                                 rtoffset);
757         }
758
759         pfree(outer_itlist);
760         pfree(inner_itlist);
761 }
762
763 /*
764  * set_inner_join_references
765  *              Handle join references appearing in an inner indexscan's quals
766  *
767  * To handle bitmap-scan plan trees, we have to be able to recurse down
768  * to the bottom BitmapIndexScan nodes; likewise, appendrel indexscans
769  * require recursing through Append nodes.      This is split out as a separate
770  * function so that it can recurse.
771  *
772  * Note we do *not* apply any rtoffset for non-join Vars; this is because
773  * the quals will be processed again by fix_scan_expr when the set_plan_refs
774  * recursion reaches the inner indexscan, and so we'd have done it twice.
775  */
776 static void
777 set_inner_join_references(PlannerGlobal *glob, Plan *inner_plan,
778                                                   indexed_tlist *outer_itlist)
779 {
780         if (IsA(inner_plan, IndexScan))
781         {
782                 /*
783                  * An index is being used to reduce the number of tuples scanned in
784                  * the inner relation.  If there are join clauses being used with the
785                  * index, we must update their outer-rel var nodes to refer to the
786                  * outer side of the join.
787                  */
788                 IndexScan  *innerscan = (IndexScan *) inner_plan;
789                 List       *indexqualorig = innerscan->indexqualorig;
790
791                 /* No work needed if indexqual refers only to its own rel... */
792                 if (NumRelids((Node *) indexqualorig) > 1)
793                 {
794                         Index           innerrel = innerscan->scan.scanrelid;
795
796                         /* only refs to outer vars get changed in the inner qual */
797                         innerscan->indexqualorig = fix_join_expr(glob,
798                                                                                                          indexqualorig,
799                                                                                                          outer_itlist,
800                                                                                                          NULL,
801                                                                                                          innerrel,
802                                                                                                          0);
803                         innerscan->indexqual = fix_join_expr(glob,
804                                                                                                  innerscan->indexqual,
805                                                                                                  outer_itlist,
806                                                                                                  NULL,
807                                                                                                  innerrel,
808                                                                                                  0);
809
810                         /*
811                          * We must fix the inner qpqual too, if it has join clauses (this
812                          * could happen if special operators are involved: some indexquals
813                          * may get rechecked as qpquals).
814                          */
815                         if (NumRelids((Node *) inner_plan->qual) > 1)
816                                 inner_plan->qual = fix_join_expr(glob,
817                                                                                                  inner_plan->qual,
818                                                                                                  outer_itlist,
819                                                                                                  NULL,
820                                                                                                  innerrel,
821                                                                                                  0);
822                 }
823         }
824         else if (IsA(inner_plan, BitmapIndexScan))
825         {
826                 /*
827                  * Same, but index is being used within a bitmap plan.
828                  */
829                 BitmapIndexScan *innerscan = (BitmapIndexScan *) inner_plan;
830                 List       *indexqualorig = innerscan->indexqualorig;
831
832                 /* No work needed if indexqual refers only to its own rel... */
833                 if (NumRelids((Node *) indexqualorig) > 1)
834                 {
835                         Index           innerrel = innerscan->scan.scanrelid;
836
837                         /* only refs to outer vars get changed in the inner qual */
838                         innerscan->indexqualorig = fix_join_expr(glob,
839                                                                                                          indexqualorig,
840                                                                                                          outer_itlist,
841                                                                                                          NULL,
842                                                                                                          innerrel,
843                                                                                                          0);
844                         innerscan->indexqual = fix_join_expr(glob,
845                                                                                                  innerscan->indexqual,
846                                                                                                  outer_itlist,
847                                                                                                  NULL,
848                                                                                                  innerrel,
849                                                                                                  0);
850                         /* no need to fix inner qpqual */
851                         Assert(inner_plan->qual == NIL);
852                 }
853         }
854         else if (IsA(inner_plan, BitmapHeapScan))
855         {
856                 /*
857                  * The inner side is a bitmap scan plan.  Fix the top node, and
858                  * recurse to get the lower nodes.
859                  *
860                  * Note: create_bitmap_scan_plan removes clauses from bitmapqualorig
861                  * if they are duplicated in qpqual, so must test these independently.
862                  */
863                 BitmapHeapScan *innerscan = (BitmapHeapScan *) inner_plan;
864                 Index           innerrel = innerscan->scan.scanrelid;
865                 List       *bitmapqualorig = innerscan->bitmapqualorig;
866
867                 /* only refs to outer vars get changed in the inner qual */
868                 if (NumRelids((Node *) bitmapqualorig) > 1)
869                         innerscan->bitmapqualorig = fix_join_expr(glob,
870                                                                                                           bitmapqualorig,
871                                                                                                           outer_itlist,
872                                                                                                           NULL,
873                                                                                                           innerrel,
874                                                                                                           0);
875
876                 /*
877                  * We must fix the inner qpqual too, if it has join clauses (this
878                  * could happen if special operators are involved: some indexquals may
879                  * get rechecked as qpquals).
880                  */
881                 if (NumRelids((Node *) inner_plan->qual) > 1)
882                         inner_plan->qual = fix_join_expr(glob,
883                                                                                          inner_plan->qual,
884                                                                                          outer_itlist,
885                                                                                          NULL,
886                                                                                          innerrel,
887                                                                                          0);
888
889                 /* Now recurse */
890                 set_inner_join_references(glob, inner_plan->lefttree, outer_itlist);
891         }
892         else if (IsA(inner_plan, BitmapAnd))
893         {
894                 /* All we need do here is recurse */
895                 BitmapAnd  *innerscan = (BitmapAnd *) inner_plan;
896                 ListCell   *l;
897
898                 foreach(l, innerscan->bitmapplans)
899                 {
900                         set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist);
901                 }
902         }
903         else if (IsA(inner_plan, BitmapOr))
904         {
905                 /* All we need do here is recurse */
906                 BitmapOr   *innerscan = (BitmapOr *) inner_plan;
907                 ListCell   *l;
908
909                 foreach(l, innerscan->bitmapplans)
910                 {
911                         set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist);
912                 }
913         }
914         else if (IsA(inner_plan, TidScan))
915         {
916                 TidScan    *innerscan = (TidScan *) inner_plan;
917                 Index           innerrel = innerscan->scan.scanrelid;
918
919                 innerscan->tidquals = fix_join_expr(glob,
920                                                                                         innerscan->tidquals,
921                                                                                         outer_itlist,
922                                                                                         NULL,
923                                                                                         innerrel,
924                                                                                         0);
925         }
926         else if (IsA(inner_plan, Append))
927         {
928                 /*
929                  * The inner side is an append plan.  Recurse to see if it contains
930                  * indexscans that need to be fixed.
931                  */
932                 Append     *appendplan = (Append *) inner_plan;
933                 ListCell   *l;
934
935                 foreach(l, appendplan->appendplans)
936                 {
937                         set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist);
938                 }
939         }
940         else if (IsA(inner_plan, Result))
941         {
942                 /* Recurse through a gating Result node (similar to Append case) */
943                 Result     *result = (Result *) inner_plan;
944
945                 if (result->plan.lefttree)
946                         set_inner_join_references(glob, result->plan.lefttree, outer_itlist);
947         }
948 }
949
950 /*
951  * set_upper_references
952  *        Update the targetlist and quals of an upper-level plan node
953  *        to refer to the tuples returned by its lefttree subplan.
954  *        Also perform opcode lookup for these expressions, and
955  *        add regclass OIDs to glob->relationOids.
956  *
957  * This is used for single-input plan types like Agg, Group, Result.
958  *
959  * In most cases, we have to match up individual Vars in the tlist and
960  * qual expressions with elements of the subplan's tlist (which was
961  * generated by flatten_tlist() from these selfsame expressions, so it
962  * should have all the required variables).  There is an important exception,
963  * however: GROUP BY and ORDER BY expressions will have been pushed into the
964  * subplan tlist unflattened.  If these values are also needed in the output
965  * then we want to reference the subplan tlist element rather than recomputing
966  * the expression.
967  */
968 static void
969 set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset)
970 {
971         Plan       *subplan = plan->lefttree;
972         indexed_tlist *subplan_itlist;
973         List       *output_targetlist;
974         ListCell   *l;
975
976         subplan_itlist = build_tlist_index(subplan->targetlist);
977
978         output_targetlist = NIL;
979         foreach(l, plan->targetlist)
980         {
981                 TargetEntry *tle = (TargetEntry *) lfirst(l);
982                 Node       *newexpr;
983
984                 newexpr = fix_upper_expr(glob,
985                                                                  (Node *) tle->expr,
986                                                                  subplan_itlist,
987                                                                  rtoffset);
988                 tle = flatCopyTargetEntry(tle);
989                 tle->expr = (Expr *) newexpr;
990                 output_targetlist = lappend(output_targetlist, tle);
991         }
992         plan->targetlist = output_targetlist;
993
994         plan->qual = (List *)
995                 fix_upper_expr(glob,
996                                            (Node *) plan->qual,
997                                            subplan_itlist,
998                                            rtoffset);
999
1000         pfree(subplan_itlist);
1001 }
1002
1003 /*
1004  * set_dummy_tlist_references
1005  *        Replace the targetlist of an upper-level plan node with a simple
1006  *        list of OUTER references to its child.
1007  *
1008  * This is used for plan types like Sort and Append that don't evaluate
1009  * their targetlists.  Although the executor doesn't care at all what's in
1010  * the tlist, EXPLAIN needs it to be realistic.
1011  *
1012  * Note: we could almost use set_upper_references() here, but it fails for
1013  * Append for lack of a lefttree subplan.  Single-purpose code is faster
1014  * anyway.
1015  */
1016 static void
1017 set_dummy_tlist_references(Plan *plan, int rtoffset)
1018 {
1019         List       *output_targetlist;
1020         ListCell   *l;
1021
1022         output_targetlist = NIL;
1023         foreach(l, plan->targetlist)
1024         {
1025                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1026                 Var                *oldvar = (Var *) tle->expr;
1027                 Var                *newvar;
1028
1029                 newvar = makeVar(OUTER,
1030                                                  tle->resno,
1031                                                  exprType((Node *) oldvar),
1032                                                  exprTypmod((Node *) oldvar),
1033                                                  0);
1034                 if (IsA(oldvar, Var))
1035                 {
1036                         newvar->varnoold = oldvar->varno + rtoffset;
1037                         newvar->varoattno = oldvar->varattno;
1038                 }
1039                 else
1040                 {
1041                         newvar->varnoold = 0;   /* wasn't ever a plain Var */
1042                         newvar->varoattno = 0;
1043                 }
1044
1045                 tle = flatCopyTargetEntry(tle);
1046                 tle->expr = (Expr *) newvar;
1047                 output_targetlist = lappend(output_targetlist, tle);
1048         }
1049         plan->targetlist = output_targetlist;
1050
1051         /* We don't touch plan->qual here */
1052 }
1053
1054
1055 /*
1056  * build_tlist_index --- build an index data structure for a child tlist
1057  *
1058  * In most cases, subplan tlists will be "flat" tlists with only Vars,
1059  * so we try to optimize that case by extracting information about Vars
1060  * in advance.  Matching a parent tlist to a child is still an O(N^2)
1061  * operation, but at least with a much smaller constant factor than plain
1062  * tlist_member() searches.
1063  *
1064  * The result of this function is an indexed_tlist struct to pass to
1065  * search_indexed_tlist_for_var() or search_indexed_tlist_for_non_var().
1066  * When done, the indexed_tlist may be freed with a single pfree().
1067  */
1068 static indexed_tlist *
1069 build_tlist_index(List *tlist)
1070 {
1071         indexed_tlist *itlist;
1072         tlist_vinfo *vinfo;
1073         ListCell   *l;
1074
1075         /* Create data structure with enough slots for all tlist entries */
1076         itlist = (indexed_tlist *)
1077                 palloc(offsetof(indexed_tlist, vars) +
1078                            list_length(tlist) * sizeof(tlist_vinfo));
1079
1080         itlist->tlist = tlist;
1081         itlist->has_non_vars = false;
1082
1083         /* Find the Vars and fill in the index array */
1084         vinfo = itlist->vars;
1085         foreach(l, tlist)
1086         {
1087                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1088
1089                 if (tle->expr && IsA(tle->expr, Var))
1090                 {
1091                         Var                *var = (Var *) tle->expr;
1092
1093                         vinfo->varno = var->varno;
1094                         vinfo->varattno = var->varattno;
1095                         vinfo->resno = tle->resno;
1096                         vinfo++;
1097                 }
1098                 else
1099                         itlist->has_non_vars = true;
1100         }
1101
1102         itlist->num_vars = (vinfo - itlist->vars);
1103
1104         return itlist;
1105 }
1106
1107 /*
1108  * build_tlist_index_other_vars --- build a restricted tlist index
1109  *
1110  * This is like build_tlist_index, but we only index tlist entries that
1111  * are Vars and belong to some rel other than the one specified.
1112  */
1113 static indexed_tlist *
1114 build_tlist_index_other_vars(List *tlist, Index ignore_rel)
1115 {
1116         indexed_tlist *itlist;
1117         tlist_vinfo *vinfo;
1118         ListCell   *l;
1119
1120         /* Create data structure with enough slots for all tlist entries */
1121         itlist = (indexed_tlist *)
1122                 palloc(offsetof(indexed_tlist, vars) +
1123                            list_length(tlist) * sizeof(tlist_vinfo));
1124
1125         itlist->tlist = tlist;
1126         itlist->has_non_vars = false;
1127
1128         /* Find the desired Vars and fill in the index array */
1129         vinfo = itlist->vars;
1130         foreach(l, tlist)
1131         {
1132                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1133
1134                 if (tle->expr && IsA(tle->expr, Var))
1135                 {
1136                         Var                *var = (Var *) tle->expr;
1137
1138                         if (var->varno != ignore_rel)
1139                         {
1140                                 vinfo->varno = var->varno;
1141                                 vinfo->varattno = var->varattno;
1142                                 vinfo->resno = tle->resno;
1143                                 vinfo++;
1144                         }
1145                 }
1146         }
1147
1148         itlist->num_vars = (vinfo - itlist->vars);
1149
1150         return itlist;
1151 }
1152
1153 /*
1154  * search_indexed_tlist_for_var --- find a Var in an indexed tlist
1155  *
1156  * If a match is found, return a copy of the given Var with suitably
1157  * modified varno/varattno (to wit, newvarno and the resno of the TLE entry).
1158  * Also ensure that varnoold is incremented by rtoffset.
1159  * If no match, return NULL.
1160  */
1161 static Var *
1162 search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
1163                                                          Index newvarno, int rtoffset)
1164 {
1165         Index           varno = var->varno;
1166         AttrNumber      varattno = var->varattno;
1167         tlist_vinfo *vinfo;
1168         int                     i;
1169
1170         vinfo = itlist->vars;
1171         i = itlist->num_vars;
1172         while (i-- > 0)
1173         {
1174                 if (vinfo->varno == varno && vinfo->varattno == varattno)
1175                 {
1176                         /* Found a match */
1177                         Var                *newvar = copyVar(var);
1178
1179                         newvar->varno = newvarno;
1180                         newvar->varattno = vinfo->resno;
1181                         if (newvar->varnoold > 0)
1182                                 newvar->varnoold += rtoffset;
1183                         return newvar;
1184                 }
1185                 vinfo++;
1186         }
1187         return NULL;                            /* no match */
1188 }
1189
1190 /*
1191  * search_indexed_tlist_for_non_var --- find a non-Var in an indexed tlist
1192  *
1193  * If a match is found, return a Var constructed to reference the tlist item.
1194  * If no match, return NULL.
1195  *
1196  * NOTE: it is a waste of time to call this if !itlist->has_non_vars
1197  */
1198 static Var *
1199 search_indexed_tlist_for_non_var(Node *node,
1200                                                                  indexed_tlist *itlist, Index newvarno)
1201 {
1202         TargetEntry *tle;
1203
1204         tle = tlist_member(node, itlist->tlist);
1205         if (tle)
1206         {
1207                 /* Found a matching subplan output expression */
1208                 Var                *newvar;
1209
1210                 newvar = makeVar(newvarno,
1211                                                  tle->resno,
1212                                                  exprType((Node *) tle->expr),
1213                                                  exprTypmod((Node *) tle->expr),
1214                                                  0);
1215                 newvar->varnoold = 0;   /* wasn't ever a plain Var */
1216                 newvar->varoattno = 0;
1217                 return newvar;
1218         }
1219         return NULL;                            /* no match */
1220 }
1221
1222 /*
1223  * fix_join_expr
1224  *         Create a new set of targetlist entries or join qual clauses by
1225  *         changing the varno/varattno values of variables in the clauses
1226  *         to reference target list values from the outer and inner join
1227  *         relation target lists.  Also perform opcode lookup and add
1228  *         regclass OIDs to glob->relationOids.
1229  *
1230  * This is used in two different scenarios: a normal join clause, where
1231  * all the Vars in the clause *must* be replaced by OUTER or INNER references;
1232  * and an indexscan being used on the inner side of a nestloop join.
1233  * In the latter case we want to replace the outer-relation Vars by OUTER
1234  * references, while Vars of the inner relation should be adjusted by rtoffset.
1235  * (We also implement RETURNING clause fixup using this second scenario.)
1236  *
1237  * For a normal join, acceptable_rel should be zero so that any failure to
1238  * match a Var will be reported as an error.  For the indexscan case,
1239  * pass inner_itlist = NULL and acceptable_rel = the (not-offseted-yet) ID
1240  * of the inner relation.
1241  *
1242  * 'clauses' is the targetlist or list of join clauses
1243  * 'outer_itlist' is the indexed target list of the outer join relation
1244  * 'inner_itlist' is the indexed target list of the inner join relation,
1245  *              or NULL
1246  * 'acceptable_rel' is either zero or the rangetable index of a relation
1247  *              whose Vars may appear in the clause without provoking an error.
1248  * 'rtoffset' is what to add to varno for Vars of acceptable_rel.
1249  *
1250  * Returns the new expression tree.  The original clause structure is
1251  * not modified.
1252  */
1253 static List *
1254 fix_join_expr(PlannerGlobal *glob,
1255                           List *clauses,
1256                           indexed_tlist *outer_itlist,
1257                           indexed_tlist *inner_itlist,
1258                           Index acceptable_rel,
1259                           int rtoffset)
1260 {
1261         fix_join_expr_context context;
1262
1263         context.glob = glob;
1264         context.outer_itlist = outer_itlist;
1265         context.inner_itlist = inner_itlist;
1266         context.acceptable_rel = acceptable_rel;
1267         context.rtoffset = rtoffset;
1268         return (List *) fix_join_expr_mutator((Node *) clauses, &context);
1269 }
1270
1271 static Node *
1272 fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
1273 {
1274         Var                *newvar;
1275
1276         if (node == NULL)
1277                 return NULL;
1278         if (IsA(node, Var))
1279         {
1280                 Var                *var = (Var *) node;
1281
1282                 /* First look for the var in the input tlists */
1283                 newvar = search_indexed_tlist_for_var(var,
1284                                                                                           context->outer_itlist,
1285                                                                                           OUTER,
1286                                                                                           context->rtoffset);
1287                 if (newvar)
1288                         return (Node *) newvar;
1289                 if (context->inner_itlist)
1290                 {
1291                         newvar = search_indexed_tlist_for_var(var,
1292                                                                                                   context->inner_itlist,
1293                                                                                                   INNER,
1294                                                                                                   context->rtoffset);
1295                         if (newvar)
1296                                 return (Node *) newvar;
1297                 }
1298
1299                 /* If it's for acceptable_rel, adjust and return it */
1300                 if (var->varno == context->acceptable_rel)
1301                 {
1302                         var = copyVar(var);
1303                         var->varno += context->rtoffset;
1304                         var->varnoold += context->rtoffset;
1305                         return (Node *) var;
1306                 }
1307
1308                 /* No referent found for Var */
1309                 elog(ERROR, "variable not found in subplan target lists");
1310         }
1311         /* Try matching more complex expressions too, if tlists have any */
1312         if (context->outer_itlist->has_non_vars)
1313         {
1314                 newvar = search_indexed_tlist_for_non_var(node,
1315                                                                                                   context->outer_itlist,
1316                                                                                                   OUTER);
1317                 if (newvar)
1318                         return (Node *) newvar;
1319         }
1320         if (context->inner_itlist && context->inner_itlist->has_non_vars)
1321         {
1322                 newvar = search_indexed_tlist_for_non_var(node,
1323                                                                                                   context->inner_itlist,
1324                                                                                                   INNER);
1325                 if (newvar)
1326                         return (Node *) newvar;
1327         }
1328         /*
1329          * Since we update opcode info in-place, this part could possibly
1330          * scribble on the planner's input data structures, but it's OK.
1331          */
1332         if (IsA(node, OpExpr))
1333                 set_opfuncid((OpExpr *) node);
1334         else if (IsA(node, DistinctExpr))
1335                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1336         else if (IsA(node, NullIfExpr))
1337                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1338         else if (IsA(node, ScalarArrayOpExpr))
1339                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1340         else if (IsA(node, Const))
1341         {
1342                 Const      *con = (Const *) node;
1343
1344                 /* Check for regclass reference */
1345                 if (con->consttype == REGCLASSOID && !con->constisnull)
1346                         context->glob->relationOids =
1347                                 lappend_oid(context->glob->relationOids,
1348                                                         DatumGetObjectId(con->constvalue));
1349                 /* Fall through to let expression_tree_mutator copy it */
1350         }
1351         return expression_tree_mutator(node,
1352                                                                    fix_join_expr_mutator,
1353                                                                    (void *) context);
1354 }
1355
1356 /*
1357  * fix_upper_expr
1358  *              Modifies an expression tree so that all Var nodes reference outputs
1359  *              of a subplan.  Also performs opcode lookup, and adds regclass OIDs to
1360  *              glob->relationOids.
1361  *
1362  * This is used to fix up target and qual expressions of non-join upper-level
1363  * plan nodes.
1364  *
1365  * An error is raised if no matching var can be found in the subplan tlist
1366  * --- so this routine should only be applied to nodes whose subplans'
1367  * targetlists were generated via flatten_tlist() or some such method.
1368  *
1369  * If itlist->has_non_vars is true, then we try to match whole subexpressions
1370  * against elements of the subplan tlist, so that we can avoid recomputing
1371  * expressions that were already computed by the subplan.  (This is relatively
1372  * expensive, so we don't want to try it in the common case where the
1373  * subplan tlist is just a flattened list of Vars.)
1374  *
1375  * 'node': the tree to be fixed (a target item or qual)
1376  * 'subplan_itlist': indexed target list for subplan
1377  * 'rtoffset': how much to increment varnoold by
1378  *
1379  * The resulting tree is a copy of the original in which all Var nodes have
1380  * varno = OUTER, varattno = resno of corresponding subplan target.
1381  * The original tree is not modified.
1382  */
1383 static Node *
1384 fix_upper_expr(PlannerGlobal *glob,
1385                            Node *node,
1386                            indexed_tlist *subplan_itlist,
1387                            int rtoffset)
1388 {
1389         fix_upper_expr_context context;
1390
1391         context.glob = glob;
1392         context.subplan_itlist = subplan_itlist;
1393         context.rtoffset = rtoffset;
1394         return fix_upper_expr_mutator(node, &context);
1395 }
1396
1397 static Node *
1398 fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
1399 {
1400         Var                *newvar;
1401
1402         if (node == NULL)
1403                 return NULL;
1404         if (IsA(node, Var))
1405         {
1406                 Var                *var = (Var *) node;
1407
1408                 newvar = search_indexed_tlist_for_var(var,
1409                                                                                           context->subplan_itlist,
1410                                                                                           OUTER,
1411                                                                                           context->rtoffset);
1412                 if (!newvar)
1413                         elog(ERROR, "variable not found in subplan target list");
1414                 return (Node *) newvar;
1415         }
1416         /* Try matching more complex expressions too, if tlist has any */
1417         if (context->subplan_itlist->has_non_vars)
1418         {
1419                 newvar = search_indexed_tlist_for_non_var(node,
1420                                                                                                   context->subplan_itlist,
1421                                                                                                   OUTER);
1422                 if (newvar)
1423                         return (Node *) newvar;
1424         }
1425         /*
1426          * Since we update opcode info in-place, this part could possibly
1427          * scribble on the planner's input data structures, but it's OK.
1428          */
1429         if (IsA(node, OpExpr))
1430                 set_opfuncid((OpExpr *) node);
1431         else if (IsA(node, DistinctExpr))
1432                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1433         else if (IsA(node, NullIfExpr))
1434                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1435         else if (IsA(node, ScalarArrayOpExpr))
1436                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1437         else if (IsA(node, Const))
1438         {
1439                 Const      *con = (Const *) node;
1440
1441                 /* Check for regclass reference */
1442                 if (con->consttype == REGCLASSOID && !con->constisnull)
1443                         context->glob->relationOids =
1444                                 lappend_oid(context->glob->relationOids,
1445                                                         DatumGetObjectId(con->constvalue));
1446                 /* Fall through to let expression_tree_mutator copy it */
1447         }
1448         return expression_tree_mutator(node,
1449                                                                    fix_upper_expr_mutator,
1450                                                                    (void *) context);
1451 }
1452
1453 /*
1454  * set_returning_clause_references
1455  *              Perform setrefs.c's work on a RETURNING targetlist
1456  *
1457  * If the query involves more than just the result table, we have to
1458  * adjust any Vars that refer to other tables to reference junk tlist
1459  * entries in the top plan's targetlist.  Vars referencing the result
1460  * table should be left alone, however (the executor will evaluate them
1461  * using the actual heap tuple, after firing triggers if any).  In the
1462  * adjusted RETURNING list, result-table Vars will still have their
1463  * original varno, but Vars for other rels will have varno OUTER.
1464  *
1465  * We also must perform opcode lookup and add regclass OIDs to
1466  * glob->relationOids.
1467  *
1468  * 'rlist': the RETURNING targetlist to be fixed
1469  * 'topplan': the top Plan node for the query (not yet passed through
1470  *              set_plan_references)
1471  * 'resultRelation': RT index of the associated result relation
1472  *
1473  * Note: we assume that result relations will have rtoffset zero, that is,
1474  * they are not coming from a subplan.
1475  */
1476 List *
1477 set_returning_clause_references(PlannerGlobal *glob,
1478                                                                 List *rlist,
1479                                                                 Plan *topplan,
1480                                                                 Index resultRelation)
1481 {
1482         indexed_tlist *itlist;
1483
1484         /*
1485          * We can perform the desired Var fixup by abusing the fix_join_expr
1486          * machinery that normally handles inner indexscan fixup.  We search the
1487          * top plan's targetlist for Vars of non-result relations, and use
1488          * fix_join_expr to convert RETURNING Vars into references to those
1489          * tlist entries, while leaving result-rel Vars as-is.
1490          */
1491         itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
1492
1493         rlist = fix_join_expr(glob,
1494                                                   rlist,
1495                                                   itlist,
1496                                                   NULL,
1497                                                   resultRelation,
1498                                                   0);
1499
1500         pfree(itlist);
1501
1502         return rlist;
1503 }
1504
1505 /*****************************************************************************
1506  *                                      OPERATOR REGPROC LOOKUP
1507  *****************************************************************************/
1508
1509 /*
1510  * fix_opfuncids
1511  *        Calculate opfuncid field from opno for each OpExpr node in given tree.
1512  *        The given tree can be anything expression_tree_walker handles.
1513  *
1514  * The argument is modified in-place.  (This is OK since we'd want the
1515  * same change for any node, even if it gets visited more than once due to
1516  * shared structure.)
1517  */
1518 void
1519 fix_opfuncids(Node *node)
1520 {
1521         /* This tree walk requires no special setup, so away we go... */
1522         fix_opfuncids_walker(node, NULL);
1523 }
1524
1525 static bool
1526 fix_opfuncids_walker(Node *node, void *context)
1527 {
1528         if (node == NULL)
1529                 return false;
1530         if (IsA(node, OpExpr))
1531                 set_opfuncid((OpExpr *) node);
1532         else if (IsA(node, DistinctExpr))
1533                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1534         else if (IsA(node, NullIfExpr))
1535                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1536         else if (IsA(node, ScalarArrayOpExpr))
1537                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1538         return expression_tree_walker(node, fix_opfuncids_walker, context);
1539 }
1540
1541 /*
1542  * set_opfuncid
1543  *              Set the opfuncid (procedure OID) in an OpExpr node,
1544  *              if it hasn't been set already.
1545  *
1546  * Because of struct equivalence, this can also be used for
1547  * DistinctExpr and NullIfExpr nodes.
1548  */
1549 void
1550 set_opfuncid(OpExpr *opexpr)
1551 {
1552         if (opexpr->opfuncid == InvalidOid)
1553                 opexpr->opfuncid = get_opcode(opexpr->opno);
1554 }
1555
1556 /*
1557  * set_sa_opfuncid
1558  *              As above, for ScalarArrayOpExpr nodes.
1559  */
1560 void
1561 set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
1562 {
1563         if (opexpr->opfuncid == InvalidOid)
1564                 opexpr->opfuncid = get_opcode(opexpr->opno);
1565 }