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