]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/subselect.c
Get rid of the rather fuzzily defined FlattenedSubLink node type in favor of
[postgresql] / src / backend / optimizer / plan / subselect.c
1 /*-------------------------------------------------------------------------
2  *
3  * subselect.c
4  *        Planning routines for subselects and parameters.
5  *
6  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  *        $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.146 2009/02/25 03:30:37 tgl Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15
16 #include "catalog/pg_operator.h"
17 #include "catalog/pg_type.h"
18 #include "miscadmin.h"
19 #include "nodes/makefuncs.h"
20 #include "nodes/nodeFuncs.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/planmain.h"
24 #include "optimizer/planner.h"
25 #include "optimizer/prep.h"
26 #include "optimizer/subselect.h"
27 #include "optimizer/var.h"
28 #include "parser/parse_relation.h"
29 #include "parser/parsetree.h"
30 #include "rewrite/rewriteManip.h"
31 #include "utils/builtins.h"
32 #include "utils/lsyscache.h"
33 #include "utils/syscache.h"
34
35
36 typedef struct convert_testexpr_context
37 {
38         PlannerInfo *root;
39         List       *subst_nodes;        /* Nodes to substitute for Params */
40 } convert_testexpr_context;
41
42 typedef struct process_sublinks_context
43 {
44         PlannerInfo *root;
45         bool            isTopQual;
46 } process_sublinks_context;
47
48 typedef struct finalize_primnode_context
49 {
50         PlannerInfo *root;
51         Bitmapset  *paramids;           /* Non-local PARAM_EXEC paramids found */
52 } finalize_primnode_context;
53
54
55 static Node *build_subplan(PlannerInfo *root, Plan *plan, List *rtable,
56                           SubLinkType subLinkType, Node *testexpr,
57                           bool adjust_testexpr, bool unknownEqFalse);
58 static List *generate_subquery_params(PlannerInfo *root, List *tlist,
59                                                                           List **paramIds);
60 static List *generate_subquery_vars(PlannerInfo *root, List *tlist,
61                                                                         Index varno);
62 static Node *convert_testexpr(PlannerInfo *root,
63                                  Node *testexpr,
64                                  List *subst_nodes);
65 static Node *convert_testexpr_mutator(Node *node,
66                                                  convert_testexpr_context *context);
67 static bool subplan_is_hashable(Plan *plan);
68 static bool testexpr_is_hashable(Node *testexpr);
69 static bool hash_ok_operator(OpExpr *expr);
70 static bool simplify_EXISTS_query(Query *query);
71 static Query *convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
72                                           Node **testexpr, List **paramIds);
73 static Node *replace_correlation_vars_mutator(Node *node, PlannerInfo *root);
74 static Node *process_sublinks_mutator(Node *node,
75                                                  process_sublinks_context *context);
76 static Bitmapset *finalize_plan(PlannerInfo *root,
77                           Plan *plan,
78                           Bitmapset *valid_params);
79 static bool finalize_primnode(Node *node, finalize_primnode_context *context);
80
81
82 /*
83  * Generate a Param node to replace the given Var,
84  * which is expected to have varlevelsup > 0 (ie, it is not local).
85  */
86 static Param *
87 replace_outer_var(PlannerInfo *root, Var *var)
88 {
89         Param      *retval;
90         ListCell   *ppl;
91         PlannerParamItem *pitem;
92         Index           abslevel;
93         int                     i;
94
95         Assert(var->varlevelsup > 0 && var->varlevelsup < root->query_level);
96         abslevel = root->query_level - var->varlevelsup;
97
98         /*
99          * If there's already a paramlist entry for this same Var, just use it.
100          * NOTE: in sufficiently complex querytrees, it is possible for the same
101          * varno/abslevel to refer to different RTEs in different parts of the
102          * parsetree, so that different fields might end up sharing the same Param
103          * number.      As long as we check the vartype/typmod as well, I believe that
104          * this sort of aliasing will cause no trouble.  The correct field should
105          * get stored into the Param slot at execution in each part of the tree.
106          */
107         i = 0;
108         foreach(ppl, root->glob->paramlist)
109         {
110                 pitem = (PlannerParamItem *) lfirst(ppl);
111                 if (pitem->abslevel == abslevel && IsA(pitem->item, Var))
112                 {
113                         Var                *pvar = (Var *) pitem->item;
114
115                         if (pvar->varno == var->varno &&
116                                 pvar->varattno == var->varattno &&
117                                 pvar->vartype == var->vartype &&
118                                 pvar->vartypmod == var->vartypmod)
119                                 break;
120                 }
121                 i++;
122         }
123
124         if (!ppl)
125         {
126                 /* Nope, so make a new one */
127                 var = (Var *) copyObject(var);
128                 var->varlevelsup = 0;
129
130                 pitem = makeNode(PlannerParamItem);
131                 pitem->item = (Node *) var;
132                 pitem->abslevel = abslevel;
133
134                 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
135                 /* i is already the correct index for the new item */
136         }
137
138         retval = makeNode(Param);
139         retval->paramkind = PARAM_EXEC;
140         retval->paramid = i;
141         retval->paramtype = var->vartype;
142         retval->paramtypmod = var->vartypmod;
143         retval->location = -1;
144
145         return retval;
146 }
147
148 /*
149  * Generate a Param node to replace the given Aggref
150  * which is expected to have agglevelsup > 0 (ie, it is not local).
151  */
152 static Param *
153 replace_outer_agg(PlannerInfo *root, Aggref *agg)
154 {
155         Param      *retval;
156         PlannerParamItem *pitem;
157         Index           abslevel;
158         int                     i;
159
160         Assert(agg->agglevelsup > 0 && agg->agglevelsup < root->query_level);
161         abslevel = root->query_level - agg->agglevelsup;
162
163         /*
164          * It does not seem worthwhile to try to match duplicate outer aggs. Just
165          * make a new slot every time.
166          */
167         agg = (Aggref *) copyObject(agg);
168         IncrementVarSublevelsUp((Node *) agg, -((int) agg->agglevelsup), 0);
169         Assert(agg->agglevelsup == 0);
170
171         pitem = makeNode(PlannerParamItem);
172         pitem->item = (Node *) agg;
173         pitem->abslevel = abslevel;
174
175         root->glob->paramlist = lappend(root->glob->paramlist, pitem);
176         i = list_length(root->glob->paramlist) - 1;
177
178         retval = makeNode(Param);
179         retval->paramkind = PARAM_EXEC;
180         retval->paramid = i;
181         retval->paramtype = agg->aggtype;
182         retval->paramtypmod = -1;
183         retval->location = -1;
184
185         return retval;
186 }
187
188 /*
189  * Generate a new Param node that will not conflict with any other.
190  *
191  * This is used to allocate PARAM_EXEC slots for subplan outputs.
192  */
193 static Param *
194 generate_new_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod)
195 {
196         Param      *retval;
197         PlannerParamItem *pitem;
198
199         retval = makeNode(Param);
200         retval->paramkind = PARAM_EXEC;
201         retval->paramid = list_length(root->glob->paramlist);
202         retval->paramtype = paramtype;
203         retval->paramtypmod = paramtypmod;
204         retval->location = -1;
205
206         pitem = makeNode(PlannerParamItem);
207         pitem->item = (Node *) retval;
208         pitem->abslevel = root->query_level;
209
210         root->glob->paramlist = lappend(root->glob->paramlist, pitem);
211
212         return retval;
213 }
214
215 /*
216  * Assign a (nonnegative) PARAM_EXEC ID for a recursive query's worktable.
217  */
218 int
219 SS_assign_worktable_param(PlannerInfo *root)
220 {
221         Param      *param;
222
223         /* We generate a Param of datatype INTERNAL */
224         param = generate_new_param(root, INTERNALOID, -1);
225         /* ... but the caller only cares about its ID */
226         return param->paramid;
227 }
228
229 /*
230  * Get the datatype of the first column of the plan's output.
231  *
232  * This is stored for ARRAY_SUBLINK and for exprType(), which doesn't have any
233  * way to get at the plan associated with a SubPlan node.  We really only need
234  * the value for EXPR_SUBLINK and ARRAY_SUBLINK subplans, but for consistency
235  * we set it always.
236  */
237 static Oid
238 get_first_col_type(Plan *plan)
239 {
240         /* In cases such as EXISTS, tlist might be empty; arbitrarily use VOID */
241         if (plan->targetlist)
242         {
243                 TargetEntry *tent = (TargetEntry *) linitial(plan->targetlist);
244
245                 Assert(IsA(tent, TargetEntry));
246                 if (!tent->resjunk)
247                         return exprType((Node *) tent->expr);
248         }
249         return VOIDOID;
250 }
251
252 /*
253  * Convert a SubLink (as created by the parser) into a SubPlan.
254  *
255  * We are given the SubLink's contained query, type, and testexpr.  We are
256  * also told if this expression appears at top level of a WHERE/HAVING qual.
257  *
258  * Note: we assume that the testexpr has been AND/OR flattened (actually,
259  * it's been through eval_const_expressions), but not converted to
260  * implicit-AND form; and any SubLinks in it should already have been
261  * converted to SubPlans.  The subquery is as yet untouched, however.
262  *
263  * The result is whatever we need to substitute in place of the SubLink
264  * node in the executable expression.  This will be either the SubPlan
265  * node (if we have to do the subplan as a subplan), or a Param node
266  * representing the result of an InitPlan, or a row comparison expression
267  * tree containing InitPlan Param nodes.
268  */
269 static Node *
270 make_subplan(PlannerInfo *root, Query *orig_subquery, SubLinkType subLinkType,
271                          Node *testexpr, bool isTopQual)
272 {
273         Query      *subquery;
274         bool            simple_exists = false;
275         double          tuple_fraction;
276         Plan       *plan;
277         PlannerInfo *subroot;
278         Node       *result;
279
280         /*
281          * Copy the source Query node.  This is a quick and dirty kluge to resolve
282          * the fact that the parser can generate trees with multiple links to the
283          * same sub-Query node, but the planner wants to scribble on the Query.
284          * Try to clean this up when we do querytree redesign...
285          */
286         subquery = (Query *) copyObject(orig_subquery);
287
288         /*
289          * If it's an EXISTS subplan, we might be able to simplify it.
290          */
291         if (subLinkType == EXISTS_SUBLINK)
292                 simple_exists = simplify_EXISTS_query(subquery);
293
294         /*
295          * For an EXISTS subplan, tell lower-level planner to expect that only the
296          * first tuple will be retrieved.  For ALL and ANY subplans, we will be
297          * able to stop evaluating if the test condition fails or matches, so very
298          * often not all the tuples will be retrieved; for lack of a better idea,
299          * specify 50% retrieval.  For EXPR and ROWCOMPARE subplans, use default
300          * behavior (we're only expecting one row out, anyway).
301          *
302          * NOTE: if you change these numbers, also change cost_subplan() in
303          * path/costsize.c.
304          *
305          * XXX If an ANY subplan is uncorrelated, build_subplan may decide to hash
306          * its output.  In that case it would've been better to specify full
307          * retrieval.  At present, however, we can only check hashability after
308          * we've made the subplan :-(.  (Determining whether it'll fit in work_mem
309          * is the really hard part.)  Therefore, we don't want to be too
310          * optimistic about the percentage of tuples retrieved, for fear of
311          * selecting a plan that's bad for the materialization case.
312          */
313         if (subLinkType == EXISTS_SUBLINK)
314                 tuple_fraction = 1.0;   /* just like a LIMIT 1 */
315         else if (subLinkType == ALL_SUBLINK ||
316                          subLinkType == ANY_SUBLINK)
317                 tuple_fraction = 0.5;   /* 50% */
318         else
319                 tuple_fraction = 0.0;   /* default behavior */
320
321         /*
322          * Generate the plan for the subquery.
323          */
324         plan = subquery_planner(root->glob, subquery,
325                                                         root,
326                                                         false, tuple_fraction,
327                                                         &subroot);
328
329         /* And convert to SubPlan or InitPlan format. */
330         result = build_subplan(root, plan, subroot->parse->rtable,
331                                                    subLinkType, testexpr, true, isTopQual);
332
333         /*
334          * If it's a correlated EXISTS with an unimportant targetlist, we might be
335          * able to transform it to the equivalent of an IN and then implement it
336          * by hashing.  We don't have enough information yet to tell which way
337          * is likely to be better (it depends on the expected number of executions
338          * of the EXISTS qual, and we are much too early in planning the outer
339          * query to be able to guess that).  So we generate both plans, if
340          * possible, and leave it to the executor to decide which to use.
341          */
342         if (simple_exists && IsA(result, SubPlan))
343         {
344                 Node       *newtestexpr;
345                 List       *paramIds;
346
347                 /* Make a second copy of the original subquery */
348                 subquery = (Query *) copyObject(orig_subquery);
349                 /* and re-simplify */
350                 simple_exists = simplify_EXISTS_query(subquery);
351                 Assert(simple_exists);
352                 /* See if it can be converted to an ANY query */
353                 subquery = convert_EXISTS_to_ANY(root, subquery,
354                                                                                  &newtestexpr, &paramIds);
355                 if (subquery)
356                 {
357                         /* Generate the plan for the ANY subquery; we'll need all rows */
358                         plan = subquery_planner(root->glob, subquery,
359                                                                         root,
360                                                                         false, 0.0,
361                                                                         &subroot);
362
363                         /* Now we can check if it'll fit in work_mem */
364                         if (subplan_is_hashable(plan))
365                         {
366                                 SubPlan    *hashplan;
367                                 AlternativeSubPlan *asplan;
368
369                                 /* OK, convert to SubPlan format. */
370                                 hashplan = (SubPlan *) build_subplan(root, plan,
371                                                                                                          subroot->parse->rtable,
372                                                                                                          ANY_SUBLINK, newtestexpr,
373                                                                                                          false, true);
374                                 /* Check we got what we expected */
375                                 Assert(IsA(hashplan, SubPlan));
376                                 Assert(hashplan->parParam == NIL);
377                                 Assert(hashplan->useHashTable);
378                                 /* build_subplan won't have filled in paramIds */
379                                 hashplan->paramIds = paramIds;
380
381                                 /* Leave it to the executor to decide which plan to use */
382                                 asplan = makeNode(AlternativeSubPlan);
383                                 asplan->subplans = list_make2(result, hashplan);
384                                 result = (Node *) asplan;
385                         }
386                 }
387         }
388
389         return result;
390 }
391
392 /*
393  * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
394  *
395  * Returns either the SubPlan, or an expression using initplan output Params,
396  * as explained in the comments for make_subplan.
397  */
398 static Node *
399 build_subplan(PlannerInfo *root, Plan *plan, List *rtable,
400                           SubLinkType subLinkType, Node *testexpr,
401                           bool adjust_testexpr, bool unknownEqFalse)
402 {
403         Node       *result;
404         SubPlan    *splan;
405         bool            isInitPlan;
406         Bitmapset  *tmpset;
407         int                     paramid;
408
409         /*
410          * Initialize the SubPlan node.  Note plan_id isn't set till further down,
411          * likewise the cost fields.
412          */
413         splan = makeNode(SubPlan);
414         splan->subLinkType = subLinkType;
415         splan->testexpr = NULL;
416         splan->paramIds = NIL;
417         splan->firstColType = get_first_col_type(plan);
418         splan->useHashTable = false;
419         splan->unknownEqFalse = unknownEqFalse;
420         splan->setParam = NIL;
421         splan->parParam = NIL;
422         splan->args = NIL;
423
424         /*
425          * Make parParam and args lists of param IDs and expressions that current
426          * query level will pass to this child plan.
427          */
428         tmpset = bms_copy(plan->extParam);
429         while ((paramid = bms_first_member(tmpset)) >= 0)
430         {
431                 PlannerParamItem *pitem = list_nth(root->glob->paramlist, paramid);
432
433                 if (pitem->abslevel == root->query_level)
434                 {
435                         splan->parParam = lappend_int(splan->parParam, paramid);
436                         /*
437                          * The Var or Aggref has already been adjusted to have the correct
438                          * varlevelsup or agglevelsup.  We probably don't even need to
439                          * copy it again, but be safe.
440                          */
441                         splan->args = lappend(splan->args, copyObject(pitem->item));
442                 }
443         }
444         bms_free(tmpset);
445
446         /*
447          * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY, or
448          * ROWCOMPARE types can be used as initPlans.  For EXISTS, EXPR, or ARRAY,
449          * we just produce a Param referring to the result of evaluating the
450          * initPlan.  For ROWCOMPARE, we must modify the testexpr tree to contain
451          * PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted by the
452          * parser.
453          */
454         if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
455         {
456                 Param      *prm;
457
458                 Assert(testexpr == NULL);
459                 prm = generate_new_param(root, BOOLOID, -1);
460                 splan->setParam = list_make1_int(prm->paramid);
461                 isInitPlan = true;
462                 result = (Node *) prm;
463         }
464         else if (splan->parParam == NIL && subLinkType == EXPR_SUBLINK)
465         {
466                 TargetEntry *te = linitial(plan->targetlist);
467                 Param      *prm;
468
469                 Assert(!te->resjunk);
470                 Assert(testexpr == NULL);
471                 prm = generate_new_param(root,
472                                                                  exprType((Node *) te->expr),
473                                                                  exprTypmod((Node *) te->expr));
474                 splan->setParam = list_make1_int(prm->paramid);
475                 isInitPlan = true;
476                 result = (Node *) prm;
477         }
478         else if (splan->parParam == NIL && subLinkType == ARRAY_SUBLINK)
479         {
480                 TargetEntry *te = linitial(plan->targetlist);
481                 Oid                     arraytype;
482                 Param      *prm;
483
484                 Assert(!te->resjunk);
485                 Assert(testexpr == NULL);
486                 arraytype = get_array_type(exprType((Node *) te->expr));
487                 if (!OidIsValid(arraytype))
488                         elog(ERROR, "could not find array type for datatype %s",
489                                  format_type_be(exprType((Node *) te->expr)));
490                 prm = generate_new_param(root,
491                                                                  arraytype,
492                                                                  exprTypmod((Node *) te->expr));
493                 splan->setParam = list_make1_int(prm->paramid);
494                 isInitPlan = true;
495                 result = (Node *) prm;
496         }
497         else if (splan->parParam == NIL && subLinkType == ROWCOMPARE_SUBLINK)
498         {
499                 /* Adjust the Params */
500                 List       *params;
501
502                 Assert(testexpr != NULL);
503                 params = generate_subquery_params(root,
504                                                                                   plan->targetlist,
505                                                                                   &splan->paramIds);
506                 result = convert_testexpr(root,
507                                                                   testexpr,
508                                                                   params);
509                 splan->setParam = list_copy(splan->paramIds);
510                 isInitPlan = true;
511
512                 /*
513                  * The executable expression is returned to become part of the outer
514                  * plan's expression tree; it is not kept in the initplan node.
515                  */
516         }
517         else
518         {
519                 /*
520                  * Adjust the Params in the testexpr, unless caller said it's not
521                  * needed.
522                  */
523                 if (testexpr && adjust_testexpr)
524                 {
525                         List       *params;
526
527                         params = generate_subquery_params(root,
528                                                                                           plan->targetlist,
529                                                                                           &splan->paramIds);
530                         splan->testexpr = convert_testexpr(root,
531                                                                                            testexpr,
532                                                                                            params);
533                 }
534                 else
535                         splan->testexpr = testexpr;
536
537                 /*
538                  * We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
539                  * initPlans, even when they are uncorrelated or undirect correlated,
540                  * because we need to scan the output of the subplan for each outer
541                  * tuple.  But if it's a not-direct-correlated IN (= ANY) test, we
542                  * might be able to use a hashtable to avoid comparing all the tuples.
543                  */
544                 if (subLinkType == ANY_SUBLINK &&
545                         splan->parParam == NIL &&
546                         subplan_is_hashable(plan) &&
547                         testexpr_is_hashable(splan->testexpr))
548                         splan->useHashTable = true;
549
550                 /*
551                  * Otherwise, we have the option to tack a MATERIAL node onto the top
552                  * of the subplan, to reduce the cost of reading it repeatedly.  This
553                  * is pointless for a direct-correlated subplan, since we'd have to
554                  * recompute its results each time anyway.      For uncorrelated/undirect
555                  * correlated subplans, we add MATERIAL unless the subplan's top plan
556                  * node would materialize its output anyway.
557                  */
558                 else if (splan->parParam == NIL)
559                 {
560                         bool            use_material;
561
562                         switch (nodeTag(plan))
563                         {
564                                 case T_Material:
565                                 case T_FunctionScan:
566                                 case T_CteScan:
567                                 case T_WorkTableScan:
568                                 case T_Sort:
569                                         use_material = false;
570                                         break;
571                                 default:
572                                         use_material = true;
573                                         break;
574                         }
575                         if (use_material)
576                                 plan = materialize_finished_plan(plan);
577                 }
578
579                 result = (Node *) splan;
580                 isInitPlan = false;
581         }
582
583         /*
584          * Add the subplan and its rtable to the global lists.
585          */
586         root->glob->subplans = lappend(root->glob->subplans, plan);
587         root->glob->subrtables = lappend(root->glob->subrtables, rtable);
588         splan->plan_id = list_length(root->glob->subplans);
589
590         if (isInitPlan)
591                 root->init_plans = lappend(root->init_plans, splan);
592
593         /*
594          * A parameterless subplan (not initplan) should be prepared to handle
595          * REWIND efficiently.  If it has direct parameters then there's no point
596          * since it'll be reset on each scan anyway; and if it's an initplan then
597          * there's no point since it won't get re-run without parameter changes
598          * anyway.      The input of a hashed subplan doesn't need REWIND either.
599          */
600         if (splan->parParam == NIL && !isInitPlan && !splan->useHashTable)
601                 root->glob->rewindPlanIDs = bms_add_member(root->glob->rewindPlanIDs,
602                                                                                                    splan->plan_id);
603
604         /* Lastly, fill in the cost estimates for use later */
605         cost_subplan(root, splan, plan);
606
607         return result;
608 }
609
610 /*
611  * generate_subquery_params: build a list of Params representing the output
612  * columns of a sublink's sub-select, given the sub-select's targetlist.
613  *
614  * We also return an integer list of the paramids of the Params.
615  */
616 static List *
617 generate_subquery_params(PlannerInfo *root, List *tlist, List **paramIds)
618 {
619         List       *result;
620         List       *ids;
621         ListCell   *lc;
622
623         result = ids = NIL;
624         foreach(lc, tlist)
625         {
626                 TargetEntry *tent = (TargetEntry *) lfirst(lc);
627                 Param      *param;
628
629                 if (tent->resjunk)
630                         continue;
631
632                 param = generate_new_param(root,
633                                                                    exprType((Node *) tent->expr),
634                                                                    exprTypmod((Node *) tent->expr));
635                 result = lappend(result, param);
636                 ids = lappend_int(ids, param->paramid);
637         }
638
639         *paramIds = ids;
640         return result;
641 }
642
643 /*
644  * generate_subquery_vars: build a list of Vars representing the output
645  * columns of a sublink's sub-select, given the sub-select's targetlist.
646  * The Vars have the specified varno (RTE index).
647  */
648 static List *
649 generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
650 {
651         List       *result;
652         ListCell   *lc;
653
654         result = NIL;
655         foreach(lc, tlist)
656         {
657                 TargetEntry *tent = (TargetEntry *) lfirst(lc);
658                 Var                *var;
659
660                 if (tent->resjunk)
661                         continue;
662
663                 var = makeVar(varno,
664                                           tent->resno,
665                                           exprType((Node *) tent->expr),
666                                           exprTypmod((Node *) tent->expr),
667                                           0);
668                 result = lappend(result, var);
669         }
670
671         return result;
672 }
673
674 /*
675  * convert_testexpr: convert the testexpr given by the parser into
676  * actually executable form.  This entails replacing PARAM_SUBLINK Params
677  * with Params or Vars representing the results of the sub-select.  The
678  * nodes to be substituted are passed in as the List result from
679  * generate_subquery_params or generate_subquery_vars.
680  *
681  * The given testexpr has already been recursively processed by
682  * process_sublinks_mutator.  Hence it can no longer contain any
683  * PARAM_SUBLINK Params for lower SubLink nodes; we can safely assume that
684  * any we find are for our own level of SubLink.
685  */
686 static Node *
687 convert_testexpr(PlannerInfo *root,
688                                  Node *testexpr,
689                                  List *subst_nodes)
690 {
691         convert_testexpr_context context;
692
693         context.root = root;
694         context.subst_nodes = subst_nodes;
695         return convert_testexpr_mutator(testexpr, &context);
696 }
697
698 static Node *
699 convert_testexpr_mutator(Node *node,
700                                                  convert_testexpr_context *context)
701 {
702         if (node == NULL)
703                 return NULL;
704         if (IsA(node, Param))
705         {
706                 Param      *param = (Param *) node;
707
708                 if (param->paramkind == PARAM_SUBLINK)
709                 {
710                         if (param->paramid <= 0 ||
711                                 param->paramid > list_length(context->subst_nodes))
712                                 elog(ERROR, "unexpected PARAM_SUBLINK ID: %d", param->paramid);
713
714                         /*
715                          * We copy the list item to avoid having doubly-linked
716                          * substructure in the modified parse tree.  This is probably
717                          * unnecessary when it's a Param, but be safe.
718                          */
719                         return (Node *) copyObject(list_nth(context->subst_nodes,
720                                                                                                 param->paramid - 1));
721                 }
722         }
723         return expression_tree_mutator(node,
724                                                                    convert_testexpr_mutator,
725                                                                    (void *) context);
726 }
727
728 /*
729  * subplan_is_hashable: can we implement an ANY subplan by hashing?
730  */
731 static bool
732 subplan_is_hashable(Plan *plan)
733 {
734         double          subquery_size;
735
736         /*
737          * The estimated size of the subquery result must fit in work_mem. (Note:
738          * we use sizeof(HeapTupleHeaderData) here even though the tuples will
739          * actually be stored as MinimalTuples; this provides some fudge factor
740          * for hashtable overhead.)
741          */
742         subquery_size = plan->plan_rows *
743                 (MAXALIGN(plan->plan_width) + MAXALIGN(sizeof(HeapTupleHeaderData)));
744         if (subquery_size > work_mem * 1024L)
745                 return false;
746
747         return true;
748 }
749
750 /*
751  * testexpr_is_hashable: is an ANY SubLink's test expression hashable?
752  */
753 static bool
754 testexpr_is_hashable(Node *testexpr)
755 {
756         /*
757          * The testexpr must be a single OpExpr, or an AND-clause containing
758          * only OpExprs.
759          *
760          * The combining operators must be hashable and strict. The need for
761          * hashability is obvious, since we want to use hashing. Without
762          * strictness, behavior in the presence of nulls is too unpredictable.  We
763          * actually must assume even more than plain strictness: they can't yield
764          * NULL for non-null inputs, either (see nodeSubplan.c).  However, hash
765          * indexes and hash joins assume that too.
766          */
767         if (testexpr && IsA(testexpr, OpExpr))
768         {
769                 if (hash_ok_operator((OpExpr *) testexpr))
770                         return true;
771         }
772         else if (and_clause(testexpr))
773         {
774                 ListCell   *l;
775
776                 foreach(l, ((BoolExpr *) testexpr)->args)
777                 {
778                         Node       *andarg = (Node *) lfirst(l);
779
780                         if (!IsA(andarg, OpExpr))
781                                 return false;
782                         if (!hash_ok_operator((OpExpr *) andarg))
783                                 return false;
784                 }
785                 return true;
786         }
787
788         return false;
789 }
790
791 static bool
792 hash_ok_operator(OpExpr *expr)
793 {
794         Oid                     opid = expr->opno;
795         HeapTuple       tup;
796         Form_pg_operator optup;
797
798         /* quick out if not a binary operator */
799         if (list_length(expr->args) != 2)
800                 return false;
801         /* else must look up the operator properties */
802         tup = SearchSysCache(OPEROID,
803                                                  ObjectIdGetDatum(opid),
804                                                  0, 0, 0);
805         if (!HeapTupleIsValid(tup))
806                 elog(ERROR, "cache lookup failed for operator %u", opid);
807         optup = (Form_pg_operator) GETSTRUCT(tup);
808         if (!optup->oprcanhash || !func_strict(optup->oprcode))
809         {
810                 ReleaseSysCache(tup);
811                 return false;
812         }
813         ReleaseSysCache(tup);
814         return true;
815 }
816
817
818 /*
819  * SS_process_ctes: process a query's WITH list
820  *
821  * We plan each interesting WITH item and convert it to an initplan.
822  * A side effect is to fill in root->cte_plan_ids with a list that
823  * parallels root->parse->cteList and provides the subplan ID for
824  * each CTE's initplan.
825  */
826 void
827 SS_process_ctes(PlannerInfo *root)
828 {
829         ListCell   *lc;
830
831         Assert(root->cte_plan_ids == NIL);
832
833         foreach(lc, root->parse->cteList)
834         {
835                 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
836                 Query      *subquery;
837                 Plan       *plan;
838                 PlannerInfo *subroot;
839                 SubPlan    *splan;
840                 Bitmapset  *tmpset;
841                 int                     paramid;
842                 Param      *prm;
843
844                 /*
845                  * Ignore CTEs that are not actually referenced anywhere.
846                  */
847                 if (cte->cterefcount == 0)
848                 {
849                         /* Make a dummy entry in cte_plan_ids */
850                         root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
851                         continue;
852                 }
853
854                 /*
855                  * Copy the source Query node.  Probably not necessary, but let's
856                  * keep this similar to make_subplan.
857                  */
858                 subquery = (Query *) copyObject(cte->ctequery);
859
860                 /*
861                  * Generate the plan for the CTE query.  Always plan for full
862                  * retrieval --- we don't have enough info to predict otherwise.
863                  */
864                 plan = subquery_planner(root->glob, subquery,
865                                                                 root,
866                                                                 cte->cterecursive, 0.0,
867                                                                 &subroot);
868
869                 /*
870                  * Make a SubPlan node for it.  This is just enough unlike
871                  * build_subplan that we can't share code.
872                  *
873                  * Note plan_id isn't set till further down, likewise the cost fields.
874                  */
875                 splan = makeNode(SubPlan);
876                 splan->subLinkType = CTE_SUBLINK;
877                 splan->testexpr = NULL;
878                 splan->paramIds = NIL;
879                 splan->firstColType = get_first_col_type(plan);
880                 splan->useHashTable = false;
881                 splan->unknownEqFalse = false;
882                 splan->setParam = NIL;
883                 splan->parParam = NIL;
884                 splan->args = NIL;
885
886                 /*
887                  * Make parParam and args lists of param IDs and expressions that
888                  * current query level will pass to this child plan.  Even though
889                  * this is an initplan, there could be side-references to earlier
890                  * initplan's outputs, specifically their CTE output parameters.
891                  */
892                 tmpset = bms_copy(plan->extParam);
893                 while ((paramid = bms_first_member(tmpset)) >= 0)
894                 {
895                         PlannerParamItem *pitem = list_nth(root->glob->paramlist, paramid);
896
897                         if (pitem->abslevel == root->query_level)
898                         {
899                                 prm = (Param *) pitem->item;
900                                 if (!IsA(prm, Param) ||
901                                         prm->paramtype != INTERNALOID)
902                                         elog(ERROR, "bogus local parameter passed to WITH query");
903
904                                 splan->parParam = lappend_int(splan->parParam, paramid);
905                                 splan->args = lappend(splan->args, copyObject(prm));
906                         }
907                 }
908                 bms_free(tmpset);
909
910                 /*
911                  * Assign a param to represent the query output.  We only really
912                  * care about reserving a parameter ID number.
913                  */
914                 prm = generate_new_param(root, INTERNALOID, -1);
915                 splan->setParam = list_make1_int(prm->paramid);
916
917                 /*
918                  * Add the subplan and its rtable to the global lists.
919                  */
920                 root->glob->subplans = lappend(root->glob->subplans, plan);
921                 root->glob->subrtables = lappend(root->glob->subrtables,
922                                                                                  subroot->parse->rtable);
923                 splan->plan_id = list_length(root->glob->subplans);
924
925                 root->init_plans = lappend(root->init_plans, splan);
926
927                 root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
928
929                 /* Lastly, fill in the cost estimates for use later */
930                 cost_subplan(root, splan, plan);
931         }
932 }
933
934 /*
935  * convert_ANY_sublink_to_join: try to convert an ANY SubLink to a join
936  *
937  * The caller has found an ANY SubLink at the top level of one of the query's
938  * qual clauses, but has not checked the properties of the SubLink further.
939  * Decide whether it is appropriate to process this SubLink in join style.
940  * If so, form a JoinExpr and return it.  Return NULL if the SubLink cannot
941  * be converted to a join.
942  *
943  * The only non-obvious input parameter is available_rels: this is the set
944  * of query rels that can safely be referenced in the sublink expression.
945  * (We must restrict this to avoid changing the semantics when a sublink
946  * is present in an outer join's ON qual.)  The conversion must fail if
947  * the converted qual would reference any but these parent-query relids.
948  *
949  * On success, the returned JoinExpr has larg = NULL and rarg = the jointree
950  * item representing the pulled-up subquery.  The caller must set larg to
951  * represent the relation(s) on the lefthand side of the new join, and insert
952  * the JoinExpr into the upper query's jointree at an appropriate place
953  * (typically, where the lefthand relation(s) had been).  Note that the
954  * passed-in SubLink must also be removed from its original position in the
955  * query quals, since the quals of the returned JoinExpr replace it.
956  * (Notionally, we replace the SubLink with a constant TRUE, then elide the
957  * redundant constant from the qual.)
958  *
959  * Side effects of a successful conversion include adding the SubLink's
960  * subselect to the query's rangetable, so that it can be referenced in
961  * the JoinExpr's rarg.
962  */
963 JoinExpr *
964 convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
965                                                         Relids available_rels)
966 {
967         JoinExpr   *result;
968         Query      *parse = root->parse;
969         Query      *subselect = (Query *) sublink->subselect;
970         Relids          upper_varnos;
971         int                     rtindex;
972         RangeTblEntry *rte;
973         RangeTblRef *rtr;
974         List       *subquery_vars;
975         Node       *quals;
976
977         Assert(sublink->subLinkType == ANY_SUBLINK);
978
979         /*
980          * The sub-select must not refer to any Vars of the parent query. (Vars of
981          * higher levels should be okay, though.)
982          */
983         if (contain_vars_of_level((Node *) subselect, 1))
984                 return NULL;
985
986         /*
987          * The test expression must contain some Vars of the parent query,
988          * else it's not gonna be a join.  (Note that it won't have Vars
989          * referring to the subquery, rather Params.)
990          */
991         upper_varnos = pull_varnos(sublink->testexpr);
992         if (bms_is_empty(upper_varnos))
993                 return NULL;
994
995         /*
996          * However, it can't refer to anything outside available_rels.
997          */
998         if (!bms_is_subset(upper_varnos, available_rels))
999                 return NULL;
1000
1001         /*
1002          * The combining operators and left-hand expressions mustn't be volatile.
1003          */
1004         if (contain_volatile_functions(sublink->testexpr))
1005                 return NULL;
1006
1007         /*
1008          * Okay, pull up the sub-select into upper range table.
1009          *
1010          * We rely here on the assumption that the outer query has no references
1011          * to the inner (necessarily true, other than the Vars that we build
1012          * below). Therefore this is a lot easier than what pull_up_subqueries has
1013          * to go through.
1014          */
1015         rte = addRangeTableEntryForSubquery(NULL,
1016                                                                                 subselect,
1017                                                                                 makeAlias("ANY_subquery", NIL),
1018                                                                                 false);
1019         parse->rtable = lappend(parse->rtable, rte);
1020         rtindex = list_length(parse->rtable);
1021
1022         /*
1023          * Form a RangeTblRef for the pulled-up sub-select.
1024          */
1025         rtr = makeNode(RangeTblRef);
1026         rtr->rtindex = rtindex;
1027
1028         /*
1029          * Build a list of Vars representing the subselect outputs.
1030          */
1031         subquery_vars = generate_subquery_vars(root,
1032                                                                                    subselect->targetList,
1033                                                                                    rtindex);
1034
1035         /*
1036          * Build the new join's qual expression, replacing Params with these Vars.
1037          */
1038         quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
1039
1040         /*
1041          * And finally, build the JoinExpr node.
1042          */
1043         result = makeNode(JoinExpr);
1044         result->jointype = JOIN_SEMI;
1045         result->isNatural = false;
1046         result->larg = NULL;            /* caller must fill this in */
1047         result->rarg = (Node *) rtr;
1048         result->using = NIL;
1049         result->quals = quals;
1050         result->alias = NULL;
1051         result->rtindex = 0;            /* we don't need an RTE for it */
1052
1053         return result;
1054 }
1055
1056 /*
1057  * convert_EXISTS_sublink_to_join: try to convert an EXISTS SubLink to a join
1058  *
1059  * The API of this function is identical to convert_ANY_sublink_to_join's,
1060  * except that we also support the case where the caller has found NOT EXISTS,
1061  * so we need an additional input parameter "under_not".
1062  */
1063 JoinExpr *
1064 convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
1065                                                            bool under_not, Relids available_rels)
1066 {
1067         JoinExpr   *result;
1068         Query      *parse = root->parse;
1069         Query      *subselect = (Query *) sublink->subselect;
1070         Node       *whereClause;
1071         int                     rtoffset;
1072         int                     varno;
1073         Relids          clause_varnos;
1074         Relids          upper_varnos;
1075
1076         Assert(sublink->subLinkType == EXISTS_SUBLINK);
1077
1078         /*
1079          * Copy the subquery so we can modify it safely (see comments in
1080          * make_subplan).
1081          */
1082         subselect = (Query *) copyObject(subselect);
1083
1084         /*
1085          * See if the subquery can be simplified based on the knowledge that
1086          * it's being used in EXISTS().  If we aren't able to get rid of its
1087          * targetlist, we have to fail, because the pullup operation leaves
1088          * us with noplace to evaluate the targetlist.
1089          */
1090         if (!simplify_EXISTS_query(subselect))
1091                 return NULL;
1092
1093         /*
1094          * The subquery must have a nonempty jointree, else we won't have a join.
1095          */
1096         if (subselect->jointree->fromlist == NIL)
1097                 return NULL;
1098
1099         /*
1100          * Separate out the WHERE clause.  (We could theoretically also remove
1101          * top-level plain JOIN/ON clauses, but it's probably not worth the
1102          * trouble.)
1103          */
1104         whereClause = subselect->jointree->quals;
1105         subselect->jointree->quals = NULL;
1106
1107         /*
1108          * The rest of the sub-select must not refer to any Vars of the parent
1109          * query.  (Vars of higher levels should be okay, though.)
1110          */
1111         if (contain_vars_of_level((Node *) subselect, 1))
1112                 return NULL;
1113
1114         /*
1115          * On the other hand, the WHERE clause must contain some Vars of the
1116          * parent query, else it's not gonna be a join.
1117          */
1118         if (!contain_vars_of_level(whereClause, 1))
1119                 return NULL;
1120
1121         /*
1122          * We don't risk optimizing if the WHERE clause is volatile, either.
1123          */
1124         if (contain_volatile_functions(whereClause))
1125                 return NULL;
1126
1127         /*
1128          * Prepare to pull up the sub-select into top range table.
1129          *
1130          * We rely here on the assumption that the outer query has no references
1131          * to the inner (necessarily true). Therefore this is a lot easier than
1132          * what pull_up_subqueries has to go through.
1133          *
1134          * In fact, it's even easier than what convert_ANY_sublink_to_join has
1135          * to do.  The machinations of simplify_EXISTS_query ensured that there
1136          * is nothing interesting in the subquery except an rtable and jointree,
1137          * and even the jointree FromExpr no longer has quals.  So we can just
1138          * append the rtable to our own and use the FromExpr in our jointree.
1139          * But first, adjust all level-zero varnos in the subquery to account
1140          * for the rtable merger.
1141          */
1142         rtoffset = list_length(parse->rtable);
1143         OffsetVarNodes((Node *) subselect, rtoffset, 0);
1144         OffsetVarNodes(whereClause, rtoffset, 0);
1145
1146         /*
1147          * Upper-level vars in subquery will now be one level closer to their
1148          * parent than before; in particular, anything that had been level 1
1149          * becomes level zero.
1150          */
1151         IncrementVarSublevelsUp((Node *) subselect, -1, 1);
1152         IncrementVarSublevelsUp(whereClause, -1, 1);
1153
1154         /*
1155          * Now that the WHERE clause is adjusted to match the parent query
1156          * environment, we can easily identify all the level-zero rels it uses.
1157          * The ones <= rtoffset belong to the upper query; the ones > rtoffset
1158          * do not.
1159          */
1160         clause_varnos = pull_varnos(whereClause);
1161         upper_varnos = NULL;
1162         while ((varno = bms_first_member(clause_varnos)) >= 0)
1163         {
1164                 if (varno <= rtoffset)
1165                         upper_varnos = bms_add_member(upper_varnos, varno);
1166         }
1167         bms_free(clause_varnos);
1168         Assert(!bms_is_empty(upper_varnos));
1169
1170         /*
1171          * Now that we've got the set of upper-level varnos, we can make the
1172          * last check: only available_rels can be referenced.
1173          */
1174         if (!bms_is_subset(upper_varnos, available_rels))
1175                 return NULL;
1176
1177         /* Now we can attach the modified subquery rtable to the parent */
1178         parse->rtable = list_concat(parse->rtable, subselect->rtable);
1179
1180         /*
1181          * And finally, build the JoinExpr node.
1182          */
1183         result = makeNode(JoinExpr);
1184         result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
1185         result->isNatural = false;
1186         result->larg = NULL;            /* caller must fill this in */
1187         /* flatten out the FromExpr node if it's useless */
1188         if (list_length(subselect->jointree->fromlist) == 1)
1189                 result->rarg = (Node *) linitial(subselect->jointree->fromlist);
1190         else
1191                 result->rarg = (Node *) subselect->jointree;
1192         result->using = NIL;
1193         result->quals = whereClause;
1194         result->alias = NULL;
1195         result->rtindex = 0;            /* we don't need an RTE for it */
1196
1197         return result;
1198 }
1199
1200 /*
1201  * simplify_EXISTS_query: remove any useless stuff in an EXISTS's subquery
1202  *
1203  * The only thing that matters about an EXISTS query is whether it returns
1204  * zero or more than zero rows.  Therefore, we can remove certain SQL features
1205  * that won't affect that.  The only part that is really likely to matter in
1206  * typical usage is simplifying the targetlist: it's a common habit to write
1207  * "SELECT * FROM" even though there is no need to evaluate any columns.
1208  *
1209  * Note: by suppressing the targetlist we could cause an observable behavioral
1210  * change, namely that any errors that might occur in evaluating the tlist
1211  * won't occur, nor will other side-effects of volatile functions.  This seems
1212  * unlikely to bother anyone in practice.
1213  *
1214  * Returns TRUE if was able to discard the targetlist, else FALSE.
1215  */
1216 static bool
1217 simplify_EXISTS_query(Query *query)
1218 {
1219         /*
1220          * We don't try to simplify at all if the query uses set operations,
1221          * aggregates, HAVING, LIMIT/OFFSET, or FOR UPDATE/SHARE; none of these
1222          * seem likely in normal usage and their possible effects are complex.
1223          */
1224         if (query->commandType != CMD_SELECT ||
1225                 query->intoClause ||
1226                 query->setOperations ||
1227                 query->hasAggs ||
1228                 query->hasWindowFuncs ||
1229                 query->havingQual ||
1230                 query->limitOffset ||
1231                 query->limitCount ||
1232                 query->rowMarks)
1233                 return false;
1234
1235         /*
1236          * Mustn't throw away the targetlist if it contains set-returning
1237          * functions; those could affect whether zero rows are returned!
1238          */
1239         if (expression_returns_set((Node *) query->targetList))
1240                 return false;
1241
1242         /*
1243          * Otherwise, we can throw away the targetlist, as well as any GROUP,
1244          * WINDOW, DISTINCT, and ORDER BY clauses; none of those clauses will
1245          * change a nonzero-rows result to zero rows or vice versa.  (Furthermore,
1246          * since our parsetree representation of these clauses depends on the
1247          * targetlist, we'd better throw them away if we drop the targetlist.)
1248          */
1249         query->targetList = NIL;
1250         query->groupClause = NIL;
1251         query->windowClause = NIL;
1252         query->distinctClause = NIL;
1253         query->sortClause = NIL;
1254         query->hasDistinctOn = false;
1255
1256         return true;
1257 }
1258
1259 /*
1260  * convert_EXISTS_to_ANY: try to convert EXISTS to a hashable ANY sublink
1261  *
1262  * The subselect is expected to be a fresh copy that we can munge up,
1263  * and to have been successfully passed through simplify_EXISTS_query.
1264  *
1265  * On success, the modified subselect is returned, and we store a suitable
1266  * upper-level test expression at *testexpr, plus a list of the subselect's
1267  * output Params at *paramIds.  (The test expression is already Param-ified
1268  * and hence need not go through convert_testexpr, which is why we have to
1269  * deal with the Param IDs specially.)
1270  *
1271  * On failure, returns NULL.
1272  */
1273 static Query *
1274 convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
1275                                           Node **testexpr, List **paramIds)
1276 {
1277         Node       *whereClause;
1278         List       *leftargs,
1279                            *rightargs,
1280                            *opids,
1281                            *newWhere,
1282                            *tlist,
1283                            *testlist,
1284                            *paramids;
1285         ListCell   *lc,
1286                            *rc,
1287                            *oc;
1288         AttrNumber      resno;
1289
1290         /*
1291          * Query must not require a targetlist, since we have to insert a new one.
1292          * Caller should have dealt with the case already.
1293          */
1294         Assert(subselect->targetList == NIL);
1295
1296         /*
1297          * Separate out the WHERE clause.  (We could theoretically also remove
1298          * top-level plain JOIN/ON clauses, but it's probably not worth the
1299          * trouble.)
1300          */
1301         whereClause = subselect->jointree->quals;
1302         subselect->jointree->quals = NULL;
1303
1304         /*
1305          * The rest of the sub-select must not refer to any Vars of the parent
1306          * query.  (Vars of higher levels should be okay, though.)
1307          *
1308          * Note: we need not check for Aggrefs separately because we know the
1309          * sub-select is as yet unoptimized; any uplevel Aggref must therefore
1310          * contain an uplevel Var reference.  This is not the case below ...
1311          */
1312         if (contain_vars_of_level((Node *) subselect, 1))
1313                 return NULL;
1314
1315         /*
1316          * We don't risk optimizing if the WHERE clause is volatile, either.
1317          */
1318         if (contain_volatile_functions(whereClause))
1319                 return NULL;
1320
1321         /*
1322          * Clean up the WHERE clause by doing const-simplification etc on it.
1323          * Aside from simplifying the processing we're about to do, this is
1324          * important for being able to pull chunks of the WHERE clause up into
1325          * the parent query.  Since we are invoked partway through the parent's
1326          * preprocess_expression() work, earlier steps of preprocess_expression()
1327          * wouldn't get applied to the pulled-up stuff unless we do them here.
1328          * For the parts of the WHERE clause that get put back into the child
1329          * query, this work is partially duplicative, but it shouldn't hurt.
1330          *
1331          * Note: we do not run flatten_join_alias_vars.  This is OK because
1332          * any parent aliases were flattened already, and we're not going to
1333          * pull any child Vars (of any description) into the parent.
1334          *
1335          * Note: passing the parent's root to eval_const_expressions is technically
1336          * wrong, but we can get away with it since only the boundParams (if any)
1337          * are used, and those would be the same in a subroot.
1338          */
1339         whereClause = eval_const_expressions(root, whereClause);
1340         whereClause = (Node *) canonicalize_qual((Expr *) whereClause);
1341         whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
1342
1343         /*
1344          * We now have a flattened implicit-AND list of clauses, which we
1345          * try to break apart into "outervar = innervar" hash clauses.
1346          * Anything that can't be broken apart just goes back into the
1347          * newWhere list.  Note that we aren't trying hard yet to ensure
1348          * that we have only outer or only inner on each side; we'll check
1349          * that if we get to the end.
1350          */
1351         leftargs = rightargs = opids = newWhere = NIL;
1352         foreach(lc, (List *) whereClause)
1353         {
1354                 OpExpr     *expr = (OpExpr *) lfirst(lc);
1355
1356                 if (IsA(expr, OpExpr) &&
1357                         hash_ok_operator(expr))
1358                 {
1359                         Node   *leftarg = (Node *) linitial(expr->args);
1360                         Node   *rightarg = (Node *) lsecond(expr->args);
1361
1362                         if (contain_vars_of_level(leftarg, 1))
1363                         {
1364                                 leftargs = lappend(leftargs, leftarg);
1365                                 rightargs = lappend(rightargs, rightarg);
1366                                 opids = lappend_oid(opids, expr->opno);
1367                                 continue;
1368                         }
1369                         if (contain_vars_of_level(rightarg, 1))
1370                         {
1371                                 /*
1372                                  * We must commute the clause to put the outer var on the
1373                                  * left, because the hashing code in nodeSubplan.c expects
1374                                  * that.  This probably shouldn't ever fail, since hashable
1375                                  * operators ought to have commutators, but be paranoid.
1376                                  */
1377                                 expr->opno = get_commutator(expr->opno);
1378                                 if (OidIsValid(expr->opno) && hash_ok_operator(expr))
1379                                 {
1380                                         leftargs = lappend(leftargs, rightarg);
1381                                         rightargs = lappend(rightargs, leftarg);
1382                                         opids = lappend_oid(opids, expr->opno);
1383                                         continue;
1384                                 }
1385                                 /* If no commutator, no chance to optimize the WHERE clause */
1386                                 return NULL;
1387                         }
1388                 }
1389                 /* Couldn't handle it as a hash clause */
1390                 newWhere = lappend(newWhere, expr);
1391         }
1392
1393         /*
1394          * If we didn't find anything we could convert, fail.
1395          */
1396         if (leftargs == NIL)
1397                 return NULL;
1398
1399         /*
1400          * There mustn't be any parent Vars or Aggs in the stuff that we intend to
1401          * put back into the child query.  Note: you might think we don't need to
1402          * check for Aggs separately, because an uplevel Agg must contain an
1403          * uplevel Var in its argument.  But it is possible that the uplevel Var
1404          * got optimized away by eval_const_expressions.  Consider
1405          *
1406          * SUM(CASE WHEN false THEN uplevelvar ELSE 0 END)
1407          */
1408         if (contain_vars_of_level((Node *) newWhere, 1) ||
1409                 contain_vars_of_level((Node *) rightargs, 1))
1410                 return NULL;
1411         if (root->parse->hasAggs &&
1412                 (contain_aggs_of_level((Node *) newWhere, 1) ||
1413                  contain_aggs_of_level((Node *) rightargs, 1)))
1414                 return NULL;
1415
1416         /*
1417          * And there can't be any child Vars in the stuff we intend to pull up.
1418          * (Note: we'd need to check for child Aggs too, except we know the
1419          * child has no aggs at all because of simplify_EXISTS_query's check.
1420          * The same goes for window functions.)
1421          */
1422         if (contain_vars_of_level((Node *) leftargs, 0))
1423                 return NULL;
1424
1425         /*
1426          * Also reject sublinks in the stuff we intend to pull up.  (It might be
1427          * possible to support this, but doesn't seem worth the complication.)
1428          */
1429         if (contain_subplans((Node *) leftargs))
1430                 return NULL;
1431
1432         /*
1433          * Okay, adjust the sublevelsup in the stuff we're pulling up.
1434          */
1435         IncrementVarSublevelsUp((Node *) leftargs, -1, 1);
1436
1437         /*
1438          * Put back any child-level-only WHERE clauses.
1439          */
1440         if (newWhere)
1441                 subselect->jointree->quals = (Node *) make_ands_explicit(newWhere);
1442
1443         /*
1444          * Build a new targetlist for the child that emits the expressions
1445          * we need.  Concurrently, build a testexpr for the parent using
1446          * Params to reference the child outputs.  (Since we generate Params
1447          * directly here, there will be no need to convert the testexpr in
1448          * build_subplan.)
1449          */
1450         tlist = testlist = paramids = NIL;
1451         resno = 1;
1452         /* there's no "for3" so we have to chase one of the lists manually */
1453         oc = list_head(opids);
1454         forboth(lc, leftargs, rc, rightargs)
1455         {
1456                 Node       *leftarg = (Node *) lfirst(lc);
1457                 Node       *rightarg = (Node *) lfirst(rc);
1458                 Oid                     opid = lfirst_oid(oc);
1459                 Param      *param;
1460
1461                 oc = lnext(oc);
1462                 param = generate_new_param(root,
1463                                                                    exprType(rightarg),
1464                                                                    exprTypmod(rightarg));
1465                 tlist = lappend(tlist,
1466                                                 makeTargetEntry((Expr *) rightarg,
1467                                                                                 resno++,
1468                                                                                 NULL,
1469                                                                                 false));
1470                 testlist = lappend(testlist,
1471                                                    make_opclause(opid, BOOLOID, false,
1472                                                                                  (Expr *) leftarg, (Expr *) param));
1473                 paramids = lappend_int(paramids, param->paramid);
1474         }
1475
1476         /* Put everything where it should go, and we're done */
1477         subselect->targetList = tlist;
1478         *testexpr = (Node *) make_ands_explicit(testlist);
1479         *paramIds = paramids;
1480
1481         return subselect;
1482 }
1483
1484
1485 /*
1486  * Replace correlation vars (uplevel vars) with Params.
1487  *
1488  * Uplevel aggregates are replaced, too.
1489  *
1490  * Note: it is critical that this runs immediately after SS_process_sublinks.
1491  * Since we do not recurse into the arguments of uplevel aggregates, they will
1492  * get copied to the appropriate subplan args list in the parent query with
1493  * uplevel vars not replaced by Params, but only adjusted in level (see
1494  * replace_outer_agg).  That's exactly what we want for the vars of the parent
1495  * level --- but if an aggregate's argument contains any further-up variables,
1496  * they have to be replaced with Params in their turn.  That will happen when
1497  * the parent level runs SS_replace_correlation_vars.  Therefore it must do
1498  * so after expanding its sublinks to subplans.  And we don't want any steps
1499  * in between, else those steps would never get applied to the aggregate
1500  * argument expressions, either in the parent or the child level.
1501  */
1502 Node *
1503 SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
1504 {
1505         /* No setup needed for tree walk, so away we go */
1506         return replace_correlation_vars_mutator(expr, root);
1507 }
1508
1509 static Node *
1510 replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
1511 {
1512         if (node == NULL)
1513                 return NULL;
1514         if (IsA(node, Var))
1515         {
1516                 if (((Var *) node)->varlevelsup > 0)
1517                         return (Node *) replace_outer_var(root, (Var *) node);
1518         }
1519         if (IsA(node, Aggref))
1520         {
1521                 if (((Aggref *) node)->agglevelsup > 0)
1522                         return (Node *) replace_outer_agg(root, (Aggref *) node);
1523         }
1524         return expression_tree_mutator(node,
1525                                                                    replace_correlation_vars_mutator,
1526                                                                    (void *) root);
1527 }
1528
1529 /*
1530  * Expand SubLinks to SubPlans in the given expression.
1531  *
1532  * The isQual argument tells whether or not this expression is a WHERE/HAVING
1533  * qualifier expression.  If it is, any sublinks appearing at top level need
1534  * not distinguish FALSE from UNKNOWN return values.
1535  */
1536 Node *
1537 SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
1538 {
1539         process_sublinks_context context;
1540
1541         context.root = root;
1542         context.isTopQual = isQual;
1543         return process_sublinks_mutator(expr, &context);
1544 }
1545
1546 static Node *
1547 process_sublinks_mutator(Node *node, process_sublinks_context *context)
1548 {
1549         process_sublinks_context locContext;
1550
1551         locContext.root = context->root;
1552
1553         if (node == NULL)
1554                 return NULL;
1555         if (IsA(node, SubLink))
1556         {
1557                 SubLink    *sublink = (SubLink *) node;
1558                 Node       *testexpr;
1559
1560                 /*
1561                  * First, recursively process the lefthand-side expressions, if any.
1562                  * They're not top-level anymore.
1563                  */
1564                 locContext.isTopQual = false;
1565                 testexpr = process_sublinks_mutator(sublink->testexpr, &locContext);
1566
1567                 /*
1568                  * Now build the SubPlan node and make the expr to return.
1569                  */
1570                 return make_subplan(context->root,
1571                                                         (Query *) sublink->subselect,
1572                                                         sublink->subLinkType,
1573                                                         testexpr,
1574                                                         context->isTopQual);
1575         }
1576
1577         /*
1578          * We should never see a SubPlan expression in the input (since this is
1579          * the very routine that creates 'em to begin with).  We shouldn't find
1580          * ourselves invoked directly on a Query, either.
1581          */
1582         Assert(!IsA(node, SubPlan));
1583         Assert(!IsA(node, AlternativeSubPlan));
1584         Assert(!IsA(node, Query));
1585
1586         /*
1587          * Because make_subplan() could return an AND or OR clause, we have to
1588          * take steps to preserve AND/OR flatness of a qual.  We assume the input
1589          * has been AND/OR flattened and so we need no recursion here.
1590          *
1591          * (Due to the coding here, we will not get called on the List subnodes of
1592          * an AND; and the input is *not* yet in implicit-AND format.  So no check
1593          * is needed for a bare List.)
1594          *
1595          * Anywhere within the top-level AND/OR clause structure, we can tell
1596          * make_subplan() that NULL and FALSE are interchangeable.  So isTopQual
1597          * propagates down in both cases.  (Note that this is unlike the meaning
1598          * of "top level qual" used in most other places in Postgres.)
1599          */
1600         if (and_clause(node))
1601         {
1602                 List       *newargs = NIL;
1603                 ListCell   *l;
1604
1605                 /* Still at qual top-level */
1606                 locContext.isTopQual = context->isTopQual;
1607
1608                 foreach(l, ((BoolExpr *) node)->args)
1609                 {
1610                         Node       *newarg;
1611
1612                         newarg = process_sublinks_mutator(lfirst(l), &locContext);
1613                         if (and_clause(newarg))
1614                                 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
1615                         else
1616                                 newargs = lappend(newargs, newarg);
1617                 }
1618                 return (Node *) make_andclause(newargs);
1619         }
1620
1621         if (or_clause(node))
1622         {
1623                 List       *newargs = NIL;
1624                 ListCell   *l;
1625
1626                 /* Still at qual top-level */
1627                 locContext.isTopQual = context->isTopQual;
1628
1629                 foreach(l, ((BoolExpr *) node)->args)
1630                 {
1631                         Node       *newarg;
1632
1633                         newarg = process_sublinks_mutator(lfirst(l), &locContext);
1634                         if (or_clause(newarg))
1635                                 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
1636                         else
1637                                 newargs = lappend(newargs, newarg);
1638                 }
1639                 return (Node *) make_orclause(newargs);
1640         }
1641
1642         /*
1643          * If we recurse down through anything other than an AND or OR node,
1644          * we are definitely not at top qual level anymore.
1645          */
1646         locContext.isTopQual = false;
1647
1648         return expression_tree_mutator(node,
1649                                                                    process_sublinks_mutator,
1650                                                                    (void *) &locContext);
1651 }
1652
1653 /*
1654  * SS_finalize_plan - do final sublink processing for a completed Plan.
1655  *
1656  * This recursively computes the extParam and allParam sets for every Plan
1657  * node in the given plan tree.  It also optionally attaches any previously
1658  * generated InitPlans to the top plan node.  (Any InitPlans should already
1659  * have been put through SS_finalize_plan.)
1660  */
1661 void
1662 SS_finalize_plan(PlannerInfo *root, Plan *plan, bool attach_initplans)
1663 {
1664         Bitmapset  *valid_params,
1665                            *initExtParam,
1666                            *initSetParam;
1667         Cost            initplan_cost;
1668         int                     paramid;
1669         ListCell   *l;
1670
1671         /*
1672          * Examine any initPlans to determine the set of external params they
1673          * reference, the set of output params they supply, and their total cost.
1674          * We'll use at least some of this info below.  (Note we are assuming that
1675          * finalize_plan doesn't touch the initPlans.)
1676          *
1677          * In the case where attach_initplans is false, we are assuming that the
1678          * existing initPlans are siblings that might supply params needed by the
1679          * current plan.
1680          */
1681         initExtParam = initSetParam = NULL;
1682         initplan_cost = 0;
1683         foreach(l, root->init_plans)
1684         {
1685                 SubPlan    *initsubplan = (SubPlan *) lfirst(l);
1686                 Plan       *initplan = planner_subplan_get_plan(root, initsubplan);
1687                 ListCell   *l2;
1688
1689                 initExtParam = bms_add_members(initExtParam, initplan->extParam);
1690                 foreach(l2, initsubplan->setParam)
1691                 {
1692                         initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
1693                 }
1694                 initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
1695         }
1696
1697         /*
1698          * Now determine the set of params that are validly referenceable in this
1699          * query level; to wit, those available from outer query levels plus the
1700          * output parameters of any initPlans.  (We do not include output
1701          * parameters of regular subplans.  Those should only appear within the
1702          * testexpr of SubPlan nodes, and are taken care of locally within
1703          * finalize_primnode.)
1704          *
1705          * Note: this is a bit overly generous since some parameters of upper
1706          * query levels might belong to query subtrees that don't include this
1707          * query.  However, valid_params is only a debugging crosscheck, so it
1708          * doesn't seem worth expending lots of cycles to try to be exact.
1709          */
1710         valid_params = bms_copy(initSetParam);
1711         paramid = 0;
1712         foreach(l, root->glob->paramlist)
1713         {
1714                 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
1715
1716                 if (pitem->abslevel < root->query_level)
1717                 {
1718                         /* valid outer-level parameter */
1719                         valid_params = bms_add_member(valid_params, paramid);
1720                 }
1721
1722                 paramid++;
1723         }
1724         /* Also include the recursion working table, if any */
1725         if (root->wt_param_id >= 0)
1726                 valid_params = bms_add_member(valid_params, root->wt_param_id);
1727
1728         /*
1729          * Now recurse through plan tree.
1730          */
1731         (void) finalize_plan(root, plan, valid_params);
1732
1733         bms_free(valid_params);
1734
1735         /*
1736          * Finally, attach any initPlans to the topmost plan node, and add their
1737          * extParams to the topmost node's, too.  However, any setParams of the
1738          * initPlans should not be present in the topmost node's extParams, only
1739          * in its allParams.  (As of PG 8.1, it's possible that some initPlans
1740          * have extParams that are setParams of other initPlans, so we have to
1741          * take care of this situation explicitly.)
1742          *
1743          * We also add the eval cost of each initPlan to the startup cost of the
1744          * top node.  This is a conservative overestimate, since in fact each
1745          * initPlan might be executed later than plan startup, or even not at all.
1746          */
1747         if (attach_initplans)
1748         {
1749                 plan->initPlan = root->init_plans;
1750                 root->init_plans = NIL;         /* make sure they're not attached twice */
1751
1752                 /* allParam must include all these params */
1753                 plan->allParam = bms_add_members(plan->allParam, initExtParam);
1754                 plan->allParam = bms_add_members(plan->allParam, initSetParam);
1755                 /* extParam must include any child extParam */
1756                 plan->extParam = bms_add_members(plan->extParam, initExtParam);
1757                 /* but extParam shouldn't include any setParams */
1758                 plan->extParam = bms_del_members(plan->extParam, initSetParam);
1759                 /* ensure extParam is exactly NULL if it's empty */
1760                 if (bms_is_empty(plan->extParam))
1761                         plan->extParam = NULL;
1762
1763                 plan->startup_cost += initplan_cost;
1764                 plan->total_cost += initplan_cost;
1765         }
1766 }
1767
1768 /*
1769  * Recursive processing of all nodes in the plan tree
1770  *
1771  * The return value is the computed allParam set for the given Plan node.
1772  * This is just an internal notational convenience.
1773  */
1774 static Bitmapset *
1775 finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params)
1776 {
1777         finalize_primnode_context context;
1778
1779         if (plan == NULL)
1780                 return NULL;
1781
1782         context.root = root;
1783         context.paramids = NULL;        /* initialize set to empty */
1784
1785         /*
1786          * When we call finalize_primnode, context.paramids sets are automatically
1787          * merged together.  But when recursing to self, we have to do it the hard
1788          * way.  We want the paramids set to include params in subplans as well as
1789          * at this level.
1790          */
1791
1792         /* Find params in targetlist and qual */
1793         finalize_primnode((Node *) plan->targetlist, &context);
1794         finalize_primnode((Node *) plan->qual, &context);
1795
1796         /* Check additional node-type-specific fields */
1797         switch (nodeTag(plan))
1798         {
1799                 case T_Result:
1800                         finalize_primnode(((Result *) plan)->resconstantqual,
1801                                                           &context);
1802                         break;
1803
1804                 case T_IndexScan:
1805                         finalize_primnode((Node *) ((IndexScan *) plan)->indexqual,
1806                                                           &context);
1807
1808                         /*
1809                          * we need not look at indexqualorig, since it will have the same
1810                          * param references as indexqual.
1811                          */
1812                         break;
1813
1814                 case T_BitmapIndexScan:
1815                         finalize_primnode((Node *) ((BitmapIndexScan *) plan)->indexqual,
1816                                                           &context);
1817
1818                         /*
1819                          * we need not look at indexqualorig, since it will have the same
1820                          * param references as indexqual.
1821                          */
1822                         break;
1823
1824                 case T_BitmapHeapScan:
1825                         finalize_primnode((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig,
1826                                                           &context);
1827                         break;
1828
1829                 case T_TidScan:
1830                         finalize_primnode((Node *) ((TidScan *) plan)->tidquals,
1831                                                           &context);
1832                         break;
1833
1834                 case T_SubqueryScan:
1835
1836                         /*
1837                          * In a SubqueryScan, SS_finalize_plan has already been run on the
1838                          * subplan by the inner invocation of subquery_planner, so there's
1839                          * no need to do it again.      Instead, just pull out the subplan's
1840                          * extParams list, which represents the params it needs from my
1841                          * level and higher levels.
1842                          */
1843                         context.paramids = bms_add_members(context.paramids,
1844                                                                  ((SubqueryScan *) plan)->subplan->extParam);
1845                         break;
1846
1847                 case T_FunctionScan:
1848                         finalize_primnode(((FunctionScan *) plan)->funcexpr,
1849                                                           &context);
1850                         break;
1851
1852                 case T_ValuesScan:
1853                         finalize_primnode((Node *) ((ValuesScan *) plan)->values_lists,
1854                                                           &context);
1855                         break;
1856
1857                 case T_CteScan:
1858                         context.paramids =
1859                                 bms_add_member(context.paramids,
1860                                                            ((CteScan *) plan)->cteParam);
1861                         break;
1862
1863                 case T_WorkTableScan:
1864                         context.paramids =
1865                                 bms_add_member(context.paramids,
1866                                                            ((WorkTableScan *) plan)->wtParam);
1867                         break;
1868
1869                 case T_Append:
1870                         {
1871                                 ListCell   *l;
1872
1873                                 foreach(l, ((Append *) plan)->appendplans)
1874                                 {
1875                                         context.paramids =
1876                                                 bms_add_members(context.paramids,
1877                                                                                 finalize_plan(root,
1878                                                                                                           (Plan *) lfirst(l),
1879                                                                                                           valid_params));
1880                                 }
1881                         }
1882                         break;
1883
1884                 case T_BitmapAnd:
1885                         {
1886                                 ListCell   *l;
1887
1888                                 foreach(l, ((BitmapAnd *) plan)->bitmapplans)
1889                                 {
1890                                         context.paramids =
1891                                                 bms_add_members(context.paramids,
1892                                                                                 finalize_plan(root,
1893                                                                                                           (Plan *) lfirst(l),
1894                                                                                                           valid_params));
1895                                 }
1896                         }
1897                         break;
1898
1899                 case T_BitmapOr:
1900                         {
1901                                 ListCell   *l;
1902
1903                                 foreach(l, ((BitmapOr *) plan)->bitmapplans)
1904                                 {
1905                                         context.paramids =
1906                                                 bms_add_members(context.paramids,
1907                                                                                 finalize_plan(root,
1908                                                                                                           (Plan *) lfirst(l),
1909                                                                                                           valid_params));
1910                                 }
1911                         }
1912                         break;
1913
1914                 case T_NestLoop:
1915                         finalize_primnode((Node *) ((Join *) plan)->joinqual,
1916                                                           &context);
1917                         break;
1918
1919                 case T_MergeJoin:
1920                         finalize_primnode((Node *) ((Join *) plan)->joinqual,
1921                                                           &context);
1922                         finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
1923                                                           &context);
1924                         break;
1925
1926                 case T_HashJoin:
1927                         finalize_primnode((Node *) ((Join *) plan)->joinqual,
1928                                                           &context);
1929                         finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
1930                                                           &context);
1931                         break;
1932
1933                 case T_Limit:
1934                         finalize_primnode(((Limit *) plan)->limitOffset,
1935                                                           &context);
1936                         finalize_primnode(((Limit *) plan)->limitCount,
1937                                                           &context);
1938                         break;
1939
1940                 case T_RecursiveUnion:
1941                 case T_Hash:
1942                 case T_Agg:
1943                 case T_WindowAgg:
1944                 case T_SeqScan:
1945                 case T_Material:
1946                 case T_Sort:
1947                 case T_Unique:
1948                 case T_SetOp:
1949                 case T_Group:
1950                         break;
1951
1952                 default:
1953                         elog(ERROR, "unrecognized node type: %d",
1954                                  (int) nodeTag(plan));
1955         }
1956
1957         /* Process left and right child plans, if any */
1958         context.paramids = bms_add_members(context.paramids,
1959                                                                            finalize_plan(root,
1960                                                                                                          plan->lefttree,
1961                                                                                                          valid_params));
1962
1963         context.paramids = bms_add_members(context.paramids,
1964                                                                            finalize_plan(root,
1965                                                                                                          plan->righttree,
1966                                                                                                          valid_params));
1967
1968         /*
1969          * RecursiveUnion *generates* its worktable param, so don't bubble that up
1970          */
1971         if (IsA(plan, RecursiveUnion))
1972         {
1973                 context.paramids = bms_del_member(context.paramids,
1974                                                                                   ((RecursiveUnion *) plan)->wtParam);
1975         }
1976
1977         /* Now we have all the paramids */
1978
1979         if (!bms_is_subset(context.paramids, valid_params))
1980                 elog(ERROR, "plan should not reference subplan's variable");
1981
1982         /*
1983          * Note: by definition, extParam and allParam should have the same value
1984          * in any plan node that doesn't have child initPlans.  We set them
1985          * equal here, and later SS_finalize_plan will update them properly
1986          * in node(s) that it attaches initPlans to.
1987          *
1988          * For speed at execution time, make sure extParam/allParam are actually
1989          * NULL if they are empty sets.
1990          */
1991         if (bms_is_empty(context.paramids))
1992         {
1993                 plan->extParam = NULL;
1994                 plan->allParam = NULL;
1995         }
1996         else
1997         {
1998                 plan->extParam = context.paramids;
1999                 plan->allParam = bms_copy(context.paramids);
2000         }
2001
2002         return plan->allParam;
2003 }
2004
2005 /*
2006  * finalize_primnode: add IDs of all PARAM_EXEC params appearing in the given
2007  * expression tree to the result set.
2008  */
2009 static bool
2010 finalize_primnode(Node *node, finalize_primnode_context *context)
2011 {
2012         if (node == NULL)
2013                 return false;
2014         if (IsA(node, Param))
2015         {
2016                 if (((Param *) node)->paramkind == PARAM_EXEC)
2017                 {
2018                         int                     paramid = ((Param *) node)->paramid;
2019
2020                         context->paramids = bms_add_member(context->paramids, paramid);
2021                 }
2022                 return false;                   /* no more to do here */
2023         }
2024         if (IsA(node, SubPlan))
2025         {
2026                 SubPlan    *subplan = (SubPlan *) node;
2027                 Plan       *plan = planner_subplan_get_plan(context->root, subplan);
2028                 ListCell   *lc;
2029                 Bitmapset  *subparamids;
2030
2031                 /* Recurse into the testexpr, but not into the Plan */
2032                 finalize_primnode(subplan->testexpr, context);
2033
2034                 /*
2035                  * Remove any param IDs of output parameters of the subplan that were
2036                  * referenced in the testexpr.  These are not interesting for
2037                  * parameter change signaling since we always re-evaluate the subplan.
2038                  * Note that this wouldn't work too well if there might be uses of the
2039                  * same param IDs elsewhere in the plan, but that can't happen because
2040                  * generate_new_param never tries to merge params.
2041                  */
2042                 foreach(lc, subplan->paramIds)
2043                 {
2044                         context->paramids = bms_del_member(context->paramids,
2045                                                                                            lfirst_int(lc));
2046                 }
2047
2048                 /* Also examine args list */
2049                 finalize_primnode((Node *) subplan->args, context);
2050
2051                 /*
2052                  * Add params needed by the subplan to paramids, but excluding those
2053                  * we will pass down to it.
2054                  */
2055                 subparamids = bms_copy(plan->extParam);
2056                 foreach(lc, subplan->parParam)
2057                 {
2058                         subparamids = bms_del_member(subparamids, lfirst_int(lc));
2059                 }
2060                 context->paramids = bms_join(context->paramids, subparamids);
2061
2062                 return false;                   /* no more to do here */
2063         }
2064         return expression_tree_walker(node, finalize_primnode,
2065                                                                   (void *) context);
2066 }
2067
2068 /*
2069  * SS_make_initplan_from_plan - given a plan tree, make it an InitPlan
2070  *
2071  * The plan is expected to return a scalar value of the indicated type.
2072  * We build an EXPR_SUBLINK SubPlan node and put it into the initplan
2073  * list for the current query level.  A Param that represents the initplan's
2074  * output is returned.
2075  *
2076  * We assume the plan hasn't been put through SS_finalize_plan.
2077  */
2078 Param *
2079 SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
2080                                                    Oid resulttype, int32 resulttypmod)
2081 {
2082         SubPlan    *node;
2083         Param      *prm;
2084
2085         /*
2086          * We must run SS_finalize_plan(), since that's normally done before a
2087          * subplan gets put into the initplan list.  Tell it not to attach any
2088          * pre-existing initplans to this one, since they are siblings not
2089          * children of this initplan.  (This is something else that could perhaps
2090          * be cleaner if we did extParam/allParam processing in setrefs.c instead
2091          * of here?  See notes for materialize_finished_plan.)
2092          */
2093
2094         /*
2095          * Build extParam/allParam sets for plan nodes.
2096          */
2097         SS_finalize_plan(root, plan, false);
2098
2099         /*
2100          * Add the subplan and its rtable to the global lists.
2101          */
2102         root->glob->subplans = lappend(root->glob->subplans,
2103                                                                    plan);
2104         root->glob->subrtables = lappend(root->glob->subrtables,
2105                                                                          root->parse->rtable);
2106
2107         /*
2108          * Create a SubPlan node and add it to the outer list of InitPlans.
2109          * Note it has to appear after any other InitPlans it might depend on
2110          * (see comments in ExecReScan).
2111          */
2112         node = makeNode(SubPlan);
2113         node->subLinkType = EXPR_SUBLINK;
2114         node->firstColType = get_first_col_type(plan);
2115         node->plan_id = list_length(root->glob->subplans);
2116
2117         root->init_plans = lappend(root->init_plans, node);
2118
2119         /*
2120          * The node can't have any inputs (since it's an initplan), so the
2121          * parParam and args lists remain empty.
2122          */
2123
2124         cost_subplan(root, node, plan);
2125
2126         /*
2127          * Make a Param that will be the subplan's output.
2128          */
2129         prm = generate_new_param(root, resulttype, resulttypmod);
2130         node->setParam = list_make1_int(prm->paramid);
2131
2132         return prm;
2133 }