]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/setrefs.c
Don't remove the 'alias' field from flattened rangetable entries;
[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.134 2007/04/06 22:57:20 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  * fix_scan_expr
570  *              Do set_plan_references processing on a scan-level expression
571  *
572  * This consists of incrementing all Vars' varnos by rtoffset and
573  * looking up operator opcode info for OpExpr and related nodes.
574  */
575 static Node *
576 fix_scan_expr(Node *node, int rtoffset)
577 {
578         fix_scan_expr_context context;
579
580         context.rtoffset = rtoffset;
581         return fix_scan_expr_mutator(node, &context);
582 }
583
584 static Node *
585 fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
586 {
587         if (node == NULL)
588                 return NULL;
589         if (IsA(node, Var))
590         {
591                 Var                *var = (Var *) copyObject(node);
592
593                 Assert(var->varlevelsup == 0);
594                 /*
595                  * We should not see any Vars marked INNER, but in a nestloop inner
596                  * scan there could be OUTER Vars.  Leave them alone.
597                  */
598                 Assert(var->varno != INNER);
599                 if (var->varno > 0 && var->varno != OUTER)
600                         var->varno += context->rtoffset;
601                 if (var->varnoold > 0)
602                         var->varnoold += context->rtoffset;
603                 return (Node *) var;
604         }
605         /*
606          * Since we update opcode info in-place, this part could possibly
607          * scribble on the planner's input data structures, but it's OK.
608          */
609         if (IsA(node, OpExpr))
610                 set_opfuncid((OpExpr *) node);
611         else if (IsA(node, DistinctExpr))
612                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
613         else if (IsA(node, NullIfExpr))
614                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
615         else if (IsA(node, ScalarArrayOpExpr))
616                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
617         return expression_tree_mutator(node, fix_scan_expr_mutator,
618                                                                    (void *) context);
619 }
620
621 /*
622  * set_join_references
623  *        Modify the target list and quals of a join node to reference its
624  *        subplans, by setting the varnos to OUTER or INNER and setting attno
625  *        values to the result domain number of either the corresponding outer
626  *        or inner join tuple item.  Also perform opcode lookup for these
627  *        expressions.
628  *
629  * In the case of a nestloop with inner indexscan, we will also need to
630  * apply the same transformation to any outer vars appearing in the
631  * quals of the child indexscan.  set_inner_join_references does that.
632  */
633 static void
634 set_join_references(Join *join, int rtoffset)
635 {
636         Plan       *outer_plan = join->plan.lefttree;
637         Plan       *inner_plan = join->plan.righttree;
638         indexed_tlist *outer_itlist;
639         indexed_tlist *inner_itlist;
640
641         outer_itlist = build_tlist_index(outer_plan->targetlist);
642         inner_itlist = build_tlist_index(inner_plan->targetlist);
643
644         /* All join plans have tlist, qual, and joinqual */
645         join->plan.targetlist = fix_join_expr(join->plan.targetlist,
646                                                                                   outer_itlist,
647                                                                                   inner_itlist,
648                                                                                   (Index) 0,
649                                                                                   rtoffset);
650         join->plan.qual = fix_join_expr(join->plan.qual,
651                                                                         outer_itlist,
652                                                                         inner_itlist,
653                                                                         (Index) 0,
654                                                                         rtoffset);
655         join->joinqual = fix_join_expr(join->joinqual,
656                                                                    outer_itlist,
657                                                                    inner_itlist,
658                                                                    (Index) 0,
659                                                                    rtoffset);
660
661         /* Now do join-type-specific stuff */
662         if (IsA(join, NestLoop))
663         {
664                 /* This processing is split out to handle possible recursion */
665                 set_inner_join_references(inner_plan, outer_itlist);
666         }
667         else if (IsA(join, MergeJoin))
668         {
669                 MergeJoin  *mj = (MergeJoin *) join;
670
671                 mj->mergeclauses = fix_join_expr(mj->mergeclauses,
672                                                                                  outer_itlist,
673                                                                                  inner_itlist,
674                                                                                  (Index) 0,
675                                                                                  rtoffset);
676         }
677         else if (IsA(join, HashJoin))
678         {
679                 HashJoin   *hj = (HashJoin *) join;
680
681                 hj->hashclauses = fix_join_expr(hj->hashclauses,
682                                                                                 outer_itlist,
683                                                                                 inner_itlist,
684                                                                                 (Index) 0,
685                                                                                 rtoffset);
686         }
687
688         pfree(outer_itlist);
689         pfree(inner_itlist);
690 }
691
692 /*
693  * set_inner_join_references
694  *              Handle join references appearing in an inner indexscan's quals
695  *
696  * To handle bitmap-scan plan trees, we have to be able to recurse down
697  * to the bottom BitmapIndexScan nodes; likewise, appendrel indexscans
698  * require recursing through Append nodes.      This is split out as a separate
699  * function so that it can recurse.
700  *
701  * Note we do *not* apply any rtoffset for non-join Vars; this is because
702  * the quals will be processed again by fix_scan_expr when the set_plan_refs
703  * recursion reaches the inner indexscan, and so we'd have done it twice.
704  */
705 static void
706 set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist)
707 {
708         if (IsA(inner_plan, IndexScan))
709         {
710                 /*
711                  * An index is being used to reduce the number of tuples scanned in
712                  * the inner relation.  If there are join clauses being used with the
713                  * index, we must update their outer-rel var nodes to refer to the
714                  * outer side of the join.
715                  */
716                 IndexScan  *innerscan = (IndexScan *) inner_plan;
717                 List       *indexqualorig = innerscan->indexqualorig;
718
719                 /* No work needed if indexqual refers only to its own rel... */
720                 if (NumRelids((Node *) indexqualorig) > 1)
721                 {
722                         Index           innerrel = innerscan->scan.scanrelid;
723
724                         /* only refs to outer vars get changed in the inner qual */
725                         innerscan->indexqualorig = fix_join_expr(indexqualorig,
726                                                                                                          outer_itlist,
727                                                                                                          NULL,
728                                                                                                          innerrel,
729                                                                                                          0);
730                         innerscan->indexqual = fix_join_expr(innerscan->indexqual,
731                                                                                                  outer_itlist,
732                                                                                                  NULL,
733                                                                                                  innerrel,
734                                                                                                  0);
735
736                         /*
737                          * We must fix the inner qpqual too, if it has join clauses (this
738                          * could happen if special operators are involved: some indexquals
739                          * may get rechecked as qpquals).
740                          */
741                         if (NumRelids((Node *) inner_plan->qual) > 1)
742                                 inner_plan->qual = fix_join_expr(inner_plan->qual,
743                                                                                                  outer_itlist,
744                                                                                                  NULL,
745                                                                                                  innerrel,
746                                                                                                  0);
747                 }
748         }
749         else if (IsA(inner_plan, BitmapIndexScan))
750         {
751                 /*
752                  * Same, but index is being used within a bitmap plan.
753                  */
754                 BitmapIndexScan *innerscan = (BitmapIndexScan *) inner_plan;
755                 List       *indexqualorig = innerscan->indexqualorig;
756
757                 /* No work needed if indexqual refers only to its own rel... */
758                 if (NumRelids((Node *) indexqualorig) > 1)
759                 {
760                         Index           innerrel = innerscan->scan.scanrelid;
761
762                         /* only refs to outer vars get changed in the inner qual */
763                         innerscan->indexqualorig = fix_join_expr(indexqualorig,
764                                                                                                          outer_itlist,
765                                                                                                          NULL,
766                                                                                                          innerrel,
767                                                                                                          0);
768                         innerscan->indexqual = fix_join_expr(innerscan->indexqual,
769                                                                                                  outer_itlist,
770                                                                                                  NULL,
771                                                                                                  innerrel,
772                                                                                                  0);
773                         /* no need to fix inner qpqual */
774                         Assert(inner_plan->qual == NIL);
775                 }
776         }
777         else if (IsA(inner_plan, BitmapHeapScan))
778         {
779                 /*
780                  * The inner side is a bitmap scan plan.  Fix the top node, and
781                  * recurse to get the lower nodes.
782                  *
783                  * Note: create_bitmap_scan_plan removes clauses from bitmapqualorig
784                  * if they are duplicated in qpqual, so must test these independently.
785                  */
786                 BitmapHeapScan *innerscan = (BitmapHeapScan *) inner_plan;
787                 Index           innerrel = innerscan->scan.scanrelid;
788                 List       *bitmapqualorig = innerscan->bitmapqualorig;
789
790                 /* only refs to outer vars get changed in the inner qual */
791                 if (NumRelids((Node *) bitmapqualorig) > 1)
792                         innerscan->bitmapqualorig = fix_join_expr(bitmapqualorig,
793                                                                                                           outer_itlist,
794                                                                                                           NULL,
795                                                                                                           innerrel,
796                                                                                                           0);
797
798                 /*
799                  * We must fix the inner qpqual too, if it has join clauses (this
800                  * could happen if special operators are involved: some indexquals may
801                  * get rechecked as qpquals).
802                  */
803                 if (NumRelids((Node *) inner_plan->qual) > 1)
804                         inner_plan->qual = fix_join_expr(inner_plan->qual,
805                                                                                          outer_itlist,
806                                                                                          NULL,
807                                                                                          innerrel,
808                                                                                          0);
809
810                 /* Now recurse */
811                 set_inner_join_references(inner_plan->lefttree, outer_itlist);
812         }
813         else if (IsA(inner_plan, BitmapAnd))
814         {
815                 /* All we need do here is recurse */
816                 BitmapAnd  *innerscan = (BitmapAnd *) inner_plan;
817                 ListCell   *l;
818
819                 foreach(l, innerscan->bitmapplans)
820                 {
821                         set_inner_join_references((Plan *) lfirst(l), outer_itlist);
822                 }
823         }
824         else if (IsA(inner_plan, BitmapOr))
825         {
826                 /* All we need do here is recurse */
827                 BitmapOr   *innerscan = (BitmapOr *) inner_plan;
828                 ListCell   *l;
829
830                 foreach(l, innerscan->bitmapplans)
831                 {
832                         set_inner_join_references((Plan *) lfirst(l), outer_itlist);
833                 }
834         }
835         else if (IsA(inner_plan, TidScan))
836         {
837                 TidScan    *innerscan = (TidScan *) inner_plan;
838                 Index           innerrel = innerscan->scan.scanrelid;
839
840                 innerscan->tidquals = fix_join_expr(innerscan->tidquals,
841                                                                                         outer_itlist,
842                                                                                         NULL,
843                                                                                         innerrel,
844                                                                                         0);
845         }
846         else if (IsA(inner_plan, Append))
847         {
848                 /*
849                  * The inner side is an append plan.  Recurse to see if it contains
850                  * indexscans that need to be fixed.
851                  */
852                 Append     *appendplan = (Append *) inner_plan;
853                 ListCell   *l;
854
855                 foreach(l, appendplan->appendplans)
856                 {
857                         set_inner_join_references((Plan *) lfirst(l), outer_itlist);
858                 }
859         }
860         else if (IsA(inner_plan, Result))
861         {
862                 /* Recurse through a gating Result node (similar to Append case) */
863                 Result     *result = (Result *) inner_plan;
864
865                 if (result->plan.lefttree)
866                         set_inner_join_references(result->plan.lefttree, outer_itlist);
867         }
868 }
869
870 /*
871  * set_upper_references
872  *        Update the targetlist and quals of an upper-level plan node
873  *        to refer to the tuples returned by its lefttree subplan.
874  *        Also perform opcode lookup for these expressions.
875  *
876  * This is used for single-input plan types like Agg, Group, Result.
877  *
878  * In most cases, we have to match up individual Vars in the tlist and
879  * qual expressions with elements of the subplan's tlist (which was
880  * generated by flatten_tlist() from these selfsame expressions, so it
881  * should have all the required variables).  There is an important exception,
882  * however: GROUP BY and ORDER BY expressions will have been pushed into the
883  * subplan tlist unflattened.  If these values are also needed in the output
884  * then we want to reference the subplan tlist element rather than recomputing
885  * the expression.
886  */
887 static void
888 set_upper_references(Plan *plan, int rtoffset)
889 {
890         Plan       *subplan = plan->lefttree;
891         indexed_tlist *subplan_itlist;
892         List       *output_targetlist;
893         ListCell   *l;
894
895         subplan_itlist = build_tlist_index(subplan->targetlist);
896
897         output_targetlist = NIL;
898         foreach(l, plan->targetlist)
899         {
900                 TargetEntry *tle = (TargetEntry *) lfirst(l);
901                 Node       *newexpr;
902
903                 newexpr = fix_upper_expr((Node *) tle->expr,
904                                                                  subplan_itlist,
905                                                                  rtoffset);
906                 tle = flatCopyTargetEntry(tle);
907                 tle->expr = (Expr *) newexpr;
908                 output_targetlist = lappend(output_targetlist, tle);
909         }
910         plan->targetlist = output_targetlist;
911
912         plan->qual = (List *)
913                 fix_upper_expr((Node *) plan->qual,
914                                            subplan_itlist,
915                                            rtoffset);
916
917         pfree(subplan_itlist);
918 }
919
920 /*
921  * set_dummy_tlist_references
922  *        Replace the targetlist of an upper-level plan node with a simple
923  *        list of OUTER references to its child.
924  *
925  * This is used for plan types like Sort and Append that don't evaluate
926  * their targetlists.  Although the executor doesn't care at all what's in
927  * the tlist, EXPLAIN needs it to be realistic.
928  *
929  * Note: we could almost use set_upper_references() here, but it fails for
930  * Append for lack of a lefttree subplan.  Single-purpose code is faster
931  * anyway.
932  */
933 static void
934 set_dummy_tlist_references(Plan *plan, int rtoffset)
935 {
936         List       *output_targetlist;
937         ListCell   *l;
938
939         output_targetlist = NIL;
940         foreach(l, plan->targetlist)
941         {
942                 TargetEntry *tle = (TargetEntry *) lfirst(l);
943                 Var                *oldvar = (Var *) tle->expr;
944                 Var                *newvar;
945
946                 newvar = makeVar(OUTER,
947                                                  tle->resno,
948                                                  exprType((Node *) oldvar),
949                                                  exprTypmod((Node *) oldvar),
950                                                  0);
951                 if (IsA(oldvar, Var))
952                 {
953                         newvar->varnoold = oldvar->varno + rtoffset;
954                         newvar->varoattno = oldvar->varattno;
955                 }
956                 else
957                 {
958                         newvar->varnoold = 0;   /* wasn't ever a plain Var */
959                         newvar->varoattno = 0;
960                 }
961
962                 tle = flatCopyTargetEntry(tle);
963                 tle->expr = (Expr *) newvar;
964                 output_targetlist = lappend(output_targetlist, tle);
965         }
966         plan->targetlist = output_targetlist;
967
968         /* We don't touch plan->qual here */
969 }
970
971
972 /*
973  * build_tlist_index --- build an index data structure for a child tlist
974  *
975  * In most cases, subplan tlists will be "flat" tlists with only Vars,
976  * so we try to optimize that case by extracting information about Vars
977  * in advance.  Matching a parent tlist to a child is still an O(N^2)
978  * operation, but at least with a much smaller constant factor than plain
979  * tlist_member() searches.
980  *
981  * The result of this function is an indexed_tlist struct to pass to
982  * search_indexed_tlist_for_var() or search_indexed_tlist_for_non_var().
983  * When done, the indexed_tlist may be freed with a single pfree().
984  */
985 static indexed_tlist *
986 build_tlist_index(List *tlist)
987 {
988         indexed_tlist *itlist;
989         tlist_vinfo *vinfo;
990         ListCell   *l;
991
992         /* Create data structure with enough slots for all tlist entries */
993         itlist = (indexed_tlist *)
994                 palloc(offsetof(indexed_tlist, vars) +
995                            list_length(tlist) * sizeof(tlist_vinfo));
996
997         itlist->tlist = tlist;
998         itlist->has_non_vars = false;
999
1000         /* Find the Vars and fill in the index array */
1001         vinfo = itlist->vars;
1002         foreach(l, tlist)
1003         {
1004                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1005
1006                 if (tle->expr && IsA(tle->expr, Var))
1007                 {
1008                         Var                *var = (Var *) tle->expr;
1009
1010                         vinfo->varno = var->varno;
1011                         vinfo->varattno = var->varattno;
1012                         vinfo->resno = tle->resno;
1013                         vinfo++;
1014                 }
1015                 else
1016                         itlist->has_non_vars = true;
1017         }
1018
1019         itlist->num_vars = (vinfo - itlist->vars);
1020
1021         return itlist;
1022 }
1023
1024 /*
1025  * build_tlist_index_other_vars --- build a restricted tlist index
1026  *
1027  * This is like build_tlist_index, but we only index tlist entries that
1028  * are Vars and belong to some rel other than the one specified.
1029  */
1030 static indexed_tlist *
1031 build_tlist_index_other_vars(List *tlist, Index ignore_rel)
1032 {
1033         indexed_tlist *itlist;
1034         tlist_vinfo *vinfo;
1035         ListCell   *l;
1036
1037         /* Create data structure with enough slots for all tlist entries */
1038         itlist = (indexed_tlist *)
1039                 palloc(offsetof(indexed_tlist, vars) +
1040                            list_length(tlist) * sizeof(tlist_vinfo));
1041
1042         itlist->tlist = tlist;
1043         itlist->has_non_vars = false;
1044
1045         /* Find the desired Vars and fill in the index array */
1046         vinfo = itlist->vars;
1047         foreach(l, tlist)
1048         {
1049                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1050
1051                 if (tle->expr && IsA(tle->expr, Var))
1052                 {
1053                         Var                *var = (Var *) tle->expr;
1054
1055                         if (var->varno != ignore_rel)
1056                         {
1057                                 vinfo->varno = var->varno;
1058                                 vinfo->varattno = var->varattno;
1059                                 vinfo->resno = tle->resno;
1060                                 vinfo++;
1061                         }
1062                 }
1063         }
1064
1065         itlist->num_vars = (vinfo - itlist->vars);
1066
1067         return itlist;
1068 }
1069
1070 /*
1071  * search_indexed_tlist_for_var --- find a Var in an indexed tlist
1072  *
1073  * If a match is found, return a copy of the given Var with suitably
1074  * modified varno/varattno (to wit, newvarno and the resno of the TLE entry).
1075  * Also ensure that varnoold is incremented by rtoffset.
1076  * If no match, return NULL.
1077  */
1078 static Var *
1079 search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
1080                                                          Index newvarno, int rtoffset)
1081 {
1082         Index           varno = var->varno;
1083         AttrNumber      varattno = var->varattno;
1084         tlist_vinfo *vinfo;
1085         int                     i;
1086
1087         vinfo = itlist->vars;
1088         i = itlist->num_vars;
1089         while (i-- > 0)
1090         {
1091                 if (vinfo->varno == varno && vinfo->varattno == varattno)
1092                 {
1093                         /* Found a match */
1094                         Var                *newvar = (Var *) copyObject(var);
1095
1096                         newvar->varno = newvarno;
1097                         newvar->varattno = vinfo->resno;
1098                         if (newvar->varnoold > 0)
1099                                 newvar->varnoold += rtoffset;
1100                         return newvar;
1101                 }
1102                 vinfo++;
1103         }
1104         return NULL;                            /* no match */
1105 }
1106
1107 /*
1108  * search_indexed_tlist_for_non_var --- find a non-Var in an indexed tlist
1109  *
1110  * If a match is found, return a Var constructed to reference the tlist item.
1111  * If no match, return NULL.
1112  *
1113  * NOTE: it is a waste of time to call this if !itlist->has_non_vars
1114  */
1115 static Var *
1116 search_indexed_tlist_for_non_var(Node *node,
1117                                                                  indexed_tlist *itlist, Index newvarno)
1118 {
1119         TargetEntry *tle;
1120
1121         tle = tlist_member(node, itlist->tlist);
1122         if (tle)
1123         {
1124                 /* Found a matching subplan output expression */
1125                 Var                *newvar;
1126
1127                 newvar = makeVar(newvarno,
1128                                                  tle->resno,
1129                                                  exprType((Node *) tle->expr),
1130                                                  exprTypmod((Node *) tle->expr),
1131                                                  0);
1132                 newvar->varnoold = 0;   /* wasn't ever a plain Var */
1133                 newvar->varoattno = 0;
1134                 return newvar;
1135         }
1136         return NULL;                            /* no match */
1137 }
1138
1139 /*
1140  * fix_join_expr
1141  *         Create a new set of targetlist entries or join qual clauses by
1142  *         changing the varno/varattno values of variables in the clauses
1143  *         to reference target list values from the outer and inner join
1144  *         relation target lists.  Also perform opcode lookup.
1145  *
1146  * This is used in two different scenarios: a normal join clause, where
1147  * all the Vars in the clause *must* be replaced by OUTER or INNER references;
1148  * and an indexscan being used on the inner side of a nestloop join.
1149  * In the latter case we want to replace the outer-relation Vars by OUTER
1150  * references, while Vars of the inner relation should be adjusted by rtoffset.
1151  * (We also implement RETURNING clause fixup using this second scenario.)
1152  *
1153  * For a normal join, acceptable_rel should be zero so that any failure to
1154  * match a Var will be reported as an error.  For the indexscan case,
1155  * pass inner_itlist = NULL and acceptable_rel = the (not-offseted-yet) ID
1156  * of the inner relation.
1157  *
1158  * 'clauses' is the targetlist or list of join clauses
1159  * 'outer_itlist' is the indexed target list of the outer join relation
1160  * 'inner_itlist' is the indexed target list of the inner join relation,
1161  *              or NULL
1162  * 'acceptable_rel' is either zero or the rangetable index of a relation
1163  *              whose Vars may appear in the clause without provoking an error.
1164  * 'rtoffset' is what to add to varno for Vars of acceptable_rel.
1165  *
1166  * Returns the new expression tree.  The original clause structure is
1167  * not modified.
1168  */
1169 static List *
1170 fix_join_expr(List *clauses,
1171                           indexed_tlist *outer_itlist,
1172                           indexed_tlist *inner_itlist,
1173                           Index acceptable_rel,
1174                           int rtoffset)
1175 {
1176         fix_join_expr_context context;
1177
1178         context.outer_itlist = outer_itlist;
1179         context.inner_itlist = inner_itlist;
1180         context.acceptable_rel = acceptable_rel;
1181         context.rtoffset = rtoffset;
1182         return (List *) fix_join_expr_mutator((Node *) clauses, &context);
1183 }
1184
1185 static Node *
1186 fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
1187 {
1188         Var                *newvar;
1189
1190         if (node == NULL)
1191                 return NULL;
1192         if (IsA(node, Var))
1193         {
1194                 Var                *var = (Var *) node;
1195
1196                 /* First look for the var in the input tlists */
1197                 newvar = search_indexed_tlist_for_var(var,
1198                                                                                           context->outer_itlist,
1199                                                                                           OUTER,
1200                                                                                           context->rtoffset);
1201                 if (newvar)
1202                         return (Node *) newvar;
1203                 if (context->inner_itlist)
1204                 {
1205                         newvar = search_indexed_tlist_for_var(var,
1206                                                                                                   context->inner_itlist,
1207                                                                                                   INNER,
1208                                                                                                   context->rtoffset);
1209                         if (newvar)
1210                                 return (Node *) newvar;
1211                 }
1212
1213                 /* If it's for acceptable_rel, adjust and return it */
1214                 if (var->varno == context->acceptable_rel)
1215                 {
1216                         var = (Var *) copyObject(var);
1217                         var->varno += context->rtoffset;
1218                         var->varnoold += context->rtoffset;
1219                         return (Node *) var;
1220                 }
1221
1222                 /* No referent found for Var */
1223                 elog(ERROR, "variable not found in subplan target lists");
1224         }
1225         /* Try matching more complex expressions too, if tlists have any */
1226         if (context->outer_itlist->has_non_vars)
1227         {
1228                 newvar = search_indexed_tlist_for_non_var(node,
1229                                                                                                   context->outer_itlist,
1230                                                                                                   OUTER);
1231                 if (newvar)
1232                         return (Node *) newvar;
1233         }
1234         if (context->inner_itlist && context->inner_itlist->has_non_vars)
1235         {
1236                 newvar = search_indexed_tlist_for_non_var(node,
1237                                                                                                   context->inner_itlist,
1238                                                                                                   INNER);
1239                 if (newvar)
1240                         return (Node *) newvar;
1241         }
1242         /*
1243          * Since we update opcode info in-place, this part could possibly
1244          * scribble on the planner's input data structures, but it's OK.
1245          */
1246         if (IsA(node, OpExpr))
1247                 set_opfuncid((OpExpr *) node);
1248         else if (IsA(node, DistinctExpr))
1249                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1250         else if (IsA(node, NullIfExpr))
1251                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1252         else if (IsA(node, ScalarArrayOpExpr))
1253                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1254         return expression_tree_mutator(node,
1255                                                                    fix_join_expr_mutator,
1256                                                                    (void *) context);
1257 }
1258
1259 /*
1260  * fix_upper_expr
1261  *              Modifies an expression tree so that all Var nodes reference outputs
1262  *              of a subplan.  Also performs opcode lookup.
1263  *
1264  * This is used to fix up target and qual expressions of non-join upper-level
1265  * plan nodes.
1266  *
1267  * An error is raised if no matching var can be found in the subplan tlist
1268  * --- so this routine should only be applied to nodes whose subplans'
1269  * targetlists were generated via flatten_tlist() or some such method.
1270  *
1271  * If itlist->has_non_vars is true, then we try to match whole subexpressions
1272  * against elements of the subplan tlist, so that we can avoid recomputing
1273  * expressions that were already computed by the subplan.  (This is relatively
1274  * expensive, so we don't want to try it in the common case where the
1275  * subplan tlist is just a flattened list of Vars.)
1276  *
1277  * 'node': the tree to be fixed (a target item or qual)
1278  * 'subplan_itlist': indexed target list for subplan
1279  * 'rtoffset': how much to increment varnoold by
1280  *
1281  * The resulting tree is a copy of the original in which all Var nodes have
1282  * varno = OUTER, varattno = resno of corresponding subplan target.
1283  * The original tree is not modified.
1284  */
1285 static Node *
1286 fix_upper_expr(Node *node,
1287                            indexed_tlist *subplan_itlist,
1288                            int rtoffset)
1289 {
1290         fix_upper_expr_context context;
1291
1292         context.subplan_itlist = subplan_itlist;
1293         context.rtoffset = rtoffset;
1294         return fix_upper_expr_mutator(node, &context);
1295 }
1296
1297 static Node *
1298 fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
1299 {
1300         Var                *newvar;
1301
1302         if (node == NULL)
1303                 return NULL;
1304         if (IsA(node, Var))
1305         {
1306                 Var                *var = (Var *) node;
1307
1308                 newvar = search_indexed_tlist_for_var(var,
1309                                                                                           context->subplan_itlist,
1310                                                                                           OUTER,
1311                                                                                           context->rtoffset);
1312                 if (!newvar)
1313                         elog(ERROR, "variable not found in subplan target list");
1314                 return (Node *) newvar;
1315         }
1316         /* Try matching more complex expressions too, if tlist has any */
1317         if (context->subplan_itlist->has_non_vars)
1318         {
1319                 newvar = search_indexed_tlist_for_non_var(node,
1320                                                                                                   context->subplan_itlist,
1321                                                                                                   OUTER);
1322                 if (newvar)
1323                         return (Node *) newvar;
1324         }
1325         /*
1326          * Since we update opcode info in-place, this part could possibly
1327          * scribble on the planner's input data structures, but it's OK.
1328          */
1329         if (IsA(node, OpExpr))
1330                 set_opfuncid((OpExpr *) node);
1331         else if (IsA(node, DistinctExpr))
1332                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1333         else if (IsA(node, NullIfExpr))
1334                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1335         else if (IsA(node, ScalarArrayOpExpr))
1336                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1337         return expression_tree_mutator(node,
1338                                                                    fix_upper_expr_mutator,
1339                                                                    (void *) context);
1340 }
1341
1342 /*
1343  * set_returning_clause_references
1344  *              Perform setrefs.c's work on a RETURNING targetlist
1345  *
1346  * If the query involves more than just the result table, we have to
1347  * adjust any Vars that refer to other tables to reference junk tlist
1348  * entries in the top plan's targetlist.  Vars referencing the result
1349  * table should be left alone, however (the executor will evaluate them
1350  * using the actual heap tuple, after firing triggers if any).  In the
1351  * adjusted RETURNING list, result-table Vars will still have their
1352  * original varno, but Vars for other rels will have varno OUTER.
1353  *
1354  * We also must perform opcode lookup.
1355  *
1356  * 'rlist': the RETURNING targetlist to be fixed
1357  * 'topplan': the top Plan node for the query (not yet passed through
1358  *              set_plan_references)
1359  * 'resultRelation': RT index of the associated result relation
1360  *
1361  * Note: we assume that result relations will have rtoffset zero, that is,
1362  * they are not coming from a subplan.
1363  */
1364 List *
1365 set_returning_clause_references(List *rlist,
1366                                                                 Plan *topplan,
1367                                                                 Index resultRelation)
1368 {
1369         indexed_tlist *itlist;
1370
1371         /*
1372          * We can perform the desired Var fixup by abusing the fix_join_expr
1373          * machinery that normally handles inner indexscan fixup.  We search the
1374          * top plan's targetlist for Vars of non-result relations, and use
1375          * fix_join_expr to convert RETURNING Vars into references to those
1376          * tlist entries, while leaving result-rel Vars as-is.
1377          */
1378         itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
1379
1380         rlist = fix_join_expr(rlist,
1381                                                   itlist,
1382                                                   NULL,
1383                                                   resultRelation,
1384                                                   0);
1385
1386         pfree(itlist);
1387
1388         return rlist;
1389 }
1390
1391 /*****************************************************************************
1392  *                                      OPERATOR REGPROC LOOKUP
1393  *****************************************************************************/
1394
1395 /*
1396  * fix_opfuncids
1397  *        Calculate opfuncid field from opno for each OpExpr node in given tree.
1398  *        The given tree can be anything expression_tree_walker handles.
1399  *
1400  * The argument is modified in-place.  (This is OK since we'd want the
1401  * same change for any node, even if it gets visited more than once due to
1402  * shared structure.)
1403  */
1404 void
1405 fix_opfuncids(Node *node)
1406 {
1407         /* This tree walk requires no special setup, so away we go... */
1408         fix_opfuncids_walker(node, NULL);
1409 }
1410
1411 static bool
1412 fix_opfuncids_walker(Node *node, void *context)
1413 {
1414         if (node == NULL)
1415                 return false;
1416         if (IsA(node, OpExpr))
1417                 set_opfuncid((OpExpr *) node);
1418         else if (IsA(node, DistinctExpr))
1419                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1420         else if (IsA(node, NullIfExpr))
1421                 set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
1422         else if (IsA(node, ScalarArrayOpExpr))
1423                 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1424         return expression_tree_walker(node, fix_opfuncids_walker, context);
1425 }
1426
1427 /*
1428  * set_opfuncid
1429  *              Set the opfuncid (procedure OID) in an OpExpr node,
1430  *              if it hasn't been set already.
1431  *
1432  * Because of struct equivalence, this can also be used for
1433  * DistinctExpr and NullIfExpr nodes.
1434  */
1435 void
1436 set_opfuncid(OpExpr *opexpr)
1437 {
1438         if (opexpr->opfuncid == InvalidOid)
1439                 opexpr->opfuncid = get_opcode(opexpr->opno);
1440 }
1441
1442 /*
1443  * set_sa_opfuncid
1444  *              As above, for ScalarArrayOpExpr nodes.
1445  */
1446 void
1447 set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
1448 {
1449         if (opexpr->opfuncid == InvalidOid)
1450                 opexpr->opfuncid = get_opcode(opexpr->opno);
1451 }