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