Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * subselect.c
4 : : * Planning routines for subselects.
5 : : *
6 : : * This module deals with SubLinks and CTEs, but not subquery RTEs (i.e.,
7 : : * not sub-SELECT-in-FROM cases).
8 : : *
9 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
10 : : * Portions Copyright (c) 1994, Regents of the University of California
11 : : *
12 : : * IDENTIFICATION
13 : : * src/backend/optimizer/plan/subselect.c
14 : : *
15 : : *-------------------------------------------------------------------------
16 : : */
17 : : #include "postgres.h"
18 : :
19 : : #include "access/htup_details.h"
20 : : #include "catalog/pg_operator.h"
21 : : #include "catalog/pg_type.h"
22 : : #include "executor/executor.h"
23 : : #include "miscadmin.h"
24 : : #include "nodes/makefuncs.h"
25 : : #include "nodes/nodeFuncs.h"
26 : : #include "optimizer/clauses.h"
27 : : #include "optimizer/cost.h"
28 : : #include "optimizer/optimizer.h"
29 : : #include "optimizer/paramassign.h"
30 : : #include "optimizer/pathnode.h"
31 : : #include "optimizer/planmain.h"
32 : : #include "optimizer/planner.h"
33 : : #include "optimizer/prep.h"
34 : : #include "optimizer/subselect.h"
35 : : #include "parser/parse_relation.h"
36 : : #include "rewrite/rewriteManip.h"
37 : : #include "utils/builtins.h"
38 : : #include "utils/lsyscache.h"
39 : : #include "utils/syscache.h"
40 : :
41 : :
42 : : typedef struct convert_testexpr_context
43 : : {
44 : : PlannerInfo *root;
45 : : List *subst_nodes; /* Nodes to substitute for Params */
46 : : } convert_testexpr_context;
47 : :
48 : : typedef struct process_sublinks_context
49 : : {
50 : : PlannerInfo *root;
51 : : bool isTopQual;
52 : : } process_sublinks_context;
53 : :
54 : : typedef struct finalize_primnode_context
55 : : {
56 : : PlannerInfo *root;
57 : : Bitmapset *paramids; /* Non-local PARAM_EXEC paramids found */
58 : : } finalize_primnode_context;
59 : :
60 : : typedef struct inline_cte_walker_context
61 : : {
62 : : const char *ctename; /* name and relative level of target CTE */
63 : : int levelsup;
64 : : Query *ctequery; /* query to substitute */
65 : : } inline_cte_walker_context;
66 : :
67 : :
68 : : static Node *build_subplan(PlannerInfo *root, Plan *plan, Path *path,
69 : : PlannerInfo *subroot, List *plan_params,
70 : : SubLinkType subLinkType, int subLinkId,
71 : : Node *testexpr, List *testexpr_paramids,
72 : : bool unknownEqFalse);
73 : : static List *generate_subquery_params(PlannerInfo *root, List *tlist,
74 : : List **paramIds);
75 : : static List *generate_subquery_vars(PlannerInfo *root, List *tlist,
76 : : Index varno);
77 : : static Node *convert_testexpr(PlannerInfo *root,
78 : : Node *testexpr,
79 : : List *subst_nodes);
80 : : static Node *convert_testexpr_mutator(Node *node,
81 : : convert_testexpr_context *context);
82 : : static bool subplan_is_hashable(Plan *plan);
83 : : static bool subpath_is_hashable(Path *path);
84 : : static bool testexpr_is_hashable(Node *testexpr, List *param_ids);
85 : : static bool test_opexpr_is_hashable(OpExpr *testexpr, List *param_ids);
86 : : static bool hash_ok_operator(OpExpr *expr);
87 : : static bool contain_dml(Node *node);
88 : : static bool contain_dml_walker(Node *node, void *context);
89 : : static bool contain_outer_selfref(Node *node);
90 : : static bool contain_outer_selfref_walker(Node *node, Index *depth);
91 : : static void inline_cte(PlannerInfo *root, CommonTableExpr *cte);
92 : : static bool inline_cte_walker(Node *node, inline_cte_walker_context *context);
93 : : static bool simplify_EXISTS_query(PlannerInfo *root, Query *query);
94 : : static Query *convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
95 : : Node **testexpr, List **paramIds);
96 : : static Node *replace_correlation_vars_mutator(Node *node, PlannerInfo *root);
97 : : static Node *process_sublinks_mutator(Node *node,
98 : : process_sublinks_context *context);
99 : : static Bitmapset *finalize_plan(PlannerInfo *root,
100 : : Plan *plan,
101 : : int gather_param,
102 : : Bitmapset *valid_params,
103 : : Bitmapset *scan_params);
104 : : static bool finalize_primnode(Node *node, finalize_primnode_context *context);
105 : : static bool finalize_agg_primnode(Node *node, finalize_primnode_context *context);
106 : : static const char *sublinktype_to_string(SubLinkType subLinkType);
107 : :
108 : :
109 : : /*
110 : : * Get the datatype/typmod/collation of the first column of the plan's output.
111 : : *
112 : : * This information is stored for ARRAY_SUBLINK execution and for
113 : : * exprType()/exprTypmod()/exprCollation(), which have no way to get at the
114 : : * plan associated with a SubPlan node. We really only need the info for
115 : : * EXPR_SUBLINK and ARRAY_SUBLINK subplans, but for consistency we save it
116 : : * always.
117 : : */
118 : : static void
5302 tgl@sss.pgh.pa.us 119 :CBC 21993 : get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod,
120 : : Oid *colcollation)
121 : : {
122 : : /* In cases such as EXISTS, tlist might be empty; arbitrarily use VOID */
6283 123 [ + + ]: 21993 : if (plan->targetlist)
124 : : {
3122 125 : 20630 : TargetEntry *tent = linitial_node(TargetEntry, plan->targetlist);
126 : :
6283 127 [ + - ]: 20630 : if (!tent->resjunk)
128 : : {
6075 129 : 20630 : *coltype = exprType((Node *) tent->expr);
130 : 20630 : *coltypmod = exprTypmod((Node *) tent->expr);
5375 peter_e@gmx.net 131 : 20630 : *colcollation = exprCollation((Node *) tent->expr);
6075 tgl@sss.pgh.pa.us 132 : 20630 : return;
133 : : }
134 : : }
135 : 1363 : *coltype = VOIDOID;
136 : 1363 : *coltypmod = -1;
5375 peter_e@gmx.net 137 : 1363 : *colcollation = InvalidOid;
138 : : }
139 : :
140 : : /*
141 : : * Convert a SubLink (as created by the parser) into a SubPlan.
142 : : *
143 : : * We are given the SubLink's contained query, type, ID, and testexpr. We are
144 : : * also told if this expression appears at top level of a WHERE/HAVING qual.
145 : : *
146 : : * Note: we assume that the testexpr has been AND/OR flattened (actually,
147 : : * it's been through eval_const_expressions), but not converted to
148 : : * implicit-AND form; and any SubLinks in it should already have been
149 : : * converted to SubPlans. The subquery is as yet untouched, however.
150 : : *
151 : : * The result is whatever we need to substitute in place of the SubLink node
152 : : * in the executable expression. If we're going to do the subplan as a
153 : : * regular subplan, this will be the constructed SubPlan node. If we're going
154 : : * to do the subplan as an InitPlan, the SubPlan node instead goes into
155 : : * root->init_plans, and what we return here is an expression tree
156 : : * representing the InitPlan's result: usually just a Param node representing
157 : : * a single scalar result, but possibly a row comparison tree containing
158 : : * multiple Param nodes, or for a MULTIEXPR subquery a simple NULL constant
159 : : * (since the real output Params are elsewhere in the tree, and the MULTIEXPR
160 : : * subquery itself is in a resjunk tlist entry whose value is uninteresting).
161 : : */
162 : : static Node *
4149 tgl@sss.pgh.pa.us 163 : 19694 : make_subplan(PlannerInfo *root, Query *orig_subquery,
164 : : SubLinkType subLinkType, int subLinkId,
165 : : Node *testexpr, bool isTopQual)
166 : : {
167 : : Query *subquery;
6275 168 : 19694 : bool simple_exists = false;
169 : : double tuple_fraction;
170 : : PlannerInfo *subroot;
171 : : RelOptInfo *final_rel;
172 : : Path *best_path;
173 : : Plan *plan;
174 : : List *plan_params;
175 : : Node *result;
20 rhaas@postgresql.org 176 :GNC 19694 : const char *sublinkstr = sublinktype_to_string(subLinkType);
177 : :
178 : : /*
179 : : * Copy the source Query node. This is a quick and dirty kluge to resolve
180 : : * the fact that the parser can generate trees with multiple links to the
181 : : * same sub-Query node, but the planner wants to scribble on the Query.
182 : : * Try to clean this up when we do querytree redesign...
183 : : */
3154 peter_e@gmx.net 184 :CBC 19694 : subquery = copyObject(orig_subquery);
185 : :
186 : : /*
187 : : * If it's an EXISTS subplan, we might be able to simplify it.
188 : : */
6275 tgl@sss.pgh.pa.us 189 [ + + ]: 19694 : if (subLinkType == EXISTS_SUBLINK)
3992 190 : 1233 : simple_exists = simplify_EXISTS_query(root, subquery);
191 : :
192 : : /*
193 : : * For an EXISTS subplan, tell lower-level planner to expect that only the
194 : : * first tuple will be retrieved. For ALL and ANY subplans, we will be
195 : : * able to stop evaluating if the test condition fails or matches, so very
196 : : * often not all the tuples will be retrieved; for lack of a better idea,
197 : : * specify 50% retrieval. For EXPR, MULTIEXPR, and ROWCOMPARE subplans,
198 : : * use default behavior (we're only expecting one row out, anyway).
199 : : *
200 : : * NOTE: if you change these numbers, also change cost_subplan() in
201 : : * path/costsize.c.
202 : : *
203 : : * XXX If an ANY subplan is uncorrelated, build_subplan may decide to hash
204 : : * its output. In that case it would've been better to specify full
205 : : * retrieval. At present, however, we can only check hashability after
206 : : * we've made the subplan :-(. (Determining whether it'll fit in hash_mem
207 : : * is the really hard part.) Therefore, we don't want to be too
208 : : * optimistic about the percentage of tuples retrieved, for fear of
209 : : * selecting a plan that's bad for the materialization case.
210 : : */
6275 211 [ + + ]: 19694 : if (subLinkType == EXISTS_SUBLINK)
9386 212 : 1233 : tuple_fraction = 1.0; /* just like a LIMIT 1 */
6275 213 [ + + + + ]: 18461 : else if (subLinkType == ALL_SUBLINK ||
214 : : subLinkType == ANY_SUBLINK)
9386 215 : 282 : tuple_fraction = 0.5; /* 50% */
216 : : else
8267 217 : 18179 : tuple_fraction = 0.0; /* default behavior */
218 : :
219 : : /* plan_params should not be in use in current query level */
4800 220 [ - + ]: 19694 : Assert(root->plan_params == NIL);
221 : :
222 : : /* Generate Paths for the subquery */
20 rhaas@postgresql.org 223 :GNC 19694 : subroot = subquery_planner(root->glob, subquery,
224 : : choose_plan_name(root->glob, sublinkstr, true),
225 : : root, false, tuple_fraction, NULL);
226 : :
227 : : /* Isolate the params needed by this specific subplan */
4800 tgl@sss.pgh.pa.us 228 :CBC 19694 : plan_params = root->plan_params;
229 : 19694 : root->plan_params = NIL;
230 : :
231 : : /*
232 : : * Select best Path and turn it into a Plan. At least for now, there
233 : : * seems no reason to postpone doing that.
234 : : */
3521 235 : 19694 : final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
236 : 19694 : best_path = get_cheapest_fractional_path(final_rel, tuple_fraction);
237 : :
238 : 19694 : plan = create_plan(subroot, best_path);
239 : :
240 : : /* And convert to SubPlan or InitPlan format. */
580 241 : 19694 : result = build_subplan(root, plan, best_path,
242 : : subroot, plan_params,
243 : : subLinkType, subLinkId,
244 : : testexpr, NIL, isTopQual);
245 : :
246 : : /*
247 : : * If it's a correlated EXISTS with an unimportant targetlist, we might be
248 : : * able to transform it to the equivalent of an IN and then implement it
249 : : * by hashing. We don't have enough information yet to tell which way is
250 : : * likely to be better (it depends on the expected number of executions of
251 : : * the EXISTS qual, and we are much too early in planning the outer query
252 : : * to be able to guess that). So we generate both plans, if possible, and
253 : : * leave it to setrefs.c to decide which to use.
254 : : */
6275 255 [ + + + + ]: 19694 : if (simple_exists && IsA(result, SubPlan))
256 : : {
257 : : Node *newtestexpr;
258 : : List *paramIds;
259 : :
260 : : /* Make a second copy of the original subquery */
3154 peter_e@gmx.net 261 : 1093 : subquery = copyObject(orig_subquery);
262 : : /* and re-simplify */
3992 tgl@sss.pgh.pa.us 263 : 1093 : simple_exists = simplify_EXISTS_query(root, subquery);
6275 264 [ - + ]: 1093 : Assert(simple_exists);
265 : : /* See if it can be converted to an ANY query */
266 : 1093 : subquery = convert_EXISTS_to_ANY(root, subquery,
267 : : &newtestexpr, ¶mIds);
268 [ + + ]: 1093 : if (subquery)
269 : : {
270 : : char *plan_name;
271 : :
272 : : /* Generate Paths for the ANY subquery; we'll need all rows */
20 rhaas@postgresql.org 273 :GNC 852 : plan_name = choose_plan_name(root->glob, sublinkstr, true);
274 : 852 : subroot = subquery_planner(root->glob, subquery, plan_name,
275 : : root, false, 0.0, NULL);
276 : :
277 : : /* Isolate the params needed by this specific subplan */
4800 tgl@sss.pgh.pa.us 278 :CBC 852 : plan_params = root->plan_params;
279 : 852 : root->plan_params = NIL;
280 : :
281 : : /* Select best Path */
3521 282 : 852 : final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
283 : 852 : best_path = final_rel->cheapest_total_path;
284 : :
285 : : /* Now we can check if it'll fit in hash_mem */
1856 286 [ + + ]: 852 : if (subpath_is_hashable(best_path))
287 : : {
288 : : SubPlan *hashplan;
289 : : AlternativeSubPlan *asplan;
290 : :
291 : : /* OK, finish planning the ANY subquery */
292 : 849 : plan = create_plan(subroot, best_path);
293 : :
294 : : /* ... and convert to SubPlan format */
3170 peter_e@gmx.net 295 : 849 : hashplan = castNode(SubPlan,
296 : : build_subplan(root, plan, best_path,
297 : : subroot, plan_params,
298 : : ANY_SUBLINK, 0,
299 : : newtestexpr,
300 : : paramIds,
301 : : true));
302 : : /* Check we got what we expected */
6275 tgl@sss.pgh.pa.us 303 [ - + ]: 849 : Assert(hashplan->parParam == NIL);
304 [ - + ]: 849 : Assert(hashplan->useHashTable);
305 : :
306 : : /* Leave it to setrefs.c to decide which plan to use */
307 : 849 : asplan = makeNode(AlternativeSubPlan);
308 : 849 : asplan->subplans = list_make2(result, hashplan);
309 : 849 : result = (Node *) asplan;
1856 310 : 849 : root->hasAlternativeSubPlans = true;
311 : : }
312 : : }
313 : : }
314 : :
6275 315 : 19694 : return result;
316 : : }
317 : :
318 : : /*
319 : : * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
320 : : *
321 : : * Returns either the SubPlan, or a replacement expression if we decide to
322 : : * make it an InitPlan, as explained in the comments for make_subplan.
323 : : */
324 : : static Node *
580 325 : 20543 : build_subplan(PlannerInfo *root, Plan *plan, Path *path,
326 : : PlannerInfo *subroot, List *plan_params,
327 : : SubLinkType subLinkType, int subLinkId,
328 : : Node *testexpr, List *testexpr_paramids,
329 : : bool unknownEqFalse)
330 : : {
331 : : Node *result;
332 : : SubPlan *splan;
333 : : ListCell *lc;
334 : :
335 : : /*
336 : : * Initialize the SubPlan node.
337 : : *
338 : : * Note: plan_id and cost fields are set further down.
339 : : */
6817 340 : 20543 : splan = makeNode(SubPlan);
6275 341 : 20543 : splan->subLinkType = subLinkType;
20 rhaas@postgresql.org 342 :GNC 20543 : splan->plan_name = subroot->plan_name;
6817 tgl@sss.pgh.pa.us 343 :CBC 20543 : splan->testexpr = NULL;
344 : 20543 : splan->paramIds = NIL;
5302 345 : 20543 : get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
346 : : &splan->firstColCollation);
6817 347 : 20543 : splan->useHashTable = false;
6275 348 : 20543 : splan->unknownEqFalse = unknownEqFalse;
3120 349 : 20543 : splan->parallel_safe = plan->parallel_safe;
6817 350 : 20543 : splan->setParam = NIL;
351 : 20543 : splan->parParam = NIL;
352 : 20543 : splan->args = NIL;
353 : :
354 : : /*
355 : : * Make parParam and args lists of param IDs and expressions that current
356 : : * query level will pass to this child plan.
357 : : */
4800 358 [ + + + + : 42874 : foreach(lc, plan_params)
+ + ]
359 : : {
360 : 22331 : PlannerParamItem *pitem = (PlannerParamItem *) lfirst(lc);
361 : 22331 : Node *arg = pitem->item;
362 : :
363 : : /*
364 : : * The Var, PlaceHolderVar, Aggref, GroupingFunc, or ReturningExpr has
365 : : * already been adjusted to have the correct varlevelsup, phlevelsup,
366 : : * agglevelsup, or retlevelsup.
367 : : *
368 : : * If it's a PlaceHolderVar, Aggref, GroupingFunc, or ReturningExpr,
369 : : * its arguments might contain SubLinks, which have not yet been
370 : : * processed (see the comments for SS_replace_correlation_vars). Do
371 : : * that now.
372 : : */
373 [ + + ]: 22331 : if (IsA(arg, PlaceHolderVar) ||
1316 374 [ + + ]: 22325 : IsA(arg, Aggref) ||
284 dean.a.rasheed@gmail 375 [ + + ]: 22299 : IsA(arg, GroupingFunc) ||
376 [ + + ]: 22267 : IsA(arg, ReturningExpr))
4800 tgl@sss.pgh.pa.us 377 : 73 : arg = SS_process_sublinks(root, arg, false);
378 : :
379 : 22331 : splan->parParam = lappend_int(splan->parParam, pitem->paramId);
380 : 22331 : splan->args = lappend(splan->args, arg);
381 : : }
382 : :
383 : : /*
384 : : * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY,
385 : : * ROWCOMPARE, or MULTIEXPR types can be used as initPlans. For EXISTS,
386 : : * EXPR, or ARRAY, we return a Param referring to the result of evaluating
387 : : * the initPlan. For ROWCOMPARE, we must modify the testexpr tree to
388 : : * contain PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted
389 : : * by the parser, and then return that tree. For MULTIEXPR, we return a
390 : : * null constant: the resjunk targetlist item containing the SubLink does
391 : : * not need to return anything useful, since the referencing Params are
392 : : * elsewhere.
393 : : */
6275 394 [ + + + + ]: 20543 : if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
9478 395 : 124 : {
396 : : Param *prm;
397 : :
6275 398 [ - + ]: 124 : Assert(testexpr == NULL);
2481 399 : 124 : prm = generate_new_exec_param(root, BOOLOID, -1, InvalidOid);
6817 400 : 124 : splan->setParam = list_make1_int(prm->paramid);
20 rhaas@postgresql.org 401 :GNC 124 : splan->isInitPlan = true;
9478 tgl@sss.pgh.pa.us 402 :CBC 124 : result = (Node *) prm;
403 : : }
6275 404 [ + + + + ]: 20419 : else if (splan->parParam == NIL && subLinkType == EXPR_SUBLINK)
9478 405 : 5071 : {
7824 neilc@samurai.com 406 : 5071 : TargetEntry *te = linitial(plan->targetlist);
407 : : Param *prm;
408 : :
7509 tgl@sss.pgh.pa.us 409 [ - + ]: 5071 : Assert(!te->resjunk);
6275 410 [ - + ]: 5071 : Assert(testexpr == NULL);
2481 411 : 5071 : prm = generate_new_exec_param(root,
412 : 5071 : exprType((Node *) te->expr),
413 : 5071 : exprTypmod((Node *) te->expr),
414 : 5071 : exprCollation((Node *) te->expr));
6817 415 : 5071 : splan->setParam = list_make1_int(prm->paramid);
20 rhaas@postgresql.org 416 :GNC 5071 : splan->isInitPlan = true;
9478 tgl@sss.pgh.pa.us 417 :CBC 5071 : result = (Node *) prm;
418 : : }
6275 419 [ + + + + ]: 15348 : else if (splan->parParam == NIL && subLinkType == ARRAY_SUBLINK)
8238 420 : 61 : {
7824 neilc@samurai.com 421 : 61 : TargetEntry *te = linitial(plan->targetlist);
422 : : Oid arraytype;
423 : : Param *prm;
424 : :
7509 tgl@sss.pgh.pa.us 425 [ - + ]: 61 : Assert(!te->resjunk);
6275 426 [ - + ]: 61 : Assert(testexpr == NULL);
3989 427 : 61 : arraytype = get_promoted_array_type(exprType((Node *) te->expr));
8238 428 [ - + ]: 61 : if (!OidIsValid(arraytype))
8130 tgl@sss.pgh.pa.us 429 [ # # ]:UBC 0 : elog(ERROR, "could not find array type for datatype %s",
430 : : format_type_be(exprType((Node *) te->expr)));
2481 tgl@sss.pgh.pa.us 431 :CBC 61 : prm = generate_new_exec_param(root,
432 : : arraytype,
433 : 61 : exprTypmod((Node *) te->expr),
434 : 61 : exprCollation((Node *) te->expr));
6817 435 : 61 : splan->setParam = list_make1_int(prm->paramid);
20 rhaas@postgresql.org 436 :GNC 61 : splan->isInitPlan = true;
8238 tgl@sss.pgh.pa.us 437 :CBC 61 : result = (Node *) prm;
438 : : }
6275 439 [ + + + + ]: 15287 : else if (splan->parParam == NIL && subLinkType == ROWCOMPARE_SUBLINK)
10118 vadim4o@yahoo.com 440 : 9 : {
441 : : /* Adjust the Params */
442 : : List *params;
443 : :
6275 tgl@sss.pgh.pa.us 444 [ - + ]: 9 : Assert(testexpr != NULL);
6493 445 : 9 : params = generate_subquery_params(root,
446 : : plan->targetlist,
447 : : &splan->paramIds);
6825 448 : 9 : result = convert_testexpr(root,
449 : : testexpr,
450 : : params);
6817 451 : 9 : splan->setParam = list_copy(splan->paramIds);
20 rhaas@postgresql.org 452 :GNC 9 : splan->isInitPlan = true;
453 : :
454 : : /*
455 : : * The executable expression is returned to become part of the outer
456 : : * plan's expression tree; it is not kept in the initplan node.
457 : : */
458 : : }
4149 tgl@sss.pgh.pa.us 459 [ + + ]:CBC 15278 : else if (subLinkType == MULTIEXPR_SUBLINK)
460 : : {
461 : : /*
462 : : * Whether it's an initplan or not, it needs to set a PARAM_EXEC Param
463 : : * for each output column.
464 : : */
465 : : List *params;
466 : :
467 [ - + ]: 66 : Assert(testexpr == NULL);
468 : 66 : params = generate_subquery_params(root,
469 : : plan->targetlist,
470 : : &splan->setParam);
471 : :
472 : : /*
473 : : * Save the list of replacement Params in the n'th cell of
474 : : * root->multiexpr_params; setrefs.c will use it to replace
475 : : * PARAM_MULTIEXPR Params.
476 : : */
477 [ + + ]: 132 : while (list_length(root->multiexpr_params) < subLinkId)
478 : 66 : root->multiexpr_params = lappend(root->multiexpr_params, NIL);
479 : 66 : lc = list_nth_cell(root->multiexpr_params, subLinkId - 1);
480 [ - + ]: 66 : Assert(lfirst(lc) == NIL);
481 : 66 : lfirst(lc) = params;
482 : :
483 : : /* It can be an initplan if there are no parParams. */
484 [ + + ]: 66 : if (splan->parParam == NIL)
485 : : {
20 rhaas@postgresql.org 486 :GNC 15 : splan->isInitPlan = true;
4149 tgl@sss.pgh.pa.us 487 :CBC 15 : result = (Node *) makeNullConst(RECORDOID, -1, InvalidOid);
488 : : }
489 : : else
490 : : {
20 rhaas@postgresql.org 491 :GNC 51 : splan->isInitPlan = false;
4149 tgl@sss.pgh.pa.us 492 :CBC 51 : result = (Node *) splan;
493 : : }
494 : : }
495 : : else
496 : : {
497 : : /*
498 : : * Adjust the Params in the testexpr, unless caller already took care
499 : : * of it (as indicated by passing a list of Param IDs).
500 : : */
1900 501 [ + + + + ]: 15212 : if (testexpr && testexpr_paramids == NIL)
6318 502 : 288 : {
503 : : List *params;
504 : :
505 : 288 : params = generate_subquery_params(root,
506 : : plan->targetlist,
507 : : &splan->paramIds);
508 : 288 : splan->testexpr = convert_testexpr(root,
509 : : testexpr,
510 : : params);
511 : : }
512 : : else
513 : : {
6275 514 : 14924 : splan->testexpr = testexpr;
1900 515 : 14924 : splan->paramIds = testexpr_paramids;
516 : : }
517 : :
518 : : /*
519 : : * We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
520 : : * initPlans, even when they are uncorrelated or undirect correlated,
521 : : * because we need to scan the output of the subplan for each outer
522 : : * tuple. But if it's a not-direct-correlated IN (= ANY) test, we
523 : : * might be able to use a hashtable to avoid comparing all the tuples.
524 : : */
6275 525 [ + + ]: 15212 : if (subLinkType == ANY_SUBLINK &&
526 [ + + + - ]: 2192 : splan->parParam == NIL &&
527 [ + + ]: 2140 : subplan_is_hashable(plan) &&
1900 528 : 1070 : testexpr_is_hashable(splan->testexpr, splan->paramIds))
6817 529 : 1058 : splan->useHashTable = true;
530 : :
531 : : /*
532 : : * Otherwise, we have the option to tack a Material node onto the top
533 : : * of the subplan, to reduce the cost of reading it repeatedly. This
534 : : * is pointless for a direct-correlated subplan, since we'd have to
535 : : * recompute its results each time anyway. For uncorrelated/undirect
536 : : * correlated subplans, we add Material unless the subplan's top plan
537 : : * node would materialize its output anyway. Also, if enable_material
538 : : * is false, then the user does not want us to materialize anything
539 : : * unnecessarily, so we don't.
540 : : */
5670 rhaas@postgresql.org 541 [ + + + - ]: 14154 : else if (splan->parParam == NIL && enable_material &&
5889 tgl@sss.pgh.pa.us 542 [ + - ]: 21 : !ExecMaterializesOutput(nodeTag(plan)))
543 : 21 : plan = materialize_finished_plan(plan);
544 : :
6817 545 : 15212 : result = (Node *) splan;
20 rhaas@postgresql.org 546 :GNC 15212 : splan->isInitPlan = false;
547 : : }
548 : :
549 : : /*
550 : : * Add the subplan, its path, and its PlannerInfo to the global lists.
551 : : */
6275 tgl@sss.pgh.pa.us 552 :CBC 20543 : root->glob->subplans = lappend(root->glob->subplans, plan);
580 553 : 20543 : root->glob->subpaths = lappend(root->glob->subpaths, path);
5168 554 : 20543 : root->glob->subroots = lappend(root->glob->subroots, subroot);
6817 555 : 20543 : splan->plan_id = list_length(root->glob->subplans);
556 : :
20 rhaas@postgresql.org 557 [ + + ]:GNC 20543 : if (splan->isInitPlan)
6817 tgl@sss.pgh.pa.us 558 :CBC 5280 : root->init_plans = lappend(root->init_plans, splan);
559 : :
560 : : /*
561 : : * A parameterless subplan (not initplan) should be prepared to handle
562 : : * REWIND efficiently. If it has direct parameters then there's no point
563 : : * since it'll be reset on each scan anyway; and if it's an initplan then
564 : : * there's no point since it won't get re-run without parameter changes
565 : : * anyway. The input of a hashed subplan doesn't need REWIND either.
566 : : */
20 rhaas@postgresql.org 567 [ + + + + :GNC 20543 : if (splan->parParam == NIL && !splan->isInitPlan && !splan->useHashTable)
+ + ]
6817 tgl@sss.pgh.pa.us 568 :CBC 21 : root->glob->rewindPlanIDs = bms_add_member(root->glob->rewindPlanIDs,
569 : : splan->plan_id);
570 : :
571 : : /* Lastly, fill in the cost estimates for use later */
6275 572 : 20543 : cost_subplan(root, splan, plan);
573 : :
8371 574 : 20543 : return result;
575 : : }
576 : :
577 : : /*
578 : : * generate_subquery_params: build a list of Params representing the output
579 : : * columns of a sublink's sub-select, given the sub-select's targetlist.
580 : : *
581 : : * We also return an integer list of the paramids of the Params.
582 : : */
583 : : static List *
6493 584 : 363 : generate_subquery_params(PlannerInfo *root, List *tlist, List **paramIds)
585 : : {
586 : : List *result;
587 : : List *ids;
588 : : ListCell *lc;
589 : :
590 : 363 : result = ids = NIL;
591 [ + - + + : 850 : foreach(lc, tlist)
+ + ]
592 : : {
593 : 487 : TargetEntry *tent = (TargetEntry *) lfirst(lc);
594 : : Param *param;
595 : :
596 [ + + ]: 487 : if (tent->resjunk)
597 : 3 : continue;
598 : :
2481 599 : 484 : param = generate_new_exec_param(root,
600 : 484 : exprType((Node *) tent->expr),
601 : 484 : exprTypmod((Node *) tent->expr),
602 : 484 : exprCollation((Node *) tent->expr));
6493 603 : 484 : result = lappend(result, param);
604 : 484 : ids = lappend_int(ids, param->paramid);
605 : : }
606 : :
607 : 363 : *paramIds = ids;
608 : 363 : return result;
609 : : }
610 : :
611 : : /*
612 : : * generate_subquery_vars: build a list of Vars representing the output
613 : : * columns of a sublink's sub-select, given the sub-select's targetlist.
614 : : * The Vars have the specified varno (RTE index).
615 : : */
616 : : static List *
617 : 2238 : generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
618 : : {
619 : : List *result;
620 : : ListCell *lc;
621 : :
622 : 2238 : result = NIL;
623 [ + - + + : 4515 : foreach(lc, tlist)
+ + ]
624 : : {
625 : 2277 : TargetEntry *tent = (TargetEntry *) lfirst(lc);
626 : : Var *var;
627 : :
628 [ - + ]: 2277 : if (tent->resjunk)
6493 tgl@sss.pgh.pa.us 629 :UBC 0 : continue;
630 : :
5540 peter_e@gmx.net 631 :CBC 2277 : var = makeVarFromTargetEntry(varno, tent);
6493 tgl@sss.pgh.pa.us 632 : 2277 : result = lappend(result, var);
633 : : }
634 : :
635 : 2238 : return result;
636 : : }
637 : :
638 : : /*
639 : : * convert_testexpr: convert the testexpr given by the parser into
640 : : * actually executable form. This entails replacing PARAM_SUBLINK Params
641 : : * with Params or Vars representing the results of the sub-select. The
642 : : * nodes to be substituted are passed in as the List result from
643 : : * generate_subquery_params or generate_subquery_vars.
644 : : */
645 : : static Node *
6825 646 : 2673 : convert_testexpr(PlannerInfo *root,
647 : : Node *testexpr,
648 : : List *subst_nodes)
649 : : {
650 : : convert_testexpr_context context;
651 : :
652 : 2673 : context.root = root;
6493 653 : 2673 : context.subst_nodes = subst_nodes;
654 : 2673 : return convert_testexpr_mutator(testexpr, &context);
655 : : }
656 : :
657 : : static Node *
7243 658 : 12786 : convert_testexpr_mutator(Node *node,
659 : : convert_testexpr_context *context)
660 : : {
661 [ + + ]: 12786 : if (node == NULL)
662 : 47 : return NULL;
663 [ + + ]: 12739 : if (IsA(node, Param))
664 : : {
6963 bruce@momjian.us 665 : 2777 : Param *param = (Param *) node;
666 : :
7243 tgl@sss.pgh.pa.us 667 [ + + ]: 2777 : if (param->paramkind == PARAM_SUBLINK)
668 : : {
6493 669 [ + - - + ]: 5548 : if (param->paramid <= 0 ||
670 : 2774 : param->paramid > list_length(context->subst_nodes))
7243 tgl@sss.pgh.pa.us 671 [ # # ]:UBC 0 : elog(ERROR, "unexpected PARAM_SUBLINK ID: %d", param->paramid);
672 : :
673 : : /*
674 : : * We copy the list item to avoid having doubly-linked
675 : : * substructure in the modified parse tree. This is probably
676 : : * unnecessary when it's a Param, but be safe.
677 : : */
6493 tgl@sss.pgh.pa.us 678 :CBC 2774 : return (Node *) copyObject(list_nth(context->subst_nodes,
679 : : param->paramid - 1));
680 : : }
681 : : }
4339 682 [ + + ]: 9965 : if (IsA(node, SubLink))
683 : : {
684 : : /*
685 : : * If we come across a nested SubLink, it is neither necessary nor
686 : : * correct to recurse into it: any PARAM_SUBLINKs we might find inside
687 : : * belong to the inner SubLink not the outer. So just return it as-is.
688 : : *
689 : : * This reasoning depends on the assumption that nothing will pull
690 : : * subexpressions into or out of the testexpr field of a SubLink, at
691 : : * least not without replacing PARAM_SUBLINKs first. If we did want
692 : : * to do that we'd need to rethink the parser-output representation
693 : : * altogether, since currently PARAM_SUBLINKs are only unique per
694 : : * SubLink not globally across the query. The whole point of
695 : : * replacing them with Vars or PARAM_EXEC nodes is to make them
696 : : * globally unique before they escape from the SubLink's testexpr.
697 : : *
698 : : * Note: this can't happen when called during SS_process_sublinks,
699 : : * because that recursively processes inner SubLinks first. It can
700 : : * happen when called from convert_ANY_sublink_to_join, though.
701 : : */
702 : 6 : return node;
703 : : }
333 peter@eisentraut.org 704 : 9959 : return expression_tree_mutator(node, convert_testexpr_mutator, context);
705 : : }
706 : :
707 : : /*
708 : : * subplan_is_hashable: can we implement an ANY subplan by hashing?
709 : : *
710 : : * This is not responsible for checking whether the combining testexpr
711 : : * is suitable for hashing. We only look at the subquery itself.
712 : : */
713 : : static bool
6275 tgl@sss.pgh.pa.us 714 : 1070 : subplan_is_hashable(Plan *plan)
715 : : {
716 : : double subquery_size;
717 : :
718 : : /*
719 : : * The estimated size of the subquery result must fit in hash_mem. (Note:
720 : : * we use heap tuple overhead here even though the tuples will actually be
721 : : * stored as MinimalTuples; this provides some fudge factor for hashtable
722 : : * overhead.)
723 : : */
6822 724 : 1070 : subquery_size = plan->plan_rows *
3901 725 : 1070 : (MAXALIGN(plan->plan_width) + MAXALIGN(SizeofHeapTupleHeader));
1555 726 [ - + ]: 1070 : if (subquery_size > get_hash_memory_limit())
1856 tgl@sss.pgh.pa.us 727 :UBC 0 : return false;
728 : :
1856 tgl@sss.pgh.pa.us 729 :CBC 1070 : return true;
730 : : }
731 : :
732 : : /*
733 : : * subpath_is_hashable: can we implement an ANY subplan by hashing?
734 : : *
735 : : * Identical to subplan_is_hashable, but work from a Path for the subplan.
736 : : */
737 : : static bool
738 : 852 : subpath_is_hashable(Path *path)
739 : : {
740 : : double subquery_size;
741 : :
742 : : /*
743 : : * The estimated size of the subquery result must fit in hash_mem. (Note:
744 : : * we use heap tuple overhead here even though the tuples will actually be
745 : : * stored as MinimalTuples; this provides some fudge factor for hashtable
746 : : * overhead.)
747 : : */
748 : 852 : subquery_size = path->rows *
749 : 852 : (MAXALIGN(path->pathtarget->width) + MAXALIGN(SizeofHeapTupleHeader));
1555 750 [ + + ]: 852 : if (subquery_size > get_hash_memory_limit())
8326 tgl@sss.pgh.pa.us 751 :GBC 3 : return false;
752 : :
6275 tgl@sss.pgh.pa.us 753 :CBC 849 : return true;
754 : : }
755 : :
756 : : /*
757 : : * testexpr_is_hashable: is an ANY SubLink's test expression hashable?
758 : : *
759 : : * To identify LHS vs RHS of the hash expression, we must be given the
760 : : * list of output Param IDs of the SubLink's subquery.
761 : : */
762 : : static bool
1900 763 : 1070 : testexpr_is_hashable(Node *testexpr, List *param_ids)
764 : : {
765 : : /*
766 : : * The testexpr must be a single OpExpr, or an AND-clause containing only
767 : : * OpExprs, each of which satisfy test_opexpr_is_hashable().
768 : : */
6275 769 [ + - + + ]: 1070 : if (testexpr && IsA(testexpr, OpExpr))
770 : : {
1900 771 [ + + ]: 637 : if (test_opexpr_is_hashable((OpExpr *) testexpr, param_ids))
6275 772 : 625 : return true;
773 : : }
2463 774 [ + - ]: 433 : else if (is_andclause(testexpr))
775 : : {
776 : : ListCell *l;
777 : :
6275 778 [ + - + + : 1299 : foreach(l, ((BoolExpr *) testexpr)->args)
+ + ]
779 : : {
6963 bruce@momjian.us 780 : 866 : Node *andarg = (Node *) lfirst(l);
781 : :
7243 tgl@sss.pgh.pa.us 782 [ - + ]: 866 : if (!IsA(andarg, OpExpr))
6275 tgl@sss.pgh.pa.us 783 :UBC 0 : return false;
1900 tgl@sss.pgh.pa.us 784 [ - + ]:CBC 866 : if (!test_opexpr_is_hashable((OpExpr *) andarg, param_ids))
7243 tgl@sss.pgh.pa.us 785 :UBC 0 : return false;
786 : : }
6275 tgl@sss.pgh.pa.us 787 :CBC 433 : return true;
788 : : }
789 : :
790 : 12 : return false;
791 : : }
792 : :
793 : : static bool
1900 794 : 1503 : test_opexpr_is_hashable(OpExpr *testexpr, List *param_ids)
795 : : {
796 : : /*
797 : : * The combining operator must be hashable and strict. The need for
798 : : * hashability is obvious, since we want to use hashing. Without
799 : : * strictness, behavior in the presence of nulls is too unpredictable. We
800 : : * actually must assume even more than plain strictness: it can't yield
801 : : * NULL for non-null inputs, either (see nodeSubplan.c). However, hash
802 : : * indexes and hash joins assume that too.
803 : : */
804 [ + + ]: 1503 : if (!hash_ok_operator(testexpr))
805 : 6 : return false;
806 : :
807 : : /*
808 : : * The left and right inputs must belong to the outer and inner queries
809 : : * respectively; hence Params that will be supplied by the subquery must
810 : : * not appear in the LHS, and Vars of the outer query must not appear in
811 : : * the RHS. (Ordinarily, this must be true because of the way that the
812 : : * parser builds an ANY SubLink's testexpr ... but inlining of functions
813 : : * could have changed the expression's structure, so we have to check.
814 : : * Such cases do not occur often enough to be worth trying to optimize, so
815 : : * we don't worry about trying to commute the clause or anything like
816 : : * that; we just need to be sure not to build an invalid plan.)
817 : : */
818 [ - + ]: 1497 : if (list_length(testexpr->args) != 2)
1900 tgl@sss.pgh.pa.us 819 :UBC 0 : return false;
1900 tgl@sss.pgh.pa.us 820 [ + + ]:CBC 1497 : if (contain_exec_param((Node *) linitial(testexpr->args), param_ids))
821 : 6 : return false;
822 [ - + ]: 1491 : if (contain_var_clause((Node *) lsecond(testexpr->args)))
1900 tgl@sss.pgh.pa.us 823 :UBC 0 : return false;
1900 tgl@sss.pgh.pa.us 824 :CBC 1491 : return true;
825 : : }
826 : :
827 : : /*
828 : : * Check expression is hashable + strict
829 : : *
830 : : * We could use op_hashjoinable() and op_strict(), but do it like this to
831 : : * avoid a redundant cache lookup.
832 : : */
833 : : static bool
7243 834 : 4643 : hash_ok_operator(OpExpr *expr)
835 : : {
836 : 4643 : Oid opid = expr->opno;
837 : :
838 : : /* quick out if not a binary operator */
6275 839 [ - + ]: 4643 : if (list_length(expr->args) != 2)
6275 tgl@sss.pgh.pa.us 840 :UBC 0 : return false;
1380 tgl@sss.pgh.pa.us 841 [ + - + + ]:CBC 4643 : if (opid == ARRAY_EQ_OP ||
842 : : opid == RECORD_EQ_OP)
843 : : {
844 : : /* these are strict, but must check input type to ensure hashable */
5476 845 : 6 : Node *leftarg = linitial(expr->args);
846 : :
847 : 6 : return op_hashjoinable(opid, exprType(leftarg));
848 : : }
849 : : else
850 : : {
851 : : /* else must look up the operator properties */
852 : : HeapTuple tup;
853 : : Form_pg_operator optup;
854 : :
855 : 4637 : tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(opid));
856 [ - + ]: 4637 : if (!HeapTupleIsValid(tup))
5476 tgl@sss.pgh.pa.us 857 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for operator %u", opid);
5476 tgl@sss.pgh.pa.us 858 :CBC 4637 : optup = (Form_pg_operator) GETSTRUCT(tup);
859 [ + + - + ]: 4637 : if (!optup->oprcanhash || !func_strict(optup->oprcode))
860 : : {
861 : 429 : ReleaseSysCache(tup);
862 : 429 : return false;
863 : : }
8326 864 : 4208 : ReleaseSysCache(tup);
5476 865 : 4208 : return true;
866 : : }
867 : : }
868 : :
869 : :
870 : : /*
871 : : * SS_process_ctes: process a query's WITH list
872 : : *
873 : : * Consider each CTE in the WITH list and either ignore it (if it's an
874 : : * unreferenced SELECT), "inline" it to create a regular sub-SELECT-in-FROM,
875 : : * or convert it to an initplan.
876 : : *
877 : : * A side effect is to fill in root->cte_plan_ids with a list that
878 : : * parallels root->parse->cteList and provides the subplan ID for
879 : : * each CTE's initplan, or a dummy ID (-1) if we didn't make an initplan.
880 : : */
881 : : void
6232 882 : 1444 : SS_process_ctes(PlannerInfo *root)
883 : : {
884 : : ListCell *lc;
885 : :
886 [ - + ]: 1444 : Assert(root->cte_plan_ids == NIL);
887 : :
888 [ + - + + : 3476 : foreach(lc, root->parse->cteList)
+ + ]
889 : : {
890 : 2035 : CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
5358 891 : 2035 : CmdType cmdType = ((Query *) cte->ctequery)->commandType;
892 : : Query *subquery;
893 : : PlannerInfo *subroot;
894 : : RelOptInfo *final_rel;
895 : : Path *best_path;
896 : : Plan *plan;
897 : : SubPlan *splan;
898 : : int paramid;
899 : :
900 : : /*
901 : : * Ignore SELECT CTEs that are not actually referenced anywhere.
902 : : */
903 [ + + + + ]: 2035 : if (cte->cterefcount == 0 && cmdType == CMD_SELECT)
904 : : {
905 : : /* Make a dummy entry in cte_plan_ids */
6232 906 : 26 : root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
907 : 782 : continue;
908 : : }
909 : :
910 : : /*
911 : : * Consider inlining the CTE (creating RTE_SUBQUERY RTE(s)) instead of
912 : : * implementing it as a separately-planned CTE.
913 : : *
914 : : * We cannot inline if any of these conditions hold:
915 : : *
916 : : * 1. The user said not to (the CTEMaterializeAlways option).
917 : : *
918 : : * 2. The CTE is recursive.
919 : : *
920 : : * 3. The CTE has side-effects; this includes either not being a plain
921 : : * SELECT, or containing volatile functions. Inlining might change
922 : : * the side-effects, which would be bad.
923 : : *
924 : : * 4. The CTE is multiply-referenced and contains a self-reference to
925 : : * a recursive CTE outside itself. Inlining would result in multiple
926 : : * recursive self-references, which we don't support.
927 : : *
928 : : * Otherwise, we have an option whether to inline or not. That should
929 : : * always be a win if there's just a single reference, but if the CTE
930 : : * is multiply-referenced then it's unclear: inlining adds duplicate
931 : : * computations, but the ability to absorb restrictions from the outer
932 : : * query level could outweigh that. We do not have nearly enough
933 : : * information at this point to tell whether that's true, so we let
934 : : * the user express a preference. Our default behavior is to inline
935 : : * only singly-referenced CTEs, but a CTE marked CTEMaterializeNever
936 : : * will be inlined even if multiply referenced.
937 : : *
938 : : * Note: we check for volatile functions last, because that's more
939 : : * expensive than the other tests needed.
940 : : */
2445 941 [ + + ]: 2009 : if ((cte->ctematerialized == CTEMaterializeNever ||
942 [ + + ]: 1985 : (cte->ctematerialized == CTEMaterializeDefault &&
943 [ + + ]: 1884 : cte->cterefcount == 1)) &&
944 [ + + + + ]: 1374 : !cte->cterecursive &&
945 : 802 : cmdType == CMD_SELECT &&
946 [ + + ]: 802 : !contain_dml(cte->ctequery) &&
2393 947 [ + + ]: 798 : (cte->cterefcount <= 1 ||
948 [ + + ]: 18 : !contain_outer_selfref(cte->ctequery)) &&
2445 949 [ + + ]: 792 : !contain_volatile_functions(cte->ctequery))
950 : : {
951 : 756 : inline_cte(root, cte);
952 : : /* Make a dummy entry in cte_plan_ids */
953 : 756 : root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
954 : 756 : continue;
955 : : }
956 : :
957 : : /*
958 : : * Copy the source Query node. Probably not necessary, but let's keep
959 : : * this similar to make_subplan.
960 : : */
6232 961 : 1253 : subquery = (Query *) copyObject(cte->ctequery);
962 : :
963 : : /* plan_params should not be in use in current query level */
4800 964 [ - + ]: 1253 : Assert(root->plan_params == NIL);
965 : :
966 : : /*
967 : : * Generate Paths for the CTE query. Always plan for full retrieval
968 : : * --- we don't have enough info to predict otherwise.
969 : : */
20 rhaas@postgresql.org 970 :GNC 1253 : subroot = subquery_planner(root->glob, subquery,
971 : 1253 : choose_plan_name(root->glob, cte->ctename, false),
972 : 1253 : root, cte->cterecursive, 0.0, NULL);
973 : :
974 : : /*
975 : : * Since the current query level doesn't yet contain any RTEs, it
976 : : * should not be possible for the CTE to have requested parameters of
977 : : * this level.
978 : : */
4800 tgl@sss.pgh.pa.us 979 [ - + ]:CBC 1250 : if (root->plan_params)
4800 tgl@sss.pgh.pa.us 980 [ # # ]:UBC 0 : elog(ERROR, "unexpected outer reference in CTE query");
981 : :
982 : : /*
983 : : * Select best Path and turn it into a Plan. At least for now, there
984 : : * seems no reason to postpone doing that.
985 : : */
3521 tgl@sss.pgh.pa.us 986 :CBC 1250 : final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
987 : 1250 : best_path = final_rel->cheapest_total_path;
988 : :
989 : 1250 : plan = create_plan(subroot, best_path);
990 : :
991 : : /*
992 : : * Make a SubPlan node for it. This is just enough unlike
993 : : * build_subplan that we can't share code.
994 : : *
995 : : * Note: plan_id and cost fields are set further down.
996 : : */
6232 997 : 1250 : splan = makeNode(SubPlan);
998 : 1250 : splan->subLinkType = CTE_SUBLINK;
20 rhaas@postgresql.org 999 :GNC 1250 : splan->plan_name = subroot->plan_name;
6232 tgl@sss.pgh.pa.us 1000 :CBC 1250 : splan->testexpr = NULL;
1001 : 1250 : splan->paramIds = NIL;
5302 1002 : 1250 : get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
1003 : : &splan->firstColCollation);
6232 1004 : 1250 : splan->useHashTable = false;
1005 : 1250 : splan->unknownEqFalse = false;
1006 : :
1007 : : /*
1008 : : * CTE scans are not considered for parallelism (cf
1009 : : * set_rel_consider_parallel).
1010 : : */
3177 rhaas@postgresql.org 1011 : 1250 : splan->parallel_safe = false;
6232 tgl@sss.pgh.pa.us 1012 : 1250 : splan->setParam = NIL;
1013 : 1250 : splan->parParam = NIL;
1014 : 1250 : splan->args = NIL;
1015 : :
1016 : : /*
1017 : : * The node can't have any inputs (since it's an initplan), so the
1018 : : * parParam and args lists remain empty. (It could contain references
1019 : : * to earlier CTEs' output param IDs, but CTE outputs are not
1020 : : * propagated via the args list.)
1021 : : */
1022 : :
1023 : : /*
1024 : : * Assign a param ID to represent the CTE's output. No ordinary
1025 : : * "evaluation" of this param slot ever happens, but we use the param
1026 : : * ID for setParam/chgParam signaling just as if the CTE plan were
1027 : : * returning a simple scalar output. (Also, the executor abuses the
1028 : : * ParamExecData slot for this param ID for communication among
1029 : : * multiple CteScan nodes that might be scanning this CTE.)
1030 : : */
2481 1031 : 1250 : paramid = assign_special_exec_param(root);
4800 1032 : 1250 : splan->setParam = list_make1_int(paramid);
1033 : :
1034 : : /*
1035 : : * Add the subplan, its path, and its PlannerInfo to the global lists.
1036 : : */
6232 1037 : 1250 : root->glob->subplans = lappend(root->glob->subplans, plan);
580 1038 : 1250 : root->glob->subpaths = lappend(root->glob->subpaths, best_path);
5168 1039 : 1250 : root->glob->subroots = lappend(root->glob->subroots, subroot);
6232 1040 : 1250 : splan->plan_id = list_length(root->glob->subplans);
1041 : :
1042 : 1250 : root->init_plans = lappend(root->init_plans, splan);
1043 : :
1044 : 1250 : root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
1045 : :
1046 : : /* Lastly, fill in the cost estimates for use later */
1047 : 1250 : cost_subplan(root, splan, plan);
1048 : : }
1049 : 1441 : }
1050 : :
1051 : : /*
1052 : : * contain_dml: is any subquery not a plain SELECT?
1053 : : *
1054 : : * We reject SELECT FOR UPDATE/SHARE as well as INSERT etc.
1055 : : */
1056 : : static bool
2445 1057 : 802 : contain_dml(Node *node)
1058 : : {
1059 : 802 : return contain_dml_walker(node, NULL);
1060 : : }
1061 : :
1062 : : static bool
1063 : 53706 : contain_dml_walker(Node *node, void *context)
1064 : : {
1065 [ + + ]: 53706 : if (node == NULL)
1066 : 18722 : return false;
1067 [ + + ]: 34984 : if (IsA(node, Query))
1068 : : {
1069 : 1476 : Query *query = (Query *) node;
1070 : :
1071 [ + - ]: 1476 : if (query->commandType != CMD_SELECT ||
1072 [ + + ]: 1476 : query->rowMarks != NIL)
1073 : 4 : return true;
1074 : :
1075 : 1472 : return query_tree_walker(query, contain_dml_walker, context, 0);
1076 : : }
1077 : 33508 : return expression_tree_walker(node, contain_dml_walker, context);
1078 : : }
1079 : :
1080 : : /*
1081 : : * contain_outer_selfref: is there an external recursive self-reference?
1082 : : */
1083 : : static bool
2393 1084 : 18 : contain_outer_selfref(Node *node)
1085 : : {
1086 : 18 : Index depth = 0;
1087 : :
1088 : : /*
1089 : : * We should be starting with a Query, so that depth will be 1 while
1090 : : * examining its immediate contents.
1091 : : */
1092 [ - + ]: 18 : Assert(IsA(node, Query));
1093 : :
1094 : 18 : return contain_outer_selfref_walker(node, &depth);
1095 : : }
1096 : :
1097 : : static bool
1098 : 405 : contain_outer_selfref_walker(Node *node, Index *depth)
1099 : : {
1100 [ + + ]: 405 : if (node == NULL)
1101 : 243 : return false;
1102 [ + + ]: 162 : if (IsA(node, RangeTblEntry))
1103 : : {
1104 : 15 : RangeTblEntry *rte = (RangeTblEntry *) node;
1105 : :
1106 : : /*
1107 : : * Check for a self-reference to a CTE that's above the Query that our
1108 : : * search started at.
1109 : : */
1110 [ + + ]: 15 : if (rte->rtekind == RTE_CTE &&
1111 [ + - ]: 6 : rte->self_reference &&
1112 [ + - ]: 6 : rte->ctelevelsup >= *depth)
1113 : 6 : return true;
1114 : 9 : return false; /* allow range_table_walker to continue */
1115 : : }
1116 [ + + ]: 147 : if (IsA(node, Query))
1117 : : {
1118 : : /* Recurse into subquery, tracking nesting depth properly */
1119 : 21 : Query *query = (Query *) node;
1120 : : bool result;
1121 : :
1122 : 21 : (*depth)++;
1123 : :
1124 : 21 : result = query_tree_walker(query, contain_outer_selfref_walker,
1125 : : depth, QTW_EXAMINE_RTES_BEFORE);
1126 : :
1127 : 21 : (*depth)--;
1128 : :
1129 : 21 : return result;
1130 : : }
333 peter@eisentraut.org 1131 : 126 : return expression_tree_walker(node, contain_outer_selfref_walker, depth);
1132 : : }
1133 : :
1134 : : /*
1135 : : * inline_cte: convert RTE_CTE references to given CTE into RTE_SUBQUERYs
1136 : : */
1137 : : static void
2445 tgl@sss.pgh.pa.us 1138 : 756 : inline_cte(PlannerInfo *root, CommonTableExpr *cte)
1139 : : {
1140 : : struct inline_cte_walker_context context;
1141 : :
1142 : 756 : context.ctename = cte->ctename;
1143 : : /* Start at levelsup = -1 because we'll immediately increment it */
1144 : 756 : context.levelsup = -1;
1145 : 756 : context.ctequery = castNode(Query, cte->ctequery);
1146 : :
1147 : 756 : (void) inline_cte_walker((Node *) root->parse, &context);
1148 : 756 : }
1149 : :
1150 : : static bool
1151 : 276615 : inline_cte_walker(Node *node, inline_cte_walker_context *context)
1152 : : {
1153 [ + + ]: 276615 : if (node == NULL)
1154 : 74630 : return false;
1155 [ + + ]: 201985 : if (IsA(node, Query))
1156 : : {
1157 : 5468 : Query *query = (Query *) node;
1158 : :
1159 : 5468 : context->levelsup++;
1160 : :
1161 : : /*
1162 : : * Visit the query's RTE nodes after their contents; otherwise
1163 : : * query_tree_walker would descend into the newly inlined CTE query,
1164 : : * which we don't want.
1165 : : */
1166 : 5468 : (void) query_tree_walker(query, inline_cte_walker, context,
1167 : : QTW_EXAMINE_RTES_AFTER);
1168 : :
1169 : 5468 : context->levelsup--;
1170 : :
1171 : 5468 : return false;
1172 : : }
1173 [ + + ]: 196517 : else if (IsA(node, RangeTblEntry))
1174 : : {
1175 : 10255 : RangeTblEntry *rte = (RangeTblEntry *) node;
1176 : :
1177 [ + + ]: 10255 : if (rte->rtekind == RTE_CTE &&
1178 [ + + ]: 3017 : strcmp(rte->ctename, context->ctename) == 0 &&
1179 [ + + ]: 771 : rte->ctelevelsup == context->levelsup)
1180 : : {
1181 : : /*
1182 : : * Found a reference to replace. Generate a copy of the CTE query
1183 : : * with appropriate level adjustment for outer references (e.g.,
1184 : : * to other CTEs).
1185 : : */
1186 : 768 : Query *newquery = copyObject(context->ctequery);
1187 : :
1188 [ + + ]: 768 : if (context->levelsup > 0)
1189 : 487 : IncrementVarSublevelsUp((Node *) newquery, context->levelsup, 1);
1190 : :
1191 : : /*
1192 : : * Convert the RTE_CTE RTE into a RTE_SUBQUERY.
1193 : : *
1194 : : * Historically, a FOR UPDATE clause has been treated as extending
1195 : : * into views and subqueries, but not into CTEs. We preserve this
1196 : : * distinction by not trying to push rowmarks into the new
1197 : : * subquery.
1198 : : */
1199 : 768 : rte->rtekind = RTE_SUBQUERY;
1200 : 768 : rte->subquery = newquery;
1201 : 768 : rte->security_barrier = false;
1202 : :
1203 : : /* Zero out CTE-specific fields */
1204 : 768 : rte->ctename = NULL;
1205 : 768 : rte->ctelevelsup = 0;
1206 : 768 : rte->self_reference = false;
1207 : 768 : rte->coltypes = NIL;
1208 : 768 : rte->coltypmods = NIL;
1209 : 768 : rte->colcollations = NIL;
1210 : : }
1211 : :
1212 : 10255 : return false;
1213 : : }
1214 : :
1215 : 186262 : return expression_tree_walker(node, inline_cte_walker, context);
1216 : : }
1217 : :
1218 : : /*
1219 : : * Attempt to transform 'testexpr' over the VALUES subquery into
1220 : : * a ScalarArrayOpExpr. We currently support the transformation only when
1221 : : * it ends up with a constant array. Otherwise, the evaluation of non-hashed
1222 : : * SAOP might be slower than the corresponding Hash Join with VALUES.
1223 : : *
1224 : : * Return transformed ScalarArrayOpExpr or NULL if transformation isn't
1225 : : * allowed.
1226 : : */
1227 : : ScalarArrayOpExpr *
206 akorotkov@postgresql 1228 : 2333 : convert_VALUES_to_ANY(PlannerInfo *root, Node *testexpr, Query *values)
1229 : : {
1230 : : RangeTblEntry *rte;
1231 : : Node *leftop;
1232 : : Node *rightop;
1233 : : Oid opno;
1234 : : ListCell *lc;
1235 : : Oid inputcollid;
1236 : 2333 : List *exprs = NIL;
1237 : :
1238 : : /*
1239 : : * Check we have a binary operator over a single-column subquery with no
1240 : : * joins and no LIMIT/OFFSET/ORDER BY clauses.
1241 : : */
1242 [ + + + - ]: 4613 : if (!IsA(testexpr, OpExpr) ||
1243 [ + - ]: 4560 : list_length(((OpExpr *) testexpr)->args) != 2 ||
1244 : 2280 : list_length(values->targetList) > 1 ||
1245 [ + + ]: 2280 : values->limitCount != NULL ||
1246 [ + + ]: 2274 : values->limitOffset != NULL ||
1247 [ + + + + ]: 4521 : values->sortClause != NIL ||
1248 : 2259 : list_length(values->rtable) != 1)
1249 : 1877 : return NULL;
1250 : :
1251 : 456 : rte = linitial_node(RangeTblEntry, values->rtable);
1252 : 456 : leftop = linitial(((OpExpr *) testexpr)->args);
1253 : 456 : rightop = lsecond(((OpExpr *) testexpr)->args);
1254 : 456 : opno = ((OpExpr *) testexpr)->opno;
1255 : 456 : inputcollid = ((OpExpr *) testexpr)->inputcollid;
1256 : :
1257 : : /*
1258 : : * Also, check that only RTE corresponds to VALUES; the list of values has
1259 : : * at least two items and no volatile functions.
1260 : : */
1261 [ + + + + ]: 522 : if (rte->rtekind != RTE_VALUES ||
1262 [ - + ]: 126 : list_length(rte->values_lists) < 2 ||
1263 : 60 : contain_volatile_functions((Node *) rte->values_lists))
1264 : 396 : return NULL;
1265 : :
1266 [ + - + + : 180 : foreach(lc, rte->values_lists)
+ + ]
1267 : : {
1268 : 138 : List *elem = lfirst(lc);
1269 : 138 : Node *value = linitial(elem);
1270 : :
1271 : : /*
1272 : : * Prepare an evaluation of the right side of the operator with
1273 : : * substitution of the given value.
1274 : : */
1275 : 138 : value = convert_testexpr(root, rightop, list_make1(value));
1276 : :
1277 : : /*
1278 : : * Try to evaluate constant expressions. We could get Const as a
1279 : : * result.
1280 : : */
1281 : 138 : value = eval_const_expressions(root, value);
1282 : :
1283 : : /*
1284 : : * As we only support constant output arrays, all the items must also
1285 : : * be constant.
1286 : : */
1287 [ + + ]: 138 : if (!IsA(value, Const))
1288 : 18 : return NULL;
1289 : :
1290 : 120 : exprs = lappend(exprs, value);
1291 : : }
1292 : :
1293 : : /* Finally, build ScalarArrayOpExpr at the top of the 'exprs' list. */
1294 : 42 : return make_SAOP_expr(opno, leftop, exprType(rightop),
1295 : 42 : linitial_oid(rte->colcollations), inputcollid,
1296 : : exprs, false);
1297 : : }
1298 : :
1299 : : /*
1300 : : * convert_ANY_sublink_to_join: try to convert an ANY SubLink to a join
1301 : : *
1302 : : * The caller has found an ANY SubLink at the top level of one of the query's
1303 : : * qual clauses, but has not checked the properties of the SubLink further.
1304 : : * Decide whether it is appropriate to process this SubLink in join style.
1305 : : * If so, form a JoinExpr and return it. Return NULL if the SubLink cannot
1306 : : * be converted to a join.
1307 : : *
1308 : : * The only non-obvious input parameter is available_rels: this is the set
1309 : : * of query rels that can safely be referenced in the sublink expression.
1310 : : * (We must restrict this to avoid changing the semantics when a sublink
1311 : : * is present in an outer join's ON qual.) The conversion must fail if
1312 : : * the converted qual would reference any but these parent-query relids.
1313 : : *
1314 : : * On success, the returned JoinExpr has larg = NULL and rarg = the jointree
1315 : : * item representing the pulled-up subquery. The caller must set larg to
1316 : : * represent the relation(s) on the lefthand side of the new join, and insert
1317 : : * the JoinExpr into the upper query's jointree at an appropriate place
1318 : : * (typically, where the lefthand relation(s) had been). Note that the
1319 : : * passed-in SubLink must also be removed from its original position in the
1320 : : * query quals, since the quals of the returned JoinExpr replace it.
1321 : : * (Notionally, we replace the SubLink with a constant TRUE, then elide the
1322 : : * redundant constant from the qual.)
1323 : : *
1324 : : * On success, the caller is also responsible for recursively applying
1325 : : * pull_up_sublinks processing to the rarg and quals of the returned JoinExpr.
1326 : : * (On failure, there is no need to do anything, since pull_up_sublinks will
1327 : : * be applied when we recursively plan the sub-select.)
1328 : : *
1329 : : * Side effects of a successful conversion include adding the SubLink's
1330 : : * subselect to the query's rangetable, so that it can be referenced in
1331 : : * the JoinExpr's rarg.
1332 : : */
1333 : : JoinExpr *
6280 tgl@sss.pgh.pa.us 1334 : 2294 : convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
1335 : : Relids available_rels)
1336 : : {
1337 : : JoinExpr *result;
7449 1338 : 2294 : Query *parse = root->parse;
8316 1339 : 2294 : Query *subselect = (Query *) sublink->subselect;
1340 : : Relids upper_varnos;
1341 : : int rtindex;
1342 : : ParseNamespaceItem *nsitem;
1343 : : RangeTblEntry *rte;
1344 : : RangeTblRef *rtr;
1345 : : List *subquery_vars;
1346 : : Node *quals;
1347 : : ParseState *pstate;
1348 : : Relids sub_ref_outer_relids;
1349 : : bool use_lateral;
1350 : :
6283 1351 [ - + ]: 2294 : Assert(sublink->subLinkType == ANY_SUBLINK);
1352 : :
1353 : : /*
1354 : : * If the sub-select contains any Vars of the parent query, we treat it as
1355 : : * LATERAL. (Vars from higher levels don't matter here.)
1356 : : */
620 akorotkov@postgresql 1357 : 2294 : sub_ref_outer_relids = pull_varnos_of_level(NULL, (Node *) subselect, 1);
1358 : 2294 : use_lateral = !bms_is_empty(sub_ref_outer_relids);
1359 : :
1360 : : /*
1361 : : * Can't convert if the sub-select contains parent-level Vars of relations
1362 : : * not in available_rels.
1363 : : */
1364 [ + + ]: 2294 : if (!bms_is_subset(sub_ref_outer_relids, available_rels))
6088 tgl@sss.pgh.pa.us 1365 : 6 : return NULL;
1366 : :
1367 : : /*
1368 : : * The test expression must contain some Vars of the parent query, else
1369 : : * it's not gonna be a join. (Note that it won't have Vars referring to
1370 : : * the subquery, rather Params.)
1371 : : */
1740 1372 : 2288 : upper_varnos = pull_varnos(root, sublink->testexpr);
6088 1373 [ + + ]: 2288 : if (bms_is_empty(upper_varnos))
1374 : 6 : return NULL;
1375 : :
1376 : : /*
1377 : : * However, it can't refer to anything outside available_rels.
1378 : : */
1379 [ + + ]: 2282 : if (!bms_is_subset(upper_varnos, available_rels))
1380 : 12 : return NULL;
1381 : :
1382 : : /*
1383 : : * The combining operators and left-hand expressions mustn't be volatile.
1384 : : */
7243 1385 [ + + ]: 2270 : if (contain_volatile_functions(sublink->testexpr))
6088 1386 : 32 : return NULL;
1387 : :
1388 : : /* Create a dummy ParseState for addRangeTableEntryForSubquery */
3883 rhaas@postgresql.org 1389 : 2238 : pstate = make_parsestate(NULL);
1390 : :
1391 : : /*
1392 : : * Okay, pull up the sub-select into upper range table.
1393 : : *
1394 : : * We rely here on the assumption that the outer query has no references
1395 : : * to the inner (necessarily true, other than the Vars that we build
1396 : : * below). Therefore this is a lot easier than what pull_up_subqueries has
1397 : : * to go through.
1398 : : */
2125 tgl@sss.pgh.pa.us 1399 : 2238 : nsitem = addRangeTableEntryForSubquery(pstate,
1400 : : subselect,
1401 : : NULL,
1402 : : use_lateral,
1403 : : false);
1404 : 2238 : rte = nsitem->p_rte;
8316 1405 : 2238 : parse->rtable = lappend(parse->rtable, rte);
7820 neilc@samurai.com 1406 : 2238 : rtindex = list_length(parse->rtable);
1407 : :
1408 : : /*
1409 : : * Form a RangeTblRef for the pulled-up sub-select.
1410 : : */
6280 tgl@sss.pgh.pa.us 1411 : 2238 : rtr = makeNode(RangeTblRef);
1412 : 2238 : rtr->rtindex = rtindex;
1413 : :
1414 : : /*
1415 : : * Build a list of Vars representing the subselect outputs.
1416 : : */
6398 1417 : 2238 : subquery_vars = generate_subquery_vars(root,
1418 : : subselect->targetList,
1419 : : rtindex);
1420 : :
1421 : : /*
1422 : : * Build the new join's qual expression, replacing Params with these Vars.
1423 : : */
6088 1424 : 2238 : quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
1425 : :
1426 : : /*
1427 : : * And finally, build the JoinExpr node.
1428 : : */
1429 : 2238 : result = makeNode(JoinExpr);
1430 : 2238 : result->jointype = JOIN_SEMI;
1431 : 2238 : result->isNatural = false;
1432 : 2238 : result->larg = NULL; /* caller must fill this in */
1433 : 2238 : result->rarg = (Node *) rtr;
5947 peter_e@gmx.net 1434 : 2238 : result->usingClause = NIL;
1671 peter@eisentraut.org 1435 : 2238 : result->join_using_alias = NULL;
6088 tgl@sss.pgh.pa.us 1436 : 2238 : result->quals = quals;
1437 : 2238 : result->alias = NULL;
1438 : 2238 : result->rtindex = 0; /* we don't need an RTE for it */
1439 : :
1440 : 2238 : return result;
1441 : : }
1442 : :
1443 : : /*
1444 : : * convert_EXISTS_sublink_to_join: try to convert an EXISTS SubLink to a join
1445 : : *
1446 : : * The API of this function is identical to convert_ANY_sublink_to_join's,
1447 : : * except that we also support the case where the caller has found NOT EXISTS,
1448 : : * so we need an additional input parameter "under_not".
1449 : : */
1450 : : JoinExpr *
6283 1451 : 1856 : convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
1452 : : bool under_not, Relids available_rels)
1453 : : {
1454 : : JoinExpr *result;
1455 : 1856 : Query *parse = root->parse;
1456 : 1856 : Query *subselect = (Query *) sublink->subselect;
1457 : : Node *whereClause;
1458 : : PlannerInfo subroot;
1459 : : int rtoffset;
1460 : : int varno;
1461 : : Relids clause_varnos;
1462 : : Relids upper_varnos;
1463 : :
1464 [ - + ]: 1856 : Assert(sublink->subLinkType == EXISTS_SUBLINK);
1465 : :
1466 : : /*
1467 : : * Can't flatten if it contains WITH. (We could arrange to pull up the
1468 : : * WITH into the parent query's cteList, but that risks changing the
1469 : : * semantics, since a WITH ought to be executed once per associated query
1470 : : * call.) Note that convert_ANY_sublink_to_join doesn't have to reject
1471 : : * this case, since it just produces a subquery RTE that doesn't have to
1472 : : * get flattened into the parent query.
1473 : : */
5761 1474 [ - + ]: 1856 : if (subselect->cteList)
5761 tgl@sss.pgh.pa.us 1475 :UBC 0 : return NULL;
1476 : :
1477 : : /*
1478 : : * Copy the subquery so we can modify it safely (see comments in
1479 : : * make_subplan).
1480 : : */
3154 peter_e@gmx.net 1481 :CBC 1856 : subselect = copyObject(subselect);
1482 : :
1483 : : /*
1484 : : * See if the subquery can be simplified based on the knowledge that it's
1485 : : * being used in EXISTS(). If we aren't able to get rid of its
1486 : : * targetlist, we have to fail, because the pullup operation leaves us
1487 : : * with noplace to evaluate the targetlist.
1488 : : */
3992 tgl@sss.pgh.pa.us 1489 [ + + ]: 1856 : if (!simplify_EXISTS_query(root, subselect))
6088 1490 : 16 : return NULL;
1491 : :
1492 : : /*
1493 : : * Separate out the WHERE clause. (We could theoretically also remove
1494 : : * top-level plain JOIN/ON clauses, but it's probably not worth the
1495 : : * trouble.)
1496 : : */
6283 1497 : 1840 : whereClause = subselect->jointree->quals;
1498 : 1840 : subselect->jointree->quals = NULL;
1499 : :
1500 : : /*
1501 : : * The rest of the sub-select must not refer to any Vars of the parent
1502 : : * query. (Vars of higher levels should be okay, though.)
1503 : : */
1504 [ - + ]: 1840 : if (contain_vars_of_level((Node *) subselect, 1))
6088 tgl@sss.pgh.pa.us 1505 :UBC 0 : return NULL;
1506 : :
1507 : : /*
1508 : : * On the other hand, the WHERE clause must contain some Vars of the
1509 : : * parent query, else it's not gonna be a join.
1510 : : */
6283 tgl@sss.pgh.pa.us 1511 [ + + ]:CBC 1840 : if (!contain_vars_of_level(whereClause, 1))
6088 1512 : 51 : return NULL;
1513 : :
1514 : : /*
1515 : : * We don't risk optimizing if the WHERE clause is volatile, either.
1516 : : */
6283 1517 [ - + ]: 1789 : if (contain_volatile_functions(whereClause))
6088 tgl@sss.pgh.pa.us 1518 :UBC 0 : return NULL;
1519 : :
1520 : : /*
1521 : : * Scan the rangetable for relation RTEs and retrieve the necessary
1522 : : * catalog information for each relation. Using this information, clear
1523 : : * the inh flag for any relation that has no children, collect not-null
1524 : : * attribute numbers for any relation that has column not-null
1525 : : * constraints, and expand virtual generated columns for any relation that
1526 : : * contains them.
1527 : : *
1528 : : * Note: we construct up an entirely dummy PlannerInfo for use here. This
1529 : : * is fine because only the "glob" and "parse" links will be used in this
1530 : : * case.
1531 : : *
1532 : : * Note: we temporarily assign back the WHERE clause so that any virtual
1533 : : * generated column references within it can be expanded. It should be
1534 : : * separated out again afterward.
1535 : : */
97 rguo@postgresql.org 1536 [ + - + - :GNC 166377 : MemSet(&subroot, 0, sizeof(subroot));
+ - + - +
+ ]
1537 : 1789 : subroot.type = T_PlannerInfo;
1538 : 1789 : subroot.glob = root->glob;
1539 : 1789 : subroot.parse = subselect;
1540 : 1789 : subselect->jointree->quals = whereClause;
1541 : 1789 : subselect = preprocess_relation_rtes(&subroot);
1542 : :
1543 : : /*
1544 : : * Now separate out the WHERE clause again.
1545 : : */
1546 : 1789 : whereClause = subselect->jointree->quals;
1547 : 1789 : subselect->jointree->quals = NULL;
1548 : :
1549 : : /*
1550 : : * The subquery must have a nonempty jointree, but we can make it so.
1551 : : */
2464 tgl@sss.pgh.pa.us 1552 :CBC 1789 : replace_empty_jointree(subselect);
1553 : :
1554 : : /*
1555 : : * Prepare to pull up the sub-select into top range table.
1556 : : *
1557 : : * We rely here on the assumption that the outer query has no references
1558 : : * to the inner (necessarily true). Therefore this is a lot easier than
1559 : : * what pull_up_subqueries has to go through.
1560 : : *
1561 : : * In fact, it's even easier than what convert_ANY_sublink_to_join has to
1562 : : * do. The machinations of simplify_EXISTS_query ensured that there is
1563 : : * nothing interesting in the subquery except an rtable and jointree, and
1564 : : * even the jointree FromExpr no longer has quals. So we can just append
1565 : : * the rtable to our own and use the FromExpr in our jointree. But first,
1566 : : * adjust all level-zero varnos in the subquery to account for the rtable
1567 : : * merger.
1568 : : */
6283 1569 : 1789 : rtoffset = list_length(parse->rtable);
1570 : 1789 : OffsetVarNodes((Node *) subselect, rtoffset, 0);
1571 : 1789 : OffsetVarNodes(whereClause, rtoffset, 0);
1572 : :
1573 : : /*
1574 : : * Upper-level vars in subquery will now be one level closer to their
1575 : : * parent than before; in particular, anything that had been level 1
1576 : : * becomes level zero.
1577 : : */
1578 : 1789 : IncrementVarSublevelsUp((Node *) subselect, -1, 1);
1579 : 1789 : IncrementVarSublevelsUp(whereClause, -1, 1);
1580 : :
1581 : : /*
1582 : : * Now that the WHERE clause is adjusted to match the parent query
1583 : : * environment, we can easily identify all the level-zero rels it uses.
1584 : : * The ones <= rtoffset belong to the upper query; the ones > rtoffset do
1585 : : * not.
1586 : : */
1740 1587 : 1789 : clause_varnos = pull_varnos(root, whereClause);
6088 1588 : 1789 : upper_varnos = NULL;
970 1589 : 1789 : varno = -1;
1590 [ + + ]: 5403 : while ((varno = bms_next_member(clause_varnos, varno)) >= 0)
1591 : : {
6283 1592 [ + + ]: 3614 : if (varno <= rtoffset)
6088 1593 : 1813 : upper_varnos = bms_add_member(upper_varnos, varno);
1594 : : }
6283 1595 : 1789 : bms_free(clause_varnos);
6088 1596 [ - + ]: 1789 : Assert(!bms_is_empty(upper_varnos));
1597 : :
1598 : : /*
1599 : : * Now that we've got the set of upper-level varnos, we can make the last
1600 : : * check: only available_rels can be referenced.
1601 : : */
1602 [ + + ]: 1789 : if (!bms_is_subset(upper_varnos, available_rels))
1603 : 22 : return NULL;
1604 : :
1605 : : /*
1606 : : * Now we can attach the modified subquery rtable to the parent. This also
1607 : : * adds subquery's RTEPermissionInfos into the upper query.
1608 : : */
1056 alvherre@alvh.no-ip. 1609 : 1767 : CombineRangeTables(&parse->rtable, &parse->rteperminfos,
1610 : : subselect->rtable, subselect->rteperminfos);
1611 : :
1612 : : /*
1613 : : * And finally, build the JoinExpr node.
1614 : : */
6088 tgl@sss.pgh.pa.us 1615 : 1767 : result = makeNode(JoinExpr);
1616 [ + + ]: 1767 : result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
1617 : 1767 : result->isNatural = false;
1618 : 1767 : result->larg = NULL; /* caller must fill this in */
1619 : : /* flatten out the FromExpr node if it's useless */
1620 [ + + ]: 1767 : if (list_length(subselect->jointree->fromlist) == 1)
1621 : 1758 : result->rarg = (Node *) linitial(subselect->jointree->fromlist);
1622 : : else
1623 : 9 : result->rarg = (Node *) subselect->jointree;
5947 peter_e@gmx.net 1624 : 1767 : result->usingClause = NIL;
1671 peter@eisentraut.org 1625 : 1767 : result->join_using_alias = NULL;
6088 tgl@sss.pgh.pa.us 1626 : 1767 : result->quals = whereClause;
1627 : 1767 : result->alias = NULL;
1628 : 1767 : result->rtindex = 0; /* we don't need an RTE for it */
1629 : :
1630 : 1767 : return result;
1631 : : }
1632 : :
1633 : : /*
1634 : : * simplify_EXISTS_query: remove any useless stuff in an EXISTS's subquery
1635 : : *
1636 : : * The only thing that matters about an EXISTS query is whether it returns
1637 : : * zero or more than zero rows. Therefore, we can remove certain SQL features
1638 : : * that won't affect that. The only part that is really likely to matter in
1639 : : * typical usage is simplifying the targetlist: it's a common habit to write
1640 : : * "SELECT * FROM" even though there is no need to evaluate any columns.
1641 : : *
1642 : : * Note: by suppressing the targetlist we could cause an observable behavioral
1643 : : * change, namely that any errors that might occur in evaluating the tlist
1644 : : * won't occur, nor will other side-effects of volatile functions. This seems
1645 : : * unlikely to bother anyone in practice.
1646 : : *
1647 : : * Returns true if was able to discard the targetlist, else false.
1648 : : */
1649 : : static bool
3992 1650 : 4182 : simplify_EXISTS_query(PlannerInfo *root, Query *query)
1651 : : {
1652 : : ListCell *lc;
1653 : :
1654 : : /*
1655 : : * We don't try to simplify at all if the query uses set operations,
1656 : : * aggregates, grouping sets, SRFs, modifying CTEs, HAVING, OFFSET, or FOR
1657 : : * UPDATE/SHARE; none of these seem likely in normal usage and their
1658 : : * possible effects are complex. (Note: we could ignore an "OFFSET 0"
1659 : : * clause, but that traditionally is used as an optimization fence, so we
1660 : : * don't.)
1661 : : */
6275 1662 [ + - ]: 4182 : if (query->commandType != CMD_SELECT ||
1663 [ + - ]: 4182 : query->setOperations ||
1664 [ + - ]: 4182 : query->hasAggs ||
3817 andres@anarazel.de 1665 [ + - ]: 4182 : query->groupingSets ||
6147 tgl@sss.pgh.pa.us 1666 [ + - ]: 4182 : query->hasWindowFuncs ||
3331 1667 [ + - ]: 4182 : query->hasTargetSRFs ||
5358 1668 [ + - ]: 4182 : query->hasModifyingCTE ||
6275 1669 [ + - ]: 4182 : query->havingQual ||
1670 [ + + ]: 4182 : query->limitOffset ||
1671 [ + + ]: 4170 : query->rowMarks)
1672 : 26 : return false;
1673 : :
1674 : : /*
1675 : : * LIMIT with a constant positive (or NULL) value doesn't affect the
1676 : : * semantics of EXISTS, so let's ignore such clauses. This is worth doing
1677 : : * because people accustomed to certain other DBMSes may be in the habit
1678 : : * of writing EXISTS(SELECT ... LIMIT 1) as an optimization. If there's a
1679 : : * LIMIT with anything else as argument, though, we can't simplify.
1680 : : */
3992 1681 [ + + ]: 4156 : if (query->limitCount)
1682 : : {
1683 : : /*
1684 : : * The LIMIT clause has not yet been through eval_const_expressions,
1685 : : * so we have to apply that here. It might seem like this is a waste
1686 : : * of cycles, since the only case plausibly worth worrying about is
1687 : : * "LIMIT 1" ... but what we'll actually see is "LIMIT int8(1::int4)",
1688 : : * so we have to fold constants or we're not going to recognize it.
1689 : : */
1690 : 12 : Node *node = eval_const_expressions(root, query->limitCount);
1691 : : Const *limit;
1692 : :
1693 : : /* Might as well update the query if we simplified the clause. */
1694 : 12 : query->limitCount = node;
1695 : :
1696 [ - + ]: 12 : if (!IsA(node, Const))
3992 tgl@sss.pgh.pa.us 1697 :UBC 0 : return false;
1698 : :
3992 tgl@sss.pgh.pa.us 1699 :CBC 12 : limit = (Const *) node;
1700 [ - + ]: 12 : Assert(limit->consttype == INT8OID);
1701 [ + + + + ]: 12 : if (!limit->constisnull && DatumGetInt64(limit->constvalue) <= 0)
1702 : 6 : return false;
1703 : :
1704 : : /* Whether or not the targetlist is safe, we can drop the LIMIT. */
1705 : 6 : query->limitCount = NULL;
1706 : : }
1707 : :
1708 : : /*
1709 : : * Otherwise, we can throw away the targetlist, as well as any GROUP,
1710 : : * WINDOW, DISTINCT, and ORDER BY clauses; none of those clauses will
1711 : : * change a nonzero-rows result to zero rows or vice versa. (Furthermore,
1712 : : * since our parsetree representation of these clauses depends on the
1713 : : * targetlist, we'd better throw them away if we drop the targetlist.)
1714 : : */
6275 1715 : 4150 : query->targetList = NIL;
1716 : 4150 : query->groupClause = NIL;
6147 1717 : 4150 : query->windowClause = NIL;
6275 1718 : 4150 : query->distinctClause = NIL;
1719 : 4150 : query->sortClause = NIL;
1720 : 4150 : query->hasDistinctOn = false;
1721 : :
1722 : : /*
1723 : : * Since we have thrown away the GROUP BY clauses, we'd better remove the
1724 : : * RTE_GROUP RTE and clear the hasGroupRTE flag.
1725 : : */
367 rguo@postgresql.org 1726 [ + + + + : 8517 : foreach(lc, query->rtable)
+ + ]
1727 : : {
1728 : 4370 : RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc);
1729 : :
1730 : : /*
1731 : : * Remove the RTE_GROUP RTE and clear the hasGroupRTE flag. (Since
1732 : : * we'll exit the foreach loop immediately, we don't bother with
1733 : : * foreach_delete_current.)
1734 : : */
1735 [ + + ]: 4370 : if (rte->rtekind == RTE_GROUP)
1736 : : {
1737 [ - + ]: 3 : Assert(query->hasGroupRTE);
1738 : 3 : query->rtable = list_delete_cell(query->rtable, lc);
1739 : 3 : query->hasGroupRTE = false;
1740 : 3 : break;
1741 : : }
1742 : : }
1743 : :
6275 tgl@sss.pgh.pa.us 1744 : 4150 : return true;
1745 : : }
1746 : :
1747 : : /*
1748 : : * convert_EXISTS_to_ANY: try to convert EXISTS to a hashable ANY sublink
1749 : : *
1750 : : * The subselect is expected to be a fresh copy that we can munge up,
1751 : : * and to have been successfully passed through simplify_EXISTS_query.
1752 : : *
1753 : : * On success, the modified subselect is returned, and we store a suitable
1754 : : * upper-level test expression at *testexpr, plus a list of the subselect's
1755 : : * output Params at *paramIds. (The test expression is already Param-ified
1756 : : * and hence need not go through convert_testexpr, which is why we have to
1757 : : * deal with the Param IDs specially.)
1758 : : *
1759 : : * On failure, returns NULL.
1760 : : */
1761 : : static Query *
1762 : 1093 : convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
1763 : : Node **testexpr, List **paramIds)
1764 : : {
1765 : : Node *whereClause;
1766 : : PlannerInfo subroot;
1767 : : List *leftargs,
1768 : : *rightargs,
1769 : : *opids,
1770 : : *opcollations,
1771 : : *newWhere,
1772 : : *tlist,
1773 : : *testlist,
1774 : : *paramids;
1775 : : ListCell *lc,
1776 : : *rc,
1777 : : *oc,
1778 : : *cc;
1779 : : AttrNumber resno;
1780 : :
1781 : : /*
1782 : : * Query must not require a targetlist, since we have to insert a new one.
1783 : : * Caller should have dealt with the case already.
1784 : : */
1785 [ - + ]: 1093 : Assert(subselect->targetList == NIL);
1786 : :
1787 : : /*
1788 : : * Separate out the WHERE clause. (We could theoretically also remove
1789 : : * top-level plain JOIN/ON clauses, but it's probably not worth the
1790 : : * trouble.)
1791 : : */
1792 : 1093 : whereClause = subselect->jointree->quals;
1793 : 1093 : subselect->jointree->quals = NULL;
1794 : :
1795 : : /*
1796 : : * The rest of the sub-select must not refer to any Vars of the parent
1797 : : * query. (Vars of higher levels should be okay, though.)
1798 : : *
1799 : : * Note: we need not check for Aggrefs separately because we know the
1800 : : * sub-select is as yet unoptimized; any uplevel Aggref must therefore
1801 : : * contain an uplevel Var reference. This is not the case below ...
1802 : : */
1803 [ + + ]: 1093 : if (contain_vars_of_level((Node *) subselect, 1))
1804 : 3 : return NULL;
1805 : :
1806 : : /*
1807 : : * We don't risk optimizing if the WHERE clause is volatile, either.
1808 : : */
1809 [ - + ]: 1090 : if (contain_volatile_functions(whereClause))
6275 tgl@sss.pgh.pa.us 1810 :UBC 0 : return NULL;
1811 : :
1812 : : /*
1813 : : * Clean up the WHERE clause by doing const-simplification etc on it.
1814 : : * Aside from simplifying the processing we're about to do, this is
1815 : : * important for being able to pull chunks of the WHERE clause up into the
1816 : : * parent query. Since we are invoked partway through the parent's
1817 : : * preprocess_expression() work, earlier steps of preprocess_expression()
1818 : : * wouldn't get applied to the pulled-up stuff unless we do them here. For
1819 : : * the parts of the WHERE clause that get put back into the child query,
1820 : : * this work is partially duplicative, but it shouldn't hurt.
1821 : : *
1822 : : * Note: we do not run flatten_join_alias_vars. This is OK because any
1823 : : * parent aliases were flattened already, and we're not going to pull any
1824 : : * child Vars (of any description) into the parent.
1825 : : *
1826 : : * Note: we construct up an entirely dummy PlannerInfo to pass to
1827 : : * eval_const_expressions. This is fine because only the "glob" and
1828 : : * "parse" links are used by eval_const_expressions.
1829 : : */
97 rguo@postgresql.org 1830 [ + - + - :GNC 101370 : MemSet(&subroot, 0, sizeof(subroot));
+ - + - +
+ ]
1831 : 1090 : subroot.type = T_PlannerInfo;
1832 : 1090 : subroot.glob = root->glob;
1833 : 1090 : subroot.parse = subselect;
1834 : 1090 : whereClause = eval_const_expressions(&subroot, whereClause);
2787 tgl@sss.pgh.pa.us 1835 :CBC 1090 : whereClause = (Node *) canonicalize_qual((Expr *) whereClause, false);
6275 1836 : 1090 : whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
1837 : :
1838 : : /*
1839 : : * We now have a flattened implicit-AND list of clauses, which we try to
1840 : : * break apart into "outervar = innervar" hash clauses. Anything that
1841 : : * can't be broken apart just goes back into the newWhere list. Note that
1842 : : * we aren't trying hard yet to ensure that we have only outer or only
1843 : : * inner on each side; we'll check that if we get to the end.
1844 : : */
5336 1845 : 1090 : leftargs = rightargs = opids = opcollations = newWhere = NIL;
6275 1846 [ + - + + : 4315 : foreach(lc, (List *) whereClause)
+ + ]
1847 : : {
1848 : 3225 : OpExpr *expr = (OpExpr *) lfirst(lc);
1849 : :
1850 [ + + + + ]: 5269 : if (IsA(expr, OpExpr) &&
1851 : 2044 : hash_ok_operator(expr))
1852 : : {
5982 bruce@momjian.us 1853 : 1615 : Node *leftarg = (Node *) linitial(expr->args);
1854 : 1615 : Node *rightarg = (Node *) lsecond(expr->args);
1855 : :
6275 tgl@sss.pgh.pa.us 1856 [ + + ]: 1615 : if (contain_vars_of_level(leftarg, 1))
1857 : : {
1858 : 206 : leftargs = lappend(leftargs, leftarg);
1859 : 206 : rightargs = lappend(rightargs, rightarg);
1860 : 206 : opids = lappend_oid(opids, expr->opno);
5336 1861 : 206 : opcollations = lappend_oid(opcollations, expr->inputcollid);
6275 1862 : 206 : continue;
1863 : : }
1864 [ + + ]: 1409 : if (contain_vars_of_level(rightarg, 1))
1865 : : {
1866 : : /*
1867 : : * We must commute the clause to put the outer var on the
1868 : : * left, because the hashing code in nodeSubplan.c expects
1869 : : * that. This probably shouldn't ever fail, since hashable
1870 : : * operators ought to have commutators, but be paranoid.
1871 : : */
1872 : 1096 : expr->opno = get_commutator(expr->opno);
1873 [ + - + - ]: 1096 : if (OidIsValid(expr->opno) && hash_ok_operator(expr))
1874 : : {
1875 : 1096 : leftargs = lappend(leftargs, rightarg);
1876 : 1096 : rightargs = lappend(rightargs, leftarg);
1877 : 1096 : opids = lappend_oid(opids, expr->opno);
5336 1878 : 1096 : opcollations = lappend_oid(opcollations, expr->inputcollid);
6275 1879 : 1096 : continue;
1880 : : }
1881 : : /* If no commutator, no chance to optimize the WHERE clause */
6275 tgl@sss.pgh.pa.us 1882 :UBC 0 : return NULL;
1883 : : }
1884 : : }
1885 : : /* Couldn't handle it as a hash clause */
6275 tgl@sss.pgh.pa.us 1886 :CBC 1923 : newWhere = lappend(newWhere, expr);
1887 : : }
1888 : :
1889 : : /*
1890 : : * If we didn't find anything we could convert, fail.
1891 : : */
1892 [ + + ]: 1090 : if (leftargs == NIL)
1893 : 188 : return NULL;
1894 : :
1895 : : /*
1896 : : * There mustn't be any parent Vars or Aggs in the stuff that we intend to
1897 : : * put back into the child query. Note: you might think we don't need to
1898 : : * check for Aggs separately, because an uplevel Agg must contain an
1899 : : * uplevel Var in its argument. But it is possible that the uplevel Var
1900 : : * got optimized away by eval_const_expressions. Consider
1901 : : *
1902 : : * SUM(CASE WHEN false THEN uplevelvar ELSE 0 END)
1903 : : */
1904 [ + + - + ]: 1754 : if (contain_vars_of_level((Node *) newWhere, 1) ||
1905 : 852 : contain_vars_of_level((Node *) rightargs, 1))
1906 : 50 : return NULL;
1907 [ + + + - ]: 873 : if (root->parse->hasAggs &&
1908 [ - + ]: 42 : (contain_aggs_of_level((Node *) newWhere, 1) ||
1909 : 21 : contain_aggs_of_level((Node *) rightargs, 1)))
6275 tgl@sss.pgh.pa.us 1910 :UBC 0 : return NULL;
1911 : :
1912 : : /*
1913 : : * And there can't be any child Vars in the stuff we intend to pull up.
1914 : : * (Note: we'd need to check for child Aggs too, except we know the child
1915 : : * has no aggs at all because of simplify_EXISTS_query's check. The same
1916 : : * goes for window functions.)
1917 : : */
6275 tgl@sss.pgh.pa.us 1918 [ - + ]:CBC 852 : if (contain_vars_of_level((Node *) leftargs, 0))
6275 tgl@sss.pgh.pa.us 1919 :UBC 0 : return NULL;
1920 : :
1921 : : /*
1922 : : * Also reject sublinks in the stuff we intend to pull up. (It might be
1923 : : * possible to support this, but doesn't seem worth the complication.)
1924 : : */
6275 tgl@sss.pgh.pa.us 1925 [ - + ]:CBC 852 : if (contain_subplans((Node *) leftargs))
6275 tgl@sss.pgh.pa.us 1926 :UBC 0 : return NULL;
1927 : :
1928 : : /*
1929 : : * Okay, adjust the sublevelsup in the stuff we're pulling up.
1930 : : */
6275 tgl@sss.pgh.pa.us 1931 :CBC 852 : IncrementVarSublevelsUp((Node *) leftargs, -1, 1);
1932 : :
1933 : : /*
1934 : : * Put back any child-level-only WHERE clauses.
1935 : : */
1936 [ + + ]: 852 : if (newWhere)
1937 : 750 : subselect->jointree->quals = (Node *) make_ands_explicit(newWhere);
1938 : :
1939 : : /*
1940 : : * Build a new targetlist for the child that emits the expressions we
1941 : : * need. Concurrently, build a testexpr for the parent using Params to
1942 : : * reference the child outputs. (Since we generate Params directly here,
1943 : : * there will be no need to convert the testexpr in build_subplan.)
1944 : : */
1945 : 852 : tlist = testlist = paramids = NIL;
1946 : 852 : resno = 1;
2433 1947 [ + - + + : 2104 : forfour(lc, leftargs, rc, rightargs, oc, opids, cc, opcollations)
+ - + + +
- + + + -
+ + + + +
- + - + -
+ + ]
1948 : : {
6275 1949 : 1252 : Node *leftarg = (Node *) lfirst(lc);
1950 : 1252 : Node *rightarg = (Node *) lfirst(rc);
1951 : 1252 : Oid opid = lfirst_oid(oc);
5336 1952 : 1252 : Oid opcollation = lfirst_oid(cc);
1953 : : Param *param;
1954 : :
2481 1955 : 1252 : param = generate_new_exec_param(root,
1956 : : exprType(rightarg),
1957 : : exprTypmod(rightarg),
1958 : : exprCollation(rightarg));
6275 1959 : 1252 : tlist = lappend(tlist,
1960 : 1252 : makeTargetEntry((Expr *) rightarg,
1961 : 1252 : resno++,
1962 : : NULL,
1963 : : false));
1964 : 1252 : testlist = lappend(testlist,
1965 : 1252 : make_opclause(opid, BOOLOID, false,
1966 : : (Expr *) leftarg, (Expr *) param,
1967 : : InvalidOid, opcollation));
1968 : 1252 : paramids = lappend_int(paramids, param->paramid);
1969 : : }
1970 : :
1971 : : /* Put everything where it should go, and we're done */
1972 : 852 : subselect->targetList = tlist;
1973 : 852 : *testexpr = (Node *) make_ands_explicit(testlist);
1974 : 852 : *paramIds = paramids;
1975 : :
1976 : 852 : return subselect;
1977 : : }
1978 : :
1979 : :
1980 : : /*
1981 : : * Replace correlation vars (uplevel vars) with Params.
1982 : : *
1983 : : * Uplevel PlaceHolderVars, aggregates, GROUPING() expressions,
1984 : : * MergeSupportFuncs, and ReturningExprs are replaced, too.
1985 : : *
1986 : : * Note: it is critical that this runs immediately after SS_process_sublinks.
1987 : : * Since we do not recurse into the arguments of uplevel PHVs and aggregates,
1988 : : * they will get copied to the appropriate subplan args list in the parent
1989 : : * query with uplevel vars not replaced by Params, but only adjusted in level
1990 : : * (see replace_outer_placeholdervar and replace_outer_agg). That's exactly
1991 : : * what we want for the vars of the parent level --- but if a PHV's or
1992 : : * aggregate's argument contains any further-up variables, they have to be
1993 : : * replaced with Params in their turn. That will happen when the parent level
1994 : : * runs SS_replace_correlation_vars. Therefore it must do so after expanding
1995 : : * its sublinks to subplans. And we don't want any steps in between, else
1996 : : * those steps would never get applied to the argument expressions, either in
1997 : : * the parent or the child level.
1998 : : *
1999 : : * Another fairly tricky thing going on here is the handling of SubLinks in
2000 : : * the arguments of uplevel PHVs/aggregates. Those are not touched inside the
2001 : : * intermediate query level, either. Instead, SS_process_sublinks recurses on
2002 : : * them after copying the PHV or Aggref expression into the parent plan level
2003 : : * (this is actually taken care of in build_subplan).
2004 : : */
2005 : : Node *
6825 2006 : 87582 : SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
2007 : : {
2008 : : /* No setup needed for tree walk, so away we go */
2009 : 87582 : return replace_correlation_vars_mutator(expr, root);
2010 : : }
2011 : :
2012 : : static Node *
2013 : 770981 : replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
2014 : : {
9560 2015 [ + + ]: 770981 : if (node == NULL)
2016 : 31640 : return NULL;
2017 [ + + ]: 739341 : if (IsA(node, Var))
2018 : : {
2019 [ + + ]: 204476 : if (((Var *) node)->varlevelsup > 0)
6825 2020 : 26597 : return (Node *) replace_outer_var(root, (Var *) node);
2021 : : }
4965 2022 [ + + ]: 712744 : if (IsA(node, PlaceHolderVar))
2023 : : {
2024 [ + + ]: 51 : if (((PlaceHolderVar *) node)->phlevelsup > 0)
2025 : 30 : return (Node *) replace_outer_placeholdervar(root,
2026 : : (PlaceHolderVar *) node);
2027 : : }
8179 2028 [ + + ]: 712714 : if (IsA(node, Aggref))
2029 : : {
2030 [ + + ]: 4387 : if (((Aggref *) node)->agglevelsup > 0)
6825 2031 : 26 : return (Node *) replace_outer_agg(root, (Aggref *) node);
2032 : : }
3817 andres@anarazel.de 2033 [ + + ]: 712688 : if (IsA(node, GroupingFunc))
2034 : : {
2035 [ + + ]: 45 : if (((GroupingFunc *) node)->agglevelsup > 0)
2036 : 32 : return (Node *) replace_outer_grouping(root, (GroupingFunc *) node);
2037 : : }
589 dean.a.rasheed@gmail 2038 [ + + ]: 712656 : if (IsA(node, MergeSupportFunc))
2039 : : {
2040 [ + + ]: 18 : if (root->parse->commandType != CMD_MERGE)
2041 : 3 : return (Node *) replace_outer_merge_support(root,
2042 : : (MergeSupportFunc *) node);
2043 : : }
284 2044 [ + + ]: 712653 : if (IsA(node, ReturningExpr))
2045 : : {
2046 [ + - ]: 9 : if (((ReturningExpr *) node)->retlevelsup > 0)
2047 : 9 : return (Node *) replace_outer_returning(root,
2048 : : (ReturningExpr *) node);
2049 : : }
333 peter@eisentraut.org 2050 : 712644 : return expression_tree_mutator(node, replace_correlation_vars_mutator, root);
2051 : : }
2052 : :
2053 : : /*
2054 : : * Expand SubLinks to SubPlans in the given expression.
2055 : : *
2056 : : * The isQual argument tells whether or not this expression is a WHERE/HAVING
2057 : : * qualifier expression. If it is, any sublinks appearing at top level need
2058 : : * not distinguish FALSE from UNKNOWN return values.
2059 : : */
2060 : : Node *
6825 tgl@sss.pgh.pa.us 2061 : 52678 : SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
2062 : : {
2063 : : process_sublinks_context context;
2064 : :
2065 : 52678 : context.root = root;
2066 : 52678 : context.isTopQual = isQual;
2067 : 52678 : return process_sublinks_mutator(expr, &context);
2068 : : }
2069 : :
2070 : : static Node *
6556 bruce@momjian.us 2071 : 687963 : process_sublinks_mutator(Node *node, process_sublinks_context *context)
2072 : : {
2073 : : process_sublinks_context locContext;
2074 : :
6825 tgl@sss.pgh.pa.us 2075 : 687963 : locContext.root = context->root;
2076 : :
9560 2077 [ + + ]: 687963 : if (node == NULL)
9918 bruce@momjian.us 2078 : 29891 : return NULL;
9560 tgl@sss.pgh.pa.us 2079 [ + + ]: 658072 : if (IsA(node, SubLink))
2080 : : {
9329 bruce@momjian.us 2081 : 19694 : SubLink *sublink = (SubLink *) node;
2082 : : Node *testexpr;
2083 : :
2084 : : /*
2085 : : * First, recursively process the lefthand-side expressions, if any.
2086 : : * They're not top-level anymore.
2087 : : */
6825 tgl@sss.pgh.pa.us 2088 : 19694 : locContext.isTopQual = false;
2089 : 19694 : testexpr = process_sublinks_mutator(sublink->testexpr, &locContext);
2090 : :
2091 : : /*
2092 : : * Now build the SubPlan node and make the expr to return.
2093 : : */
2094 : 19694 : return make_subplan(context->root,
6275 2095 : 19694 : (Query *) sublink->subselect,
2096 : : sublink->subLinkType,
2097 : : sublink->subLinkId,
2098 : : testexpr,
6825 2099 : 19694 : context->isTopQual);
2100 : : }
2101 : :
2102 : : /*
2103 : : * Don't recurse into the arguments of an outer PHV, Aggref, GroupingFunc,
2104 : : * or ReturningExpr here. Any SubLinks in the arguments have to be dealt
2105 : : * with at the outer query level; they'll be handled when build_subplan
2106 : : * collects the PHV, Aggref, GroupingFunc, or ReturningExpr into the
2107 : : * arguments to be passed down to the current subplan.
2108 : : */
4965 2109 [ + + ]: 638378 : if (IsA(node, PlaceHolderVar))
2110 : : {
2111 [ + + ]: 119 : if (((PlaceHolderVar *) node)->phlevelsup > 0)
2112 : 6 : return node;
2113 : : }
2114 [ + + ]: 638259 : else if (IsA(node, Aggref))
2115 : : {
6029 2116 [ + + ]: 306 : if (((Aggref *) node)->agglevelsup > 0)
2117 : 9 : return node;
2118 : : }
1316 2119 [ + + ]: 637953 : else if (IsA(node, GroupingFunc))
2120 : : {
2121 [ + + ]: 80 : if (((GroupingFunc *) node)->agglevelsup > 0)
2122 : 18 : return node;
2123 : : }
284 dean.a.rasheed@gmail 2124 [ + + ]: 637873 : else if (IsA(node, ReturningExpr))
2125 : : {
2126 [ + + ]: 99 : if (((ReturningExpr *) node)->retlevelsup > 0)
2127 : 3 : return node;
2128 : : }
2129 : :
2130 : : /*
2131 : : * We should never see a SubPlan expression in the input (since this is
2132 : : * the very routine that creates 'em to begin with). We shouldn't find
2133 : : * ourselves invoked directly on a Query, either.
2134 : : */
6275 tgl@sss.pgh.pa.us 2135 [ - + ]: 638342 : Assert(!IsA(node, SubPlan));
2136 [ - + ]: 638342 : Assert(!IsA(node, AlternativeSubPlan));
8319 2137 [ - + ]: 638342 : Assert(!IsA(node, Query));
2138 : :
2139 : : /*
2140 : : * Because make_subplan() could return an AND or OR clause, we have to
2141 : : * take steps to preserve AND/OR flatness of a qual. We assume the input
2142 : : * has been AND/OR flattened and so we need no recursion here.
2143 : : *
2144 : : * (Due to the coding here, we will not get called on the List subnodes of
2145 : : * an AND; and the input is *not* yet in implicit-AND format. So no check
2146 : : * is needed for a bare List.)
2147 : : *
2148 : : * Anywhere within the top-level AND/OR clause structure, we can tell
2149 : : * make_subplan() that NULL and FALSE are interchangeable. So isTopQual
2150 : : * propagates down in both cases. (Note that this is unlike the meaning
2151 : : * of "top level qual" used in most other places in Postgres.)
2152 : : */
2463 2153 [ + + ]: 638342 : if (is_andclause(node))
2154 : : {
7729 bruce@momjian.us 2155 : 10012 : List *newargs = NIL;
2156 : : ListCell *l;
2157 : :
2158 : : /* Still at qual top-level */
6825 tgl@sss.pgh.pa.us 2159 : 10012 : locContext.isTopQual = context->isTopQual;
2160 : :
7959 2161 [ + - + + : 36556 : foreach(l, ((BoolExpr *) node)->args)
+ + ]
2162 : : {
2163 : : Node *newarg;
2164 : :
6825 2165 : 26544 : newarg = process_sublinks_mutator(lfirst(l), &locContext);
2463 2166 [ - + ]: 26544 : if (is_andclause(newarg))
7820 neilc@samurai.com 2167 :UBC 0 : newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
2168 : : else
7959 tgl@sss.pgh.pa.us 2169 :CBC 26544 : newargs = lappend(newargs, newarg);
2170 : : }
2171 : 10012 : return (Node *) make_andclause(newargs);
2172 : : }
2173 : :
2463 2174 [ + + ]: 628330 : if (is_orclause(node))
2175 : : {
7729 bruce@momjian.us 2176 : 1130 : List *newargs = NIL;
2177 : : ListCell *l;
2178 : :
2179 : : /* Still at qual top-level */
6277 tgl@sss.pgh.pa.us 2180 : 1130 : locContext.isTopQual = context->isTopQual;
2181 : :
7959 2182 [ + - + + : 4046 : foreach(l, ((BoolExpr *) node)->args)
+ + ]
2183 : : {
2184 : : Node *newarg;
2185 : :
6825 2186 : 2916 : newarg = process_sublinks_mutator(lfirst(l), &locContext);
2463 2187 [ - + ]: 2916 : if (is_orclause(newarg))
7820 neilc@samurai.com 2188 :UBC 0 : newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
2189 : : else
7959 tgl@sss.pgh.pa.us 2190 :CBC 2916 : newargs = lappend(newargs, newarg);
2191 : : }
2192 : 1130 : return (Node *) make_orclause(newargs);
2193 : : }
2194 : :
2195 : : /*
2196 : : * If we recurse down through anything other than an AND or OR node, we
2197 : : * are definitely not at top qual level anymore.
2198 : : */
6277 2199 : 627200 : locContext.isTopQual = false;
2200 : :
9560 2201 : 627200 : return expression_tree_mutator(node,
2202 : : process_sublinks_mutator,
2203 : : &locContext);
2204 : : }
2205 : :
2206 : : /*
2207 : : * SS_identify_outer_params - identify the Params available from outer levels
2208 : : *
2209 : : * This must be run after SS_replace_correlation_vars and SS_process_sublinks
2210 : : * processing is complete in a given query level as well as all of its
2211 : : * descendant levels (which means it's most practical to do it at the end of
2212 : : * processing the query level). We compute the set of paramIds that outer
2213 : : * levels will make available to this level+descendants, and record it in
2214 : : * root->outer_params for use while computing extParam/allParam sets in final
2215 : : * plan cleanup. (We can't just compute it then, because the upper levels'
2216 : : * plan_params lists are transient and will be gone by then.)
2217 : : */
2218 : : void
3730 2219 : 260082 : SS_identify_outer_params(PlannerInfo *root)
2220 : : {
2221 : : Bitmapset *outer_params;
2222 : : PlannerInfo *proot;
2223 : : ListCell *l;
2224 : :
2225 : : /*
2226 : : * If no parameters have been assigned anywhere in the tree, we certainly
2227 : : * don't need to do anything here.
2228 : : */
2905 rhaas@postgresql.org 2229 [ + + ]: 260082 : if (root->glob->paramExecTypes == NIL)
3730 tgl@sss.pgh.pa.us 2230 : 176963 : return;
2231 : :
2232 : : /*
2233 : : * Scan all query levels above this one to see which parameters are due to
2234 : : * be available from them, either because lower query levels have
2235 : : * requested them (via plan_params) or because they will be available from
2236 : : * initPlans of those levels.
2237 : : */
2238 : 83119 : outer_params = NULL;
4800 2239 [ + + ]: 111642 : for (proot = root->parent_root; proot != NULL; proot = proot->parent_root)
2240 : : {
2241 : : /*
2242 : : * Include ordinary Var/PHV/Aggref/GroupingFunc/ReturningExpr params.
2243 : : */
2244 [ + + + + : 51553 : foreach(l, proot->plan_params)
+ + ]
2245 : : {
2246 : 23030 : PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
2247 : :
3730 2248 : 23030 : outer_params = bms_add_member(outer_params, pitem->paramId);
2249 : : }
2250 : : /* Include any outputs of outer-level initPlans */
4800 2251 [ + + + + : 31646 : foreach(l, proot->init_plans)
+ + ]
2252 : : {
2253 : 3123 : SubPlan *initsubplan = (SubPlan *) lfirst(l);
2254 : : ListCell *l2;
2255 : :
2256 [ + - + + : 6246 : foreach(l2, initsubplan->setParam)
+ + ]
2257 : : {
3730 2258 : 3123 : outer_params = bms_add_member(outer_params, lfirst_int(l2));
2259 : : }
2260 : : }
2261 : : /* Include worktable ID, if a recursive query is being planned */
4800 2262 [ + + ]: 28523 : if (proot->wt_param_id >= 0)
3730 2263 : 1626 : outer_params = bms_add_member(outer_params, proot->wt_param_id);
2264 : : }
2265 : 83119 : root->outer_params = outer_params;
2266 : : }
2267 : :
2268 : : /*
2269 : : * SS_charge_for_initplans - account for initplans in Path costs & parallelism
2270 : : *
2271 : : * If any initPlans have been created in the current query level, they will
2272 : : * get attached to the Plan tree created from whichever Path we select from
2273 : : * the given rel. Increment all that rel's Paths' costs to account for them,
2274 : : * and if any of the initPlans are parallel-unsafe, mark all the rel's Paths
2275 : : * parallel-unsafe as well.
2276 : : *
2277 : : * This is separate from SS_attach_initplans because we might conditionally
2278 : : * create more initPlans during create_plan(), depending on which Path we
2279 : : * select. However, Paths that would generate such initPlans are expected
2280 : : * to have included their cost and parallel-safety effects already.
2281 : : */
2282 : : void
3521 2283 : 260082 : SS_charge_for_initplans(PlannerInfo *root, RelOptInfo *final_rel)
2284 : : {
2285 : : Cost initplan_cost;
2286 : : bool unsafe_initplans;
2287 : : ListCell *lc;
2288 : :
2289 : : /* Nothing to do if no initPlans */
2290 [ + + ]: 260082 : if (root->init_plans == NIL)
2291 : 254162 : return;
2292 : :
2293 : : /*
2294 : : * Compute the cost increment just once, since it will be the same for all
2295 : : * Paths. Also check for parallel-unsafe initPlans.
2296 : : */
837 2297 : 5920 : SS_compute_initplan_cost(root->init_plans,
2298 : : &initplan_cost, &unsafe_initplans);
2299 : :
2300 : : /*
2301 : : * Now adjust the costs and parallel_safe flags.
2302 : : */
3521 2303 [ + - + + : 11927 : foreach(lc, final_rel->pathlist)
+ + ]
2304 : : {
2305 : 6007 : Path *path = (Path *) lfirst(lc);
2306 : :
2307 : 6007 : path->startup_cost += initplan_cost;
2308 : 6007 : path->total_cost += initplan_cost;
837 2309 [ + + ]: 6007 : if (unsafe_initplans)
2310 : 3555 : path->parallel_safe = false;
2311 : : }
2312 : :
2313 : : /*
2314 : : * Adjust partial paths' costs too, or forget them entirely if we must
2315 : : * consider the rel parallel-unsafe.
2316 : : */
2317 [ + + ]: 5920 : if (unsafe_initplans)
2318 : : {
2319 : 3515 : final_rel->partial_pathlist = NIL;
2320 : 3515 : final_rel->consider_parallel = false;
2321 : : }
2322 : : else
2323 : : {
2324 [ + + + + : 2411 : foreach(lc, final_rel->partial_pathlist)
+ + ]
2325 : : {
2326 : 6 : Path *path = (Path *) lfirst(lc);
2327 : :
2328 : 6 : path->startup_cost += initplan_cost;
2329 : 6 : path->total_cost += initplan_cost;
2330 : : }
2331 : : }
2332 : :
2333 : : /* We needn't do set_cheapest() here, caller will do it */
2334 : : }
2335 : :
2336 : : /*
2337 : : * SS_compute_initplan_cost - count up the cost delta for some initplans
2338 : : *
2339 : : * The total cost returned in *initplan_cost_p should be added to both the
2340 : : * startup and total costs of the plan node the initplans get attached to.
2341 : : * We also report whether any of the initplans are not parallel-safe.
2342 : : *
2343 : : * The primary user of this is SS_charge_for_initplans, but it's also
2344 : : * used in adjusting costs when we move initplans to another plan node.
2345 : : */
2346 : : void
2347 : 6042 : SS_compute_initplan_cost(List *init_plans,
2348 : : Cost *initplan_cost_p,
2349 : : bool *unsafe_initplans_p)
2350 : : {
2351 : : Cost initplan_cost;
2352 : : bool unsafe_initplans;
2353 : : ListCell *lc;
2354 : :
2355 : : /*
2356 : : * We assume each initPlan gets run once during top plan startup. This is
2357 : : * a conservative overestimate, since in fact an initPlan might be
2358 : : * executed later than plan startup, or even not at all.
2359 : : */
2360 : 6042 : initplan_cost = 0;
2361 : 6042 : unsafe_initplans = false;
2362 [ + + + + : 12605 : foreach(lc, init_plans)
+ + ]
2363 : : {
2364 : 6563 : SubPlan *initsubplan = lfirst_node(SubPlan, lc);
2365 : :
2366 : 6563 : initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
2367 [ + + ]: 6563 : if (!initsubplan->parallel_safe)
2368 : 4012 : unsafe_initplans = true;
2369 : : }
2370 : 6042 : *initplan_cost_p = initplan_cost;
2371 : 6042 : *unsafe_initplans_p = unsafe_initplans;
2372 : 6042 : }
2373 : :
2374 : : /*
2375 : : * SS_attach_initplans - attach initplans to topmost plan node
2376 : : *
2377 : : * Attach any initplans created in the current query level to the specified
2378 : : * plan node, which should normally be the topmost node for the query level.
2379 : : * (In principle the initPlans could go in any node at or above where they're
2380 : : * referenced; but there seems no reason to put them any lower than the
2381 : : * topmost node, so we don't bother to track exactly where they came from.)
2382 : : *
2383 : : * We do not touch the plan node's cost or parallel_safe flag. The initplans
2384 : : * must have been accounted for in SS_charge_for_initplans, or by any later
2385 : : * code that adds initplans via SS_make_initplan_from_plan.
2386 : : */
2387 : : void
3521 2388 : 259392 : SS_attach_initplans(PlannerInfo *root, Plan *plan)
2389 : : {
2390 : 259392 : plan->initPlan = root->init_plans;
8296 2391 : 259392 : }
2392 : :
2393 : : /*
2394 : : * SS_finalize_plan - do final parameter processing for a completed Plan.
2395 : : *
2396 : : * This recursively computes the extParam and allParam sets for every Plan
2397 : : * node in the given plan tree. (Oh, and RangeTblFunction.funcparams too.)
2398 : : *
2399 : : * We assume that SS_finalize_plan has already been run on any initplans or
2400 : : * subplans the plan tree could reference.
2401 : : */
2402 : : void
3730 2403 : 96523 : SS_finalize_plan(PlannerInfo *root, Plan *plan)
2404 : : {
2405 : : /* No setup needed, just recurse through plan tree. */
2980 2406 : 96523 : (void) finalize_plan(root, plan, -1, root->outer_params, NULL);
3730 2407 : 96523 : }
2408 : :
2409 : : /*
2410 : : * Recursive processing of all nodes in the plan tree
2411 : : *
2412 : : * gather_param is the rescan_param of an ancestral Gather/GatherMerge,
2413 : : * or -1 if there is none.
2414 : : *
2415 : : * valid_params is the set of param IDs supplied by outer plan levels
2416 : : * that are valid to reference in this plan node or its children.
2417 : : *
2418 : : * scan_params is a set of param IDs to force scan plan nodes to reference.
2419 : : * This is for EvalPlanQual support, and is always NULL at the top of the
2420 : : * recursion.
2421 : : *
2422 : : * The return value is the computed allParam set for the given Plan node.
2423 : : * This is just an internal notational convenience: we can add a child
2424 : : * plan's allParams to the set of param IDs of interest to this level
2425 : : * in the same statement that recurses to that child.
2426 : : *
2427 : : * Do not scribble on caller's values of valid_params or scan_params!
2428 : : *
2429 : : * Note: although we attempt to deal with initPlans anywhere in the tree, the
2430 : : * logic is not really right. The problem is that a plan node might return an
2431 : : * output Param of its initPlan as a targetlist item, in which case it's valid
2432 : : * for the parent plan level to reference that same Param; the parent's usage
2433 : : * will be converted into a Var referencing the child plan node by setrefs.c.
2434 : : * But this function would see the parent's reference as out of scope and
2435 : : * complain about it. For now, this does not matter because the planner only
2436 : : * attaches initPlans to the topmost plan node in a query level, so the case
2437 : : * doesn't arise. If we ever merge this processing into setrefs.c, maybe it
2438 : : * can be handled more cleanly.
2439 : : */
2440 : : static Bitmapset *
2980 2441 : 765785 : finalize_plan(PlannerInfo *root, Plan *plan,
2442 : : int gather_param,
2443 : : Bitmapset *valid_params,
2444 : : Bitmapset *scan_params)
2445 : : {
2446 : : finalize_primnode_context context;
2447 : : int locally_added_param;
2448 : : Bitmapset *nestloop_params;
2449 : : Bitmapset *initExtParam;
2450 : : Bitmapset *initSetParam;
2451 : : Bitmapset *child_params;
2452 : : ListCell *l;
2453 : :
10105 bruce@momjian.us 2454 [ + + ]: 765785 : if (plan == NULL)
8296 tgl@sss.pgh.pa.us 2455 : 445492 : return NULL;
2456 : :
6822 2457 : 320293 : context.root = root;
8296 2458 : 320293 : context.paramids = NULL; /* initialize set to empty */
5845 2459 : 320293 : locally_added_param = -1; /* there isn't one */
5586 2460 : 320293 : nestloop_params = NULL; /* there aren't any */
2461 : :
2462 : : /*
2463 : : * Examine any initPlans to determine the set of external params they
2464 : : * reference and the set of output params they supply. (We assume
2465 : : * SS_finalize_plan was run on them already.)
2466 : : */
3730 2467 : 320293 : initExtParam = initSetParam = NULL;
2468 [ + + + + : 327023 : foreach(l, plan->initPlan)
+ + ]
2469 : : {
2470 : 6730 : SubPlan *initsubplan = (SubPlan *) lfirst(l);
2471 : 6730 : Plan *initplan = planner_subplan_get_plan(root, initsubplan);
2472 : : ListCell *l2;
2473 : :
2474 : 6730 : initExtParam = bms_add_members(initExtParam, initplan->extParam);
2475 [ + - + + : 13484 : foreach(l2, initsubplan->setParam)
+ + ]
2476 : : {
2477 : 6754 : initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
2478 : : }
2479 : : }
2480 : :
2481 : : /* Any setParams are validly referenceable in this node and children */
2482 [ + + ]: 320293 : if (initSetParam)
2483 : 6096 : valid_params = bms_union(valid_params, initSetParam);
2484 : :
2485 : : /*
2486 : : * When we call finalize_primnode, context.paramids sets are automatically
2487 : : * merged together. But when recursing to self, we have to do it the hard
2488 : : * way. We want the paramids set to include params in subplans as well as
2489 : : * at this level.
2490 : : */
2491 : :
2492 : : /* Find params in targetlist and qual */
8296 2493 : 320293 : finalize_primnode((Node *) plan->targetlist, &context);
2494 : 320293 : finalize_primnode((Node *) plan->qual, &context);
2495 : :
2496 : : /*
2497 : : * If it's a parallel-aware scan node, mark it as dependent on the parent
2498 : : * Gather/GatherMerge's rescan Param.
2499 : : */
2980 2500 [ + + ]: 320293 : if (plan->parallel_aware)
2501 : : {
2502 [ - + ]: 1301 : if (gather_param < 0)
2980 tgl@sss.pgh.pa.us 2503 [ # # ]:UBC 0 : elog(ERROR, "parallel-aware plan node is not below a Gather");
2980 tgl@sss.pgh.pa.us 2504 :CBC 1301 : context.paramids =
2505 : 1301 : bms_add_member(context.paramids, gather_param);
2506 : : }
2507 : :
2508 : : /* Check additional node-type-specific fields */
10118 vadim4o@yahoo.com 2509 [ + + + + : 320293 : switch (nodeTag(plan))
+ + + + +
+ + + + +
+ + + - +
+ + + + +
+ + + + +
+ + + + +
+ + - ]
2510 : : {
2511 : 34616 : case T_Result:
9478 tgl@sss.pgh.pa.us 2512 : 34616 : finalize_primnode(((Result *) plan)->resconstantqual,
2513 : : &context);
10118 vadim4o@yahoo.com 2514 : 34616 : break;
2515 : :
5845 tgl@sss.pgh.pa.us 2516 : 50507 : case T_SeqScan:
3747 2517 : 50507 : context.paramids = bms_add_members(context.paramids, scan_params);
2518 : 50507 : break;
2519 : :
3818 simon@2ndQuadrant.co 2520 : 52 : case T_SampleScan:
3747 tgl@sss.pgh.pa.us 2521 : 52 : finalize_primnode((Node *) ((SampleScan *) plan)->tablesample,
2522 : : &context);
5845 2523 : 52 : context.paramids = bms_add_members(context.paramids, scan_params);
2524 : 52 : break;
2525 : :
8563 2526 : 49723 : case T_IndexScan:
7490 2527 : 49723 : finalize_primnode((Node *) ((IndexScan *) plan)->indexqual,
2528 : : &context);
5443 2529 : 49723 : finalize_primnode((Node *) ((IndexScan *) plan)->indexorderby,
2530 : : &context);
2531 : :
2532 : : /*
2533 : : * we need not look at indexqualorig, since it will have the same
2534 : : * param references as indexqual. Likewise, we can ignore
2535 : : * indexorderbyorig.
2536 : : */
5845 2537 : 49723 : context.paramids = bms_add_members(context.paramids, scan_params);
8563 2538 : 49723 : break;
2539 : :
5130 2540 : 4715 : case T_IndexOnlyScan:
2541 : 4715 : finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexqual,
2542 : : &context);
1393 2543 : 4715 : finalize_primnode((Node *) ((IndexOnlyScan *) plan)->recheckqual,
2544 : : &context);
5130 2545 : 4715 : finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexorderby,
2546 : : &context);
2547 : :
2548 : : /*
2549 : : * we need not look at indextlist, since it cannot contain Params.
2550 : : */
2551 : 4715 : context.paramids = bms_add_members(context.paramids, scan_params);
2552 : 4715 : break;
2553 : :
7496 2554 : 3580 : case T_BitmapIndexScan:
7490 2555 : 3580 : finalize_primnode((Node *) ((BitmapIndexScan *) plan)->indexqual,
2556 : : &context);
2557 : :
2558 : : /*
2559 : : * we need not look at indexqualorig, since it will have the same
2560 : : * param references as indexqual.
2561 : : */
7496 2562 : 3580 : break;
2563 : :
2564 : 3382 : case T_BitmapHeapScan:
2565 : 3382 : finalize_primnode((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig,
2566 : : &context);
5845 2567 : 3382 : context.paramids = bms_add_members(context.paramids, scan_params);
7496 2568 : 3382 : break;
2569 : :
8563 2570 : 312 : case T_TidScan:
7275 2571 : 312 : finalize_primnode((Node *) ((TidScan *) plan)->tidquals,
2572 : : &context);
5845 2573 : 312 : context.paramids = bms_add_members(context.paramids, scan_params);
10118 vadim4o@yahoo.com 2574 : 312 : break;
2575 : :
1703 drowley@postgresql.o 2576 : 19 : case T_TidRangeScan:
2577 : 19 : finalize_primnode((Node *) ((TidRangeScan *) plan)->tidrangequals,
2578 : : &context);
2579 : 19 : context.paramids = bms_add_members(context.paramids, scan_params);
2580 : 19 : break;
2581 : :
9153 tgl@sss.pgh.pa.us 2582 : 12446 : case T_SubqueryScan:
2583 : : {
3730 2584 : 12446 : SubqueryScan *sscan = (SubqueryScan *) plan;
2585 : : RelOptInfo *rel;
2586 : : Bitmapset *subquery_params;
2587 : :
2588 : : /* We must run finalize_plan on the subquery */
2589 : 12446 : rel = find_base_rel(root, sscan->scan.scanrelid);
2785 rhaas@postgresql.org 2590 : 12446 : subquery_params = rel->subroot->outer_params;
2591 [ + + ]: 12446 : if (gather_param >= 0)
2592 : 12 : subquery_params = bms_add_member(bms_copy(subquery_params),
2593 : : gather_param);
2594 : 12446 : finalize_plan(rel->subroot, sscan->subplan, gather_param,
2595 : : subquery_params, NULL);
2596 : :
2597 : : /* Now we can add its extParams to the parent's params */
3730 tgl@sss.pgh.pa.us 2598 : 24892 : context.paramids = bms_add_members(context.paramids,
2599 : 12446 : sscan->subplan->extParam);
2600 : : /* We need scan_params too, though */
2601 : 12446 : context.paramids = bms_add_members(context.paramids,
2602 : : scan_params);
2603 : : }
9153 2604 : 12446 : break;
2605 : :
8563 2606 : 12156 : case T_FunctionScan:
2607 : : {
4358 2608 : 12156 : FunctionScan *fscan = (FunctionScan *) plan;
2609 : : ListCell *lc;
2610 : :
2611 : : /*
2612 : : * Call finalize_primnode independently on each function
2613 : : * expression, so that we can record which params are
2614 : : * referenced in each, in order to decide which need
2615 : : * re-evaluating during rescan.
2616 : : */
2617 [ + - + + : 24421 : foreach(lc, fscan->functions)
+ + ]
2618 : : {
2619 : 12265 : RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2620 : : finalize_primnode_context funccontext;
2621 : :
2622 : 12265 : funccontext = context;
2623 : 12265 : funccontext.paramids = NULL;
2624 : :
2625 : 12265 : finalize_primnode(rtfunc->funcexpr, &funccontext);
2626 : :
2627 : : /* remember results for execution */
2628 : 12265 : rtfunc->funcparams = funccontext.paramids;
2629 : :
2630 : : /* add the function's params to the overall set */
2631 : 12265 : context.paramids = bms_add_members(context.paramids,
2632 : 12265 : funccontext.paramids);
2633 : : }
2634 : :
2635 : 12156 : context.paramids = bms_add_members(context.paramids,
2636 : : scan_params);
2637 : : }
8563 2638 : 12156 : break;
2639 : :
3155 alvherre@alvh.no-ip. 2640 : 117 : case T_TableFuncScan:
2641 : 117 : finalize_primnode((Node *) ((TableFuncScan *) plan)->tablefunc,
2642 : : &context);
2643 : 117 : context.paramids = bms_add_members(context.paramids, scan_params);
2644 : 117 : break;
2645 : :
7026 mail@joeconway.com 2646 : 2874 : case T_ValuesScan:
6825 tgl@sss.pgh.pa.us 2647 : 2874 : finalize_primnode((Node *) ((ValuesScan *) plan)->values_lists,
2648 : : &context);
5845 2649 : 2874 : context.paramids = bms_add_members(context.paramids, scan_params);
7026 mail@joeconway.com 2650 : 2874 : break;
2651 : :
6232 tgl@sss.pgh.pa.us 2652 : 2120 : case T_CteScan:
2653 : : {
2654 : : /*
2655 : : * You might think we should add the node's cteParam to
2656 : : * paramids, but we shouldn't because that param is just a
2657 : : * linkage mechanism for multiple CteScan nodes for the same
2658 : : * CTE; it is never used for changed-param signaling. What we
2659 : : * have to do instead is to find the referenced CTE plan and
2660 : : * incorporate its external paramids, so that the correct
2661 : : * things will happen if the CTE references outer-level
2662 : : * variables. See test cases for bug #4902. (We assume
2663 : : * SS_finalize_plan was run on the CTE plan already.)
2664 : : */
5722 bruce@momjian.us 2665 : 2120 : int plan_id = ((CteScan *) plan)->ctePlanId;
2666 : : Plan *cteplan;
2667 : :
2668 : : /* so, do this ... */
5957 tgl@sss.pgh.pa.us 2669 [ + - - + ]: 2120 : if (plan_id < 1 || plan_id > list_length(root->glob->subplans))
5957 tgl@sss.pgh.pa.us 2670 [ # # ]:UBC 0 : elog(ERROR, "could not find plan for CteScan referencing plan ID %d",
2671 : : plan_id);
5957 tgl@sss.pgh.pa.us 2672 :CBC 2120 : cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2673 : 2120 : context.paramids =
2674 : 2120 : bms_add_members(context.paramids, cteplan->extParam);
2675 : :
2676 : : #ifdef NOT_USED
2677 : : /* ... but not this */
2678 : : context.paramids =
2679 : : bms_add_member(context.paramids,
2680 : : ((CteScan *) plan)->cteParam);
2681 : : #endif
2682 : :
5845 2683 : 2120 : context.paramids = bms_add_members(context.paramids,
2684 : : scan_params);
2685 : : }
6232 2686 : 2120 : break;
2687 : :
2688 : 467 : case T_WorkTableScan:
2689 : 467 : context.paramids =
2690 : 467 : bms_add_member(context.paramids,
2691 : : ((WorkTableScan *) plan)->wtParam);
5845 2692 : 467 : context.paramids = bms_add_members(context.paramids, scan_params);
6232 2693 : 467 : break;
2694 : :
3132 kgrittn@postgresql.o 2695 : 207 : case T_NamedTuplestoreScan:
2696 : 207 : context.paramids = bms_add_members(context.paramids, scan_params);
2697 : 207 : break;
2698 : :
5363 tgl@sss.pgh.pa.us 2699 : 412 : case T_ForeignScan:
2700 : : {
3665 rhaas@postgresql.org 2701 : 412 : ForeignScan *fscan = (ForeignScan *) plan;
2702 : :
2703 : 412 : finalize_primnode((Node *) fscan->fdw_exprs,
2704 : : &context);
2705 : 412 : finalize_primnode((Node *) fscan->fdw_recheck_quals,
2706 : : &context);
2707 : :
2708 : : /* We assume fdw_scan_tlist cannot contain Params */
2709 : 412 : context.paramids = bms_add_members(context.paramids,
2710 : : scan_params);
2711 : : }
5363 tgl@sss.pgh.pa.us 2712 : 412 : break;
2713 : :
4007 rhaas@postgresql.org 2714 :UBC 0 : case T_CustomScan:
2715 : : {
3776 2716 : 0 : CustomScan *cscan = (CustomScan *) plan;
2717 : : ListCell *lc;
2718 : :
2719 : 0 : finalize_primnode((Node *) cscan->custom_exprs,
2720 : : &context);
2721 : : /* We assume custom_scan_tlist cannot contain Params */
2722 : 0 : context.paramids =
2723 : 0 : bms_add_members(context.paramids, scan_params);
2724 : :
2725 : : /* child nodes if any */
3747 tgl@sss.pgh.pa.us 2726 [ # # # # : 0 : foreach(lc, cscan->custom_plans)
# # ]
2727 : : {
3776 rhaas@postgresql.org 2728 : 0 : context.paramids =
2729 : 0 : bms_add_members(context.paramids,
2730 : 0 : finalize_plan(root,
2731 : 0 : (Plan *) lfirst(lc),
2732 : : gather_param,
2733 : : valid_params,
2734 : : scan_params));
2735 : : }
2736 : : }
4007 2737 : 0 : break;
2738 : :
5861 tgl@sss.pgh.pa.us 2739 :CBC 41950 : case T_ModifyTable:
2740 : : {
5845 2741 : 41950 : ModifyTable *mtplan = (ModifyTable *) plan;
2742 : :
2743 : : /* Force descendant scan nodes to reference epqParam */
2744 : 41950 : locally_added_param = mtplan->epqParam;
2745 : 41950 : valid_params = bms_add_member(bms_copy(valid_params),
2746 : : locally_added_param);
2747 : 41950 : scan_params = bms_add_member(bms_copy(scan_params),
2748 : : locally_added_param);
2749 : 41950 : finalize_primnode((Node *) mtplan->returningLists,
2750 : : &context);
3825 andres@anarazel.de 2751 : 41950 : finalize_primnode((Node *) mtplan->onConflictSet,
2752 : : &context);
2753 : 41950 : finalize_primnode((Node *) mtplan->onConflictWhere,
2754 : : &context);
2755 : : /* exclRelTlist contains only Vars, doesn't need examination */
2756 : : }
5861 tgl@sss.pgh.pa.us 2757 : 41950 : break;
2758 : :
8563 2759 : 5573 : case T_Append:
2760 : : {
7824 neilc@samurai.com 2761 [ + - + + : 21182 : foreach(l, ((Append *) plan)->appendplans)
+ + ]
2762 : : {
2763 : 15609 : context.paramids =
2764 : 15609 : bms_add_members(context.paramids,
6822 tgl@sss.pgh.pa.us 2765 : 15609 : finalize_plan(root,
2766 : 15609 : (Plan *) lfirst(l),
2767 : : gather_param,
2768 : : valid_params,
2769 : : scan_params));
2770 : : }
2771 : : }
10118 vadim4o@yahoo.com 2772 : 5573 : break;
2773 : :
5492 tgl@sss.pgh.pa.us 2774 : 72 : case T_MergeAppend:
2775 : : {
2776 [ + - + + : 297 : foreach(l, ((MergeAppend *) plan)->mergeplans)
+ + ]
2777 : : {
2778 : 225 : context.paramids =
2779 : 225 : bms_add_members(context.paramids,
2780 : 225 : finalize_plan(root,
2781 : 225 : (Plan *) lfirst(l),
2782 : : gather_param,
2783 : : valid_params,
2784 : : scan_params));
2785 : : }
2786 : : }
2787 : 72 : break;
2788 : :
7496 2789 : 85 : case T_BitmapAnd:
2790 : : {
2791 [ + - + + : 255 : foreach(l, ((BitmapAnd *) plan)->bitmapplans)
+ + ]
2792 : : {
2793 : 170 : context.paramids =
2794 : 170 : bms_add_members(context.paramids,
6822 2795 : 170 : finalize_plan(root,
2796 : 170 : (Plan *) lfirst(l),
2797 : : gather_param,
2798 : : valid_params,
2799 : : scan_params));
2800 : : }
2801 : : }
7496 2802 : 85 : break;
2803 : :
2804 : 113 : case T_BitmapOr:
2805 : : {
2806 [ + - + + : 339 : foreach(l, ((BitmapOr *) plan)->bitmapplans)
+ + ]
2807 : : {
2808 : 226 : context.paramids =
2809 : 226 : bms_add_members(context.paramids,
6822 2810 : 226 : finalize_plan(root,
2811 : 226 : (Plan *) lfirst(l),
2812 : : gather_param,
2813 : : valid_params,
2814 : : scan_params));
2815 : : }
2816 : : }
7496 2817 : 113 : break;
2818 : :
9176 2819 : 36599 : case T_NestLoop:
2820 : : {
5586 2821 : 36599 : finalize_primnode((Node *) ((Join *) plan)->joinqual,
2822 : : &context);
2823 : : /* collect set of params that will be passed to right child */
2824 [ + + + + : 64374 : foreach(l, ((NestLoop *) plan)->nestParams)
+ + ]
2825 : : {
2826 : 27775 : NestLoopParam *nlp = (NestLoopParam *) lfirst(l);
2827 : :
2828 : 27775 : nestloop_params = bms_add_member(nestloop_params,
2829 : : nlp->paramno);
2830 : : }
2831 : : }
9176 2832 : 36599 : break;
2833 : :
10118 vadim4o@yahoo.com 2834 : 2052 : case T_MergeJoin:
9176 tgl@sss.pgh.pa.us 2835 : 2052 : finalize_primnode((Node *) ((Join *) plan)->joinqual,
2836 : : &context);
9478 2837 : 2052 : finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
2838 : : &context);
10118 vadim4o@yahoo.com 2839 : 2052 : break;
2840 : :
2841 : 10402 : case T_HashJoin:
9176 tgl@sss.pgh.pa.us 2842 : 10402 : finalize_primnode((Node *) ((Join *) plan)->joinqual,
2843 : : &context);
9478 2844 : 10402 : finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
2845 : : &context);
10118 vadim4o@yahoo.com 2846 : 10402 : break;
2847 : :
860 tgl@sss.pgh.pa.us 2848 : 10402 : case T_Hash:
2849 : 10402 : finalize_primnode((Node *) ((Hash *) plan)->hashkeys,
2850 : : &context);
2851 : 10402 : break;
2852 : :
7839 2853 : 1148 : case T_Limit:
2854 : 1148 : finalize_primnode(((Limit *) plan)->limitOffset,
2855 : : &context);
2856 : 1148 : finalize_primnode(((Limit *) plan)->limitCount,
2857 : : &context);
2858 : 1148 : break;
2859 : :
6232 2860 : 467 : case T_RecursiveUnion:
2861 : : /* child nodes are allowed to reference wtParam */
5845 2862 : 467 : locally_added_param = ((RecursiveUnion *) plan)->wtParam;
2863 : 467 : valid_params = bms_add_member(bms_copy(valid_params),
2864 : : locally_added_param);
2865 : : /* wtParam does *not* get added to scan_params */
2866 : 467 : break;
2867 : :
2868 : 3927 : case T_LockRows:
2869 : : /* Force descendant scan nodes to reference epqParam */
2870 : 3927 : locally_added_param = ((LockRows *) plan)->epqParam;
2871 : 3927 : valid_params = bms_add_member(bms_copy(valid_params),
2872 : : locally_added_param);
2873 : 3927 : scan_params = bms_add_member(bms_copy(scan_params),
2874 : : locally_added_param);
2875 : 3927 : break;
2876 : :
3351 2877 : 5650 : case T_Agg:
2878 : : {
2879 : 5650 : Agg *agg = (Agg *) plan;
2880 : :
2881 : : /*
2882 : : * AGG_HASHED plans need to know which Params are referenced
2883 : : * in aggregate calls. Do a separate scan to identify them.
2884 : : */
2885 [ + + ]: 5650 : if (agg->aggstrategy == AGG_HASHED)
2886 : : {
2887 : : finalize_primnode_context aggcontext;
2888 : :
2889 : 959 : aggcontext.root = root;
2890 : 959 : aggcontext.paramids = NULL;
2891 : 959 : finalize_agg_primnode((Node *) agg->plan.targetlist,
2892 : : &aggcontext);
2893 : 959 : finalize_agg_primnode((Node *) agg->plan.qual,
2894 : : &aggcontext);
2895 : 959 : agg->aggParams = aggcontext.paramids;
2896 : : }
2897 : : }
2898 : 5650 : break;
2899 : :
5736 2900 : 61 : case T_WindowAgg:
2901 : 61 : finalize_primnode(((WindowAgg *) plan)->startOffset,
2902 : : &context);
2903 : 61 : finalize_primnode(((WindowAgg *) plan)->endOffset,
2904 : : &context);
2905 : 61 : break;
2906 : :
2980 2907 : 479 : case T_Gather:
2908 : : /* child nodes are allowed to reference rescan_param, if any */
2909 : 479 : locally_added_param = ((Gather *) plan)->rescan_param;
2910 [ + + ]: 479 : if (locally_added_param >= 0)
2911 : : {
2912 : 476 : valid_params = bms_add_member(bms_copy(valid_params),
2913 : : locally_added_param);
2914 : :
2915 : : /*
2916 : : * We currently don't support nested Gathers. The issue so
2917 : : * far as this function is concerned would be how to identify
2918 : : * which child nodes depend on which Gather.
2919 : : */
2920 [ - + ]: 476 : Assert(gather_param < 0);
2921 : : /* Pass down rescan_param to child parallel-aware nodes */
2922 : 476 : gather_param = locally_added_param;
2923 : : }
2924 : : /* rescan_param does *not* get added to scan_params */
2925 : 479 : break;
2926 : :
2927 : 177 : case T_GatherMerge:
2928 : : /* child nodes are allowed to reference rescan_param, if any */
2929 : 177 : locally_added_param = ((GatherMerge *) plan)->rescan_param;
2930 [ + - ]: 177 : if (locally_added_param >= 0)
2931 : : {
2932 : 177 : valid_params = bms_add_member(bms_copy(valid_params),
2933 : : locally_added_param);
2934 : :
2935 : : /*
2936 : : * We currently don't support nested Gathers. The issue so
2937 : : * far as this function is concerned would be how to identify
2938 : : * which child nodes depend on which Gather.
2939 : : */
2940 [ - + ]: 177 : Assert(gather_param < 0);
2941 : : /* Pass down rescan_param to child parallel-aware nodes */
2942 : 177 : gather_param = locally_added_param;
2943 : : }
2944 : : /* rescan_param does *not* get added to scan_params */
2945 : 177 : break;
2946 : :
1566 drowley@postgresql.o 2947 : 1018 : case T_Memoize:
2948 : 1018 : finalize_primnode((Node *) ((Memoize *) plan)->param_exprs,
2949 : : &context);
1669 2950 : 1018 : break;
2951 : :
3204 andres@anarazel.de 2952 : 22413 : case T_ProjectSet:
2953 : : case T_Material:
2954 : : case T_Sort:
2955 : : case T_IncrementalSort:
2956 : : case T_Unique:
2957 : : case T_SetOp:
2958 : : case T_Group:
2959 : : /* no node-type-specific fields need fixing */
10118 vadim4o@yahoo.com 2960 : 22413 : break;
2961 : :
10118 vadim4o@yahoo.com 2962 :UBC 0 : default:
8130 tgl@sss.pgh.pa.us 2963 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
2964 : : (int) nodeTag(plan));
2965 : : }
2966 : :
2967 : : /* Process left and right child plans, if any */
5586 tgl@sss.pgh.pa.us 2968 :CBC 320293 : child_params = finalize_plan(root,
2969 : 320293 : plan->lefttree,
2970 : : gather_param,
2971 : : valid_params,
2972 : : scan_params);
2973 : 320293 : context.paramids = bms_add_members(context.paramids, child_params);
2974 : :
2975 [ + + ]: 320293 : if (nestloop_params)
2976 : : {
2977 : : /* right child can reference nestloop_params as well as valid_params */
2978 : 24510 : child_params = finalize_plan(root,
2979 : 24510 : plan->righttree,
2980 : : gather_param,
2981 : : bms_union(nestloop_params, valid_params),
2982 : : scan_params);
2983 : : /* ... and they don't count as parameters used at my level */
2984 : 24510 : child_params = bms_difference(child_params, nestloop_params);
2985 : 24510 : bms_free(nestloop_params);
2986 : : }
2987 : : else
2988 : : {
2989 : : /* easy case */
2990 : 295783 : child_params = finalize_plan(root,
2991 : 295783 : plan->righttree,
2992 : : gather_param,
2993 : : valid_params,
2994 : : scan_params);
2995 : : }
2996 : 320293 : context.paramids = bms_add_members(context.paramids, child_params);
2997 : :
2998 : : /*
2999 : : * Any locally generated parameter doesn't count towards its generating
3000 : : * plan node's external dependencies. (Note: if we changed valid_params
3001 : : * and/or scan_params, we leak those bitmapsets; not worth the notational
3002 : : * trouble to clean them up.)
3003 : : */
5845 3004 [ + + ]: 320293 : if (locally_added_param >= 0)
3005 : : {
6232 3006 : 46997 : context.paramids = bms_del_member(context.paramids,
3007 : : locally_added_param);
3008 : : }
3009 : :
3010 : : /* Now we have all the paramids referenced in this node and children */
3011 : :
8296 3012 [ - + ]: 320293 : if (!bms_is_subset(context.paramids, valid_params))
8130 tgl@sss.pgh.pa.us 3013 [ # # ]:UBC 0 : elog(ERROR, "plan should not reference subplan's variable");
3014 : :
3015 : : /*
3016 : : * The plan node's allParam and extParam fields should include all its
3017 : : * referenced paramids, plus contributions from any child initPlans.
3018 : : * However, any setParams of the initPlans should not be present in the
3019 : : * parent node's extParams, only in its allParams. (It's possible that
3020 : : * some initPlans have extParams that are setParams of other initPlans.)
3021 : : */
3022 : :
3023 : : /* allParam must include initplans' extParams and setParams */
3730 tgl@sss.pgh.pa.us 3024 :CBC 320293 : plan->allParam = bms_union(context.paramids, initExtParam);
3025 : 320293 : plan->allParam = bms_add_members(plan->allParam, initSetParam);
3026 : : /* extParam must include any initplan extParams */
3027 : 320293 : plan->extParam = bms_union(context.paramids, initExtParam);
3028 : : /* but not any initplan setParams */
3029 : 320293 : plan->extParam = bms_del_members(plan->extParam, initSetParam);
3030 : :
8296 3031 : 320293 : return plan->allParam;
3032 : : }
3033 : :
3034 : : /*
3035 : : * finalize_primnode: add IDs of all PARAM_EXEC params that appear (or will
3036 : : * appear) in the given expression tree to the result set.
3037 : : */
3038 : : static bool
8116 bruce@momjian.us 3039 : 5378553 : finalize_primnode(Node *node, finalize_primnode_context *context)
3040 : : {
8353 tgl@sss.pgh.pa.us 3041 [ + + ]: 5378553 : if (node == NULL)
3042 : 620958 : return false;
3043 [ + + ]: 4757595 : if (IsA(node, Param))
3044 : : {
3045 [ + + ]: 67917 : if (((Param *) node)->paramkind == PARAM_EXEC)
3046 : : {
7128 3047 : 66751 : int paramid = ((Param *) node)->paramid;
3048 : :
8296 3049 : 66751 : context->paramids = bms_add_member(context->paramids, paramid);
3050 : : }
8353 3051 : 67917 : return false; /* no more to do here */
3052 : : }
837 3053 [ + + ]: 4689678 : else if (IsA(node, Aggref))
3054 : : {
3055 : : /*
3056 : : * Check to see if the aggregate will be replaced by a Param
3057 : : * referencing a subquery output during setrefs.c. If so, we must
3058 : : * account for that Param here. (For various reasons, it's not
3059 : : * convenient to perform that substitution earlier than setrefs.c, nor
3060 : : * to perform this processing after setrefs.c. Thus we need a wart
3061 : : * here.)
3062 : : */
3063 : 7774 : Aggref *aggref = (Aggref *) node;
3064 : : Param *aggparam;
3065 : :
3066 : 7774 : aggparam = find_minmax_agg_replacement_param(context->root, aggref);
3067 [ + + ]: 7774 : if (aggparam != NULL)
3068 : 278 : context->paramids = bms_add_member(context->paramids,
3069 : : aggparam->paramid);
3070 : : /* Fall through to examine the agg's arguments */
3071 : : }
3072 [ + + ]: 4681904 : else if (IsA(node, SubPlan))
3073 : : {
8120 bruce@momjian.us 3074 : 18206 : SubPlan *subplan = (SubPlan *) node;
6822 tgl@sss.pgh.pa.us 3075 : 18206 : Plan *plan = planner_subplan_get_plan(context->root, subplan);
3076 : : ListCell *lc;
3077 : : Bitmapset *subparamids;
3078 : :
3079 : : /* Recurse into the testexpr, but not into the Plan */
6318 3080 : 18206 : finalize_primnode(subplan->testexpr, context);
3081 : :
3082 : : /*
3083 : : * Remove any param IDs of output parameters of the subplan that were
3084 : : * referenced in the testexpr. These are not interesting for
3085 : : * parameter change signaling since we always re-evaluate the subplan.
3086 : : * Note that this wouldn't work too well if there might be uses of the
3087 : : * same param IDs elsewhere in the plan, but that can't happen because
3088 : : * generate_new_exec_param never tries to merge params.
3089 : : */
3090 [ + + + + : 19824 : foreach(lc, subplan->paramIds)
+ + ]
3091 : : {
3092 : 1618 : context->paramids = bms_del_member(context->paramids,
3093 : : lfirst_int(lc));
3094 : : }
3095 : :
3096 : : /* Also examine args list */
3097 : 18206 : finalize_primnode((Node *) subplan->args, context);
3098 : :
3099 : : /*
3100 : : * Add params needed by the subplan to paramids, but excluding those
3101 : : * we will pass down to it. (We assume SS_finalize_plan was run on
3102 : : * the subplan already.)
3103 : : */
3104 : 18206 : subparamids = bms_copy(plan->extParam);
3105 [ + + + + : 44070 : foreach(lc, subplan->parParam)
+ + ]
3106 : : {
3107 : 25864 : subparamids = bms_del_member(subparamids, lfirst_int(lc));
3108 : : }
3109 : 18206 : context->paramids = bms_join(context->paramids, subparamids);
3110 : :
3111 : 18206 : return false; /* no more to do here */
3112 : : }
333 peter@eisentraut.org 3113 : 4671472 : return expression_tree_walker(node, finalize_primnode, context);
3114 : : }
3115 : :
3116 : : /*
3117 : : * finalize_agg_primnode: find all Aggref nodes in the given expression tree,
3118 : : * and add IDs of all PARAM_EXEC params appearing within their aggregated
3119 : : * arguments to the result set.
3120 : : */
3121 : : static bool
3351 tgl@sss.pgh.pa.us 3122 : 8121 : finalize_agg_primnode(Node *node, finalize_primnode_context *context)
3123 : : {
3124 [ + + ]: 8121 : if (node == NULL)
3125 : 998 : return false;
3126 [ + + ]: 7123 : if (IsA(node, Aggref))
3127 : : {
3128 : 581 : Aggref *agg = (Aggref *) node;
3129 : :
3130 : : /* we should not consider the direct arguments, if any */
3131 : 581 : finalize_primnode((Node *) agg->args, context);
3132 : 581 : finalize_primnode((Node *) agg->aggfilter, context);
3133 : 581 : return false; /* there can't be any Aggrefs below here */
3134 : : }
333 peter@eisentraut.org 3135 : 6542 : return expression_tree_walker(node, finalize_agg_primnode, context);
3136 : : }
3137 : :
3138 : : /*
3139 : : * SS_make_initplan_output_param - make a Param for an initPlan's output
3140 : : *
3141 : : * The plan is expected to return a scalar value of the given type/collation.
3142 : : *
3143 : : * Note that in some cases the initplan may not ever appear in the finished
3144 : : * plan tree. If that happens, we'll have wasted a PARAM_EXEC slot, which
3145 : : * is no big deal.
3146 : : */
3147 : : Param *
3521 tgl@sss.pgh.pa.us 3148 : 223 : SS_make_initplan_output_param(PlannerInfo *root,
3149 : : Oid resulttype, int32 resulttypmod,
3150 : : Oid resultcollation)
3151 : : {
2481 3152 : 223 : return generate_new_exec_param(root, resulttype,
3153 : : resulttypmod, resultcollation);
3154 : : }
3155 : :
3156 : : /*
3157 : : * SS_make_initplan_from_plan - given a plan tree, make it an InitPlan
3158 : : *
3159 : : * We build an EXPR_SUBLINK SubPlan node and put it into the initplan
3160 : : * list for the outer query level. A Param that represents the initplan's
3161 : : * output has already been assigned using SS_make_initplan_output_param.
3162 : : */
3163 : : void
3730 3164 : 200 : SS_make_initplan_from_plan(PlannerInfo *root,
3165 : : PlannerInfo *subroot, Plan *plan,
3166 : : Param *prm)
3167 : : {
3168 : : SubPlan *node;
3169 : :
3170 : : /*
3171 : : * Add the subplan and its PlannerInfo, as well as a dummy path entry, to
3172 : : * the global lists. Ideally we'd save a real path, but right now our
3173 : : * sole caller doesn't build a path that exactly matches the plan. Since
3174 : : * we're not currently going to need the path for an initplan, it's not
3175 : : * worth requiring construction of such a path.
3176 : : */
5168 3177 : 200 : root->glob->subplans = lappend(root->glob->subplans, plan);
580 3178 : 200 : root->glob->subpaths = lappend(root->glob->subpaths, NULL);
3730 3179 : 200 : root->glob->subroots = lappend(root->glob->subroots, subroot);
3180 : :
3181 : : /*
3182 : : * Create a SubPlan node and add it to the outer list of InitPlans. Note
3183 : : * it has to appear after any other InitPlans it might depend on (see
3184 : : * comments in ExecReScan).
3185 : : */
7504 3186 : 200 : node = makeNode(SubPlan);
3187 : 200 : node->subLinkType = EXPR_SUBLINK;
3521 3188 : 200 : node->plan_id = list_length(root->glob->subplans);
20 rhaas@postgresql.org 3189 :GNC 200 : node->plan_name = subroot->plan_name;
3190 : 200 : node->isInitPlan = true;
5336 tgl@sss.pgh.pa.us 3191 :CBC 200 : get_first_col_type(plan, &node->firstColType, &node->firstColTypmod,
3192 : : &node->firstColCollation);
837 3193 : 200 : node->parallel_safe = plan->parallel_safe;
3521 3194 : 200 : node->setParam = list_make1_int(prm->paramid);
3195 : :
6825 3196 : 200 : root->init_plans = lappend(root->init_plans, node);
3197 : :
3198 : : /*
3199 : : * The node can't have any inputs (since it's an initplan), so the
3200 : : * parParam and args lists remain empty.
3201 : : */
3202 : :
3203 : : /* Set costs of SubPlan using info from the plan tree */
3730 3204 : 200 : cost_subplan(subroot, node, plan);
7504 3205 : 200 : }
3206 : :
3207 : : /*
3208 : : * Get a string equivalent of a given subLinkType.
3209 : : */
3210 : : static const char *
20 rhaas@postgresql.org 3211 :GNC 19694 : sublinktype_to_string(SubLinkType subLinkType)
3212 : : {
3213 [ + + + + : 19694 : switch (subLinkType)
+ + + -
- ]
3214 : : {
3215 : 1233 : case EXISTS_SUBLINK:
3216 : 1233 : return "exists";
3217 : 9 : case ALL_SUBLINK:
3218 : 9 : return "all";
3219 : 273 : case ANY_SUBLINK:
3220 : 273 : return "any";
3221 : 15 : case ROWCOMPARE_SUBLINK:
3222 : 15 : return "rowcompare";
3223 : 13989 : case EXPR_SUBLINK:
3224 : 13989 : return "expr";
3225 : 66 : case MULTIEXPR_SUBLINK:
3226 : 66 : return "multiexpr";
3227 : 4109 : case ARRAY_SUBLINK:
3228 : 4109 : return "array";
20 rhaas@postgresql.org 3229 :UNC 0 : case CTE_SUBLINK:
3230 : 0 : return "cte";
3231 : : }
3232 : 0 : Assert(false);
3233 : : return "???";
3234 : : }
|