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