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