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