Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * allpaths.c
4 : : * Routines to find possible search paths for processing a query
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/optimizer/path/allpaths.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : :
16 : : #include "postgres.h"
17 : :
18 : : #include <limits.h>
19 : : #include <math.h>
20 : :
21 : : #include "access/sysattr.h"
22 : : #include "access/tsmapi.h"
23 : : #include "catalog/pg_class.h"
24 : : #include "catalog/pg_operator.h"
25 : : #include "catalog/pg_proc.h"
26 : : #include "foreign/fdwapi.h"
27 : : #include "miscadmin.h"
28 : : #include "nodes/makefuncs.h"
29 : : #include "nodes/nodeFuncs.h"
30 : : #include "nodes/supportnodes.h"
31 : : #ifdef OPTIMIZER_DEBUG
32 : : #include "nodes/print.h"
33 : : #endif
34 : : #include "optimizer/appendinfo.h"
35 : : #include "optimizer/clauses.h"
36 : : #include "optimizer/cost.h"
37 : : #include "optimizer/geqo.h"
38 : : #include "optimizer/optimizer.h"
39 : : #include "optimizer/pathnode.h"
40 : : #include "optimizer/paths.h"
41 : : #include "optimizer/plancat.h"
42 : : #include "optimizer/planner.h"
43 : : #include "optimizer/prep.h"
44 : : #include "optimizer/tlist.h"
45 : : #include "parser/parse_clause.h"
46 : : #include "parser/parsetree.h"
47 : : #include "partitioning/partbounds.h"
48 : : #include "port/pg_bitutils.h"
49 : : #include "rewrite/rewriteManip.h"
50 : : #include "utils/lsyscache.h"
51 : : #include "utils/selfuncs.h"
52 : :
53 : :
54 : : /* Bitmask flags for pushdown_safety_info.unsafeFlags */
55 : : #define UNSAFE_HAS_VOLATILE_FUNC (1 << 0)
56 : : #define UNSAFE_HAS_SET_FUNC (1 << 1)
57 : : #define UNSAFE_NOTIN_DISTINCTON_CLAUSE (1 << 2)
58 : : #define UNSAFE_NOTIN_PARTITIONBY_CLAUSE (1 << 3)
59 : : #define UNSAFE_TYPE_MISMATCH (1 << 4)
60 : :
61 : : /* results of subquery_is_pushdown_safe */
62 : : typedef struct pushdown_safety_info
63 : : {
64 : : unsigned char *unsafeFlags; /* bitmask of reasons why this target list
65 : : * column is unsafe for qual pushdown, or 0 if
66 : : * no reason. */
67 : : bool unsafeVolatile; /* don't push down volatile quals */
68 : : bool unsafeLeaky; /* don't push down leaky quals */
69 : : } pushdown_safety_info;
70 : :
71 : : /* Return type for qual_is_pushdown_safe */
72 : : typedef enum pushdown_safe_type
73 : : {
74 : : PUSHDOWN_UNSAFE, /* unsafe to push qual into subquery */
75 : : PUSHDOWN_SAFE, /* safe to push qual into subquery */
76 : : PUSHDOWN_WINDOWCLAUSE_RUNCOND, /* unsafe, but may work as WindowClause
77 : : * run condition */
78 : : } pushdown_safe_type;
79 : :
80 : : /* These parameters are set by GUC */
81 : : bool enable_geqo = false; /* just in case GUC doesn't set it */
82 : : bool enable_eager_aggregate = true;
83 : : int geqo_threshold;
84 : : double min_eager_agg_group_size;
85 : : int min_parallel_table_scan_size;
86 : : int min_parallel_index_scan_size;
87 : :
88 : : /* Hook for plugins to get control in set_rel_pathlist() */
89 : : set_rel_pathlist_hook_type set_rel_pathlist_hook = NULL;
90 : :
91 : : /* Hook for plugins to replace standard_join_search() */
92 : : join_search_hook_type join_search_hook = NULL;
93 : :
94 : :
95 : : static void set_base_rel_consider_startup(PlannerInfo *root);
96 : : static void set_base_rel_sizes(PlannerInfo *root);
97 : : static void setup_simple_grouped_rels(PlannerInfo *root);
98 : : static void set_base_rel_pathlists(PlannerInfo *root);
99 : : static void set_rel_size(PlannerInfo *root, RelOptInfo *rel,
100 : : Index rti, RangeTblEntry *rte);
101 : : static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
102 : : Index rti, RangeTblEntry *rte);
103 : : static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel,
104 : : RangeTblEntry *rte);
105 : : static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel);
106 : : static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
107 : : RangeTblEntry *rte);
108 : : static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
109 : : RangeTblEntry *rte);
110 : : static void set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel,
111 : : RangeTblEntry *rte);
112 : : static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
113 : : RangeTblEntry *rte);
114 : : static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel,
115 : : RangeTblEntry *rte);
116 : : static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel,
117 : : RangeTblEntry *rte);
118 : : static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
119 : : Index rti, RangeTblEntry *rte);
120 : : static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
121 : : Index rti, RangeTblEntry *rte);
122 : : static void set_grouped_rel_pathlist(PlannerInfo *root, RelOptInfo *rel);
123 : : static void generate_orderedappend_paths(PlannerInfo *root, RelOptInfo *rel,
124 : : List *live_childrels,
125 : : List *all_child_pathkeys);
126 : : static Path *get_cheapest_parameterized_child_path(PlannerInfo *root,
127 : : RelOptInfo *rel,
128 : : Relids required_outer);
129 : : static void accumulate_append_subpath(Path *path,
130 : : List **subpaths,
131 : : List **special_subpaths,
132 : : List **child_append_relid_sets);
133 : : static Path *get_singleton_append_subpath(Path *path,
134 : : List **child_append_relid_sets);
135 : : static void set_dummy_rel_pathlist(RelOptInfo *rel);
136 : : static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
137 : : Index rti, RangeTblEntry *rte);
138 : : static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
139 : : RangeTblEntry *rte);
140 : : static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
141 : : RangeTblEntry *rte);
142 : : static void set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel,
143 : : RangeTblEntry *rte);
144 : : static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
145 : : RangeTblEntry *rte);
146 : : static void set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
147 : : RangeTblEntry *rte);
148 : : static void set_result_pathlist(PlannerInfo *root, RelOptInfo *rel,
149 : : RangeTblEntry *rte);
150 : : static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
151 : : RangeTblEntry *rte);
152 : : static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
153 : : static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
154 : : pushdown_safety_info *safetyInfo);
155 : : static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
156 : : pushdown_safety_info *safetyInfo);
157 : : static void check_output_expressions(Query *subquery,
158 : : pushdown_safety_info *safetyInfo);
159 : : static void compare_tlist_datatypes(List *tlist, List *colTypes,
160 : : pushdown_safety_info *safetyInfo);
161 : : static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query);
162 : : static pushdown_safe_type qual_is_pushdown_safe(Query *subquery, Index rti,
163 : : RestrictInfo *rinfo,
164 : : pushdown_safety_info *safetyInfo);
165 : : static void subquery_push_qual(Query *subquery,
166 : : RangeTblEntry *rte, Index rti, Node *qual);
167 : : static void recurse_push_qual(Node *setOp, Query *topquery,
168 : : RangeTblEntry *rte, Index rti, Node *qual);
169 : : static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel,
170 : : Bitmapset *extra_used_attrs);
171 : :
172 : :
173 : : /*
174 : : * make_one_rel
175 : : * Finds all possible access paths for executing a query, returning a
176 : : * single rel that represents the join of all base rels in the query.
177 : : */
178 : : RelOptInfo *
7441 tgl@sss.pgh.pa.us 179 :CBC 255116 : make_one_rel(PlannerInfo *root, List *joinlist)
180 : : {
181 : : RelOptInfo *rel;
182 : : Index rti;
183 : : double total_pages;
184 : :
185 : : /* Mark base rels as to whether we care about fast-start plans */
3989 186 : 255116 : set_base_rel_consider_startup(root);
187 : :
188 : : /*
189 : : * Compute size estimates and consider_parallel flags for each base rel.
190 : : */
5212 191 : 255116 : set_base_rel_sizes(root);
192 : :
193 : : /*
194 : : * Build grouped relations for simple rels (i.e., base or "other" member
195 : : * relations) where possible.
196 : : */
209 rguo@postgresql.org 197 :GNC 255094 : setup_simple_grouped_rels(root);
198 : :
199 : : /*
200 : : * We should now have size estimates for every actual table involved in
201 : : * the query, and we also know which if any have been deleted from the
202 : : * query by join removal, pruned by partition pruning, or eliminated by
203 : : * constraint exclusion. So we can now compute total_table_pages.
204 : : *
205 : : * Note that appendrels are not double-counted here, even though we don't
206 : : * bother to distinguish RelOptInfos for appendrel parents, because the
207 : : * parents will have pages = 0.
208 : : *
209 : : * XXX if a table is self-joined, we will count it once per appearance,
210 : : * which perhaps is the wrong thing ... but that's not completely clear,
211 : : * and detecting self-joins here is difficult, so ignore it for now.
212 : : */
2736 tgl@sss.pgh.pa.us 213 :CBC 255094 : total_pages = 0;
214 [ + + ]: 799255 : for (rti = 1; rti < root->simple_rel_array_size; rti++)
215 : : {
216 : 544161 : RelOptInfo *brel = root->simple_rel_array[rti];
217 : :
218 : : /* there may be empty slots corresponding to non-baserel RTEs */
219 [ + + ]: 544161 : if (brel == NULL)
220 : 129080 : continue;
221 : :
222 [ - + ]: 415081 : Assert(brel->relid == rti); /* sanity check on array */
223 : :
224 [ + + ]: 415081 : if (IS_DUMMY_REL(brel))
225 : 1276 : continue;
226 : :
227 [ + + + - ]: 413805 : if (IS_SIMPLE_REL(brel))
228 : 413805 : total_pages += (double) brel->pages;
229 : : }
230 : 255094 : root->total_table_pages = total_pages;
231 : :
232 : : /*
233 : : * Generate access paths for each base rel.
234 : : */
9305 235 : 255094 : set_base_rel_pathlists(root);
236 : :
237 : : /*
238 : : * Generate access paths for the entire join tree.
239 : : */
7441 240 : 255094 : rel = make_rel_from_joinlist(root, joinlist);
241 : :
242 : : /*
243 : : * The result should join all and only the query's base + outer-join rels.
244 : : */
1191 245 [ - + ]: 255094 : Assert(bms_equal(rel->relids, root->all_query_rels));
246 : :
5212 247 : 255094 : return rel;
248 : : }
249 : :
250 : : /*
251 : : * set_base_rel_consider_startup
252 : : * Set the consider_[param_]startup flags for each base-relation entry.
253 : : *
254 : : * For the moment, we only deal with consider_param_startup here; because the
255 : : * logic for consider_startup is pretty trivial and is the same for every base
256 : : * relation, we just let build_simple_rel() initialize that flag correctly to
257 : : * start with. If that logic ever gets more complicated it would probably
258 : : * be better to move it here.
259 : : */
260 : : static void
3989 261 : 255116 : set_base_rel_consider_startup(PlannerInfo *root)
262 : : {
263 : : /*
264 : : * Since parameterized paths can only be used on the inside of a nestloop
265 : : * join plan, there is usually little value in considering fast-start
266 : : * plans for them. However, for relations that are on the RHS of a SEMI
267 : : * or ANTI join, a fast-start plan can be useful because we're only going
268 : : * to care about fetching one tuple anyway.
269 : : *
270 : : * To minimize growth of planning time, we currently restrict this to
271 : : * cases where the RHS is a single base relation, not a join; there is no
272 : : * provision for consider_param_startup to get set at all on joinrels.
273 : : * Also we don't worry about appendrels. costsize.c's costing rules for
274 : : * nestloop semi/antijoins don't consider such cases either.
275 : : */
276 : : ListCell *lc;
277 : :
278 [ + + + + : 289997 : foreach(lc, root->join_info_list)
+ + ]
279 : : {
280 : 34881 : SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
281 : : int varno;
282 : :
283 [ + + + + : 44518 : if ((sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI) &&
+ + ]
284 : 9637 : bms_get_singleton_member(sjinfo->syn_righthand, &varno))
285 : : {
286 : 9457 : RelOptInfo *rel = find_base_rel(root, varno);
287 : :
288 : 9457 : rel->consider_param_startup = true;
289 : : }
290 : : }
291 : 255116 : }
292 : :
293 : : /*
294 : : * set_base_rel_sizes
295 : : * Set the size estimates (rows and widths) for each base-relation entry.
296 : : * Also determine whether to consider parallel paths for base relations.
297 : : *
298 : : * We do this in a separate pass over the base rels so that rowcount
299 : : * estimates are available for parameterized path generation, and also so
300 : : * that each rel's consider_parallel flag is set correctly before we begin to
301 : : * generate paths.
302 : : */
303 : : static void
5212 304 : 255116 : set_base_rel_sizes(PlannerInfo *root)
305 : : {
306 : : Index rti;
307 : :
308 [ + + ]: 799278 : for (rti = 1; rti < root->simple_rel_array_size; rti++)
309 : : {
310 : 544184 : RelOptInfo *rel = root->simple_rel_array[rti];
311 : : RangeTblEntry *rte;
312 : :
313 : : /* there may be empty slots corresponding to non-baserel RTEs */
314 [ + + ]: 544184 : if (rel == NULL)
315 : 129081 : continue;
316 : :
3240 317 [ - + ]: 415103 : Assert(rel->relid == rti); /* sanity check on array */
318 : :
319 : : /* ignore RTEs that are "other rels" */
5212 320 [ + + ]: 415103 : if (rel->reloptkind != RELOPT_BASEREL)
321 : 48758 : continue;
322 : :
3828 rhaas@postgresql.org 323 : 366345 : rte = root->simple_rte_array[rti];
324 : :
325 : : /*
326 : : * If parallelism is allowable for this query in general, see whether
327 : : * it's allowable for this rel in particular. We have to do this
328 : : * before set_rel_size(), because (a) if this rel is an inheritance
329 : : * parent, set_append_rel_size() will use and perhaps change the rel's
330 : : * consider_parallel flag, and (b) for some RTE types, set_rel_size()
331 : : * goes ahead and makes paths immediately.
332 : : */
333 [ + + ]: 366345 : if (root->glob->parallelModeOK)
334 : 294606 : set_rel_consider_parallel(root, rel, rte);
335 : :
336 : 366345 : set_rel_size(root, rel, rti, rte);
337 : : }
10892 scrappy@hub.org 338 : 255094 : }
339 : :
340 : : /*
341 : : * setup_simple_grouped_rels
342 : : * For each simple relation, build a grouped simple relation if eager
343 : : * aggregation is possible and if this relation can produce grouped paths.
344 : : */
345 : : static void
209 rguo@postgresql.org 346 :GNC 255094 : setup_simple_grouped_rels(PlannerInfo *root)
347 : : {
348 : : Index rti;
349 : :
350 : : /*
351 : : * If there are no aggregate expressions or grouping expressions, eager
352 : : * aggregation is not possible.
353 : : */
354 [ + + ]: 255094 : if (root->agg_clause_list == NIL ||
355 [ + + ]: 551 : root->group_expr_list == NIL)
356 : 254654 : return;
357 : :
358 [ + + ]: 3761 : for (rti = 1; rti < root->simple_rel_array_size; rti++)
359 : : {
360 : 3321 : RelOptInfo *rel = root->simple_rel_array[rti];
361 : :
362 : : /* there may be empty slots corresponding to non-baserel RTEs */
363 [ + + ]: 3321 : if (rel == NULL)
364 : 1071 : continue;
365 : :
366 [ - + ]: 2250 : Assert(rel->relid == rti); /* sanity check on array */
367 [ + + - + ]: 2250 : Assert(IS_SIMPLE_REL(rel)); /* sanity check on rel */
368 : :
369 : 2250 : (void) build_simple_grouped_rel(root, rel);
370 : : }
371 : : }
372 : :
373 : : /*
374 : : * set_base_rel_pathlists
375 : : * Finds all paths available for scanning each base-relation entry.
376 : : * Sequential scan and any available indices are considered.
377 : : * Each useful path is attached to its relation's 'pathlist' field.
378 : : */
379 : : static void
7639 tgl@sss.pgh.pa.us 380 :CBC 255094 : set_base_rel_pathlists(PlannerInfo *root)
381 : : {
382 : : Index rti;
383 : :
7399 384 [ + + ]: 799255 : for (rti = 1; rti < root->simple_rel_array_size; rti++)
385 : : {
386 : 544161 : RelOptInfo *rel = root->simple_rel_array[rti];
387 : :
388 : : /* there may be empty slots corresponding to non-baserel RTEs */
7638 389 [ + + ]: 544161 : if (rel == NULL)
390 : 129080 : continue;
391 : :
3240 392 [ - + ]: 415081 : Assert(rel->relid == rti); /* sanity check on array */
393 : :
394 : : /* ignore RTEs that are "other rels" */
7638 395 [ + + ]: 415081 : if (rel->reloptkind != RELOPT_BASEREL)
396 : 48758 : continue;
397 : :
6954 398 : 366323 : set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
399 : : }
7396 400 : 255094 : }
401 : :
402 : : /*
403 : : * set_rel_size
404 : : * Set size estimates for a base relation
405 : : */
406 : : static void
5212 407 : 414831 : set_rel_size(PlannerInfo *root, RelOptInfo *rel,
408 : : Index rti, RangeTblEntry *rte)
409 : : {
5337 410 [ + + + + ]: 781176 : if (rel->reloptkind == RELOPT_BASEREL &&
411 : 366345 : relation_excluded_by_constraints(root, rel, rte))
412 : : {
413 : : /*
414 : : * We proved we don't need to scan the rel via constraint exclusion,
415 : : * so set up a single dummy path for it. Here we only check this for
416 : : * regular baserels; if it's an otherrel, CE was already checked in
417 : : * set_append_rel_size().
418 : : *
419 : : * In this case, we go ahead and set up the relation's path right away
420 : : * instead of leaving it for set_rel_pathlist to do. This is because
421 : : * we don't have a convention for marking a rel as dummy except by
422 : : * assigning a dummy path to it.
423 : : */
424 : 646 : set_dummy_rel_pathlist(rel);
425 : : }
426 [ + + ]: 414185 : else if (rte->inh)
427 : : {
428 : : /* It's an "append relation", process accordingly */
5212 429 : 20870 : set_append_rel_size(root, rel, rti, rte);
430 : : }
431 : : else
432 : : {
5551 433 [ + + + + : 393315 : switch (rel->rtekind)
+ + + +
- ]
434 : : {
435 : 325028 : case RTE_RELATION:
436 [ + + ]: 325028 : if (rte->relkind == RELKIND_FOREIGN_TABLE)
437 : : {
438 : : /* Foreign table */
5212 439 : 1257 : set_foreign_size(root, rel, rte);
440 : : }
3332 rhaas@postgresql.org 441 [ + + ]: 323771 : else if (rte->relkind == RELKIND_PARTITIONED_TABLE)
442 : : {
443 : : /*
444 : : * We could get here if asked to scan a partitioned table
445 : : * with ONLY. In that case we shouldn't scan any of the
446 : : * partitions, so mark it as a dummy rel.
447 : : */
448 : 26 : set_dummy_rel_pathlist(rel);
449 : : }
4008 simon@2ndQuadrant.co 450 [ + + ]: 323745 : else if (rte->tablesample != NULL)
451 : : {
452 : : /* Sampled relation */
453 : 245 : set_tablesample_rel_size(root, rel, rte);
454 : : }
455 : : else
456 : : {
457 : : /* Plain relation */
5212 tgl@sss.pgh.pa.us 458 : 323500 : set_plain_rel_size(root, rel, rte);
459 : : }
5551 460 : 325006 : break;
461 : 17487 : case RTE_SUBQUERY:
462 : :
463 : : /*
464 : : * Subqueries don't support making a choice between
465 : : * parameterized and unparameterized paths, so just go ahead
466 : : * and build their paths immediately.
467 : : */
468 : 17487 : set_subquery_pathlist(root, rel, rti, rte);
469 : 17487 : break;
470 : 35764 : case RTE_FUNCTION:
5212 471 : 35764 : set_function_size_estimates(root, rel);
5551 472 : 35764 : break;
3345 alvherre@alvh.no-ip. 473 : 517 : case RTE_TABLEFUNC:
474 : 517 : set_tablefunc_size_estimates(root, rel);
475 : 517 : break;
5551 tgl@sss.pgh.pa.us 476 : 6858 : case RTE_VALUES:
5212 477 : 6858 : set_values_size_estimates(root, rel);
5551 478 : 6858 : break;
479 : 3607 : case RTE_CTE:
480 : :
481 : : /*
482 : : * CTEs don't support making a choice between parameterized
483 : : * and unparameterized paths, so just go ahead and build their
484 : : * paths immediately.
485 : : */
486 [ + + ]: 3607 : if (rte->self_reference)
487 : 694 : set_worktable_pathlist(root, rel, rte);
488 : : else
489 : 2913 : set_cte_pathlist(root, rel, rte);
490 : 3607 : break;
3322 kgrittn@postgresql.o 491 : 438 : case RTE_NAMEDTUPLESTORE:
492 : : /* Might as well just build the path immediately */
493 : 438 : set_namedtuplestore_pathlist(root, rel, rte);
494 : 438 : break;
2654 tgl@sss.pgh.pa.us 495 : 3616 : case RTE_RESULT:
496 : : /* Might as well just build the path immediately */
497 : 3616 : set_result_pathlist(root, rel, rte);
498 : 3616 : break;
5551 tgl@sss.pgh.pa.us 499 :UBC 0 : default:
500 [ # # ]: 0 : elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
501 : : break;
502 : : }
503 : : }
504 : :
505 : : /*
506 : : * We insist that all non-dummy rels have a nonzero rowcount estimate.
507 : : */
3936 tgl@sss.pgh.pa.us 508 [ + + - + ]:CBC 414808 : Assert(rel->rows > 0 || IS_DUMMY_REL(rel));
5212 509 : 414808 : }
510 : :
511 : : /*
512 : : * set_rel_pathlist
513 : : * Build access paths for a base relation
514 : : */
515 : : static void
516 : 414933 : set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
517 : : Index rti, RangeTblEntry *rte)
518 : : {
519 [ + + ]: 414933 : if (IS_DUMMY_REL(rel))
520 : : {
521 : : /* We already proved the relation empty, so nothing more to do */
522 : : }
523 [ + + ]: 413775 : else if (rte->inh)
524 : : {
525 : : /* It's an "append relation", process accordingly */
526 : 20613 : set_append_rel_pathlist(root, rel, rti, rte);
527 : : }
528 : : else
529 : : {
530 [ + + + + : 393162 : switch (rel->rtekind)
+ + + +
- ]
531 : : {
532 : 324980 : case RTE_RELATION:
533 [ + + ]: 324980 : if (rte->relkind == RELKIND_FOREIGN_TABLE)
534 : : {
535 : : /* Foreign table */
536 : 1255 : set_foreign_pathlist(root, rel, rte);
537 : : }
4008 simon@2ndQuadrant.co 538 [ + + ]: 323725 : else if (rte->tablesample != NULL)
539 : : {
540 : : /* Sampled relation */
541 : 245 : set_tablesample_rel_pathlist(root, rel, rte);
542 : : }
543 : : else
544 : : {
545 : : /* Plain relation */
5212 tgl@sss.pgh.pa.us 546 : 323480 : set_plain_rel_pathlist(root, rel, rte);
547 : : }
548 : 324980 : break;
549 : 17382 : case RTE_SUBQUERY:
550 : : /* Subquery --- fully handled during set_rel_size */
551 : 17382 : break;
552 : 35764 : case RTE_FUNCTION:
553 : : /* RangeFunction */
554 : 35764 : set_function_pathlist(root, rel, rte);
555 : 35764 : break;
3345 alvherre@alvh.no-ip. 556 : 517 : case RTE_TABLEFUNC:
557 : : /* Table Function */
558 : 517 : set_tablefunc_pathlist(root, rel, rte);
559 : 517 : break;
5212 tgl@sss.pgh.pa.us 560 : 6858 : case RTE_VALUES:
561 : : /* Values list */
562 : 6858 : set_values_pathlist(root, rel, rte);
563 : 6858 : break;
564 : 3607 : case RTE_CTE:
565 : : /* CTE reference --- fully handled during set_rel_size */
566 : 3607 : break;
3322 kgrittn@postgresql.o 567 : 438 : case RTE_NAMEDTUPLESTORE:
568 : : /* tuplestore reference --- fully handled during set_rel_size */
569 : 438 : break;
2654 tgl@sss.pgh.pa.us 570 : 3616 : case RTE_RESULT:
571 : : /* simple Result --- fully handled during set_rel_size */
572 : 3616 : break;
5212 tgl@sss.pgh.pa.us 573 :UBC 0 : default:
574 [ # # ]: 0 : elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
575 : : break;
576 : : }
577 : : }
578 : :
579 : : /*
580 : : * Allow a plugin to editorialize on the set of Paths for this base
581 : : * relation. It could add new paths (such as CustomPaths) by calling
582 : : * add_path(), or add_partial_path() if parallel aware. It could also
583 : : * delete or modify paths added by the core code.
584 : : */
2642 tgl@sss.pgh.pa.us 585 [ - + ]:CBC 414933 : if (set_rel_pathlist_hook)
2642 tgl@sss.pgh.pa.us 586 :UBC 0 : (*set_rel_pathlist_hook) (root, rel, rti, rte);
587 : :
588 : : /*
589 : : * If this is a baserel, we should normally consider gathering any partial
590 : : * paths we may have created for it. We have to do this after calling the
591 : : * set_rel_pathlist_hook, else it cannot add partial paths to be included
592 : : * here.
593 : : *
594 : : * However, if this is an inheritance child, skip it. Otherwise, we could
595 : : * end up with a very large number of gather nodes, each trying to grab
596 : : * its own pool of workers. Instead, we'll consider gathering partial
597 : : * paths for the parent appendrel.
598 : : *
599 : : * Also, if this is the topmost scan/join rel, we postpone gathering until
600 : : * the final scan/join targetlist is available (see grouping_planner).
601 : : */
2976 rhaas@postgresql.org 602 [ + + ]:CBC 414933 : if (rel->reloptkind == RELOPT_BASEREL &&
1191 tgl@sss.pgh.pa.us 603 [ + + ]: 366323 : !bms_equal(rel->relids, root->all_query_rels))
2219 tomas.vondra@postgre 604 : 191925 : generate_useful_gather_paths(root, rel, false);
605 : :
606 : : /* Now find the cheapest of the paths for this rel */
4183 tgl@sss.pgh.pa.us 607 : 414933 : set_cheapest(rel);
608 : :
609 : : /*
610 : : * If a grouped relation for this rel exists, build partial aggregation
611 : : * paths for it.
612 : : *
613 : : * Note that this can only happen after we've called set_cheapest() for
614 : : * this base rel, because we need its cheapest paths.
615 : : */
209 rguo@postgresql.org 616 :GNC 414933 : set_grouped_rel_pathlist(root, rel);
617 : :
618 : : #ifdef OPTIMIZER_DEBUG
619 : : pprint(rel);
620 : : #endif
9305 tgl@sss.pgh.pa.us 621 :CBC 414933 : }
622 : :
623 : : /*
624 : : * set_plain_rel_size
625 : : * Set size estimates for a plain relation (no subquery, no inheritance)
626 : : */
627 : : static void
5212 628 : 323500 : set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
629 : : {
630 : : /*
631 : : * Test any partial indexes of rel for applicability. We must do this
632 : : * first since partial unique indexes can affect size estimates.
633 : : */
3687 634 : 323500 : check_index_predicates(root, rel);
635 : :
636 : : /* Mark rel with estimated output rows, width, etc */
6954 637 : 323500 : set_baserel_size_estimates(root, rel);
5212 638 : 323480 : }
639 : :
640 : : /*
641 : : * If this relation could possibly be scanned from within a worker, then set
642 : : * its consider_parallel flag.
643 : : */
644 : : static void
3828 rhaas@postgresql.org 645 : 331015 : set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
646 : : RangeTblEntry *rte)
647 : : {
648 : : /*
649 : : * The flag has previously been initialized to false, so we can just
650 : : * return if it becomes clear that we can't safely set it.
651 : : */
3593 tgl@sss.pgh.pa.us 652 [ - + ]: 331015 : Assert(!rel->consider_parallel);
653 : :
654 : : /* Don't call this if parallelism is disallowed for the entire query. */
3828 rhaas@postgresql.org 655 [ - + ]: 331015 : Assert(root->glob->parallelModeOK);
656 : :
657 : : /* This should only be called for baserels and appendrel children. */
3319 658 [ + + - + ]: 331015 : Assert(IS_SIMPLE_REL(rel));
659 : :
660 : : /* Assorted checks based on rtekind. */
3828 661 [ + + - + : 331015 : switch (rte->rtekind)
+ + + + +
- - - ]
662 : : {
663 : 284970 : case RTE_RELATION:
664 : :
665 : : /*
666 : : * Currently, parallel workers can't access the leader's temporary
667 : : * tables. We could possibly relax this if we wrote all of its
668 : : * local buffers at the start of the query and made no changes
669 : : * thereafter (maybe we could allow hint bit changes), and if we
670 : : * taught the workers to read them. Writing a large number of
671 : : * temporary buffers could be expensive, though, and we don't have
672 : : * the rest of the necessary infrastructure right now anyway. So
673 : : * for now, bail out if we see a temporary table.
674 : : */
675 [ + + ]: 284970 : if (get_rel_persistence(rte->relid) == RELPERSISTENCE_TEMP)
676 : 7466 : return;
677 : :
678 : : /*
679 : : * Table sampling can be pushed down to workers if the sample
680 : : * function and its arguments are safe.
681 : : */
682 [ + + ]: 277504 : if (rte->tablesample != NULL)
683 : : {
3444 tgl@sss.pgh.pa.us 684 : 263 : char proparallel = func_parallel(rte->tablesample->tsmhandler);
685 : :
3828 rhaas@postgresql.org 686 [ + + ]: 263 : if (proparallel != PROPARALLEL_SAFE)
687 : 18 : return;
3546 tgl@sss.pgh.pa.us 688 [ + + ]: 245 : if (!is_parallel_safe(root, (Node *) rte->tablesample->args))
3828 rhaas@postgresql.org 689 : 10 : return;
690 : : }
691 : :
692 : : /*
693 : : * Ask FDWs whether they can support performing a ForeignScan
694 : : * within a worker. Most often, the answer will be no. For
695 : : * example, if the nature of the FDW is such that it opens a TCP
696 : : * connection with a remote server, each parallel worker would end
697 : : * up with a separate connection, and these connections might not
698 : : * be appropriately coordinated between workers and the leader.
699 : : */
3721 700 [ + + ]: 277476 : if (rte->relkind == RELKIND_FOREIGN_TABLE)
701 : : {
702 [ - + ]: 802 : Assert(rel->fdwroutine);
703 [ + + ]: 802 : if (!rel->fdwroutine->IsForeignScanParallelSafe)
704 : 763 : return;
705 [ - + ]: 39 : if (!rel->fdwroutine->IsForeignScanParallelSafe(root, rel, rte))
3721 rhaas@postgresql.org 706 :UBC 0 : return;
707 : : }
708 : :
709 : : /*
710 : : * There are additional considerations for appendrels, which we'll
711 : : * deal with in set_append_rel_size and set_append_rel_pathlist.
712 : : * For now, just set consider_parallel based on the rel's own
713 : : * quals and targetlist.
714 : : */
3828 rhaas@postgresql.org 715 :CBC 276713 : break;
716 : :
717 : 16420 : case RTE_SUBQUERY:
718 : :
719 : : /*
720 : : * There's no intrinsic problem with scanning a subquery-in-FROM
721 : : * (as distinct from a SubPlan or InitPlan) in a parallel worker.
722 : : * If the subquery doesn't happen to have any parallel-safe paths,
723 : : * then flagging it as consider_parallel won't change anything,
724 : : * but that's true for plain tables, too. We must set
725 : : * consider_parallel based on the rel's own quals and targetlist,
726 : : * so that if a subquery path is parallel-safe but the quals and
727 : : * projection we're sticking onto it are not, we correctly mark
728 : : * the SubqueryScanPath as not parallel-safe. (Note that
729 : : * set_subquery_pathlist() might push some of these quals down
730 : : * into the subquery itself, but that doesn't change anything.)
731 : : *
732 : : * We can't push sub-select containing LIMIT/OFFSET to workers as
733 : : * there is no guarantee that the row order will be fully
734 : : * deterministic, and applying LIMIT/OFFSET will lead to
735 : : * inconsistent results at the top-level. (In some cases, where
736 : : * the result is ordered, we could relax this restriction. But it
737 : : * doesn't currently seem worth expending extra effort to do so.)
738 : : */
739 : : {
2790 akapila@postgresql.o 740 : 16420 : Query *subquery = castNode(Query, rte->subquery);
741 : :
742 [ + + ]: 16420 : if (limit_needed(subquery))
743 : 449 : return;
744 : : }
3593 tgl@sss.pgh.pa.us 745 : 15971 : break;
746 : :
3828 rhaas@postgresql.org 747 :UBC 0 : case RTE_JOIN:
748 : : /* Shouldn't happen; we're only considering baserels here. */
749 : 0 : Assert(false);
750 : : return;
751 : :
3828 rhaas@postgresql.org 752 :CBC 20257 : case RTE_FUNCTION:
753 : : /* Check for parallel-restricted functions. */
3546 tgl@sss.pgh.pa.us 754 [ + + ]: 20257 : if (!is_parallel_safe(root, (Node *) rte->functions))
3828 rhaas@postgresql.org 755 : 9025 : return;
756 : 11232 : break;
757 : :
3345 alvherre@alvh.no-ip. 758 : 517 : case RTE_TABLEFUNC:
759 : : /* not parallel safe */
760 : 517 : return;
761 : :
3828 rhaas@postgresql.org 762 : 2415 : case RTE_VALUES:
763 : : /* Check for parallel-restricted functions. */
3546 tgl@sss.pgh.pa.us 764 [ + + ]: 2415 : if (!is_parallel_safe(root, (Node *) rte->values_lists))
765 : 10 : return;
3828 rhaas@postgresql.org 766 : 2405 : break;
767 : :
768 : 2745 : case RTE_CTE:
769 : :
770 : : /*
771 : : * CTE tuplestores aren't shared among parallel workers, so we
772 : : * force all CTE scans to happen in the leader. Also, populating
773 : : * the CTE would require executing a subplan that's not available
774 : : * in the worker, might be parallel-restricted, and must get
775 : : * executed only once.
776 : : */
777 : 2745 : return;
778 : :
3322 kgrittn@postgresql.o 779 : 416 : case RTE_NAMEDTUPLESTORE:
780 : :
781 : : /*
782 : : * tuplestore cannot be shared, at least without more
783 : : * infrastructure to support that.
784 : : */
785 : 416 : return;
786 : :
2654 tgl@sss.pgh.pa.us 787 : 3275 : case RTE_RESULT:
788 : : /* RESULT RTEs, in themselves, are no problem. */
789 : 3275 : break;
790 : :
50 peter@eisentraut.org 791 :UNC 0 : case RTE_GRAPH_TABLE:
792 : :
793 : : /*
794 : : * Shouldn't happen since these are replaced by subquery RTEs when
795 : : * rewriting queries.
796 : : */
797 : 0 : Assert(false);
798 : : return;
799 : :
602 rguo@postgresql.org 800 :UBC 0 : case RTE_GROUP:
801 : : /* Shouldn't happen; we're only considering baserels here. */
802 : 0 : Assert(false);
803 : : return;
804 : : }
805 : :
806 : : /*
807 : : * If there's anything in baserestrictinfo that's parallel-restricted, we
808 : : * give up on parallelizing access to this relation. We could consider
809 : : * instead postponing application of the restricted quals until we're
810 : : * above all the parallelism in the plan tree, but it's not clear that
811 : : * that would be a win in very many cases, and it might be tricky to make
812 : : * outer join clauses work correctly. It would likely break equivalence
813 : : * classes, too.
814 : : */
3546 tgl@sss.pgh.pa.us 815 [ + + ]:CBC 309596 : if (!is_parallel_safe(root, (Node *) rel->baserestrictinfo))
3828 rhaas@postgresql.org 816 : 21869 : return;
817 : :
818 : : /*
819 : : * Likewise, if the relation's outputs are not parallel-safe, give up.
820 : : * (Usually, they're just Vars, but sometimes they're not.)
821 : : */
3546 tgl@sss.pgh.pa.us 822 [ + + ]: 287727 : if (!is_parallel_safe(root, (Node *) rel->reltarget->exprs))
3617 rhaas@postgresql.org 823 : 15 : return;
824 : :
825 : : /* We have a winner. */
3828 826 : 287712 : rel->consider_parallel = true;
827 : : }
828 : :
829 : : /*
830 : : * set_plain_rel_pathlist
831 : : * Build access paths for a plain relation (no subquery, no inheritance)
832 : : */
833 : : static void
5212 tgl@sss.pgh.pa.us 834 : 323480 : set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
835 : : {
836 : : Relids required_outer;
837 : :
838 : : /*
839 : : * We don't support pushing join clauses into the quals of a seqscan, but
840 : : * it could still have required parameterization due to LATERAL refs in
841 : : * its tlist.
842 : : */
5000 843 : 323480 : required_outer = rel->lateral_relids;
844 : :
845 : : /*
846 : : * Consider TID scans.
847 : : *
848 : : * If create_tidscan_paths returns true, then a TID scan path is forced.
849 : : * This happens when rel->baserestrictinfo contains CurrentOfExpr, because
850 : : * the executor can't handle any other type of path for such queries.
851 : : * Hence, we return without adding any other paths.
852 : : */
652 rhaas@postgresql.org 853 [ + + ]: 323480 : if (create_tidscan_paths(root, rel))
854 : 344 : return;
855 : :
856 : : /* Consider sequential scan */
3828 857 : 323136 : add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
858 : :
859 : : /* If appropriate, consider parallel sequential scan */
3758 860 [ + + + + ]: 323136 : if (rel->consider_parallel && required_outer == NULL)
3657 tgl@sss.pgh.pa.us 861 : 244043 : create_plain_partial_paths(root, rel);
862 : :
863 : : /* Consider index scans */
7680 864 : 323136 : create_index_paths(root, rel);
865 : : }
866 : :
867 : : /*
868 : : * create_plain_partial_paths
869 : : * Build partial access paths for parallel scan of a plain relation
870 : : */
871 : : static void
3657 872 : 244043 : create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
873 : : {
874 : : int parallel_workers;
875 : :
3014 rhaas@postgresql.org 876 : 244043 : parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
877 : : max_parallel_workers_per_gather);
878 : :
879 : : /* If any limit was set to zero, the user doesn't want a parallel scan. */
3617 tgl@sss.pgh.pa.us 880 [ + + ]: 244043 : if (parallel_workers <= 0)
881 : 219795 : return;
882 : :
883 : : /* Add an unordered partial path based on a parallel sequential scan. */
rhaas@postgresql.org 884 : 24248 : add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
885 : : }
886 : :
887 : : /*
888 : : * set_tablesample_rel_size
889 : : * Set size estimates for a sampled relation
890 : : */
891 : : static void
4008 simon@2ndQuadrant.co 892 : 245 : set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
893 : : {
3937 tgl@sss.pgh.pa.us 894 : 245 : TableSampleClause *tsc = rte->tablesample;
895 : : TsmRoutine *tsm;
896 : : BlockNumber pages;
897 : : double tuples;
898 : :
899 : : /*
900 : : * Test any partial indexes of rel for applicability. We must do this
901 : : * first since partial unique indexes can affect size estimates.
902 : : */
3687 903 : 245 : check_index_predicates(root, rel);
904 : :
905 : : /*
906 : : * Call the sampling method's estimation function to estimate the number
907 : : * of pages it will read and the number of tuples it will return. (Note:
908 : : * we assume the function returns sane values.)
909 : : */
3937 910 : 245 : tsm = GetTsmRoutine(tsc->tsmhandler);
911 : 245 : tsm->SampleScanGetSampleSize(root, rel, tsc->args,
912 : : &pages, &tuples);
913 : :
914 : : /*
915 : : * For the moment, because we will only consider a SampleScan path for the
916 : : * rel, it's okay to just overwrite the pages and tuples estimates for the
917 : : * whole relation. If we ever consider multiple path types for sampled
918 : : * rels, we'll need more complication.
919 : : */
920 : 245 : rel->pages = pages;
921 : 245 : rel->tuples = tuples;
922 : :
923 : : /* Mark rel with estimated output rows, width, etc */
4008 simon@2ndQuadrant.co 924 : 245 : set_baserel_size_estimates(root, rel);
925 : 245 : }
926 : :
927 : : /*
928 : : * set_tablesample_rel_pathlist
929 : : * Build access paths for a sampled relation
930 : : */
931 : : static void
932 : 245 : set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
933 : : {
934 : : Relids required_outer;
935 : : Path *path;
936 : :
937 : : /*
938 : : * We don't support pushing join clauses into the quals of a samplescan,
939 : : * but it could still have required parameterization due to LATERAL refs
940 : : * in its tlist or TABLESAMPLE arguments.
941 : : */
942 : 245 : required_outer = rel->lateral_relids;
943 : :
944 : : /* Consider sampled scan */
945 : 245 : path = create_samplescan_path(root, rel, required_outer);
946 : :
947 : : /*
948 : : * If the sampling method does not support repeatable scans, we must avoid
949 : : * plans that would scan the rel multiple times. Ideally, we'd simply
950 : : * avoid putting the rel on the inside of a nestloop join; but adding such
951 : : * a consideration to the planner seems like a great deal of complication
952 : : * to support an uncommon usage of second-rate sampling methods. Instead,
953 : : * if there is a risk that the query might perform an unsafe join, just
954 : : * wrap the SampleScan in a Materialize node. We can check for joins by
955 : : * counting the membership of all_query_rels (note that this correctly
956 : : * counts inheritance trees as single rels). If we're inside a subquery,
957 : : * we can't easily check whether a join might occur in the outer query, so
958 : : * just assume one is possible.
959 : : *
960 : : * GetTsmRoutine is relatively expensive compared to the other tests here,
961 : : * so check repeatable_across_scans last, even though that's a bit odd.
962 : : */
3937 tgl@sss.pgh.pa.us 963 [ + + + + ]: 473 : if ((root->query_level > 1 ||
1191 964 : 228 : bms_membership(root->all_query_rels) != BMS_SINGLETON) &&
3240 965 [ + + ]: 79 : !(GetTsmRoutine(rte->tablesample->tsmhandler)->repeatable_across_scans))
966 : : {
97 rhaas@postgresql.org 967 :GNC 6 : path = (Path *) create_material_path(rel, path, true);
968 : : }
969 : :
3937 tgl@sss.pgh.pa.us 970 :CBC 245 : add_path(rel, path);
971 : :
972 : : /* For the moment, at least, there are no other paths to consider */
4008 simon@2ndQuadrant.co 973 : 245 : }
974 : :
975 : : /*
976 : : * set_foreign_size
977 : : * Set size estimates for a foreign table RTE
978 : : */
979 : : static void
5212 tgl@sss.pgh.pa.us 980 : 1257 : set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
981 : : {
982 : : /* Mark rel with estimated output rows, width, etc */
983 : 1257 : set_foreign_size_estimates(root, rel);
984 : :
985 : : /* Let FDW adjust the size estimates, if it can */
5170 986 : 1257 : rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
987 : :
988 : : /* ... but do not let it set the rows estimate to zero */
3936 989 : 1255 : rel->rows = clamp_row_est(rel->rows);
990 : :
991 : : /*
992 : : * Also, make sure rel->tuples is not insane relative to rel->rows.
993 : : * Notably, this ensures sanity if pg_class.reltuples contains -1 and the
994 : : * FDW doesn't do anything to replace that.
995 : : */
2132 996 [ + + ]: 1255 : rel->tuples = Max(rel->tuples, rel->rows);
5212 997 : 1255 : }
998 : :
999 : : /*
1000 : : * set_foreign_pathlist
1001 : : * Build access paths for a foreign table RTE
1002 : : */
1003 : : static void
1004 : 1255 : set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1005 : : {
1006 : : /* Call the FDW's GetForeignPaths function to generate path(s) */
5170 1007 : 1255 : rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
5212 1008 : 1255 : }
1009 : :
1010 : : /*
1011 : : * set_append_rel_size
1012 : : * Set size estimates for a simple "append relation"
1013 : : *
1014 : : * The passed-in rel and RTE represent the entire append relation. The
1015 : : * relation's contents are computed by appending together the output of the
1016 : : * individual member relations. Note that in the non-partitioned inheritance
1017 : : * case, the first member relation is actually the same table as is mentioned
1018 : : * in the parent RTE ... but it has a different RTE and RelOptInfo. This is
1019 : : * a good thing because their outputs are not the same size.
1020 : : */
1021 : : static void
1022 : 20870 : set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
1023 : : Index rti, RangeTblEntry *rte)
1024 : : {
9116 1025 : 20870 : int parentRTindex = rti;
1026 : : bool has_live_children;
1027 : : double parent_tuples;
1028 : : double parent_rows;
1029 : : double parent_size;
1030 : : double *parent_attrsizes;
1031 : : int nattrs;
1032 : : ListCell *l;
1033 : :
1034 : : /* Guard against stack overflow due to overly deep inheritance tree. */
3155 rhaas@postgresql.org 1035 : 20870 : check_stack_depth();
1036 : :
3319 1037 [ + + - + ]: 20870 : Assert(IS_SIMPLE_REL(rel));
1038 : :
1039 : : /*
1040 : : * If this is a partitioned baserel, set the consider_partitionwise_join
1041 : : * flag; currently, we only consider partitionwise joins with the baserel
1042 : : * if its targetlist doesn't contain a whole-row Var.
1043 : : */
2804 efujita@postgresql.o 1044 [ + + ]: 20870 : if (enable_partitionwise_join &&
1045 [ + + ]: 4159 : rel->reloptkind == RELOPT_BASEREL &&
1046 [ + - ]: 3309 : rte->relkind == RELKIND_PARTITIONED_TABLE &&
1191 tgl@sss.pgh.pa.us 1047 [ + + ]: 3309 : bms_is_empty(rel->attr_needed[InvalidAttrNumber - rel->min_attr]))
2804 efujita@postgresql.o 1048 : 3251 : rel->consider_partitionwise_join = true;
1049 : :
1050 : : /*
1051 : : * Initialize to compute size estimates for whole append relation.
1052 : : *
1053 : : * We handle tuples estimates by setting "tuples" to the total number of
1054 : : * tuples accumulated from each live child, rather than using "rows".
1055 : : * Although an appendrel itself doesn't directly enforce any quals, its
1056 : : * child relations may. Therefore, setting "tuples" equal to "rows" for
1057 : : * an appendrel isn't always appropriate, and can lead to inaccurate cost
1058 : : * estimates. For example, when estimating the number of distinct values
1059 : : * from an appendrel, we would be unable to adjust the estimate based on
1060 : : * the restriction selectivity (see estimate_num_groups).
1061 : : *
1062 : : * We handle width estimates by weighting the widths of different child
1063 : : * rels proportionally to their number of rows. This is sensible because
1064 : : * the use of width estimates is mainly to compute the total relation
1065 : : * "footprint" if we have to sort or hash it. To do this, we sum the
1066 : : * total equivalent size (in "double" arithmetic) and then divide by the
1067 : : * total rowcount estimate. This is done separately for the total rel
1068 : : * width and each attribute.
1069 : : *
1070 : : * Note: if you consider changing this logic, beware that child rels could
1071 : : * have zero rows and/or width, if they were excluded by constraints.
1072 : : */
3936 tgl@sss.pgh.pa.us 1073 : 20870 : has_live_children = false;
442 rguo@postgresql.org 1074 : 20870 : parent_tuples = 0;
6521 tgl@sss.pgh.pa.us 1075 : 20870 : parent_rows = 0;
1076 : 20870 : parent_size = 0;
1077 : 20870 : nattrs = rel->max_attr - rel->min_attr + 1;
1078 : 20870 : parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
1079 : :
7399 1080 [ + + + + : 113127 : foreach(l, root->append_rel_list)
+ + ]
1081 : : {
1082 : 92258 : AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1083 : : int childRTindex;
1084 : : RangeTblEntry *childRTE;
1085 : : RelOptInfo *childrel;
1086 : : List *childrinfos;
1087 : : ListCell *parentvars;
1088 : : ListCell *childvars;
1089 : : ListCell *lc;
1090 : :
1091 : : /* append_rel_list contains all append rels; ignore others */
1092 [ + + ]: 92258 : if (appinfo->parent_relid != parentRTindex)
1093 : 43907 : continue;
1094 : :
1095 : 48666 : childRTindex = appinfo->child_relid;
6954 1096 : 48666 : childRTE = root->simple_rte_array[childRTindex];
1097 : :
1098 : : /*
1099 : : * The child rel's RelOptInfo was already created during
1100 : : * add_other_rels_to_query.
1101 : : */
7168 1102 : 48666 : childrel = find_base_rel(root, childRTindex);
1103 [ - + ]: 48666 : Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1104 : :
1105 : : /* We may have already proven the child to be dummy. */
2593 1106 [ + + ]: 48666 : if (IS_DUMMY_REL(childrel))
2650 alvherre@alvh.no-ip. 1107 : 20 : continue;
1108 : :
1109 : : /*
1110 : : * We have to copy the parent's targetlist and quals to the child,
1111 : : * with appropriate substitution of variables. However, the
1112 : : * baserestrictinfo quals were already copied/substituted when the
1113 : : * child RelOptInfo was built. So we don't need any additional setup
1114 : : * before applying constraint exclusion.
1115 : : */
6608 tgl@sss.pgh.pa.us 1116 [ + + ]: 48646 : if (relation_excluded_by_constraints(root, childrel, childRTE))
1117 : : {
1118 : : /*
1119 : : * This child need not be scanned, so we can omit it from the
1120 : : * appendrel.
1121 : : */
6919 1122 : 160 : set_dummy_rel_pathlist(childrel);
6954 1123 : 160 : continue;
1124 : : }
1125 : :
1126 : : /*
1127 : : * Constraint exclusion failed, so copy the parent's join quals and
1128 : : * targetlist to the child, with appropriate variable substitutions.
1129 : : *
1130 : : * We skip join quals that came from above outer joins that can null
1131 : : * this rel, since they would be of no value while generating paths
1132 : : * for the child. This saves some effort while processing the child
1133 : : * rel, and it also avoids an implementation restriction in
1134 : : * adjust_appendrel_attrs (it can't apply nullingrels to a non-Var).
1135 : : */
1150 1136 : 48486 : childrinfos = NIL;
1137 [ + + + + : 59588 : foreach(lc, rel->joininfo)
+ + ]
1138 : : {
1139 : 11102 : RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1140 : :
1141 [ + + ]: 11102 : if (!bms_overlap(rinfo->clause_relids, rel->nulling_relids))
1142 : 9167 : childrinfos = lappend(childrinfos,
1143 : 9167 : adjust_appendrel_attrs(root,
1144 : : (Node *) rinfo,
1145 : : 1, &appinfo));
1146 : : }
1147 : 48486 : childrel->joininfo = childrinfos;
1148 : :
1149 : : /*
1150 : : * Now for the child's targetlist.
1151 : : *
1152 : : * NB: the resulting childrel->reltarget->exprs may contain arbitrary
1153 : : * expressions, which otherwise would not occur in a rel's targetlist.
1154 : : * Code that might be looking at an appendrel child must cope with
1155 : : * such. (Normally, a rel's targetlist would only include Vars and
1156 : : * PlaceHolderVars.) XXX we do not bother to update the cost or width
1157 : : * fields of childrel->reltarget; not clear if that would be useful.
1158 : : */
2661 efujita@postgresql.o 1159 : 96972 : childrel->reltarget->exprs = (List *)
1160 : 48486 : adjust_appendrel_attrs(root,
1161 : 48486 : (Node *) rel->reltarget->exprs,
1162 : : 1, &appinfo);
1163 : :
1164 : : /*
1165 : : * We have to make child entries in the EquivalenceClass data
1166 : : * structures as well. This is needed either if the parent
1167 : : * participates in some eclass joins (because we will want to consider
1168 : : * inner-indexscan joins on the individual children) or if the parent
1169 : : * has useful pathkeys (because we should try to build MergeAppend
1170 : : * paths that produce those sort orderings).
1171 : : */
1172 [ + + + + ]: 48486 : if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
1173 : 30030 : add_child_rel_equivalences(root, appinfo, rel, childrel);
1174 : 48486 : childrel->has_eclass_joins = rel->has_eclass_joins;
1175 : :
1176 : : /*
1177 : : * Note: we could compute appropriate attr_needed data for the child's
1178 : : * variables, by transforming the parent's attr_needed through the
1179 : : * translated_vars mapping. However, currently there's no need
1180 : : * because attr_needed is only examined for base relations not
1181 : : * otherrels. So we just leave the child's attr_needed empty.
1182 : : */
1183 : :
1184 : : /*
1185 : : * If we consider partitionwise joins with the parent rel, do the same
1186 : : * for partitioned child rels.
1187 : : *
1188 : : * Note: here we abuse the consider_partitionwise_join flag by setting
1189 : : * it for child rels that are not themselves partitioned. We do so to
1190 : : * tell try_partitionwise_join() that the child rel is sufficiently
1191 : : * valid to be used as a per-partition input, even if it later gets
1192 : : * proven to be dummy. (It's not usable until we've set up the
1193 : : * reltarget and EC entries, which we just did.)
1194 : : */
1195 [ + + ]: 48486 : if (rel->consider_partitionwise_join)
2804 1196 : 11007 : childrel->consider_partitionwise_join = true;
1197 : :
1198 : : /*
1199 : : * If parallelism is allowable for this query in general, see whether
1200 : : * it's allowable for this childrel in particular. But if we've
1201 : : * already decided the appendrel is not parallel-safe as a whole,
1202 : : * there's no point in considering parallelism for this child. For
1203 : : * consistency, do this before calling set_rel_size() for the child.
1204 : : */
3593 tgl@sss.pgh.pa.us 1205 [ + + + + ]: 48486 : if (root->glob->parallelModeOK && rel->consider_parallel)
1206 : 36409 : set_rel_consider_parallel(root, childrel, childRTE);
1207 : :
1208 : : /*
1209 : : * Compute the child's size.
1210 : : */
5212 1211 : 48486 : set_rel_size(root, childrel, childRTindex, childRTE);
1212 : :
1213 : : /*
1214 : : * It is possible that constraint exclusion detected a contradiction
1215 : : * within a child subquery, even though we didn't prove one above. If
1216 : : * so, we can skip this child.
1217 : : */
1218 [ + + ]: 48485 : if (IS_DUMMY_REL(childrel))
5337 1219 : 135 : continue;
1220 : :
1221 : : /* We have at least one live child. */
3936 1222 : 48350 : has_live_children = true;
1223 : :
1224 : : /*
1225 : : * If any live child is not parallel-safe, treat the whole appendrel
1226 : : * as not parallel-safe. In future we might be able to generate plans
1227 : : * in which some children are farmed out to workers while others are
1228 : : * not; but we don't have that today, so it's a waste to consider
1229 : : * partial paths anywhere in the appendrel unless it's all safe.
1230 : : * (Child rels visited before this one will be unmarked in
1231 : : * set_append_rel_pathlist().)
1232 : : */
3593 1233 [ + + ]: 48350 : if (!childrel->consider_parallel)
1234 : 12459 : rel->consider_parallel = false;
1235 : :
1236 : : /*
1237 : : * Accumulate size information from each live child.
1238 : : */
3936 1239 [ - + ]: 48350 : Assert(childrel->rows > 0);
1240 : :
442 rguo@postgresql.org 1241 : 48350 : parent_tuples += childrel->tuples;
3936 tgl@sss.pgh.pa.us 1242 : 48350 : parent_rows += childrel->rows;
3704 1243 : 48350 : parent_size += childrel->reltarget->width * childrel->rows;
1244 : :
1245 : : /*
1246 : : * Accumulate per-column estimates too. We need not do anything for
1247 : : * PlaceHolderVars in the parent list. If child expression isn't a
1248 : : * Var, or we didn't record a width estimate for it, we have to fall
1249 : : * back on a datatype-based estimate.
1250 : : *
1251 : : * By construction, child's targetlist is 1-to-1 with parent's.
1252 : : */
1253 [ + + + + : 157998 : forboth(parentvars, rel->reltarget->exprs,
+ + + + +
+ + - +
+ ]
1254 : : childvars, childrel->reltarget->exprs)
1255 : : {
3936 1256 : 109648 : Var *parentvar = (Var *) lfirst(parentvars);
1257 : 109648 : Node *childvar = (Node *) lfirst(childvars);
1258 : :
1861 1259 [ + + + + ]: 109648 : if (IsA(parentvar, Var) && parentvar->varno == parentRTindex)
1260 : : {
3936 1261 : 99170 : int pndx = parentvar->varattno - rel->min_attr;
1262 : 99170 : int32 child_width = 0;
1263 : :
1264 [ + + ]: 99170 : if (IsA(childvar, Var) &&
1265 [ + + ]: 94961 : ((Var *) childvar)->varno == childrel->relid)
1266 : : {
1267 : 94886 : int cndx = ((Var *) childvar)->varattno - childrel->min_attr;
1268 : :
1269 : 94886 : child_width = childrel->attr_widths[cndx];
1270 : : }
1271 [ + + ]: 99170 : if (child_width <= 0)
1272 : 4284 : child_width = get_typavgwidth(exprType(childvar),
1273 : : exprTypmod(childvar));
1274 [ - + ]: 99170 : Assert(child_width > 0);
1275 : 99170 : parent_attrsizes[pndx] += child_width * childrel->rows;
1276 : : }
1277 : : }
1278 : : }
1279 : :
1280 [ + + ]: 20869 : if (has_live_children)
1281 : : {
1282 : : /*
1283 : : * Save the finished size estimates.
1284 : : */
1285 : : int i;
1286 : :
1287 [ - + ]: 20613 : Assert(parent_rows > 0);
442 rguo@postgresql.org 1288 : 20613 : rel->tuples = parent_tuples;
3936 tgl@sss.pgh.pa.us 1289 : 20613 : rel->rows = parent_rows;
3704 1290 : 20613 : rel->reltarget->width = rint(parent_size / parent_rows);
6521 1291 [ + + ]: 189866 : for (i = 0; i < nattrs; i++)
1292 : 169253 : rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
1293 : :
1294 : : /*
1295 : : * Note that we leave rel->pages as zero; this is important to avoid
1296 : : * double-counting the appendrel tree in total_table_pages.
1297 : : */
1298 : : }
1299 : : else
1300 : : {
1301 : : /*
1302 : : * All children were excluded by constraints, so mark the whole
1303 : : * appendrel dummy. We must do this in this phase so that the rel's
1304 : : * dummy-ness is visible when we generate paths for other rels.
1305 : : */
3936 1306 : 256 : set_dummy_rel_pathlist(rel);
1307 : : }
1308 : :
6521 1309 : 20869 : pfree(parent_attrsizes);
5212 1310 : 20869 : }
1311 : :
1312 : : /*
1313 : : * set_append_rel_pathlist
1314 : : * Build access paths for an "append relation"
1315 : : */
1316 : : static void
1317 : 20613 : set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
1318 : : Index rti, RangeTblEntry *rte)
1319 : : {
1320 : 20613 : int parentRTindex = rti;
1321 : 20613 : List *live_childrels = NIL;
1322 : : ListCell *l;
1323 : :
1324 : : /*
1325 : : * Generate access paths for each member relation, and remember the
1326 : : * non-dummy children.
1327 : : */
1328 [ + - + + : 112465 : foreach(l, root->append_rel_list)
+ + ]
1329 : : {
1330 : 91852 : AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1331 : : int childRTindex;
1332 : : RangeTblEntry *childRTE;
1333 : : RelOptInfo *childrel;
1334 : :
1335 : : /* append_rel_list contains all append rels; ignore others */
1336 [ + + ]: 91852 : if (appinfo->parent_relid != parentRTindex)
1337 : 43242 : continue;
1338 : :
1339 : : /* Re-locate the child RTE and RelOptInfo */
1340 : 48610 : childRTindex = appinfo->child_relid;
1341 : 48610 : childRTE = root->simple_rte_array[childRTindex];
1342 : 48610 : childrel = root->simple_rel_array[childRTindex];
1343 : :
1344 : : /*
1345 : : * If set_append_rel_size() decided the parent appendrel was
1346 : : * parallel-unsafe at some point after visiting this child rel, we
1347 : : * need to propagate the unsafety marking down to the child, so that
1348 : : * we don't generate useless partial paths for it.
1349 : : */
3593 1350 [ + + ]: 48610 : if (!rel->consider_parallel)
1351 : 12628 : childrel->consider_parallel = false;
1352 : :
1353 : : /*
1354 : : * Compute the child's access paths.
1355 : : */
5212 1356 : 48610 : set_rel_pathlist(root, childrel, childRTindex, childRTE);
1357 : :
1358 : : /*
1359 : : * If child is dummy, ignore it.
1360 : : */
1361 [ + + ]: 48610 : if (IS_DUMMY_REL(childrel))
1362 : 260 : continue;
1363 : :
1364 : : /*
1365 : : * Child is live, so add it to the live_childrels list for use below.
1366 : : */
5015 1367 : 48350 : live_childrels = lappend(live_childrels, childrel);
1368 : : }
1369 : :
1370 : : /* Add paths to the append relation. */
3339 rhaas@postgresql.org 1371 : 20613 : add_paths_to_append_rel(root, rel, live_childrels);
1372 : 20613 : }
1373 : :
1374 : : /*
1375 : : * set_grouped_rel_pathlist
1376 : : * If a grouped relation for the given 'rel' exists, build partial
1377 : : * aggregation paths for it.
1378 : : */
1379 : : static void
209 rguo@postgresql.org 1380 :GNC 414933 : set_grouped_rel_pathlist(PlannerInfo *root, RelOptInfo *rel)
1381 : : {
1382 : : RelOptInfo *grouped_rel;
1383 : :
1384 : : /*
1385 : : * If there are no aggregate expressions or grouping expressions, eager
1386 : : * aggregation is not possible.
1387 : : */
1388 [ + + ]: 414933 : if (root->agg_clause_list == NIL ||
1389 [ + + ]: 2774 : root->group_expr_list == NIL)
1390 : 412683 : return;
1391 : :
1392 : : /* Add paths to the grouped base relation if one exists. */
1393 : 2250 : grouped_rel = rel->grouped_rel;
1394 [ + + ]: 2250 : if (grouped_rel)
1395 : : {
1396 [ - + ]: 467 : Assert(IS_GROUPED_REL(grouped_rel));
1397 : :
1398 : 467 : generate_grouped_paths(root, grouped_rel, rel);
1399 : 467 : set_cheapest(grouped_rel);
1400 : : }
1401 : : }
1402 : :
1403 : :
1404 : : /*
1405 : : * add_paths_to_append_rel
1406 : : * Generate paths for the given append relation given the set of non-dummy
1407 : : * child rels.
1408 : : *
1409 : : * The function collects all parameterizations and orderings supported by the
1410 : : * non-dummy children. For every such parameterization or ordering, it creates
1411 : : * an append path collecting one path from each non-dummy child with given
1412 : : * parameterization or ordering. Similarly it collects partial paths from
1413 : : * non-dummy children to create partial append paths.
1414 : : */
1415 : : void
3339 rhaas@postgresql.org 1416 :CBC 37516 : add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
1417 : : List *live_childrels)
1418 : : {
84 rhaas@postgresql.org 1419 :GNC 37516 : AppendPathInput unparameterized = {0};
1420 : 37516 : AppendPathInput startup = {0};
1421 : 37516 : AppendPathInput partial_only = {0};
1422 : 37516 : AppendPathInput parallel_append = {0};
1423 : 37516 : bool unparameterized_valid = true;
1424 : 37516 : bool startup_valid = true;
1425 : 37516 : bool partial_only_valid = true;
1426 : 37516 : bool parallel_append_valid = true;
3339 rhaas@postgresql.org 1427 :CBC 37516 : List *all_child_pathkeys = NIL;
1428 : 37516 : List *all_child_outers = NIL;
1429 : : ListCell *l;
3073 1430 : 37516 : double partial_rows = -1;
1431 : :
1432 : : /* If appropriate, consider parallel append */
84 rhaas@postgresql.org 1433 [ + + + + ]:GNC 37516 : parallel_append_valid = enable_parallel_append && rel->consider_parallel;
1434 : :
1435 : : /*
1436 : : * For every non-dummy child, remember the cheapest path. Also, identify
1437 : : * all pathkeys (orderings) and parameterizations (required_outer sets)
1438 : : * available for the non-dummy member relations.
1439 : : */
3339 rhaas@postgresql.org 1440 [ + - + + :CBC 123536 : foreach(l, live_childrels)
+ + ]
1441 : : {
1442 : 86020 : RelOptInfo *childrel = lfirst(l);
1443 : : ListCell *lcp;
3073 1444 : 86020 : Path *cheapest_partial_path = NULL;
1445 : :
1446 : : /*
1447 : : * If child has an unparameterized cheapest-total path, add that to
1448 : : * the unparameterized Append path we are constructing for the parent.
1449 : : * If not, there's no workable unparameterized path.
1450 : : *
1451 : : * With partitionwise aggregates, the child rel's pathlist may be
1452 : : * empty, so don't assume that a path exists here.
1453 : : */
2966 1454 [ + - ]: 86020 : if (childrel->pathlist != NIL &&
1455 [ + + ]: 86020 : childrel->cheapest_total_path->param_info == NULL)
3073 1456 : 85390 : accumulate_append_subpath(childrel->cheapest_total_path,
1457 : : &unparameterized.subpaths, NULL, &unparameterized.child_append_relid_sets);
1458 : : else
84 rhaas@postgresql.org 1459 :GNC 630 : unparameterized_valid = false;
1460 : :
1461 : : /*
1462 : : * When the planner is considering cheap startup plans, we'll also
1463 : : * collect all the cheapest_startup_paths (if set) and build an
1464 : : * AppendPath containing those as subpaths.
1465 : : */
938 drowley@postgresql.o 1466 [ + + + + ]:CBC 86020 : if (rel->consider_startup && childrel->cheapest_startup_path != NULL)
1467 : 1435 : {
1468 : : Path *cheapest_path;
1469 : :
1470 : : /*
1471 : : * With an indication of how many tuples the query should provide,
1472 : : * the optimizer tries to choose the path optimal for that
1473 : : * specific number of tuples.
1474 : : */
421 akorotkov@postgresql 1475 [ + - ]: 1435 : if (root->tuple_fraction > 0.0)
1476 : : cheapest_path =
1477 : 1435 : get_cheapest_fractional_path(childrel,
1478 : : root->tuple_fraction);
1479 : : else
421 akorotkov@postgresql 1480 :UBC 0 : cheapest_path = childrel->cheapest_startup_path;
1481 : :
1482 : : /* cheapest_startup_path must not be a parameterized path. */
421 akorotkov@postgresql 1483 [ - + ]:CBC 1435 : Assert(cheapest_path->param_info == NULL);
1484 : 1435 : accumulate_append_subpath(cheapest_path,
1485 : : &startup.subpaths,
1486 : : NULL,
1487 : : &startup.child_append_relid_sets);
1488 : : }
1489 : : else
84 rhaas@postgresql.org 1490 :GNC 84585 : startup_valid = false;
1491 : :
1492 : :
1493 : : /* Same idea, but for a partial plan. */
3758 rhaas@postgresql.org 1494 [ + + ]:CBC 86020 : if (childrel->partial_pathlist != NIL)
1495 : : {
3073 1496 : 54072 : cheapest_partial_path = linitial(childrel->partial_pathlist);
1497 : 54072 : accumulate_append_subpath(cheapest_partial_path,
1498 : : &partial_only.partial_subpaths, NULL,
1499 : : &partial_only.child_append_relid_sets);
1500 : : }
1501 : : else
84 rhaas@postgresql.org 1502 :GNC 31948 : partial_only_valid = false;
1503 : :
1504 : : /*
1505 : : * Same idea, but for a parallel append mixing partial and non-partial
1506 : : * paths.
1507 : : */
1508 [ + + ]: 86020 : if (parallel_append_valid)
1509 : : {
3073 rhaas@postgresql.org 1510 :CBC 65563 : Path *nppath = NULL;
1511 : :
1512 : : nppath =
1513 : 65563 : get_cheapest_parallel_safe_total_inner(childrel->pathlist);
1514 : :
1515 [ + + + + ]: 65563 : if (cheapest_partial_path == NULL && nppath == NULL)
1516 : : {
1517 : : /* Neither a partial nor a parallel-safe path? Forget it. */
84 rhaas@postgresql.org 1518 :GNC 413 : parallel_append_valid = false;
1519 : : }
3073 rhaas@postgresql.org 1520 [ + + + + ]:CBC 65150 : else if (nppath == NULL ||
1521 : 53697 : (cheapest_partial_path != NULL &&
1522 [ + + ]: 53697 : cheapest_partial_path->total_cost < nppath->total_cost))
1523 : : {
1524 : : /* Partial path is cheaper or the only option. */
1525 [ - + ]: 53500 : Assert(cheapest_partial_path != NULL);
1526 : 53500 : accumulate_append_subpath(cheapest_partial_path,
1527 : : ¶llel_append.partial_subpaths,
1528 : : ¶llel_append.subpaths,
1529 : : ¶llel_append.child_append_relid_sets);
1530 : : }
1531 : : else
1532 : : {
1533 : : /*
1534 : : * Either we've got only a non-partial path, or we think that
1535 : : * a single backend can execute the best non-partial path
1536 : : * faster than all the parallel backends working together can
1537 : : * execute the best partial path.
1538 : : *
1539 : : * It might make sense to be more aggressive here. Even if
1540 : : * the best non-partial path is more expensive than the best
1541 : : * partial path, it could still be better to choose the
1542 : : * non-partial path if there are several such paths that can
1543 : : * be given to different workers. For now, we don't try to
1544 : : * figure that out.
1545 : : */
1546 : 11650 : accumulate_append_subpath(nppath,
1547 : : ¶llel_append.subpaths,
1548 : : NULL,
1549 : : ¶llel_append.child_append_relid_sets);
1550 : : }
1551 : : }
1552 : :
1553 : : /*
1554 : : * Collect lists of all the available path orderings and
1555 : : * parameterizations for all the children. We use these as a
1556 : : * heuristic to indicate which sort orderings and parameterizations we
1557 : : * should build Append and MergeAppend paths for.
1558 : : */
5212 tgl@sss.pgh.pa.us 1559 [ + - + + : 200424 : foreach(lcp, childrel->pathlist)
+ + ]
1560 : : {
1561 : 114404 : Path *childpath = (Path *) lfirst(lcp);
1562 : 114404 : List *childkeys = childpath->pathkeys;
5129 1563 [ + + ]: 114404 : Relids childouter = PATH_REQ_OUTER(childpath);
1564 : :
1565 : : /* Unsorted paths don't contribute to pathkey list */
5212 1566 [ + + ]: 114404 : if (childkeys != NIL)
1567 : : {
1568 : : ListCell *lpk;
1569 : 28680 : bool found = false;
1570 : :
1571 : : /* Have we already seen this ordering? */
1572 [ + + + + : 28833 : foreach(lpk, all_child_pathkeys)
+ + ]
1573 : : {
1574 : 19540 : List *existing_pathkeys = (List *) lfirst(lpk);
1575 : :
1576 [ + + ]: 19540 : if (compare_pathkeys(existing_pathkeys,
1577 : : childkeys) == PATHKEYS_EQUAL)
1578 : : {
1579 : 19387 : found = true;
1580 : 19387 : break;
1581 : : }
1582 : : }
1583 [ + + ]: 28680 : if (!found)
1584 : : {
1585 : : /* No, so add it to all_child_pathkeys */
1586 : 9293 : all_child_pathkeys = lappend(all_child_pathkeys,
1587 : : childkeys);
1588 : : }
1589 : : }
1590 : :
1591 : : /* Unparameterized paths don't contribute to param-set list */
1592 [ + + ]: 114404 : if (childouter)
1593 : : {
1594 : : ListCell *lco;
1595 : 5693 : bool found = false;
1596 : :
1597 : : /* Have we already seen this param set? */
1598 [ + + + + : 6341 : foreach(lco, all_child_outers)
+ + ]
1599 : : {
5077 bruce@momjian.us 1600 : 4149 : Relids existing_outers = (Relids) lfirst(lco);
1601 : :
5212 tgl@sss.pgh.pa.us 1602 [ + + ]: 4149 : if (bms_equal(existing_outers, childouter))
1603 : : {
1604 : 3501 : found = true;
1605 : 3501 : break;
1606 : : }
1607 : : }
1608 [ + + ]: 5693 : if (!found)
1609 : : {
1610 : : /* No, so add it to all_child_outers */
1611 : 2192 : all_child_outers = lappend(all_child_outers,
1612 : : childouter);
1613 : : }
1614 : : }
1615 : : }
1616 : : }
1617 : :
1618 : : /*
1619 : : * If we found unparameterized paths for all children, build an unordered,
1620 : : * unparameterized Append path for the rel. (Note: this is correct even
1621 : : * if we have zero or one live subpath due to constraint exclusion.)
1622 : : */
84 rhaas@postgresql.org 1623 [ + + ]:GNC 37516 : if (unparameterized_valid)
1624 : 37246 : add_path(rel, (Path *) create_append_path(root, rel, unparameterized,
1625 : : NIL, NULL, 0, false,
1626 : : -1));
1627 : :
1628 : : /* build an AppendPath for the cheap startup paths, if valid */
1629 [ + + ]: 37516 : if (startup_valid)
1630 : 580 : add_path(rel, (Path *) create_append_path(root, rel, startup,
1631 : : NIL, NULL, 0, false, -1));
1632 : :
1633 : : /*
1634 : : * Consider an append of unordered, unparameterized partial paths. Make
1635 : : * it parallel-aware if possible.
1636 : : */
1637 [ + + + - ]: 37516 : if (partial_only_valid && partial_only.partial_subpaths != NIL)
1638 : : {
1639 : : AppendPath *appendpath;
1640 : : ListCell *lc;
3617 rhaas@postgresql.org 1641 :CBC 22085 : int parallel_workers = 0;
1642 : :
1643 : : /* Find the highest number of workers requested for any subpath. */
84 rhaas@postgresql.org 1644 [ + - + + :GNC 79863 : foreach(lc, partial_only.partial_subpaths)
+ + ]
1645 : : {
3758 rhaas@postgresql.org 1646 :CBC 57778 : Path *path = lfirst(lc);
1647 : :
3617 1648 : 57778 : parallel_workers = Max(parallel_workers, path->parallel_workers);
1649 : : }
1650 [ - + ]: 22085 : Assert(parallel_workers > 0);
1651 : :
1652 : : /*
1653 : : * If the use of parallel append is permitted, always request at least
1654 : : * log2(# of children) workers. We assume it can be useful to have
1655 : : * extra workers in this case because they will be spread out across
1656 : : * the children. The precise formula is just a guess, but we don't
1657 : : * want to end up with a radically different answer for a table with N
1658 : : * partitions vs. an unpartitioned table with the same data, so the
1659 : : * use of some kind of log-scaling here seems to make some sense.
1660 : : */
3073 1661 [ + + ]: 22085 : if (enable_parallel_append)
1662 : : {
1663 [ + + ]: 22045 : parallel_workers = Max(parallel_workers,
1664 : : pg_leftmost_one_pos32(list_length(live_childrels)) + 1);
1665 : 22045 : parallel_workers = Min(parallel_workers,
1666 : : max_parallel_workers_per_gather);
1667 : : }
1668 [ - + ]: 22085 : Assert(parallel_workers > 0);
1669 : :
1670 : : /* Generate a partial append path. */
84 rhaas@postgresql.org 1671 :GNC 22085 : appendpath = create_append_path(root, rel, partial_only,
1672 : : NIL, NULL, parallel_workers,
1673 : : enable_parallel_append,
1674 : : -1);
1675 : :
1676 : : /*
1677 : : * Make sure any subsequent partial paths use the same row count
1678 : : * estimate.
1679 : : */
3073 rhaas@postgresql.org 1680 :CBC 22085 : partial_rows = appendpath->path.rows;
1681 : :
1682 : : /* Add the path. */
1683 : 22085 : add_partial_path(rel, (Path *) appendpath);
1684 : : }
1685 : :
1686 : : /*
1687 : : * Consider a parallel-aware append using a mix of partial and non-partial
1688 : : * paths. (This only makes sense if there's at least one child which has
1689 : : * a non-partial path that is substantially cheaper than any partial path;
1690 : : * otherwise, we should use the append path added in the previous step.)
1691 : : */
84 rhaas@postgresql.org 1692 [ + + + + ]:GNC 37516 : if (parallel_append_valid && parallel_append.subpaths != NIL)
1693 : : {
1694 : : AppendPath *appendpath;
1695 : : ListCell *lc;
3073 rhaas@postgresql.org 1696 :CBC 4259 : int parallel_workers = 0;
1697 : :
1698 : : /*
1699 : : * Find the highest number of workers requested for any partial
1700 : : * subpath.
1701 : : */
84 rhaas@postgresql.org 1702 [ + + + + :GNC 5004 : foreach(lc, parallel_append.partial_subpaths)
+ + ]
1703 : : {
3073 rhaas@postgresql.org 1704 :CBC 745 : Path *path = lfirst(lc);
1705 : :
1706 : 745 : parallel_workers = Max(parallel_workers, path->parallel_workers);
1707 : : }
1708 : :
1709 : : /*
1710 : : * Same formula here as above. It's even more important in this
1711 : : * instance because the non-partial paths won't contribute anything to
1712 : : * the planned number of parallel workers.
1713 : : */
1714 [ + - ]: 4259 : parallel_workers = Max(parallel_workers,
1715 : : pg_leftmost_one_pos32(list_length(live_childrels)) + 1);
1716 : 4259 : parallel_workers = Min(parallel_workers,
1717 : : max_parallel_workers_per_gather);
1718 [ - + ]: 4259 : Assert(parallel_workers > 0);
1719 : :
84 rhaas@postgresql.org 1720 :GNC 4259 : appendpath = create_append_path(root, rel, parallel_append,
1721 : : NIL, NULL, parallel_workers, true,
1722 : : partial_rows);
3758 rhaas@postgresql.org 1723 :CBC 4259 : add_partial_path(rel, (Path *) appendpath);
1724 : : }
1725 : :
1726 : : /*
1727 : : * Also build unparameterized ordered append paths based on the collected
1728 : : * list of child pathkeys.
1729 : : */
84 rhaas@postgresql.org 1730 [ + + ]:GNC 37516 : if (unparameterized_valid)
2587 tgl@sss.pgh.pa.us 1731 :CBC 37246 : generate_orderedappend_paths(root, rel, live_childrels,
1732 : : all_child_pathkeys);
1733 : :
1734 : : /*
1735 : : * Build Append paths for each parameterization seen among the child rels.
1736 : : * (This may look pretty expensive, but in most cases of practical
1737 : : * interest, the child rels will expose mostly the same parameterizations,
1738 : : * so that not that many cases actually get considered here.)
1739 : : *
1740 : : * The Append node itself cannot enforce quals, so all qual checking must
1741 : : * be done in the child paths. This means that to have a parameterized
1742 : : * Append path, we must have the exact same parameterization for each
1743 : : * child path; otherwise some children might be failing to check the
1744 : : * moved-down quals. To make them match up, we can try to increase the
1745 : : * parameterization of lesser-parameterized paths.
1746 : : */
5212 1747 [ + + + + : 39708 : foreach(l, all_child_outers)
+ + ]
1748 : : {
5077 bruce@momjian.us 1749 : 2192 : Relids required_outer = (Relids) lfirst(l);
1750 : : ListCell *lcr;
84 rhaas@postgresql.org 1751 :GNC 2192 : AppendPathInput parameterized = {0};
1752 : 2192 : bool parameterized_valid = true;
1753 : :
1754 : : /* Select the child paths for an Append with this parameterization */
5212 tgl@sss.pgh.pa.us 1755 [ + - + + :CBC 7984 : foreach(lcr, live_childrels)
+ + ]
1756 : : {
1757 : 5802 : RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1758 : : Path *subpath;
1759 : :
2966 rhaas@postgresql.org 1760 [ - + ]: 5802 : if (childrel->pathlist == NIL)
1761 : : {
1762 : : /* failed to make a suitable path for this child */
84 rhaas@postgresql.org 1763 :UNC 0 : parameterized_valid = false;
2966 rhaas@postgresql.org 1764 :UBC 0 : break;
1765 : : }
1766 : :
4685 tgl@sss.pgh.pa.us 1767 :CBC 5802 : subpath = get_cheapest_parameterized_child_path(root,
1768 : : childrel,
1769 : : required_outer);
1770 [ + + ]: 5802 : if (subpath == NULL)
1771 : : {
1772 : : /* failed to make a suitable path for this child */
84 rhaas@postgresql.org 1773 :GNC 10 : parameterized_valid = false;
4685 tgl@sss.pgh.pa.us 1774 :CBC 10 : break;
1775 : : }
84 rhaas@postgresql.org 1776 :GNC 5792 : accumulate_append_subpath(subpath, ¶meterized.subpaths, NULL,
1777 : : ¶meterized.child_append_relid_sets);
1778 : : }
1779 : :
1780 [ + + ]: 2192 : if (parameterized_valid)
5129 tgl@sss.pgh.pa.us 1781 :CBC 2182 : add_path(rel, (Path *)
84 rhaas@postgresql.org 1782 :GNC 2182 : create_append_path(root, rel, parameterized,
1783 : : NIL, required_outer, 0, false,
1784 : : -1));
1785 : : }
1786 : :
1787 : : /*
1788 : : * When there is only a single child relation, the Append path can inherit
1789 : : * any ordering available for the child rel's path, so that it's useful to
1790 : : * consider ordered partial paths. Above we only considered the cheapest
1791 : : * partial path for each child, but let's also make paths using any
1792 : : * partial paths that have pathkeys.
1793 : : */
2598 tgl@sss.pgh.pa.us 1794 [ + + ]:CBC 37516 : if (list_length(live_childrels) == 1)
1795 : : {
1796 : 10453 : RelOptInfo *childrel = (RelOptInfo *) linitial(live_childrels);
1797 : :
1798 : : /* skip the cheapest partial path, since we already used that above */
2010 drowley@postgresql.o 1799 [ + + + + : 10602 : for_each_from(l, childrel->partial_pathlist, 1)
+ + ]
1800 : : {
2598 tgl@sss.pgh.pa.us 1801 : 149 : Path *path = (Path *) lfirst(l);
1802 : : AppendPath *appendpath;
84 rhaas@postgresql.org 1803 :GNC 149 : AppendPathInput append = {0};
1804 : :
1805 : : /* skip paths with no pathkeys. */
2010 drowley@postgresql.o 1806 [ - + ]:CBC 149 : if (path->pathkeys == NIL)
2598 tgl@sss.pgh.pa.us 1807 :UBC 0 : continue;
1808 : :
84 rhaas@postgresql.org 1809 :GNC 149 : append.partial_subpaths = list_make1(path);
1810 : 149 : appendpath = create_append_path(root, rel, append, NIL, NULL,
1811 : : path->parallel_workers, true,
1812 : : partial_rows);
2598 tgl@sss.pgh.pa.us 1813 :CBC 149 : add_partial_path(rel, (Path *) appendpath);
1814 : : }
1815 : : }
5212 1816 : 37516 : }
1817 : :
1818 : : /*
1819 : : * generate_orderedappend_paths
1820 : : * Generate ordered append paths for an append relation
1821 : : *
1822 : : * Usually we generate MergeAppend paths here, but there are some special
1823 : : * cases where we can generate simple Append paths, because the subpaths
1824 : : * can provide tuples in the required order already.
1825 : : *
1826 : : * We generate a path for each ordering (pathkey list) appearing in
1827 : : * all_child_pathkeys.
1828 : : *
1829 : : * We consider the cheapest-startup and cheapest-total cases, and also the
1830 : : * cheapest-fractional case when not all tuples need to be retrieved. For each
1831 : : * interesting ordering, we collect all the cheapest startup subpaths, all the
1832 : : * cheapest total paths, and, if applicable, all the cheapest fractional paths,
1833 : : * and build a suitable path for each case.
1834 : : *
1835 : : * We don't currently generate any parameterized ordered paths here. While
1836 : : * it would not take much more code here to do so, it's very unclear that it
1837 : : * is worth the planning cycles to investigate such paths: there's little
1838 : : * use for an ordered path on the inside of a nestloop. In fact, it's likely
1839 : : * that the current coding of add_path would reject such paths out of hand,
1840 : : * because add_path gives no credit for sort ordering of parameterized paths,
1841 : : * and a parameterized MergeAppend is going to be more expensive than the
1842 : : * corresponding parameterized Append path. If we ever try harder to support
1843 : : * parameterized mergejoin plans, it might be worth adding support for
1844 : : * parameterized paths here to feed such joins. (See notes in
1845 : : * optimizer/README for why that might not ever happen, though.)
1846 : : */
1847 : : static void
2587 1848 : 37246 : generate_orderedappend_paths(PlannerInfo *root, RelOptInfo *rel,
1849 : : List *live_childrels,
1850 : : List *all_child_pathkeys)
1851 : : {
1852 : : ListCell *lcp;
1853 : 37246 : List *partition_pathkeys = NIL;
1854 : 37246 : List *partition_pathkeys_desc = NIL;
1855 : 37246 : bool partition_pathkeys_partial = true;
1856 : 37246 : bool partition_pathkeys_desc_partial = true;
1857 : :
1858 : : /*
1859 : : * Some partitioned table setups may allow us to use an Append node
1860 : : * instead of a MergeAppend. This is possible in cases such as RANGE
1861 : : * partitioned tables where it's guaranteed that an earlier partition must
1862 : : * contain rows which come earlier in the sort order. To detect whether
1863 : : * this is relevant, build pathkey descriptions of the partition ordering,
1864 : : * for both forward and reverse scans.
1865 : : */
1866 [ + + + + : 59640 : if (rel->part_scheme != NULL && IS_SIMPLE_REL(rel) &&
+ + + + ]
1736 drowley@postgresql.o 1867 : 22394 : partitions_are_ordered(rel->boundinfo, rel->live_parts))
1868 : : {
2587 tgl@sss.pgh.pa.us 1869 : 18390 : partition_pathkeys = build_partition_pathkeys(root, rel,
1870 : : ForwardScanDirection,
1871 : : &partition_pathkeys_partial);
1872 : :
1873 : 18390 : partition_pathkeys_desc = build_partition_pathkeys(root, rel,
1874 : : BackwardScanDirection,
1875 : : &partition_pathkeys_desc_partial);
1876 : :
1877 : : /*
1878 : : * You might think we should truncate_useless_pathkeys here, but
1879 : : * allowing partition keys which are a subset of the query's pathkeys
1880 : : * can often be useful. For example, consider a table partitioned by
1881 : : * RANGE (a, b), and a query with ORDER BY a, b, c. If we have child
1882 : : * paths that can produce the a, b, c ordering (perhaps via indexes on
1883 : : * (a, b, c)) then it works to consider the appendrel output as
1884 : : * ordered by a, b, c.
1885 : : */
1886 : : }
1887 : :
1888 : : /* Now consider each interesting sort ordering */
5212 1889 [ + + + + : 46489 : foreach(lcp, all_child_pathkeys)
+ + ]
1890 : : {
1891 : 9243 : List *pathkeys = (List *) lfirst(lcp);
84 rhaas@postgresql.org 1892 :GNC 9243 : AppendPathInput startup = {0};
1893 : 9243 : AppendPathInput total = {0};
1894 : 9243 : AppendPathInput fractional = {0};
5504 bruce@momjian.us 1895 :CBC 9243 : bool startup_neq_total = false;
181 rguo@postgresql.org 1896 :GNC 9243 : bool fraction_neq_total = false;
1897 : : bool match_partition_order;
1898 : : bool match_partition_order_desc;
1899 : : int end_index;
1900 : : int first_index;
1901 : : int direction;
1902 : :
1903 : : /*
1904 : : * Determine if this sort ordering matches any partition pathkeys we
1905 : : * have, for both ascending and descending partition order. If the
1906 : : * partition pathkeys happen to be contained in pathkeys then it still
1907 : : * works, as described above, providing that the partition pathkeys
1908 : : * are complete and not just a prefix of the partition keys. (In such
1909 : : * cases we'll be relying on the child paths to have sorted the
1910 : : * lower-order columns of the required pathkeys.)
1911 : : */
2587 tgl@sss.pgh.pa.us 1912 :CBC 9243 : match_partition_order =
1913 [ + + ]: 16563 : pathkeys_contained_in(pathkeys, partition_pathkeys) ||
1914 [ + + + + ]: 7484 : (!partition_pathkeys_partial &&
1915 : 164 : pathkeys_contained_in(partition_pathkeys, pathkeys));
1916 : :
1917 [ + + + + ]: 23681 : match_partition_order_desc = !match_partition_order &&
1918 : 7234 : (pathkeys_contained_in(pathkeys, partition_pathkeys_desc) ||
1919 [ + + + + ]: 7252 : (!partition_pathkeys_desc_partial &&
1920 : 48 : pathkeys_contained_in(partition_pathkeys_desc, pathkeys)));
1921 : :
1922 : : /*
1923 : : * When the required pathkeys match the reverse of the partition
1924 : : * order, we must build the list of paths in reverse starting with the
1925 : : * last matching partition first. We can get away without making any
1926 : : * special cases for this in the loop below by just looping backward
1927 : : * over the child relations in this case.
1928 : : */
1170 drowley@postgresql.o 1929 [ + + ]: 9243 : if (match_partition_order_desc)
1930 : : {
1931 : : /* loop backward */
1932 : 40 : first_index = list_length(live_childrels) - 1;
1933 : 40 : end_index = -1;
1934 : 40 : direction = -1;
1935 : :
1936 : : /*
1937 : : * Set this to true to save us having to check for
1938 : : * match_partition_order_desc in the loop below.
1939 : : */
1940 : 40 : match_partition_order = true;
1941 : : }
1942 : : else
1943 : : {
1944 : : /* for all other case, loop forward */
1945 : 9203 : first_index = 0;
1946 : 9203 : end_index = list_length(live_childrels);
1947 : 9203 : direction = 1;
1948 : : }
1949 : :
1950 : : /* Select the child paths for this ordering... */
1951 [ + + ]: 33383 : for (int i = first_index; i != end_index; i += direction)
1952 : : {
1953 : 24140 : RelOptInfo *childrel = list_nth_node(RelOptInfo, live_childrels, i);
1954 : : Path *cheapest_startup,
1955 : : *cheapest_total,
1574 tomas.vondra@postgre 1956 : 24140 : *cheapest_fractional = NULL;
1957 : :
1958 : : /* Locate the right paths, if they are available. */
1959 : : cheapest_startup =
5682 tgl@sss.pgh.pa.us 1960 : 24140 : get_cheapest_path_for_pathkeys(childrel->pathlist,
1961 : : pathkeys,
1962 : : NULL,
1963 : : STARTUP_COST,
1964 : : false);
1965 : : cheapest_total =
1966 : 24140 : get_cheapest_path_for_pathkeys(childrel->pathlist,
1967 : : pathkeys,
1968 : : NULL,
1969 : : TOTAL_COST,
1970 : : false);
1971 : :
1972 : : /*
1973 : : * If we can't find any paths with the right order just use the
1974 : : * cheapest-total path; we'll have to sort it later.
1975 : : */
5212 1976 [ + + - + ]: 24140 : if (cheapest_startup == NULL || cheapest_total == NULL)
1977 : : {
5129 1978 : 509 : cheapest_startup = cheapest_total =
1979 : : childrel->cheapest_total_path;
1980 : : /* Assert we do have an unparameterized path for this child */
5015 1981 [ - + ]: 509 : Assert(cheapest_total->param_info == NULL);
1982 : : }
1983 : :
1984 : : /*
1985 : : * When building a fractional path, determine a cheapest
1986 : : * fractional path for each child relation too. Looking at startup
1987 : : * and total costs is not enough, because the cheapest fractional
1988 : : * path may be dominated by two separate paths (one for startup,
1989 : : * one for total).
1990 : : *
1991 : : * When needed (building fractional path), determine the cheapest
1992 : : * fractional path too.
1993 : : */
1574 tomas.vondra@postgre 1994 [ + + ]: 24140 : if (root->tuple_fraction > 0)
1995 : : {
352 akorotkov@postgresql 1996 : 736 : double path_fraction = root->tuple_fraction;
1997 : :
1998 : : /*
1999 : : * We should not have a dummy child relation here. However,
2000 : : * we cannot use childrel->rows to compute the tuple fraction,
2001 : : * as childrel can be an upper relation with an unset row
2002 : : * estimate. Instead, we use the row estimate from the
2003 : : * cheapest_total path, which should already have been forced
2004 : : * to a sane value.
2005 : : */
181 rguo@postgresql.org 2006 [ - + ]: 736 : Assert(cheapest_total->rows > 0);
2007 : :
2008 : : /* Convert absolute limit to a path fraction */
352 akorotkov@postgresql 2009 [ + - ]: 736 : if (path_fraction >= 1.0)
181 rguo@postgresql.org 2010 : 736 : path_fraction /= cheapest_total->rows;
2011 : :
2012 : : cheapest_fractional =
1574 tomas.vondra@postgre 2013 : 736 : get_cheapest_fractional_path_for_pathkeys(childrel->pathlist,
2014 : : pathkeys,
2015 : : NULL,
2016 : : path_fraction);
2017 : :
2018 : : /*
2019 : : * If we found no path with matching pathkeys, use the
2020 : : * cheapest total path instead.
2021 : : *
2022 : : * XXX We might consider partially sorted paths too (with an
2023 : : * incremental sort on top). But we'd have to build all the
2024 : : * incremental paths, do the costing etc.
2025 : : *
2026 : : * Also, notice whether we actually have different paths for
2027 : : * the "fractional" and "total" cases. This helps avoid
2028 : : * generating two identical ordered append paths.
2029 : : */
181 rguo@postgresql.org 2030 [ + + ]:GNC 736 : if (cheapest_fractional == NULL)
1574 tomas.vondra@postgre 2031 :CBC 34 : cheapest_fractional = cheapest_total;
181 rguo@postgresql.org 2032 [ - + ]:GNC 702 : else if (cheapest_fractional != cheapest_total)
181 rguo@postgresql.org 2033 :UNC 0 : fraction_neq_total = true;
2034 : : }
2035 : :
2036 : : /*
2037 : : * Notice whether we actually have different paths for the
2038 : : * "cheapest" and "total" cases. This helps avoid generating two
2039 : : * identical ordered append paths.
2040 : : */
5682 tgl@sss.pgh.pa.us 2041 [ + + ]:CBC 24140 : if (cheapest_startup != cheapest_total)
2042 : 68 : startup_neq_total = true;
2043 : :
2044 : : /*
2045 : : * Collect the appropriate child paths. The required logic varies
2046 : : * for the Append and MergeAppend cases.
2047 : : */
2587 2048 [ + + ]: 24140 : if (match_partition_order)
2049 : : {
2050 : : /*
2051 : : * We're going to make a plain Append path. We don't need
2052 : : * most of what accumulate_append_subpath would do, but we do
2053 : : * want to cut out child Appends or MergeAppends if they have
2054 : : * just a single subpath (and hence aren't doing anything
2055 : : * useful).
2056 : : */
2057 : : cheapest_startup =
84 rhaas@postgresql.org 2058 :GNC 5472 : get_singleton_append_subpath(cheapest_startup,
2059 : : &startup.child_append_relid_sets);
2060 : : cheapest_total =
2061 : 5472 : get_singleton_append_subpath(cheapest_total,
2062 : : &total.child_append_relid_sets);
2063 : :
2064 : 5472 : startup.subpaths = lappend(startup.subpaths, cheapest_startup);
2065 : 5472 : total.subpaths = lappend(total.subpaths, cheapest_total);
2066 : :
1574 tomas.vondra@postgre 2067 [ + + ]:CBC 5472 : if (cheapest_fractional)
2068 : : {
2069 : : cheapest_fractional =
84 rhaas@postgresql.org 2070 :GNC 120 : get_singleton_append_subpath(cheapest_fractional,
2071 : : &fractional.child_append_relid_sets);
2072 : 120 : fractional.subpaths =
2073 : 120 : lappend(fractional.subpaths, cheapest_fractional);
2074 : : }
2075 : : }
2076 : : else
2077 : : {
2078 : : /*
2079 : : * Otherwise, rely on accumulate_append_subpath to collect the
2080 : : * child paths for the MergeAppend.
2081 : : */
2587 tgl@sss.pgh.pa.us 2082 :CBC 18668 : accumulate_append_subpath(cheapest_startup,
2083 : : &startup.subpaths, NULL,
2084 : : &startup.child_append_relid_sets);
2085 : 18668 : accumulate_append_subpath(cheapest_total,
2086 : : &total.subpaths, NULL,
2087 : : &total.child_append_relid_sets);
2088 : :
1574 tomas.vondra@postgre 2089 [ + + ]: 18668 : if (cheapest_fractional)
2090 : 616 : accumulate_append_subpath(cheapest_fractional,
2091 : : &fractional.subpaths, NULL,
2092 : : &fractional.child_append_relid_sets);
2093 : : }
2094 : : }
2095 : :
2096 : : /* ... and build the Append or MergeAppend paths */
1170 drowley@postgresql.o 2097 [ + + ]: 9243 : if (match_partition_order)
2098 : : {
2099 : : /* We only need Append */
2587 tgl@sss.pgh.pa.us 2100 : 2049 : add_path(rel, (Path *) create_append_path(root,
2101 : : rel,
2102 : : startup,
2103 : : pathkeys,
2104 : : NULL,
2105 : : 0,
2106 : : false,
2107 : : -1));
2108 [ - + ]: 2049 : if (startup_neq_total)
2587 tgl@sss.pgh.pa.us 2109 :UBC 0 : add_path(rel, (Path *) create_append_path(root,
2110 : : rel,
2111 : : total,
2112 : : pathkeys,
2113 : : NULL,
2114 : : 0,
2115 : : false,
2116 : : -1));
2117 : :
84 rhaas@postgresql.org 2118 [ + + - + ]:GNC 2049 : if (fractional.subpaths && fraction_neq_total)
1574 tomas.vondra@postgre 2119 :LBC (36) : add_path(rel, (Path *) create_append_path(root,
2120 : : rel,
2121 : : fractional,
2122 : : pathkeys,
2123 : : NULL,
2124 : : 0,
2125 : : false,
2126 : : -1));
2127 : : }
2128 : : else
2129 : : {
2130 : : /* We need MergeAppend */
5682 tgl@sss.pgh.pa.us 2131 :CBC 7194 : add_path(rel, (Path *) create_merge_append_path(root,
2132 : : rel,
2133 : : startup.subpaths,
2134 : : startup.child_append_relid_sets,
2135 : : pathkeys,
2136 : : NULL));
2587 2137 [ + + ]: 7194 : if (startup_neq_total)
2138 : 44 : add_path(rel, (Path *) create_merge_append_path(root,
2139 : : rel,
2140 : : total.subpaths,
2141 : : total.child_append_relid_sets,
2142 : : pathkeys,
2143 : : NULL));
2144 : :
84 rhaas@postgresql.org 2145 [ + + - + ]:GNC 7194 : if (fractional.subpaths && fraction_neq_total)
1574 tomas.vondra@postgre 2146 :LBC (134) : add_path(rel, (Path *) create_merge_append_path(root,
2147 : : rel,
2148 : : fractional.subpaths,
2149 : : fractional.child_append_relid_sets,
2150 : : pathkeys,
2151 : : NULL));
2152 : : }
2153 : : }
10892 scrappy@hub.org 2154 :CBC 37246 : }
2155 : :
2156 : : /*
2157 : : * get_cheapest_parameterized_child_path
2158 : : * Get cheapest path for this relation that has exactly the requested
2159 : : * parameterization.
2160 : : *
2161 : : * Returns NULL if unable to create such a path.
2162 : : */
2163 : : static Path *
4685 tgl@sss.pgh.pa.us 2164 : 5802 : get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
2165 : : Relids required_outer)
2166 : : {
2167 : : Path *cheapest;
2168 : : ListCell *lc;
2169 : :
2170 : : /*
2171 : : * Look up the cheapest existing path with no more than the needed
2172 : : * parameterization. If it has exactly the needed parameterization, we're
2173 : : * done.
2174 : : */
2175 : 5802 : cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
2176 : : NIL,
2177 : : required_outer,
2178 : : TOTAL_COST,
2179 : : false);
2180 [ - + ]: 5802 : Assert(cheapest != NULL);
2181 [ + + + + ]: 5802 : if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
2182 : 4962 : return cheapest;
2183 : :
2184 : : /*
2185 : : * Otherwise, we can "reparameterize" an existing path to match the given
2186 : : * parameterization, which effectively means pushing down additional
2187 : : * joinquals to be checked within the path's scan. However, some existing
2188 : : * paths might check the available joinquals already while others don't;
2189 : : * therefore, it's not clear which existing path will be cheapest after
2190 : : * reparameterization. We have to go through them all and find out.
2191 : : */
2192 : 840 : cheapest = NULL;
2193 [ + - + + : 3298 : foreach(lc, rel->pathlist)
+ + ]
2194 : : {
2195 : 2458 : Path *path = (Path *) lfirst(lc);
2196 : :
2197 : : /* Can't use it if it needs more than requested parameterization */
2198 [ + + + + ]: 2458 : if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
2199 : 148 : continue;
2200 : :
2201 : : /*
2202 : : * Reparameterization can only increase the path's cost, so if it's
2203 : : * already more expensive than the current cheapest, forget it.
2204 : : */
2205 [ + + + + ]: 3732 : if (cheapest != NULL &&
2206 : 1422 : compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
2207 : 1372 : continue;
2208 : :
2209 : : /* Reparameterize if needed, then recheck cost */
2210 [ + + + + ]: 938 : if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
2211 : : {
2212 : 853 : path = reparameterize_path(root, path, required_outer, 1.0);
2213 [ + + ]: 853 : if (path == NULL)
2214 : 58 : continue; /* failed to reparameterize this one */
2215 [ + - - + ]: 795 : Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
2216 : :
2217 [ - + - - ]: 795 : if (cheapest != NULL &&
4685 tgl@sss.pgh.pa.us 2218 :UBC 0 : compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
2219 : 0 : continue;
2220 : : }
2221 : :
2222 : : /* We have a new best path */
4685 tgl@sss.pgh.pa.us 2223 :CBC 880 : cheapest = path;
2224 : : }
2225 : :
2226 : : /* Return the best path, or NULL if we found no suitable candidate */
2227 : 840 : return cheapest;
2228 : : }
2229 : :
2230 : : /*
2231 : : * accumulate_append_subpath
2232 : : * Add a subpath to the list being built for an Append or MergeAppend.
2233 : : *
2234 : : * It's possible that the child is itself an Append or MergeAppend path, in
2235 : : * which case we can "cut out the middleman" and just add its child paths to
2236 : : * our own list. (We don't try to do this earlier because we need to apply
2237 : : * both levels of transformation to the quals.)
2238 : : *
2239 : : * Note that if we omit a child MergeAppend in this way, we are effectively
2240 : : * omitting a sort step, which seems fine: if the parent is to be an Append,
2241 : : * its result would be unsorted anyway, while if the parent is to be a
2242 : : * MergeAppend, there's no point in a separate sort on a child.
2243 : : *
2244 : : * Normally, either path is a partial path and subpaths is a list of partial
2245 : : * paths, or else path is a non-partial plan and subpaths is a list of those.
2246 : : * However, if path is a parallel-aware Append, then we add its partial path
2247 : : * children to subpaths and the rest to special_subpaths. If the latter is
2248 : : * NULL, we don't flatten the path at all (unless it contains only partial
2249 : : * paths).
2250 : : */
2251 : : static void
84 rhaas@postgresql.org 2252 :GNC 249791 : accumulate_append_subpath(Path *path, List **subpaths, List **special_subpaths,
2253 : : List **child_append_relid_sets)
2254 : : {
5682 tgl@sss.pgh.pa.us 2255 [ + + ]:CBC 249791 : if (IsA(path, AppendPath))
2256 : : {
5504 bruce@momjian.us 2257 : 13082 : AppendPath *apath = (AppendPath *) path;
2258 : :
3073 rhaas@postgresql.org 2259 [ + + + + ]: 13082 : if (!apath->path.parallel_aware || apath->first_partial_path == 0)
2260 : : {
2458 tgl@sss.pgh.pa.us 2261 : 12802 : *subpaths = list_concat(*subpaths, apath->subpaths);
84 rhaas@postgresql.org 2262 :GNC 12802 : *child_append_relid_sets =
2263 : 12802 : lappend(*child_append_relid_sets, path->parent->relids);
2264 : 12802 : *child_append_relid_sets =
2265 : 12802 : list_concat(*child_append_relid_sets,
2266 : 12802 : apath->child_append_relid_sets);
3073 rhaas@postgresql.org 2267 :CBC 12802 : return;
2268 : : }
2269 [ + + ]: 280 : else if (special_subpaths != NULL)
2270 : : {
2271 : : List *new_special_subpaths;
2272 : :
2273 : : /* Split Parallel Append into partial and non-partial subpaths */
2274 : 140 : *subpaths = list_concat(*subpaths,
2275 : 140 : list_copy_tail(apath->subpaths,
2276 : : apath->first_partial_path));
1392 drowley@postgresql.o 2277 : 140 : new_special_subpaths = list_copy_head(apath->subpaths,
2278 : : apath->first_partial_path);
3073 rhaas@postgresql.org 2279 : 140 : *special_subpaths = list_concat(*special_subpaths,
2280 : : new_special_subpaths);
84 rhaas@postgresql.org 2281 :GNC 140 : *child_append_relid_sets =
2282 : 140 : lappend(*child_append_relid_sets, path->parent->relids);
2283 : 140 : *child_append_relid_sets =
2284 : 140 : list_concat(*child_append_relid_sets,
2285 : 140 : apath->child_append_relid_sets);
3037 rhaas@postgresql.org 2286 :CBC 140 : return;
2287 : : }
2288 : : }
4421 tgl@sss.pgh.pa.us 2289 [ + + ]: 236709 : else if (IsA(path, MergeAppendPath))
2290 : : {
2291 : 818 : MergeAppendPath *mpath = (MergeAppendPath *) path;
2292 : :
2458 2293 : 818 : *subpaths = list_concat(*subpaths, mpath->subpaths);
84 rhaas@postgresql.org 2294 :GNC 818 : *child_append_relid_sets =
2295 : 818 : lappend(*child_append_relid_sets, path->parent->relids);
2296 : 818 : *child_append_relid_sets =
2297 : 818 : list_concat(*child_append_relid_sets,
2298 : 818 : mpath->child_append_relid_sets);
3073 rhaas@postgresql.org 2299 :CBC 818 : return;
2300 : : }
2301 : :
2302 : 236031 : *subpaths = lappend(*subpaths, path);
2303 : : }
2304 : :
2305 : : /*
2306 : : * get_singleton_append_subpath
2307 : : * Returns the single subpath of an Append/MergeAppend, or just
2308 : : * return 'path' if it's not a single sub-path Append/MergeAppend.
2309 : : *
2310 : : * As a side effect, whenever we return a single subpath rather than the
2311 : : * original path, add the relid sets for the original path to
2312 : : * child_append_relid_sets, so that those relids don't entirely disappear
2313 : : * from the final plan.
2314 : : *
2315 : : * Note: 'path' must not be a parallel-aware path.
2316 : : */
2317 : : static Path *
84 rhaas@postgresql.org 2318 :GNC 11064 : get_singleton_append_subpath(Path *path, List **child_append_relid_sets)
2319 : : {
2587 tgl@sss.pgh.pa.us 2320 [ - + ]:CBC 11064 : Assert(!path->parallel_aware);
2321 : :
2322 [ + + ]: 11064 : if (IsA(path, AppendPath))
2323 : : {
2324 : 328 : AppendPath *apath = (AppendPath *) path;
2325 : :
2326 [ + + ]: 328 : if (list_length(apath->subpaths) == 1)
2327 : : {
84 rhaas@postgresql.org 2328 :GNC 178 : *child_append_relid_sets =
2329 : 178 : lappend(*child_append_relid_sets, path->parent->relids);
2330 : 178 : *child_append_relid_sets =
2331 : 178 : list_concat(*child_append_relid_sets,
2332 : 178 : apath->child_append_relid_sets);
2587 tgl@sss.pgh.pa.us 2333 :CBC 178 : return (Path *) linitial(apath->subpaths);
2334 : : }
2335 : : }
2336 [ + + ]: 10736 : else if (IsA(path, MergeAppendPath))
2337 : : {
2338 : 290 : MergeAppendPath *mpath = (MergeAppendPath *) path;
2339 : :
2340 [ - + ]: 290 : if (list_length(mpath->subpaths) == 1)
2341 : : {
84 rhaas@postgresql.org 2342 :UNC 0 : *child_append_relid_sets =
2343 : 0 : lappend(*child_append_relid_sets, path->parent->relids);
2344 : 0 : *child_append_relid_sets =
2345 : 0 : list_concat(*child_append_relid_sets,
2346 : 0 : mpath->child_append_relid_sets);
2587 tgl@sss.pgh.pa.us 2347 :UBC 0 : return (Path *) linitial(mpath->subpaths);
2348 : : }
2349 : : }
2350 : :
2587 tgl@sss.pgh.pa.us 2351 :CBC 10886 : return path;
2352 : : }
2353 : :
2354 : : /*
2355 : : * set_dummy_rel_pathlist
2356 : : * Build a dummy path for a relation that's been excluded by constraints
2357 : : *
2358 : : * Rather than inventing a special "dummy" path type, we represent this as an
2359 : : * AppendPath with no members (see also IS_DUMMY_APPEND/IS_DUMMY_REL macros).
2360 : : *
2361 : : * (See also mark_dummy_rel, which does basically the same thing, but is
2362 : : * typically used to change a rel into dummy state after we already made
2363 : : * paths for it.)
2364 : : */
2365 : : static void
6919 2366 : 1193 : set_dummy_rel_pathlist(RelOptInfo *rel)
2367 : : {
84 rhaas@postgresql.org 2368 :GNC 1193 : AppendPathInput in = {0};
2369 : :
2370 : : /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
6919 tgl@sss.pgh.pa.us 2371 :CBC 1193 : rel->rows = 0;
3704 2372 : 1193 : rel->reltarget->width = 0;
2373 : :
2374 : : /* Discard any pre-existing paths; no further need for them */
5212 2375 : 1193 : rel->pathlist = NIL;
3758 rhaas@postgresql.org 2376 : 1193 : rel->partial_pathlist = NIL;
2377 : :
2378 : : /* Set up the dummy path */
84 rhaas@postgresql.org 2379 :GNC 1193 : add_path(rel, (Path *) create_append_path(NULL, rel, in,
2380 : : NIL, rel->lateral_relids,
2381 : : 0, false, -1));
2382 : :
2383 : : /*
2384 : : * We set the cheapest-path fields immediately, just in case they were
2385 : : * pointing at some discarded path. This is redundant in current usage
2386 : : * because set_rel_pathlist will do it later, but it's cheap so we keep it
2387 : : * for safety and consistency with mark_dummy_rel.
2388 : : */
6919 tgl@sss.pgh.pa.us 2389 :CBC 1193 : set_cheapest(rel);
2390 : 1193 : }
2391 : :
2392 : : /*
2393 : : * find_window_run_conditions
2394 : : * Determine if 'wfunc' is really a WindowFunc and call its prosupport
2395 : : * function to determine the function's monotonic properties. We then
2396 : : * see if 'opexpr' can be used to short-circuit execution.
2397 : : *
2398 : : * For example row_number() over (order by ...) always produces a value one
2399 : : * higher than the previous. If someone has a window function in a subquery
2400 : : * and has a WHERE clause in the outer query to filter rows <= 10, then we may
2401 : : * as well stop processing the windowagg once the row number reaches 11. Here
2402 : : * we check if 'opexpr' might help us to stop doing needless extra processing
2403 : : * in WindowAgg nodes.
2404 : : *
2405 : : * '*keep_original' is set to true if the caller should also use 'opexpr' for
2406 : : * its original purpose. This is set to false if the caller can assume that
2407 : : * the run condition will handle all of the required filtering.
2408 : : *
2409 : : * Returns true if 'opexpr' was found to be useful and was added to the
2410 : : * WindowFunc's runCondition. We also set *keep_original accordingly and add
2411 : : * 'attno' to *run_cond_attrs offset by FirstLowInvalidHeapAttributeNumber.
2412 : : * If the 'opexpr' cannot be used then we set *keep_original to true and
2413 : : * return false.
2414 : : */
2415 : : static bool
217 drowley@postgresql.o 2416 :GNC 200 : find_window_run_conditions(Query *subquery, AttrNumber attno,
2417 : : WindowFunc *wfunc, OpExpr *opexpr, bool wfunc_left,
2418 : : bool *keep_original, Bitmapset **run_cond_attrs)
2419 : : {
2420 : : Oid prosupport;
2421 : : Expr *otherexpr;
2422 : : SupportRequestWFuncMonotonic req;
2423 : : SupportRequestWFuncMonotonic *res;
2424 : : WindowClause *wclause;
2425 : : List *opinfos;
2426 : : OpExpr *runopexpr;
2427 : : Oid runoperator;
2428 : : ListCell *lc;
2429 : :
1488 drowley@postgresql.o 2430 :CBC 200 : *keep_original = true;
2431 : :
2432 [ - + ]: 200 : while (IsA(wfunc, RelabelType))
1488 drowley@postgresql.o 2433 :UBC 0 : wfunc = (WindowFunc *) ((RelabelType *) wfunc)->arg;
2434 : :
2435 : : /* we can only work with window functions */
1488 drowley@postgresql.o 2436 [ + + ]:CBC 200 : if (!IsA(wfunc, WindowFunc))
2437 : 20 : return false;
2438 : :
2439 : : /* can't use it if there are subplans in the WindowFunc */
1145 2440 [ + + ]: 180 : if (contain_subplans((Node *) wfunc))
2441 : 5 : return false;
2442 : :
1488 2443 : 175 : prosupport = get_func_support(wfunc->winfnoid);
2444 : :
2445 : : /* Check if there's a support function for 'wfunc' */
2446 [ + + ]: 175 : if (!OidIsValid(prosupport))
2447 : 15 : return false;
2448 : :
2449 : : /* get the Expr from the other side of the OpExpr */
2450 [ + + ]: 160 : if (wfunc_left)
2451 : 140 : otherexpr = lsecond(opexpr->args);
2452 : : else
2453 : 20 : otherexpr = linitial(opexpr->args);
2454 : :
2455 : : /*
2456 : : * The value being compared must not change during the evaluation of the
2457 : : * window partition.
2458 : : */
2459 [ - + ]: 160 : if (!is_pseudo_constant_clause((Node *) otherexpr))
1488 drowley@postgresql.o 2460 :UBC 0 : return false;
2461 : :
2462 : : /* find the window clause belonging to the window function */
1488 drowley@postgresql.o 2463 :CBC 160 : wclause = (WindowClause *) list_nth(subquery->windowClause,
2464 : 160 : wfunc->winref - 1);
2465 : :
2466 : 160 : req.type = T_SupportRequestWFuncMonotonic;
2467 : 160 : req.window_func = wfunc;
2468 : 160 : req.window_clause = wclause;
2469 : :
2470 : : /* call the support function */
2471 : : res = (SupportRequestWFuncMonotonic *)
2472 : 160 : DatumGetPointer(OidFunctionCall1(prosupport,
2473 : : PointerGetDatum(&req)));
2474 : :
2475 : : /*
2476 : : * Nothing to do if the function is neither monotonically increasing nor
2477 : : * monotonically decreasing.
2478 : : */
2479 [ + - - + ]: 160 : if (res == NULL || res->monotonic == MONOTONICFUNC_NONE)
1488 drowley@postgresql.o 2480 :UBC 0 : return false;
2481 : :
1488 drowley@postgresql.o 2482 :CBC 160 : runopexpr = NULL;
2483 : 160 : runoperator = InvalidOid;
394 peter@eisentraut.org 2484 : 160 : opinfos = get_op_index_interpretation(opexpr->opno);
2485 : :
1488 drowley@postgresql.o 2486 [ + - + - : 160 : foreach(lc, opinfos)
+ - ]
2487 : : {
394 peter@eisentraut.org 2488 : 160 : OpIndexInterpretation *opinfo = (OpIndexInterpretation *) lfirst(lc);
2489 : 160 : CompareType cmptype = opinfo->cmptype;
2490 : :
2491 : : /* handle < / <= */
2492 [ + + + + ]: 160 : if (cmptype == COMPARE_LT || cmptype == COMPARE_LE)
2493 : : {
2494 : : /*
2495 : : * < / <= is supported for monotonically increasing functions in
2496 : : * the form <wfunc> op <pseudoconst> and <pseudoconst> op <wfunc>
2497 : : * for monotonically decreasing functions.
2498 : : */
1488 drowley@postgresql.o 2499 [ + + + + ]: 115 : if ((wfunc_left && (res->monotonic & MONOTONICFUNC_INCREASING)) ||
2500 [ + + + + ]: 15 : (!wfunc_left && (res->monotonic & MONOTONICFUNC_DECREASING)))
2501 : : {
2502 : 105 : *keep_original = false;
2503 : 105 : runopexpr = opexpr;
2504 : 105 : runoperator = opexpr->opno;
2505 : : }
2506 : 115 : break;
2507 : : }
2508 : : /* handle > / >= */
394 peter@eisentraut.org 2509 [ + + + + ]: 45 : else if (cmptype == COMPARE_GT || cmptype == COMPARE_GE)
2510 : : {
2511 : : /*
2512 : : * > / >= is supported for monotonically decreasing functions in
2513 : : * the form <wfunc> op <pseudoconst> and <pseudoconst> op <wfunc>
2514 : : * for monotonically increasing functions.
2515 : : */
1488 drowley@postgresql.o 2516 [ + + - + ]: 15 : if ((wfunc_left && (res->monotonic & MONOTONICFUNC_DECREASING)) ||
2517 [ + - + - ]: 10 : (!wfunc_left && (res->monotonic & MONOTONICFUNC_INCREASING)))
2518 : : {
2519 : 15 : *keep_original = false;
2520 : 15 : runopexpr = opexpr;
2521 : 15 : runoperator = opexpr->opno;
2522 : : }
2523 : 15 : break;
2524 : : }
2525 : : /* handle = */
394 peter@eisentraut.org 2526 [ + - ]: 30 : else if (cmptype == COMPARE_EQ)
2527 : : {
2528 : : CompareType newcmptype;
2529 : :
2530 : : /*
2531 : : * When both monotonically increasing and decreasing then the
2532 : : * return value of the window function will be the same each time.
2533 : : * We can simply use 'opexpr' as the run condition without
2534 : : * modifying it.
2535 : : */
1488 drowley@postgresql.o 2536 [ + + ]: 30 : if ((res->monotonic & MONOTONICFUNC_BOTH) == MONOTONICFUNC_BOTH)
2537 : : {
2538 : 5 : *keep_original = false;
2539 : 5 : runopexpr = opexpr;
1369 2540 : 5 : runoperator = opexpr->opno;
1488 2541 : 5 : break;
2542 : : }
2543 : :
2544 : : /*
2545 : : * When monotonically increasing we make a qual with <wfunc> <=
2546 : : * <value> or <value> >= <wfunc> in order to filter out values
2547 : : * which are above the value in the equality condition. For
2548 : : * monotonically decreasing functions we want to filter values
2549 : : * below the value in the equality condition.
2550 : : */
2551 [ + - ]: 25 : if (res->monotonic & MONOTONICFUNC_INCREASING)
394 peter@eisentraut.org 2552 [ + - ]: 25 : newcmptype = wfunc_left ? COMPARE_LE : COMPARE_GE;
2553 : : else
394 peter@eisentraut.org 2554 [ # # ]:UBC 0 : newcmptype = wfunc_left ? COMPARE_GE : COMPARE_LE;
2555 : :
2556 : : /* We must keep the original equality qual */
1488 drowley@postgresql.o 2557 :CBC 25 : *keep_original = true;
2558 : 25 : runopexpr = opexpr;
2559 : :
2560 : : /* determine the operator to use for the WindowFuncRunCondition */
394 peter@eisentraut.org 2561 : 25 : runoperator = get_opfamily_member_for_cmptype(opinfo->opfamily_id,
2562 : : opinfo->oplefttype,
2563 : : opinfo->oprighttype,
2564 : : newcmptype);
1488 drowley@postgresql.o 2565 : 25 : break;
2566 : : }
2567 : : }
2568 : :
2569 [ + + ]: 160 : if (runopexpr != NULL)
2570 : : {
2571 : : WindowFuncRunCondition *wfuncrc;
2572 : :
730 2573 : 150 : wfuncrc = makeNode(WindowFuncRunCondition);
2574 : 150 : wfuncrc->opno = runoperator;
2575 : 150 : wfuncrc->inputcollid = runopexpr->inputcollid;
2576 : 150 : wfuncrc->wfunc_left = wfunc_left;
2577 : 150 : wfuncrc->arg = copyObject(otherexpr);
2578 : :
2579 : 150 : wfunc->runCondition = lappend(wfunc->runCondition, wfuncrc);
2580 : :
2581 : : /* record that this attno was used in a run condition */
1439 2582 : 150 : *run_cond_attrs = bms_add_member(*run_cond_attrs,
2583 : : attno - FirstLowInvalidHeapAttributeNumber);
1488 2584 : 150 : return true;
2585 : : }
2586 : :
2587 : : /* unsupported OpExpr */
2588 : 10 : return false;
2589 : : }
2590 : :
2591 : : /*
2592 : : * check_and_push_window_quals
2593 : : * Check if 'clause' is a qual that can be pushed into a WindowFunc
2594 : : * as a 'runCondition' qual. These, when present, allow some unnecessary
2595 : : * work to be skipped during execution.
2596 : : *
2597 : : * 'run_cond_attrs' will be populated with all targetlist resnos of subquery
2598 : : * targets (offset by FirstLowInvalidHeapAttributeNumber) that we pushed
2599 : : * window quals for.
2600 : : *
2601 : : * Returns true if the caller still must keep the original qual or false if
2602 : : * the caller can safely ignore the original qual because the WindowAgg node
2603 : : * will use the runCondition to stop returning tuples.
2604 : : */
2605 : : static bool
217 drowley@postgresql.o 2606 :GNC 210 : check_and_push_window_quals(Query *subquery, Node *clause,
2607 : : Bitmapset **run_cond_attrs)
2608 : : {
1488 drowley@postgresql.o 2609 :CBC 210 : OpExpr *opexpr = (OpExpr *) clause;
2610 : 210 : bool keep_original = true;
2611 : : Var *var1;
2612 : : Var *var2;
2613 : :
2614 : : /* We're only able to use OpExprs with 2 operands */
2615 [ + + ]: 210 : if (!IsA(opexpr, OpExpr))
2616 : 15 : return true;
2617 : :
2618 [ - + ]: 195 : if (list_length(opexpr->args) != 2)
1488 drowley@postgresql.o 2619 :UBC 0 : return true;
2620 : :
2621 : : /*
2622 : : * Currently, we restrict this optimization to strict OpExprs. The reason
2623 : : * for this is that during execution, once the runcondition becomes false,
2624 : : * we stop evaluating WindowFuncs. To avoid leaving around stale window
2625 : : * function result values, we set them to NULL. Having only strict
2626 : : * OpExprs here ensures that we properly filter out the tuples with NULLs
2627 : : * in the top-level WindowAgg.
2628 : : */
1245 drowley@postgresql.o 2629 :CBC 195 : set_opfuncid(opexpr);
2630 [ - + ]: 195 : if (!func_strict(opexpr->opfuncid))
1245 drowley@postgresql.o 2631 :UBC 0 : return true;
2632 : :
2633 : : /*
2634 : : * Check for plain Vars that reference window functions in the subquery.
2635 : : * If we find any, we'll ask find_window_run_conditions() if 'opexpr' can
2636 : : * be used as part of the run condition.
2637 : : */
2638 : :
2639 : : /* Check the left side of the OpExpr */
1488 drowley@postgresql.o 2640 :CBC 195 : var1 = linitial(opexpr->args);
2641 [ + + + - ]: 195 : if (IsA(var1, Var) && var1->varattno > 0)
2642 : : {
2643 : 165 : TargetEntry *tle = list_nth(subquery->targetList, var1->varattno - 1);
2644 : 165 : WindowFunc *wfunc = (WindowFunc *) tle->expr;
2645 : :
217 drowley@postgresql.o 2646 [ + + ]:GNC 165 : if (find_window_run_conditions(subquery, tle->resno, wfunc, opexpr,
2647 : : true, &keep_original, run_cond_attrs))
1488 drowley@postgresql.o 2648 :CBC 135 : return keep_original;
2649 : : }
2650 : :
2651 : : /* and check the right side */
2652 : 60 : var2 = lsecond(opexpr->args);
2653 [ + + + - ]: 60 : if (IsA(var2, Var) && var2->varattno > 0)
2654 : : {
2655 : 35 : TargetEntry *tle = list_nth(subquery->targetList, var2->varattno - 1);
2656 : 35 : WindowFunc *wfunc = (WindowFunc *) tle->expr;
2657 : :
217 drowley@postgresql.o 2658 [ + + ]:GNC 35 : if (find_window_run_conditions(subquery, tle->resno, wfunc, opexpr,
2659 : : false, &keep_original, run_cond_attrs))
1488 drowley@postgresql.o 2660 :CBC 15 : return keep_original;
2661 : : }
2662 : :
2663 : 45 : return true;
2664 : : }
2665 : :
2666 : : /*
2667 : : * set_subquery_pathlist
2668 : : * Generate SubqueryScan access paths for a subquery RTE
2669 : : *
2670 : : * We don't currently support generating parameterized paths for subqueries
2671 : : * by pushing join clauses down into them; it seems too expensive to re-plan
2672 : : * the subquery multiple times to consider different alternatives.
2673 : : * (XXX that could stand to be reconsidered, now that we use Paths.)
2674 : : * So the paths made here will be parameterized if the subquery contains
2675 : : * LATERAL references, otherwise not. As long as that's true, there's no need
2676 : : * for a separate set_subquery_size phase: just make the paths right away.
2677 : : */
2678 : : static void
7639 tgl@sss.pgh.pa.us 2679 : 17487 : set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
2680 : : Index rti, RangeTblEntry *rte)
2681 : : {
7634 2682 : 17487 : Query *parse = root->parse;
9059 2683 : 17487 : Query *subquery = rte->subquery;
2684 : : bool trivial_pathtarget;
2685 : : Relids required_outer;
2686 : : pushdown_safety_info safetyInfo;
2687 : : double tuple_fraction;
2688 : : RelOptInfo *sub_final_rel;
1439 drowley@postgresql.o 2689 : 17487 : Bitmapset *run_cond_attrs = NULL;
2690 : : ListCell *lc;
2691 : : char *plan_name;
2692 : :
2693 : : /*
2694 : : * Must copy the Query so that planning doesn't mess up the RTE contents
2695 : : * (really really need to fix the planner to not scribble on its input,
2696 : : * someday ... but see remove_unused_subquery_outputs to start with).
2697 : : */
6265 tgl@sss.pgh.pa.us 2698 : 17487 : subquery = copyObject(subquery);
2699 : :
2700 : : /*
2701 : : * If it's a LATERAL subquery, it might contain some Vars of the current
2702 : : * query level, requiring it to be treated as parameterized, even though
2703 : : * we don't support pushing down join quals into subqueries.
2704 : : */
5000 2705 : 17487 : required_outer = rel->lateral_relids;
2706 : :
2707 : : /*
2708 : : * Zero out result area for subquery_is_pushdown_safe, so that it can set
2709 : : * flags as needed while recursing. In particular, we need a workspace
2710 : : * for keeping track of the reasons why columns are unsafe to reference.
2711 : : * These reasons are stored in the bits inside unsafeFlags[i] when we
2712 : : * discover reasons that column i of the subquery is unsafe to be used in
2713 : : * a pushed-down qual.
2714 : : */
4330 2715 : 17487 : memset(&safetyInfo, 0, sizeof(safetyInfo));
1145 drowley@postgresql.o 2716 : 17487 : safetyInfo.unsafeFlags = (unsigned char *)
2717 : 17487 : palloc0((list_length(subquery->targetList) + 1) * sizeof(unsigned char));
2718 : :
2719 : : /*
2720 : : * If the subquery has the "security_barrier" flag, it means the subquery
2721 : : * originated from a view that must enforce row-level security. Then we
2722 : : * must not push down quals that contain leaky functions. (Ideally this
2723 : : * would be checked inside subquery_is_pushdown_safe, but since we don't
2724 : : * currently pass the RTE to that function, we must do it here.)
2725 : : */
4330 tgl@sss.pgh.pa.us 2726 : 17487 : safetyInfo.unsafeLeaky = rte->security_barrier;
2727 : :
2728 : : /*
2729 : : * If there are any restriction clauses that have been attached to the
2730 : : * subquery relation, consider pushing them down to become WHERE or HAVING
2731 : : * quals of the subquery itself. This transformation is useful because it
2732 : : * may allow us to generate a better plan for the subquery than evaluating
2733 : : * all the subquery output rows and then filtering them.
2734 : : *
2735 : : * There are several cases where we cannot push down clauses. Restrictions
2736 : : * involving the subquery are checked by subquery_is_pushdown_safe().
2737 : : * Restrictions on individual clauses are checked by
2738 : : * qual_is_pushdown_safe(). Also, we don't want to push down
2739 : : * pseudoconstant clauses; better to have the gating node above the
2740 : : * subquery.
2741 : : *
2742 : : * Non-pushed-down clauses will get evaluated as qpquals of the
2743 : : * SubqueryScan node.
2744 : : *
2745 : : * XXX Are there any cases where we want to make a policy decision not to
2746 : : * push down a pushable qual, because it'd result in a worse plan?
2747 : : */
8650 2748 [ + + + + ]: 19676 : if (rel->baserestrictinfo != NIL &&
4330 2749 : 2189 : subquery_is_pushdown_safe(subquery, subquery, &safetyInfo))
2750 : : {
2751 : : /* OK to consider pushing down individual quals */
9059 2752 : 2068 : List *upperrestrictlist = NIL;
2753 : : ListCell *l;
2754 : :
8014 neilc@samurai.com 2755 [ + - + + : 5610 : foreach(l, rel->baserestrictinfo)
+ + ]
2756 : : {
2757 : 3542 : RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1488 drowley@postgresql.o 2758 : 3542 : Node *clause = (Node *) rinfo->clause;
2759 : :
1145 2760 [ + + ]: 3542 : if (rinfo->pseudoconstant)
2761 : : {
2762 : 2 : upperrestrictlist = lappend(upperrestrictlist, rinfo);
2763 : 2 : continue;
2764 : : }
2765 : :
2766 [ + + + - ]: 3540 : switch (qual_is_pushdown_safe(subquery, rti, rinfo, &safetyInfo))
2767 : : {
2768 : 2680 : case PUSHDOWN_SAFE:
2769 : : /* Push it down */
2770 : 2680 : subquery_push_qual(subquery, rte, rti, clause);
2771 : 2680 : break;
2772 : :
2773 : 210 : case PUSHDOWN_WINDOWCLAUSE_RUNCOND:
2774 : :
2775 : : /*
2776 : : * Since we can't push the qual down into the subquery,
2777 : : * check if it happens to reference a window function. If
2778 : : * so then it might be useful to use for the WindowAgg's
2779 : : * runCondition.
2780 : : */
2781 [ + - + + ]: 420 : if (!subquery->hasWindowFuncs ||
217 drowley@postgresql.o 2782 :GNC 210 : check_and_push_window_quals(subquery, clause,
2783 : : &run_cond_attrs))
2784 : : {
2785 : : /*
2786 : : * subquery has no window funcs or the clause is not a
2787 : : * suitable window run condition qual or it is, but
2788 : : * the original must also be kept in the upper query.
2789 : : */
1145 drowley@postgresql.o 2790 :CBC 85 : upperrestrictlist = lappend(upperrestrictlist, rinfo);
2791 : : }
2792 : 210 : break;
2793 : :
2794 : 650 : case PUSHDOWN_UNSAFE:
1488 2795 : 650 : upperrestrictlist = lappend(upperrestrictlist, rinfo);
1145 2796 : 650 : break;
2797 : : }
2798 : : }
9059 tgl@sss.pgh.pa.us 2799 : 2068 : rel->baserestrictinfo = upperrestrictlist;
2800 : : /* We don't bother recomputing baserestrict_min_security */
2801 : : }
2802 : :
1145 drowley@postgresql.o 2803 : 17487 : pfree(safetyInfo.unsafeFlags);
2804 : :
2805 : : /*
2806 : : * The upper query might not use all the subquery's output columns; if
2807 : : * not, we can simplify. Pass the attributes that were pushed down into
2808 : : * WindowAgg run conditions to ensure we don't accidentally think those
2809 : : * are unused.
2810 : : */
1439 2811 : 17487 : remove_unused_subquery_outputs(subquery, rel, run_cond_attrs);
2812 : :
2813 : : /*
2814 : : * We can safely pass the outer tuple_fraction down to the subquery if the
2815 : : * outer level has no joining, aggregation, or sorting to do. Otherwise
2816 : : * we'd better tell the subquery to plan for full retrieval. (XXX This
2817 : : * could probably be made more intelligent ...)
2818 : : */
7634 tgl@sss.pgh.pa.us 2819 [ + + ]: 17487 : if (parse->hasAggs ||
2820 [ + + ]: 13415 : parse->groupClause ||
4007 andres@anarazel.de 2821 [ + - ]: 13400 : parse->groupingSets ||
1295 tgl@sss.pgh.pa.us 2822 [ + - ]: 13400 : root->hasHavingQual ||
7634 2823 [ + + ]: 13400 : parse->distinctClause ||
2824 [ + + + + ]: 18366 : parse->sortClause ||
938 2825 : 5307 : bms_membership(root->all_baserels) == BMS_MULTIPLE)
7634 2826 : 13662 : tuple_fraction = 0.0; /* default case */
2827 : : else
2828 : 3825 : tuple_fraction = root->tuple_fraction;
2829 : :
2830 : : /* plan_params should not be in use in current query level */
4990 2831 [ - + ]: 17487 : Assert(root->plan_params == NIL);
2832 : :
2833 : : /* Generate a subroot and Paths for the subquery */
210 rhaas@postgresql.org 2834 :GNC 17487 : plan_name = choose_plan_name(root->glob, rte->eref->aliasname, false);
2835 : 17487 : rel->subroot = subquery_planner(root->glob, subquery, plan_name,
2836 : : root, NULL, false, tuple_fraction, NULL);
2837 : :
2838 : : /* Isolate the params needed by this specific subplan */
4990 tgl@sss.pgh.pa.us 2839 :CBC 17487 : rel->subplan_params = root->plan_params;
2840 : 17487 : root->plan_params = NIL;
2841 : :
2842 : : /*
2843 : : * It's possible that constraint exclusion proved the subquery empty. If
2844 : : * so, it's desirable to produce an unadorned dummy path so that we will
2845 : : * recognize appropriate optimizations at this query level.
2846 : : */
3711 2847 : 17487 : sub_final_rel = fetch_upper_rel(rel->subroot, UPPERREL_FINAL, NULL);
2848 : :
2849 [ + + ]: 17487 : if (IS_DUMMY_REL(sub_final_rel))
2850 : : {
5337 2851 : 105 : set_dummy_rel_pathlist(rel);
2852 : 105 : return;
2853 : : }
2854 : :
2855 : : /*
2856 : : * Mark rel with estimated output rows, width, etc. Note that we have to
2857 : : * do this before generating outer-query paths, else cost_subqueryscan is
2858 : : * not happy.
2859 : : */
5358 2860 : 17382 : set_subquery_size_estimates(root, rel);
2861 : :
2862 : : /*
2863 : : * Also detect whether the reltarget is trivial, so that we can pass that
2864 : : * info to cost_subqueryscan (rather than re-deriving it multiple times).
2865 : : * It's trivial if it fetches all the subplan output columns in order.
2866 : : */
1386 2867 [ + + ]: 17382 : if (list_length(rel->reltarget->exprs) != list_length(subquery->targetList))
2868 : 9618 : trivial_pathtarget = false;
2869 : : else
2870 : : {
2871 : 7764 : trivial_pathtarget = true;
2872 [ + + + + : 21502 : foreach(lc, rel->reltarget->exprs)
+ + ]
2873 : : {
2874 : 13995 : Node *node = (Node *) lfirst(lc);
2875 : : Var *var;
2876 : :
2877 [ - + ]: 13995 : if (!IsA(node, Var))
2878 : : {
1386 tgl@sss.pgh.pa.us 2879 :UBC 0 : trivial_pathtarget = false;
2880 : 0 : break;
2881 : : }
1386 tgl@sss.pgh.pa.us 2882 :CBC 13995 : var = (Var *) node;
2883 [ + - ]: 13995 : if (var->varno != rti ||
2884 [ + + ]: 13995 : var->varattno != foreach_current_index(lc) + 1)
2885 : : {
2886 : 257 : trivial_pathtarget = false;
2887 : 257 : break;
2888 : : }
2889 : : }
2890 : : }
2891 : :
2892 : : /*
2893 : : * For each Path that subquery_planner produced, make a SubqueryScanPath
2894 : : * in the outer query.
2895 : : */
3711 2896 [ + - + + : 36158 : foreach(lc, sub_final_rel->pathlist)
+ + ]
2897 : : {
2898 : 18776 : Path *subpath = (Path *) lfirst(lc);
2899 : : List *pathkeys;
2900 : :
2901 : : /* Convert subpath's pathkeys to outer representation */
2902 : 18776 : pathkeys = convert_subquery_pathkeys(root,
2903 : : rel,
2904 : : subpath->pathkeys,
2905 : : make_tlist_from_pathtarget(subpath->pathtarget));
2906 : :
2907 : : /* Generate outer path using this subpath */
2908 : 18776 : add_path(rel, (Path *)
2909 : 18776 : create_subqueryscan_path(root, rel, subpath,
2910 : : trivial_pathtarget,
2911 : : pathkeys, required_outer));
2912 : : }
2913 : :
2914 : : /* If outer rel allows parallelism, do same for partial paths. */
2932 rhaas@postgresql.org 2915 [ + + + + ]: 17382 : if (rel->consider_parallel && bms_is_empty(required_outer))
2916 : : {
2917 : : /* If consider_parallel is false, there should be no partial paths. */
2918 [ + + - + ]: 10811 : Assert(sub_final_rel->consider_parallel ||
2919 : : sub_final_rel->partial_pathlist == NIL);
2920 : :
2921 : : /* Same for partial paths. */
2922 [ + + + + : 10856 : foreach(lc, sub_final_rel->partial_pathlist)
+ + ]
2923 : : {
2924 : 45 : Path *subpath = (Path *) lfirst(lc);
2925 : : List *pathkeys;
2926 : :
2927 : : /* Convert subpath's pathkeys to outer representation */
2928 : 45 : pathkeys = convert_subquery_pathkeys(root,
2929 : : rel,
2930 : : subpath->pathkeys,
2931 : : make_tlist_from_pathtarget(subpath->pathtarget));
2932 : :
2933 : : /* Generate outer path using this subpath */
2934 : 45 : add_partial_path(rel, (Path *)
2935 : 45 : create_subqueryscan_path(root, rel, subpath,
2936 : : trivial_pathtarget,
2937 : : pathkeys,
2938 : : required_outer));
2939 : : }
2940 : : }
2941 : : }
2942 : :
2943 : : /*
2944 : : * set_function_pathlist
2945 : : * Build the (single) access path for a function RTE
2946 : : */
2947 : : static void
7639 tgl@sss.pgh.pa.us 2948 : 35764 : set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2949 : : {
2950 : : Relids required_outer;
4548 2951 : 35764 : List *pathkeys = NIL;
2952 : :
2953 : : /*
2954 : : * We don't support pushing join clauses into the quals of a function
2955 : : * scan, but it could still have required parameterization due to LATERAL
2956 : : * refs in the function expression.
2957 : : */
5000 2958 : 35764 : required_outer = rel->lateral_relids;
2959 : :
2960 : : /*
2961 : : * The result is considered unordered unless ORDINALITY was used, in which
2962 : : * case it is ordered by the ordinal column (the last one). See if we
2963 : : * care, by checking for uses of that Var in equivalence classes.
2964 : : */
4548 2965 [ + + ]: 35764 : if (rte->funcordinality)
2966 : : {
2967 : 778 : AttrNumber ordattno = rel->max_attr;
2968 : 778 : Var *var = NULL;
2969 : : ListCell *lc;
2970 : :
2971 : : /*
2972 : : * Is there a Var for it in rel's targetlist? If not, the query did
2973 : : * not reference the ordinality column, or at least not in any way
2974 : : * that would be interesting for sorting.
2975 : : */
3704 2976 [ + - + + : 1796 : foreach(lc, rel->reltarget->exprs)
+ + ]
2977 : : {
4548 2978 : 1791 : Var *node = (Var *) lfirst(lc);
2979 : :
2980 : : /* checking varno/varlevelsup is just paranoia */
2981 [ + - ]: 1791 : if (IsA(node, Var) &&
2982 [ + + ]: 1791 : node->varattno == ordattno &&
2983 [ + - ]: 773 : node->varno == rel->relid &&
2984 [ + - ]: 773 : node->varlevelsup == 0)
2985 : : {
2986 : 773 : var = node;
2987 : 773 : break;
2988 : : }
2989 : : }
2990 : :
2991 : : /*
2992 : : * Try to build pathkeys for this Var with int8 sorting. We tell
2993 : : * build_expression_pathkey not to build any new equivalence class; if
2994 : : * the Var isn't already mentioned in some EC, it means that nothing
2995 : : * cares about the ordering.
2996 : : */
2997 [ + + ]: 778 : if (var)
2998 : 773 : pathkeys = build_expression_pathkey(root,
2999 : : (Expr *) var,
3000 : : Int8LessOperator,
3001 : : rel->relids,
3002 : : false);
3003 : : }
3004 : :
3005 : : /* Generate appropriate path */
3006 : 35764 : add_path(rel, create_functionscan_path(root, rel,
3007 : : pathkeys, required_outer));
8759 3008 : 35764 : }
3009 : :
3010 : : /*
3011 : : * set_values_pathlist
3012 : : * Build the (single) access path for a VALUES RTE
3013 : : */
3014 : : static void
7216 mail@joeconway.com 3015 : 6858 : set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
3016 : : {
3017 : : Relids required_outer;
3018 : :
3019 : : /*
3020 : : * We don't support pushing join clauses into the quals of a values scan,
3021 : : * but it could still have required parameterization due to LATERAL refs
3022 : : * in the values expressions.
3023 : : */
5000 tgl@sss.pgh.pa.us 3024 : 6858 : required_outer = rel->lateral_relids;
3025 : :
3026 : : /* Generate appropriate path */
5014 3027 : 6858 : add_path(rel, create_valuesscan_path(root, rel, required_outer));
7216 mail@joeconway.com 3028 : 6858 : }
3029 : :
3030 : : /*
3031 : : * set_tablefunc_pathlist
3032 : : * Build the (single) access path for a table func RTE
3033 : : */
3034 : : static void
3345 alvherre@alvh.no-ip. 3035 : 517 : set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
3036 : : {
3037 : : Relids required_outer;
3038 : :
3039 : : /*
3040 : : * We don't support pushing join clauses into the quals of a tablefunc
3041 : : * scan, but it could still have required parameterization due to LATERAL
3042 : : * refs in the function expression.
3043 : : */
3044 : 517 : required_outer = rel->lateral_relids;
3045 : :
3046 : : /* Generate appropriate path */
3047 : 517 : add_path(rel, create_tablefuncscan_path(root, rel,
3048 : : required_outer));
3049 : 517 : }
3050 : :
3051 : : /*
3052 : : * set_cte_pathlist
3053 : : * Build the (single) access path for a non-self-reference CTE RTE
3054 : : *
3055 : : * There's no need for a separate set_cte_size phase, since we don't
3056 : : * support join-qual-parameterized paths for CTEs.
3057 : : */
3058 : : static void
6422 tgl@sss.pgh.pa.us 3059 : 2913 : set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
3060 : : {
3061 : : Path *ctepath;
3062 : : Plan *cteplan;
3063 : : PlannerInfo *cteroot;
3064 : : Index levelsup;
3065 : : List *pathkeys;
3066 : : int ndx;
3067 : : ListCell *lc;
3068 : : int plan_id;
3069 : : Relids required_outer;
3070 : :
3071 : : /*
3072 : : * Find the referenced CTE, and locate the path and plan previously made
3073 : : * for it.
3074 : : */
3075 : 2913 : levelsup = rte->ctelevelsup;
3076 : 2913 : cteroot = root;
3077 [ + + ]: 4948 : while (levelsup-- > 0)
3078 : : {
3079 : 2035 : cteroot = cteroot->parent_root;
3080 [ - + ]: 2035 : if (!cteroot) /* shouldn't happen */
6422 tgl@sss.pgh.pa.us 3081 [ # # ]:UBC 0 : elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3082 : : }
3083 : :
3084 : : /*
3085 : : * Note: cte_plan_ids can be shorter than cteList, if we are still working
3086 : : * on planning the CTEs (ie, this is a side-reference from another CTE).
3087 : : * So we mustn't use forboth here.
3088 : : */
6422 tgl@sss.pgh.pa.us 3089 :CBC 2913 : ndx = 0;
3090 [ + - + - : 3832 : foreach(lc, cteroot->parse->cteList)
+ - ]
3091 : : {
3092 : 3832 : CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3093 : :
3094 [ + + ]: 3832 : if (strcmp(cte->ctename, rte->ctename) == 0)
3095 : 2913 : break;
3096 : 919 : ndx++;
3097 : : }
3098 [ - + ]: 2913 : if (lc == NULL) /* shouldn't happen */
6422 tgl@sss.pgh.pa.us 3099 [ # # ]:UBC 0 : elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
6422 tgl@sss.pgh.pa.us 3100 [ - + ]:CBC 2913 : if (ndx >= list_length(cteroot->cte_plan_ids))
6422 tgl@sss.pgh.pa.us 3101 [ # # ]:UBC 0 : elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
6422 tgl@sss.pgh.pa.us 3102 :CBC 2913 : plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1475 3103 [ - + ]: 2913 : if (plan_id <= 0)
1475 tgl@sss.pgh.pa.us 3104 [ # # ]:UBC 0 : elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
3105 : :
770 tgl@sss.pgh.pa.us 3106 [ - + ]:CBC 2913 : Assert(list_length(root->glob->subpaths) == list_length(root->glob->subplans));
3107 : 2913 : ctepath = (Path *) list_nth(root->glob->subpaths, plan_id - 1);
6422 3108 : 2913 : cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
3109 : :
3110 : : /* Mark rel with estimated output rows, width, etc */
3711 3111 : 2913 : set_cte_size_estimates(root, rel, cteplan->plan_rows);
3112 : :
3113 : : /* Convert the ctepath's pathkeys to outer query's representation */
770 3114 : 2913 : pathkeys = convert_subquery_pathkeys(root,
3115 : : rel,
3116 : : ctepath->pathkeys,
3117 : : cteplan->targetlist);
3118 : :
3119 : : /*
3120 : : * We don't support pushing join clauses into the quals of a CTE scan, but
3121 : : * it could still have required parameterization due to LATERAL refs in
3122 : : * its tlist.
3123 : : */
5000 3124 : 2913 : required_outer = rel->lateral_relids;
3125 : :
3126 : : /* Generate appropriate path */
770 3127 : 2913 : add_path(rel, create_ctescan_path(root, rel, pathkeys, required_outer));
6422 3128 : 2913 : }
3129 : :
3130 : : /*
3131 : : * set_namedtuplestore_pathlist
3132 : : * Build the (single) access path for a named tuplestore RTE
3133 : : *
3134 : : * There's no need for a separate set_namedtuplestore_size phase, since we
3135 : : * don't support join-qual-parameterized paths for tuplestores.
3136 : : */
3137 : : static void
3322 kgrittn@postgresql.o 3138 : 438 : set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
3139 : : RangeTblEntry *rte)
3140 : : {
3141 : : Relids required_outer;
3142 : :
3143 : : /* Mark rel with estimated output rows, width, etc */
3144 : 438 : set_namedtuplestore_size_estimates(root, rel);
3145 : :
3146 : : /*
3147 : : * We don't support pushing join clauses into the quals of a tuplestore
3148 : : * scan, but it could still have required parameterization due to LATERAL
3149 : : * refs in its tlist.
3150 : : */
3151 : 438 : required_outer = rel->lateral_relids;
3152 : :
3153 : : /* Generate appropriate path */
3154 : 438 : add_path(rel, create_namedtuplestorescan_path(root, rel, required_outer));
3155 : 438 : }
3156 : :
3157 : : /*
3158 : : * set_result_pathlist
3159 : : * Build the (single) access path for an RTE_RESULT RTE
3160 : : *
3161 : : * There's no need for a separate set_result_size phase, since we
3162 : : * don't support join-qual-parameterized paths for these RTEs.
3163 : : */
3164 : : static void
2654 tgl@sss.pgh.pa.us 3165 : 3616 : set_result_pathlist(PlannerInfo *root, RelOptInfo *rel,
3166 : : RangeTblEntry *rte)
3167 : : {
3168 : : Relids required_outer;
3169 : :
3170 : : /* Mark rel with estimated output rows, width, etc */
3171 : 3616 : set_result_size_estimates(root, rel);
3172 : :
3173 : : /*
3174 : : * We don't support pushing join clauses into the quals of a Result scan,
3175 : : * but it could still have required parameterization due to LATERAL refs
3176 : : * in its tlist.
3177 : : */
3178 : 3616 : required_outer = rel->lateral_relids;
3179 : :
3180 : : /* Generate appropriate path */
3181 : 3616 : add_path(rel, create_resultscan_path(root, rel, required_outer));
3182 : 3616 : }
3183 : :
3184 : : /*
3185 : : * set_worktable_pathlist
3186 : : * Build the (single) access path for a self-reference CTE RTE
3187 : : *
3188 : : * There's no need for a separate set_worktable_size phase, since we don't
3189 : : * support join-qual-parameterized paths for CTEs.
3190 : : */
3191 : : static void
6422 3192 : 694 : set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
3193 : : {
3194 : : Path *ctepath;
3195 : : PlannerInfo *cteroot;
3196 : : Index levelsup;
3197 : : Relids required_outer;
3198 : :
3199 : : /*
3200 : : * We need to find the non-recursive term's path, which is in the plan
3201 : : * level that's processing the recursive UNION, which is one level *below*
3202 : : * where the CTE comes from.
3203 : : */
3204 : 694 : levelsup = rte->ctelevelsup;
3205 [ - + ]: 694 : if (levelsup == 0) /* shouldn't happen */
6422 tgl@sss.pgh.pa.us 3206 [ # # ]:UBC 0 : elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
6422 tgl@sss.pgh.pa.us 3207 :CBC 694 : levelsup--;
3208 : 694 : cteroot = root;
3209 [ + + ]: 1630 : while (levelsup-- > 0)
3210 : : {
3211 : 936 : cteroot = cteroot->parent_root;
3212 [ - + ]: 936 : if (!cteroot) /* shouldn't happen */
6422 tgl@sss.pgh.pa.us 3213 [ # # ]:UBC 0 : elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3214 : : }
3711 tgl@sss.pgh.pa.us 3215 :CBC 694 : ctepath = cteroot->non_recursive_path;
3216 [ - + ]: 694 : if (!ctepath) /* shouldn't happen */
3711 tgl@sss.pgh.pa.us 3217 [ # # ]:UBC 0 : elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
3218 : :
3219 : : /* Mark rel with estimated output rows, width, etc */
3711 tgl@sss.pgh.pa.us 3220 :CBC 694 : set_cte_size_estimates(root, rel, ctepath->rows);
3221 : :
3222 : : /*
3223 : : * We don't support pushing join clauses into the quals of a worktable
3224 : : * scan, but it could still have required parameterization due to LATERAL
3225 : : * refs in its tlist. (I'm not sure this is actually possible given the
3226 : : * restrictions on recursive references, but it's easy enough to support.)
3227 : : */
5000 3228 : 694 : required_outer = rel->lateral_relids;
3229 : :
3230 : : /* Generate appropriate path */
3231 : 694 : add_path(rel, create_worktablescan_path(root, rel, required_outer));
6422 3232 : 694 : }
3233 : :
3234 : : /*
3235 : : * generate_gather_paths
3236 : : * Generate parallel access paths for a relation by pushing a Gather or
3237 : : * Gather Merge on top of a partial path.
3238 : : *
3239 : : * This must not be called until after we're done creating all partial paths
3240 : : * for the specified relation. (Otherwise, add_partial_path might delete a
3241 : : * path that some GatherPath or GatherMergePath has a reference to.)
3242 : : *
3243 : : * If we're generating paths for a scan or join relation, override_rows will
3244 : : * be false, and we'll just use the relation's size estimate. When we're
3245 : : * being called for a partially-grouped or partially-distinct path, though, we
3246 : : * need to override the rowcount estimate. (It's not clear that the
3247 : : * particular value we're using here is actually best, but the underlying rel
3248 : : * has no estimate so we must do something.)
3249 : : */
3250 : : void
2990 rhaas@postgresql.org 3251 : 21937 : generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
3252 : : {
3253 : : Path *cheapest_partial_path;
3254 : : Path *simple_gather_path;
3255 : : ListCell *lc;
3256 : : double rows;
3257 : 21937 : double *rowsp = NULL;
3258 : :
3259 : : /* If there are no partial paths, there's nothing to do here. */
3758 3260 [ - + ]: 21937 : if (rel->partial_pathlist == NIL)
3758 rhaas@postgresql.org 3261 :UBC 0 : return;
3262 : :
3263 : : /* Should we override the rel's rowcount estimate? */
2990 rhaas@postgresql.org 3264 [ + + ]:CBC 21937 : if (override_rows)
3265 : 5815 : rowsp = &rows;
3266 : :
3267 : : /*
3268 : : * The output of Gather is always unsorted, so there's only one partial
3269 : : * path of interest: the cheapest one. That will be the one at the front
3270 : : * of partial_pathlist because of the way add_partial_path works.
3271 : : */
3758 3272 : 21937 : cheapest_partial_path = linitial(rel->partial_pathlist);
651 rguo@postgresql.org 3273 : 21937 : rows = compute_gather_rows(cheapest_partial_path);
3274 : : simple_gather_path = (Path *)
3697 rhaas@postgresql.org 3275 : 21937 : create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
3276 : : NULL, rowsp);
3758 3277 : 21937 : add_path(rel, simple_gather_path);
3278 : :
3279 : : /*
3280 : : * For each useful ordering, we can consider an order-preserving Gather
3281 : : * Merge.
3282 : : */
3275 bruce@momjian.us 3283 [ + - + + : 48855 : foreach(lc, rel->partial_pathlist)
+ + ]
3284 : : {
3285 : 26918 : Path *subpath = (Path *) lfirst(lc);
3286 : : GatherMergePath *path;
3287 : :
3344 rhaas@postgresql.org 3288 [ + + ]: 26918 : if (subpath->pathkeys == NIL)
3289 : 21372 : continue;
3290 : :
651 rguo@postgresql.org 3291 : 5546 : rows = compute_gather_rows(subpath);
3344 rhaas@postgresql.org 3292 : 5546 : path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
3293 : : subpath->pathkeys, NULL, rowsp);
3294 : 5546 : add_path(rel, &path->path);
3295 : : }
3296 : : }
3297 : :
3298 : : /*
3299 : : * get_useful_pathkeys_for_relation
3300 : : * Determine which orderings of a relation might be useful.
3301 : : *
3302 : : * Getting data in sorted order can be useful either because the requested
3303 : : * order matches the final output ordering for the overall query we're
3304 : : * planning, or because it enables an efficient merge join. Here, we try
3305 : : * to figure out which pathkeys to consider.
3306 : : *
3307 : : * This allows us to do incremental sort on top of an index scan under a gather
3308 : : * merge node, i.e. parallelized.
3309 : : *
3310 : : * If the require_parallel_safe is true, we also require the expressions to
3311 : : * be parallel safe (which allows pushing the sort below Gather Merge).
3312 : : *
3313 : : * XXX At the moment this can only ever return a list with a single element,
3314 : : * because it looks at query_pathkeys only. So we might return the pathkeys
3315 : : * directly, but it seems plausible we'll want to consider other orderings
3316 : : * in the future. For example, we might want to consider pathkeys useful for
3317 : : * merge joins.
3318 : : */
3319 : : static List *
1961 tomas.vondra@postgre 3320 : 21937 : get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel,
3321 : : bool require_parallel_safe)
3322 : : {
2219 3323 : 21937 : List *useful_pathkeys_list = NIL;
3324 : :
3325 : : /*
3326 : : * Considering query_pathkeys is always worth it, because it might allow
3327 : : * us to avoid a total sort when we have a partially presorted path
3328 : : * available or to push the total sort into the parallel portion of the
3329 : : * query.
3330 : : */
3331 [ + + ]: 21937 : if (root->query_pathkeys)
3332 : : {
3333 : : ListCell *lc;
2182 tgl@sss.pgh.pa.us 3334 : 13191 : int npathkeys = 0; /* useful pathkeys */
3335 : :
2219 tomas.vondra@postgre 3336 [ + - + + : 23272 : foreach(lc, root->query_pathkeys)
+ + ]
3337 : : {
3338 : 17003 : PathKey *pathkey = (PathKey *) lfirst(lc);
3339 : 17003 : EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
3340 : :
3341 : : /*
3342 : : * We can only build a sort for pathkeys that contain a
3343 : : * safe-to-compute-early EC member computable from the current
3344 : : * relation's reltarget, so ignore the remainder of the list as
3345 : : * soon as we find a pathkey without such a member.
3346 : : *
3347 : : * It's still worthwhile to return any prefix of the pathkeys list
3348 : : * that meets this requirement, as we may be able to do an
3349 : : * incremental sort.
3350 : : *
3351 : : * If requested, ensure the sort expression is parallel-safe too.
3352 : : */
1841 tgl@sss.pgh.pa.us 3353 [ + + ]: 17003 : if (!relation_can_be_sorted_early(root, rel, pathkey_ec,
3354 : : require_parallel_safe))
2219 tomas.vondra@postgre 3355 : 6922 : break;
3356 : :
3357 : 10081 : npathkeys++;
3358 : : }
3359 : :
3360 : : /*
3361 : : * The whole query_pathkeys list matches, so append it directly, to
3362 : : * allow comparing pathkeys easily by comparing list pointer. If we
3363 : : * have to truncate the pathkeys, we gotta do a copy though.
3364 : : */
3365 [ + + ]: 13191 : if (npathkeys == list_length(root->query_pathkeys))
3366 : 6269 : useful_pathkeys_list = lappend(useful_pathkeys_list,
3367 : 6269 : root->query_pathkeys);
3368 [ + + ]: 6922 : else if (npathkeys > 0)
3369 : 545 : useful_pathkeys_list = lappend(useful_pathkeys_list,
1392 drowley@postgresql.o 3370 : 545 : list_copy_head(root->query_pathkeys,
3371 : : npathkeys));
3372 : : }
3373 : :
2219 tomas.vondra@postgre 3374 : 21937 : return useful_pathkeys_list;
3375 : : }
3376 : :
3377 : : /*
3378 : : * generate_useful_gather_paths
3379 : : * Generate parallel access paths for a relation by pushing a Gather or
3380 : : * Gather Merge on top of a partial path.
3381 : : *
3382 : : * Unlike plain generate_gather_paths, this looks both at pathkeys of input
3383 : : * paths (aiming to preserve the ordering), but also considers ordering that
3384 : : * might be useful for nodes above the gather merge node, and tries to add
3385 : : * a sort (regular or incremental) to provide that.
3386 : : */
3387 : : void
3388 : 487655 : generate_useful_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
3389 : : {
3390 : : ListCell *lc;
3391 : : double rows;
3392 : 487655 : double *rowsp = NULL;
3393 : 487655 : List *useful_pathkeys_list = NIL;
3394 : 487655 : Path *cheapest_partial_path = NULL;
3395 : :
3396 : : /* If there are no partial paths, there's nothing to do here. */
3397 [ + + ]: 487655 : if (rel->partial_pathlist == NIL)
3398 : 465718 : return;
3399 : :
3400 : : /* Should we override the rel's rowcount estimate? */
3401 [ + + ]: 21937 : if (override_rows)
3402 : 5815 : rowsp = &rows;
3403 : :
3404 : : /* generate the regular gather (merge) paths */
3405 : 21937 : generate_gather_paths(root, rel, override_rows);
3406 : :
3407 : : /* consider incremental sort for interesting orderings */
1961 3408 : 21937 : useful_pathkeys_list = get_useful_pathkeys_for_relation(root, rel, true);
3409 : :
3410 : : /* used for explicit (full) sort paths */
2219 3411 : 21937 : cheapest_partial_path = linitial(rel->partial_pathlist);
3412 : :
3413 : : /*
3414 : : * Consider sorted paths for each interesting ordering. We generate both
3415 : : * incremental and full sort.
3416 : : */
3417 [ + + + + : 28751 : foreach(lc, useful_pathkeys_list)
+ + ]
3418 : : {
3419 : 6814 : List *useful_pathkeys = lfirst(lc);
3420 : : ListCell *lc2;
3421 : : bool is_sorted;
3422 : : int presorted_keys;
3423 : :
3424 [ + - + + : 15857 : foreach(lc2, rel->partial_pathlist)
+ + ]
3425 : : {
3426 : 9043 : Path *subpath = (Path *) lfirst(lc2);
3427 : : GatherMergePath *path;
3428 : :
3429 : 9043 : is_sorted = pathkeys_count_contained_in(useful_pathkeys,
3430 : : subpath->pathkeys,
3431 : : &presorted_keys);
3432 : :
3433 : : /*
3434 : : * We don't need to consider the case where a subpath is already
3435 : : * fully sorted because generate_gather_paths already creates a
3436 : : * gather merge path for every subpath that has pathkeys present.
3437 : : *
3438 : : * But since the subpath is already sorted, we know we don't need
3439 : : * to consider adding a sort (full or incremental) on top of it,
3440 : : * so we can continue here.
3441 : : */
3442 [ + + ]: 9043 : if (is_sorted)
3443 : 2380 : continue;
3444 : :
3445 : : /*
3446 : : * Try at least sorting the cheapest path and also try
3447 : : * incrementally sorting any path which is partially sorted
3448 : : * already (no need to deal with paths which have presorted keys
3449 : : * when incremental sort is disabled unless it's the cheapest
3450 : : * input path).
3451 : : */
1236 drowley@postgresql.o 3452 [ + + ]: 6663 : if (subpath != cheapest_partial_path &&
3453 [ + + + + ]: 260 : (presorted_keys == 0 || !enable_incremental_sort))
3454 : 73 : continue;
3455 : :
3456 : : /*
3457 : : * Consider regular sort for any path that's not presorted or if
3458 : : * incremental sort is disabled. We've no need to consider both
3459 : : * sort and incremental sort on the same path. We assume that
3460 : : * incremental sort is always faster when there are presorted
3461 : : * keys.
3462 : : *
3463 : : * This is not redundant with the gather paths created in
3464 : : * generate_gather_paths, because that doesn't generate ordered
3465 : : * output. Here we add an explicit sort to match the useful
3466 : : * ordering.
3467 : : */
3468 [ + + + + ]: 6590 : if (presorted_keys == 0 || !enable_incremental_sort)
3469 : : {
3470 : 6392 : subpath = (Path *) create_sort_path(root,
3471 : : rel,
3472 : : subpath,
3473 : : useful_pathkeys,
3474 : : -1.0);
3475 : : }
3476 : : else
3477 : 198 : subpath = (Path *) create_incremental_sort_path(root,
3478 : : rel,
3479 : : subpath,
3480 : : useful_pathkeys,
3481 : : presorted_keys,
3482 : : -1);
651 rguo@postgresql.org 3483 : 6590 : rows = compute_gather_rows(subpath);
1236 drowley@postgresql.o 3484 : 6590 : path = create_gather_merge_path(root, rel,
3485 : : subpath,
3486 : 6590 : rel->reltarget,
3487 : : subpath->pathkeys,
3488 : : NULL,
3489 : : rowsp);
3490 : :
3491 : 6590 : add_path(rel, &path->path);
3492 : : }
3493 : : }
3494 : : }
3495 : :
3496 : : /*
3497 : : * generate_grouped_paths
3498 : : * Generate paths for a grouped relation by adding sorted and hashed
3499 : : * partial aggregation paths on top of paths of the ungrouped relation.
3500 : : *
3501 : : * The information needed is provided by the RelAggInfo structure stored in
3502 : : * "grouped_rel".
3503 : : */
3504 : : void
209 rguo@postgresql.org 3505 :GNC 727 : generate_grouped_paths(PlannerInfo *root, RelOptInfo *grouped_rel,
3506 : : RelOptInfo *rel)
3507 : : {
3508 : 727 : RelAggInfo *agg_info = grouped_rel->agg_info;
3509 : : AggClauseCosts agg_costs;
3510 : : bool can_hash;
3511 : : bool can_sort;
3512 : 727 : Path *cheapest_total_path = NULL;
3513 : 727 : Path *cheapest_partial_path = NULL;
3514 : 727 : double dNumGroups = 0;
3515 : 727 : double dNumPartialGroups = 0;
3516 : 727 : List *group_pathkeys = NIL;
3517 : :
3518 [ - + ]: 727 : if (IS_DUMMY_REL(rel))
3519 : : {
209 rguo@postgresql.org 3520 :UNC 0 : mark_dummy_rel(grouped_rel);
3521 : 0 : return;
3522 : : }
3523 : :
3524 : : /*
3525 : : * We push partial aggregation only to the lowest possible level in the
3526 : : * join tree that is deemed useful.
3527 : : */
203 rguo@postgresql.org 3528 [ + - ]:GNC 727 : if (!bms_equal(agg_info->apply_agg_at, rel->relids) ||
209 3529 [ - + ]: 727 : !agg_info->agg_useful)
209 rguo@postgresql.org 3530 :UNC 0 : return;
3531 : :
209 rguo@postgresql.org 3532 [ + - + - :GNC 4362 : MemSet(&agg_costs, 0, sizeof(AggClauseCosts));
+ - + - +
+ ]
3533 : 727 : get_agg_clause_costs(root, AGGSPLIT_INITIAL_SERIAL, &agg_costs);
3534 : :
3535 : : /*
3536 : : * Determine whether it's possible to perform sort-based implementations
3537 : : * of grouping, and generate the pathkeys that represent the grouping
3538 : : * requirements in that case.
3539 : : */
3540 : 727 : can_sort = grouping_is_sortable(agg_info->group_clauses);
3541 [ + - ]: 727 : if (can_sort)
3542 : : {
3543 : : RelOptInfo *top_grouped_rel;
3544 : : List *top_group_tlist;
3545 : :
3546 [ + + - + ]: 397 : top_grouped_rel = IS_OTHER_REL(rel) ?
3547 [ + + ]: 1124 : rel->top_parent->grouped_rel : grouped_rel;
3548 : : top_group_tlist =
3549 : 727 : make_tlist_from_pathtarget(top_grouped_rel->agg_info->target);
3550 : :
3551 : : group_pathkeys =
3552 : 727 : make_pathkeys_for_sortclauses(root, agg_info->group_clauses,
3553 : : top_group_tlist);
3554 : : }
3555 : :
3556 : : /*
3557 : : * Determine whether we should consider hash-based implementations of
3558 : : * grouping.
3559 : : */
3560 [ - + ]: 727 : Assert(root->numOrderedAggs == 0);
3561 [ + - + - ]: 1454 : can_hash = (agg_info->group_clauses != NIL &&
3562 : 727 : grouping_is_hashable(agg_info->group_clauses));
3563 : :
3564 : : /*
3565 : : * Consider whether we should generate partially aggregated non-partial
3566 : : * paths. We can only do this if we have a non-partial path.
3567 : : */
3568 [ + - ]: 727 : if (rel->pathlist != NIL)
3569 : : {
3570 : 727 : cheapest_total_path = rel->cheapest_total_path;
3571 [ - + ]: 727 : Assert(cheapest_total_path != NULL);
3572 : : }
3573 : :
3574 : : /*
3575 : : * If parallelism is possible for grouped_rel, then we should consider
3576 : : * generating partially-grouped partial paths. However, if the ungrouped
3577 : : * rel has no partial paths, then we can't.
3578 : : */
3579 [ + + + + ]: 727 : if (grouped_rel->consider_parallel && rel->partial_pathlist != NIL)
3580 : : {
3581 : 610 : cheapest_partial_path = linitial(rel->partial_pathlist);
3582 [ - + ]: 610 : Assert(cheapest_partial_path != NULL);
3583 : : }
3584 : :
3585 : : /* Estimate number of partial groups. */
3586 [ + - ]: 727 : if (cheapest_total_path != NULL)
3587 : 727 : dNumGroups = estimate_num_groups(root,
3588 : : agg_info->group_exprs,
3589 : : cheapest_total_path->rows,
3590 : : NULL, NULL);
3591 [ + + ]: 727 : if (cheapest_partial_path != NULL)
3592 : 610 : dNumPartialGroups = estimate_num_groups(root,
3593 : : agg_info->group_exprs,
3594 : : cheapest_partial_path->rows,
3595 : : NULL, NULL);
3596 : :
3597 [ + - + - ]: 727 : if (can_sort && cheapest_total_path != NULL)
3598 : : {
3599 : : ListCell *lc;
3600 : :
3601 : : /*
3602 : : * Use any available suitably-sorted path as input, and also consider
3603 : : * sorting the cheapest-total path and incremental sort on any paths
3604 : : * with presorted keys.
3605 : : *
3606 : : * To save planning time, we ignore parameterized input paths unless
3607 : : * they are the cheapest-total path.
3608 : : */
3609 [ + - + + : 1735 : foreach(lc, rel->pathlist)
+ + ]
3610 : : {
3611 : 1008 : Path *input_path = (Path *) lfirst(lc);
3612 : : Path *path;
3613 : : bool is_sorted;
3614 : : int presorted_keys;
3615 : :
3616 : : /*
3617 : : * Ignore parameterized paths that are not the cheapest-total
3618 : : * path.
3619 : : */
3620 [ + + + - ]: 1008 : if (input_path->param_info &&
3621 : : input_path != cheapest_total_path)
3622 : 23 : continue;
3623 : :
3624 : 1003 : is_sorted = pathkeys_count_contained_in(group_pathkeys,
3625 : : input_path->pathkeys,
3626 : : &presorted_keys);
3627 : :
3628 : : /*
3629 : : * Ignore paths that are not suitably or partially sorted, unless
3630 : : * they are the cheapest total path (no need to deal with paths
3631 : : * which have presorted keys when incremental sort is disabled).
3632 : : */
3633 [ + + + + ]: 1003 : if (!is_sorted && input_path != cheapest_total_path &&
3634 [ + + - + ]: 122 : (presorted_keys == 0 || !enable_incremental_sort))
3635 : 18 : continue;
3636 : :
3637 : : /*
3638 : : * Since the path originates from a non-grouped relation that is
3639 : : * not aware of eager aggregation, we must ensure that it provides
3640 : : * the correct input for partial aggregation.
3641 : : */
3642 : 985 : path = (Path *) create_projection_path(root,
3643 : : grouped_rel,
3644 : : input_path,
3645 : 985 : agg_info->agg_input);
3646 : :
3647 [ + + ]: 985 : if (!is_sorted)
3648 : : {
3649 : : /*
3650 : : * We've no need to consider both a sort and incremental sort.
3651 : : * We'll just do a sort if there are no presorted keys and an
3652 : : * incremental sort when there are presorted keys.
3653 : : */
3654 [ + + - + ]: 826 : if (presorted_keys == 0 || !enable_incremental_sort)
3655 : 716 : path = (Path *) create_sort_path(root,
3656 : : grouped_rel,
3657 : : path,
3658 : : group_pathkeys,
3659 : : -1.0);
3660 : : else
3661 : 110 : path = (Path *) create_incremental_sort_path(root,
3662 : : grouped_rel,
3663 : : path,
3664 : : group_pathkeys,
3665 : : presorted_keys,
3666 : : -1.0);
3667 : : }
3668 : :
3669 : : /*
3670 : : * qual is NIL because the HAVING clause cannot be evaluated until
3671 : : * the final value of the aggregate is known.
3672 : : */
3673 : 985 : path = (Path *) create_agg_path(root,
3674 : : grouped_rel,
3675 : : path,
3676 : 985 : agg_info->target,
3677 : : AGG_SORTED,
3678 : : AGGSPLIT_INITIAL_SERIAL,
3679 : : agg_info->group_clauses,
3680 : : NIL,
3681 : : &agg_costs,
3682 : : dNumGroups);
3683 : :
3684 : 985 : add_path(grouped_rel, path);
3685 : : }
3686 : : }
3687 : :
3688 [ + - + + ]: 727 : if (can_sort && cheapest_partial_path != NULL)
3689 : : {
3690 : : ListCell *lc;
3691 : :
3692 : : /* Similar to above logic, but for partial paths. */
3693 [ + - + + : 1394 : foreach(lc, rel->partial_pathlist)
+ + ]
3694 : : {
3695 : 784 : Path *input_path = (Path *) lfirst(lc);
3696 : : Path *path;
3697 : : bool is_sorted;
3698 : : int presorted_keys;
3699 : :
3700 : 784 : is_sorted = pathkeys_count_contained_in(group_pathkeys,
3701 : : input_path->pathkeys,
3702 : : &presorted_keys);
3703 : :
3704 : : /*
3705 : : * Ignore paths that are not suitably or partially sorted, unless
3706 : : * they are the cheapest partial path (no need to deal with paths
3707 : : * which have presorted keys when incremental sort is disabled).
3708 : : */
3709 [ + + + + ]: 784 : if (!is_sorted && input_path != cheapest_partial_path &&
3710 [ + - - + ]: 70 : (presorted_keys == 0 || !enable_incremental_sort))
209 rguo@postgresql.org 3711 :UNC 0 : continue;
3712 : :
3713 : : /*
3714 : : * Since the path originates from a non-grouped relation that is
3715 : : * not aware of eager aggregation, we must ensure that it provides
3716 : : * the correct input for partial aggregation.
3717 : : */
209 rguo@postgresql.org 3718 :GNC 784 : path = (Path *) create_projection_path(root,
3719 : : grouped_rel,
3720 : : input_path,
3721 : 784 : agg_info->agg_input);
3722 : :
3723 [ + + ]: 784 : if (!is_sorted)
3724 : : {
3725 : : /*
3726 : : * We've no need to consider both a sort and incremental sort.
3727 : : * We'll just do a sort if there are no presorted keys and an
3728 : : * incremental sort when there are presorted keys.
3729 : : */
3730 [ + + - + ]: 680 : if (presorted_keys == 0 || !enable_incremental_sort)
3731 : 610 : path = (Path *) create_sort_path(root,
3732 : : grouped_rel,
3733 : : path,
3734 : : group_pathkeys,
3735 : : -1.0);
3736 : : else
3737 : 70 : path = (Path *) create_incremental_sort_path(root,
3738 : : grouped_rel,
3739 : : path,
3740 : : group_pathkeys,
3741 : : presorted_keys,
3742 : : -1.0);
3743 : : }
3744 : :
3745 : : /*
3746 : : * qual is NIL because the HAVING clause cannot be evaluated until
3747 : : * the final value of the aggregate is known.
3748 : : */
3749 : 784 : path = (Path *) create_agg_path(root,
3750 : : grouped_rel,
3751 : : path,
3752 : 784 : agg_info->target,
3753 : : AGG_SORTED,
3754 : : AGGSPLIT_INITIAL_SERIAL,
3755 : : agg_info->group_clauses,
3756 : : NIL,
3757 : : &agg_costs,
3758 : : dNumPartialGroups);
3759 : :
3760 : 784 : add_partial_path(grouped_rel, path);
3761 : : }
3762 : : }
3763 : :
3764 : : /*
3765 : : * Add a partially-grouped HashAgg Path where possible
3766 : : */
3767 [ + - + - ]: 727 : if (can_hash && cheapest_total_path != NULL)
3768 : : {
3769 : : Path *path;
3770 : :
3771 : : /*
3772 : : * Since the path originates from a non-grouped relation that is not
3773 : : * aware of eager aggregation, we must ensure that it provides the
3774 : : * correct input for partial aggregation.
3775 : : */
3776 : 727 : path = (Path *) create_projection_path(root,
3777 : : grouped_rel,
3778 : : cheapest_total_path,
3779 : 727 : agg_info->agg_input);
3780 : :
3781 : : /*
3782 : : * qual is NIL because the HAVING clause cannot be evaluated until the
3783 : : * final value of the aggregate is known.
3784 : : */
3785 : 727 : path = (Path *) create_agg_path(root,
3786 : : grouped_rel,
3787 : : path,
3788 : 727 : agg_info->target,
3789 : : AGG_HASHED,
3790 : : AGGSPLIT_INITIAL_SERIAL,
3791 : : agg_info->group_clauses,
3792 : : NIL,
3793 : : &agg_costs,
3794 : : dNumGroups);
3795 : :
3796 : 727 : add_path(grouped_rel, path);
3797 : : }
3798 : :
3799 : : /*
3800 : : * Now add a partially-grouped HashAgg partial Path where possible
3801 : : */
3802 [ + - + + ]: 727 : if (can_hash && cheapest_partial_path != NULL)
3803 : : {
3804 : : Path *path;
3805 : :
3806 : : /*
3807 : : * Since the path originates from a non-grouped relation that is not
3808 : : * aware of eager aggregation, we must ensure that it provides the
3809 : : * correct input for partial aggregation.
3810 : : */
3811 : 610 : path = (Path *) create_projection_path(root,
3812 : : grouped_rel,
3813 : : cheapest_partial_path,
3814 : 610 : agg_info->agg_input);
3815 : :
3816 : : /*
3817 : : * qual is NIL because the HAVING clause cannot be evaluated until the
3818 : : * final value of the aggregate is known.
3819 : : */
3820 : 610 : path = (Path *) create_agg_path(root,
3821 : : grouped_rel,
3822 : : path,
3823 : 610 : agg_info->target,
3824 : : AGG_HASHED,
3825 : : AGGSPLIT_INITIAL_SERIAL,
3826 : : agg_info->group_clauses,
3827 : : NIL,
3828 : : &agg_costs,
3829 : : dNumPartialGroups);
3830 : :
3831 : 610 : add_partial_path(grouped_rel, path);
3832 : : }
3833 : : }
3834 : :
3835 : : /*
3836 : : * make_rel_from_joinlist
3837 : : * Build access paths using a "joinlist" to guide the join path search.
3838 : : *
3839 : : * See comments for deconstruct_jointree() for definition of the joinlist
3840 : : * data structure.
3841 : : */
3842 : : static RelOptInfo *
7441 tgl@sss.pgh.pa.us 3843 :CBC 257812 : make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
3844 : : {
3845 : : int levels_needed;
3846 : : List *initial_rels;
3847 : : ListCell *jl;
3848 : :
3849 : : /*
3850 : : * Count the number of child joinlist nodes. This is the depth of the
3851 : : * dynamic-programming algorithm we must employ to consider all ways of
3852 : : * joining the child nodes.
3853 : : */
3854 : 257812 : levels_needed = list_length(joinlist);
3855 : :
9349 3856 [ - + ]: 257812 : if (levels_needed <= 0)
9349 tgl@sss.pgh.pa.us 3857 :UBC 0 : return NULL; /* nothing to do? */
3858 : :
3859 : : /*
3860 : : * Construct a list of rels corresponding to the child joinlist nodes.
3861 : : * This may contain both base rels and rels constructed according to
3862 : : * sub-joinlists.
3863 : : */
7441 tgl@sss.pgh.pa.us 3864 :CBC 257812 : initial_rels = NIL;
3865 [ + - + + : 626853 : foreach(jl, joinlist)
+ + ]
3866 : : {
3867 : 369041 : Node *jlnode = (Node *) lfirst(jl);
3868 : : RelOptInfo *thisrel;
3869 : :
3870 [ + + ]: 369041 : if (IsA(jlnode, RangeTblRef))
3871 : : {
3872 : 366323 : int varno = ((RangeTblRef *) jlnode)->rtindex;
3873 : :
3874 : 366323 : thisrel = find_base_rel(root, varno);
3875 : : }
3876 [ + - ]: 2718 : else if (IsA(jlnode, List))
3877 : : {
3878 : : /* Recurse to handle subproblem */
3879 : 2718 : thisrel = make_rel_from_joinlist(root, (List *) jlnode);
3880 : : }
3881 : : else
3882 : : {
7441 tgl@sss.pgh.pa.us 3883 [ # # ]:UBC 0 : elog(ERROR, "unrecognized joinlist node type: %d",
3884 : : (int) nodeTag(jlnode));
3885 : : thisrel = NULL; /* keep compiler quiet */
3886 : : }
3887 : :
7441 tgl@sss.pgh.pa.us 3888 :CBC 369041 : initial_rels = lappend(initial_rels, thisrel);
3889 : : }
3890 : :
9349 3891 [ + + ]: 257812 : if (levels_needed == 1)
3892 : : {
3893 : : /*
3894 : : * Single joinlist node, so we're done.
3895 : : */
8014 neilc@samurai.com 3896 : 176707 : return (RelOptInfo *) linitial(initial_rels);
3897 : : }
3898 : : else
3899 : : {
3900 : : /*
3901 : : * Consider the different orders in which we could join the rels,
3902 : : * using a plugin, GEQO, or the regular join search code.
3903 : : *
3904 : : * We put the initial_rels list into a PlannerInfo field because
3905 : : * has_legal_joinclause() needs to look at it (ugly :-().
3906 : : */
6689 tgl@sss.pgh.pa.us 3907 : 81105 : root->initial_rels = initial_rels;
3908 : :
6796 3909 [ - + ]: 81105 : if (join_search_hook)
6796 tgl@sss.pgh.pa.us 3910 :UBC 0 : return (*join_search_hook) (root, levels_needed, initial_rels);
6796 tgl@sss.pgh.pa.us 3911 [ + - + + ]:CBC 81105 : else if (enable_geqo && levels_needed >= geqo_threshold)
9349 3912 : 35 : return geqo(root, levels_needed, initial_rels);
3913 : : else
6796 3914 : 81070 : return standard_join_search(root, levels_needed, initial_rels);
3915 : : }
3916 : : }
3917 : :
3918 : : /*
3919 : : * standard_join_search
3920 : : * Find possible joinpaths for a query by successively finding ways
3921 : : * to join component relations into join relations.
3922 : : *
3923 : : * 'levels_needed' is the number of iterations needed, ie, the number of
3924 : : * independent jointree items in the query. This is > 1.
3925 : : *
3926 : : * 'initial_rels' is a list of RelOptInfo nodes for each independent
3927 : : * jointree item. These are the components to be joined together.
3928 : : * Note that levels_needed == list_length(initial_rels).
3929 : : *
3930 : : * Returns the final level of join relations, i.e., the relation that is
3931 : : * the result of joining all the original relations together.
3932 : : * At least one implementation path must be provided for this relation and
3933 : : * all required sub-relations.
3934 : : *
3935 : : * To support loadable plugins that modify planner behavior by changing the
3936 : : * join searching algorithm, we provide a hook variable that lets a plugin
3937 : : * replace or supplement this function. Any such hook must return the same
3938 : : * final join relation as the standard code would, but it might have a
3939 : : * different set of implementation paths attached, and only the sub-joinrels
3940 : : * needed for these paths need have been instantiated.
3941 : : *
3942 : : * Note to plugin authors: the functions invoked during standard_join_search()
3943 : : * modify root->join_rel_list and root->join_rel_hash. If you want to do more
3944 : : * than one join-order search, you'll probably need to save and restore the
3945 : : * original states of those data structures. See geqo_eval() for an example.
3946 : : */
3947 : : RelOptInfo *
3948 : 81070 : standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
3949 : : {
3950 : : int lev;
3951 : : RelOptInfo *rel;
3952 : :
3953 : : /*
3954 : : * This function cannot be invoked recursively within any one planning
3955 : : * problem, so join_rel_level[] can't be in use already.
3956 : : */
6002 3957 [ - + ]: 81070 : Assert(root->join_rel_level == NULL);
3958 : :
3959 : : /*
3960 : : * We employ a simple "dynamic programming" algorithm: we first find all
3961 : : * ways to build joins of two jointree items, then all ways to build joins
3962 : : * of three items (from two-item joins and single items), then four-item
3963 : : * joins, and so on until we have considered all ways to join all the
3964 : : * items into one rel.
3965 : : *
3966 : : * root->join_rel_level[j] is a list of all the j-item rels. Initially we
3967 : : * set root->join_rel_level[1] to represent all the single-jointree-item
3968 : : * relations.
3969 : : */
3970 : 81070 : root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
3971 : :
3972 : 81070 : root->join_rel_level[1] = initial_rels;
3973 : :
9584 3974 [ + + ]: 192249 : for (lev = 2; lev <= levels_needed; lev++)
3975 : : {
3976 : : ListCell *lc;
3977 : :
3978 : : /*
3979 : : * Determine all possible pairs of relations to be joined at this
3980 : : * level, and build paths for making each one from every available
3981 : : * pair of lower-level relations.
3982 : : */
6002 3983 : 111179 : join_search_one_level(root, lev);
3984 : :
3985 : : /*
3986 : : * Run generate_partitionwise_join_paths() and
3987 : : * generate_useful_gather_paths() for each just-processed joinrel. We
3988 : : * could not do this earlier because both regular and partial paths
3989 : : * can get added to a particular joinrel at multiple times within
3990 : : * join_search_one_level.
3991 : : *
3992 : : * After that, we're done creating paths for the joinrel, so run
3993 : : * set_cheapest().
3994 : : *
3995 : : * In addition, we also run generate_grouped_paths() for the grouped
3996 : : * relation of each just-processed joinrel, and run set_cheapest() for
3997 : : * the grouped relation afterwards.
3998 : : */
3999 [ + + + + : 284934 : foreach(lc, root->join_rel_level[lev])
+ + ]
4000 : : {
4001 : : bool is_top_rel;
4002 : :
4003 : 173755 : rel = (RelOptInfo *) lfirst(lc);
4004 : :
209 rguo@postgresql.org 4005 :GNC 173755 : is_top_rel = bms_equal(rel->relids, root->all_query_rels);
4006 : :
4007 : : /* Create paths for partitionwise joins. */
3000 peter_e@gmx.net 4008 :CBC 173755 : generate_partitionwise_join_paths(root, rel);
4009 : :
4010 : : /*
4011 : : * Except for the topmost scan/join rel, consider gathering
4012 : : * partial paths. We'll do the same for the topmost scan/join rel
4013 : : * once we know the final targetlist (see grouping_planner's and
4014 : : * its call to apply_scanjoin_target_to_paths).
4015 : : */
209 rguo@postgresql.org 4016 [ + + ]:GNC 173755 : if (!is_top_rel)
2219 tomas.vondra@postgre 4017 :CBC 93094 : generate_useful_gather_paths(root, rel, false);
4018 : :
4019 : : /* Find and save the cheapest paths for this rel */
9576 tgl@sss.pgh.pa.us 4020 : 173755 : set_cheapest(rel);
4021 : :
4022 : : /*
4023 : : * Except for the topmost scan/join rel, consider generating
4024 : : * partial aggregation paths for the grouped relation on top of
4025 : : * the paths of this rel. After that, we're done creating paths
4026 : : * for the grouped relation, so run set_cheapest().
4027 : : */
209 rguo@postgresql.org 4028 [ + + + + ]:GNC 173755 : if (rel->grouped_rel != NULL && !is_top_rel)
4029 : : {
4030 : 60 : RelOptInfo *grouped_rel = rel->grouped_rel;
4031 : :
4032 [ - + ]: 60 : Assert(IS_GROUPED_REL(grouped_rel));
4033 : :
4034 : 60 : generate_grouped_paths(root, grouped_rel, rel);
4035 : 60 : set_cheapest(grouped_rel);
4036 : : }
4037 : :
4038 : : #ifdef OPTIMIZER_DEBUG
4039 : : pprint(rel);
4040 : : #endif
4041 : : }
4042 : : }
4043 : :
4044 : : /*
4045 : : * We should have a single rel at the final level.
4046 : : */
6002 tgl@sss.pgh.pa.us 4047 [ - + ]:CBC 81070 : if (root->join_rel_level[levels_needed] == NIL)
8175 tgl@sss.pgh.pa.us 4048 [ # # ]:UBC 0 : elog(ERROR, "failed to build any %d-way joins", levels_needed);
6002 tgl@sss.pgh.pa.us 4049 [ - + ]:CBC 81070 : Assert(list_length(root->join_rel_level[levels_needed]) == 1);
4050 : :
4051 : 81070 : rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
4052 : :
4053 : 81070 : root->join_rel_level = NULL;
4054 : :
9584 4055 : 81070 : return rel;
4056 : : }
4057 : :
4058 : : /*****************************************************************************
4059 : : * PUSHING QUALS DOWN INTO SUBQUERIES
4060 : : *****************************************************************************/
4061 : :
4062 : : /*
4063 : : * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
4064 : : *
4065 : : * subquery is the particular component query being checked. topquery
4066 : : * is the top component of a set-operations tree (the same Query if no
4067 : : * set-op is involved).
4068 : : *
4069 : : * Conditions checked here:
4070 : : *
4071 : : * 1. If the subquery has a LIMIT clause, we must not push down any quals,
4072 : : * since that could change the set of rows returned.
4073 : : *
4074 : : * 2. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
4075 : : * quals into it, because that could change the results.
4076 : : *
4077 : : * 3. If the subquery uses DISTINCT, we cannot push volatile quals into it.
4078 : : * This is because upper-level quals should semantically be evaluated only
4079 : : * once per distinct row, not once per original row, and if the qual is
4080 : : * volatile then extra evaluations could change the results. (This issue
4081 : : * does not apply to other forms of aggregation such as GROUP BY, because
4082 : : * when those are present we push into HAVING not WHERE, so that the quals
4083 : : * are still applied after aggregation.)
4084 : : *
4085 : : * 4. If the subquery contains window functions, we cannot push volatile quals
4086 : : * into it. The issue here is a bit different from DISTINCT: a volatile qual
4087 : : * might succeed for some rows of a window partition and fail for others,
4088 : : * thereby changing the partition contents and thus the window functions'
4089 : : * results for rows that remain.
4090 : : *
4091 : : * 5. If the subquery contains any set-returning functions in its targetlist,
4092 : : * we cannot push volatile quals into it. That would push them below the SRFs
4093 : : * and thereby change the number of times they are evaluated. Also, a
4094 : : * volatile qual could succeed for some SRF output rows and fail for others,
4095 : : * a behavior that cannot occur if it's evaluated before SRF expansion.
4096 : : *
4097 : : * 6. If the subquery has nonempty grouping sets, we cannot push down any
4098 : : * quals. The concern here is that a qual referencing a "constant" grouping
4099 : : * column could get constant-folded, which would be improper because the value
4100 : : * is potentially nullable by grouping-set expansion. This restriction could
4101 : : * be removed if we had a parsetree representation that shows that such
4102 : : * grouping columns are not really constant. (There are other ideas that
4103 : : * could be used to relax this restriction, but that's the approach most
4104 : : * likely to get taken in the future. Note that there's not much to be gained
4105 : : * so long as subquery_planner can't move HAVING clauses to WHERE within such
4106 : : * a subquery.)
4107 : : *
4108 : : * In addition, we make several checks on the subquery's output columns to see
4109 : : * if it is safe to reference them in pushed-down quals. If output column k
4110 : : * is found to be unsafe to reference, we set the reason for that inside
4111 : : * safetyInfo->unsafeFlags[k], but we don't reject the subquery overall since
4112 : : * column k might not be referenced by some/all quals. The unsafeFlags[]
4113 : : * array will be consulted later by qual_is_pushdown_safe(). It's better to
4114 : : * do it this way than to make the checks directly in qual_is_pushdown_safe(),
4115 : : * because when the subquery involves set operations we have to check the
4116 : : * output expressions in each arm of the set op.
4117 : : *
4118 : : * Note: pushing quals into a DISTINCT subquery is theoretically dubious:
4119 : : * we're effectively assuming that the quals cannot distinguish values that
4120 : : * the DISTINCT's equality operator sees as equal, yet there are many
4121 : : * counterexamples to that assumption. However use of such a qual with a
4122 : : * DISTINCT subquery would be unsafe anyway, since there's no guarantee which
4123 : : * "equal" value will be chosen as the output value by the DISTINCT operation.
4124 : : * So we don't worry too much about that. Another objection is that if the
4125 : : * qual is expensive to evaluate, running it for each original row might cost
4126 : : * more than we save by eliminating rows before the DISTINCT step. But it
4127 : : * would be very hard to estimate that at this stage, and in practice pushdown
4128 : : * seldom seems to make things worse, so we ignore that problem too.
4129 : : *
4130 : : * Note: likewise, pushing quals into a subquery with window functions is a
4131 : : * bit dubious: the quals might remove some rows of a window partition while
4132 : : * leaving others, causing changes in the window functions' results for the
4133 : : * surviving rows. We insist that such a qual reference only partitioning
4134 : : * columns, but again that only protects us if the qual does not distinguish
4135 : : * values that the partitioning equality operator sees as equal. The risks
4136 : : * here are perhaps larger than for DISTINCT, since no de-duplication of rows
4137 : : * occurs and thus there is no theoretical problem with such a qual. But
4138 : : * we'll do this anyway because the potential performance benefits are very
4139 : : * large, and we've seen no field complaints about the longstanding comparable
4140 : : * behavior with DISTINCT.
4141 : : */
4142 : : static bool
8412 4143 : 2453 : subquery_is_pushdown_safe(Query *subquery, Query *topquery,
4144 : : pushdown_safety_info *safetyInfo)
4145 : : {
4146 : : SetOperationStmt *topop;
4147 : :
4148 : : /* Check point 1 */
8445 4149 [ + + + + ]: 2453 : if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
8650 4150 : 111 : return false;
4151 : :
4152 : : /* Check point 6 */
2082 4153 [ + + + + ]: 2342 : if (subquery->groupClause && subquery->groupingSets)
4154 : 10 : return false;
4155 : :
4156 : : /* Check points 3, 4, and 5 */
3507 4157 [ + + ]: 2332 : if (subquery->distinctClause ||
4158 [ + + ]: 2262 : subquery->hasWindowFuncs ||
4159 [ + + ]: 2041 : subquery->hasTargetSRFs)
4330 4160 : 706 : safetyInfo->unsafeVolatile = true;
4161 : :
4162 : : /*
4163 : : * If we're at a leaf query, check for unsafe expressions in its target
4164 : : * list, and mark any reasons why they're unsafe in unsafeFlags[].
4165 : : * (Non-leaf nodes in setop trees have only simple Vars in their tlists,
4166 : : * so no need to check them.)
4167 : : */
4717 4168 [ + + ]: 2332 : if (subquery->setOperations == NULL)
4330 4169 : 2200 : check_output_expressions(subquery, safetyInfo);
4170 : :
4171 : : /* Are we at top level, or looking at a setop component? */
8650 4172 [ + + ]: 2332 : if (subquery == topquery)
4173 : : {
4174 : : /* Top level, so check any component queries */
4175 [ + + ]: 2068 : if (subquery->setOperations != NULL)
8412 4176 [ - + ]: 132 : if (!recurse_pushdown_safe(subquery->setOperations, topquery,
4177 : : safetyInfo))
8650 tgl@sss.pgh.pa.us 4178 :UBC 0 : return false;
4179 : : }
4180 : : else
4181 : : {
4182 : : /* Setop component must not have more components (too weird) */
8650 tgl@sss.pgh.pa.us 4183 [ - + ]:CBC 264 : if (subquery->setOperations != NULL)
8650 tgl@sss.pgh.pa.us 4184 :UBC 0 : return false;
4185 : : /* Check whether setop component output types match top level */
3360 peter_e@gmx.net 4186 :CBC 264 : topop = castNode(SetOperationStmt, topquery->setOperations);
4187 [ - + ]: 264 : Assert(topop);
8412 tgl@sss.pgh.pa.us 4188 : 264 : compare_tlist_datatypes(subquery->targetList,
4189 : : topop->colTypes,
4190 : : safetyInfo);
4191 : : }
8650 4192 : 2332 : return true;
4193 : : }
4194 : :
4195 : : /*
4196 : : * Helper routine to recurse through setOperations tree
4197 : : */
4198 : : static bool
8412 4199 : 396 : recurse_pushdown_safe(Node *setOp, Query *topquery,
4200 : : pushdown_safety_info *safetyInfo)
4201 : : {
8650 4202 [ + + ]: 396 : if (IsA(setOp, RangeTblRef))
4203 : : {
4204 : 264 : RangeTblRef *rtr = (RangeTblRef *) setOp;
4205 : 264 : RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
4206 : 264 : Query *subquery = rte->subquery;
4207 : :
4208 [ - + ]: 264 : Assert(subquery != NULL);
4330 4209 : 264 : return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
4210 : : }
8650 4211 [ + - ]: 132 : else if (IsA(setOp, SetOperationStmt))
4212 : : {
4213 : 132 : SetOperationStmt *op = (SetOperationStmt *) setOp;
4214 : :
4215 : : /* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
4216 [ - + ]: 132 : if (op->op == SETOP_EXCEPT)
8650 tgl@sss.pgh.pa.us 4217 :UBC 0 : return false;
4218 : : /* Else recurse */
4330 tgl@sss.pgh.pa.us 4219 [ - + ]:CBC 132 : if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
8650 tgl@sss.pgh.pa.us 4220 :UBC 0 : return false;
4330 tgl@sss.pgh.pa.us 4221 [ - + ]:CBC 132 : if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
8650 tgl@sss.pgh.pa.us 4222 :UBC 0 : return false;
4223 : : }
4224 : : else
4225 : : {
8320 4226 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
4227 : : (int) nodeTag(setOp));
4228 : : }
8650 tgl@sss.pgh.pa.us 4229 :CBC 132 : return true;
4230 : : }
4231 : :
4232 : : /*
4233 : : * check_output_expressions - check subquery's output expressions for safety
4234 : : *
4235 : : * There are several cases in which it's unsafe to push down an upper-level
4236 : : * qual if it references a particular output column of a subquery. We check
4237 : : * each output column of the subquery and set flags in unsafeFlags[k] when we
4238 : : * see that column is unsafe for a pushed-down qual to reference. The
4239 : : * conditions checked here are:
4240 : : *
4241 : : * 1. We must not push down any quals that refer to subselect outputs that
4242 : : * return sets, else we'd introduce functions-returning-sets into the
4243 : : * subquery's WHERE/HAVING quals.
4244 : : *
4245 : : * 2. We must not push down any quals that refer to subselect outputs that
4246 : : * contain volatile functions, for fear of introducing strange results due
4247 : : * to multiple evaluation of a volatile function.
4248 : : *
4249 : : * 3. If the subquery uses DISTINCT ON, we must not push down any quals that
4250 : : * refer to non-DISTINCT output columns, because that could change the set
4251 : : * of rows returned. (This condition is vacuous for DISTINCT, because then
4252 : : * there are no non-DISTINCT output columns, so we needn't check. Note that
4253 : : * subquery_is_pushdown_safe already reported that we can't use volatile
4254 : : * quals if there's DISTINCT or DISTINCT ON.)
4255 : : *
4256 : : * 4. If the subquery has any window functions, we must not push down quals
4257 : : * that reference any output columns that are not listed in all the subquery's
4258 : : * window PARTITION BY clauses. We can push down quals that use only
4259 : : * partitioning columns because they should succeed or fail identically for
4260 : : * every row of any one window partition, and totally excluding some
4261 : : * partitions will not change a window function's results for remaining
4262 : : * partitions. (Again, this also requires nonvolatile quals, but
4263 : : * subquery_is_pushdown_safe handles that.). Subquery columns marked as
4264 : : * unsafe for this reason can still have WindowClause run conditions pushed
4265 : : * down.
4266 : : */
4267 : : static void
4330 4268 : 2200 : check_output_expressions(Query *subquery, pushdown_safety_info *safetyInfo)
4269 : : {
106 rguo@postgresql.org 4270 : 2200 : List *flattened_targetList = subquery->targetList;
4271 : : ListCell *lc;
4272 : :
4273 : : /*
4274 : : * We must be careful with grouping Vars and join alias Vars in the
4275 : : * subquery's outputs, as they hide the underlying expressions.
4276 : : *
4277 : : * We need to expand grouping Vars to their underlying expressions (the
4278 : : * grouping clauses) because the grouping expressions themselves might be
4279 : : * volatile or set-returning. However, we do not need to expand join
4280 : : * alias Vars, as their underlying structure does not introduce volatile
4281 : : * or set-returning functions at the current level.
4282 : : *
4283 : : * In neither case do we need to recursively examine the Vars contained in
4284 : : * these underlying expressions. Even if they reference outputs from
4285 : : * lower-level subqueries (at any depth), those references are guaranteed
4286 : : * not to expand to volatile or set-returning functions, because
4287 : : * subqueries containing such functions in their targetlists are never
4288 : : * pulled up.
4289 : : */
4290 [ + + ]: 2200 : if (subquery->hasGroupRTE)
4291 : : {
4292 : : /*
4293 : : * We can safely pass NULL for the root here. This function uses the
4294 : : * expanded expressions solely to check for volatile or set-returning
4295 : : * functions, which is independent of the Vars' nullingrels.
4296 : : */
4297 : : flattened_targetList = (List *)
4298 : 252 : flatten_group_exprs(NULL, subquery, (Node *) subquery->targetList);
4299 : : }
4300 : :
4301 [ + - + + : 25403 : foreach(lc, flattened_targetList)
+ + ]
4302 : : {
4717 tgl@sss.pgh.pa.us 4303 : 23203 : TargetEntry *tle = (TargetEntry *) lfirst(lc);
4304 : :
4305 [ + + ]: 23203 : if (tle->resjunk)
4306 : 109 : continue; /* ignore resjunk columns */
4307 : :
4308 : : /* Functions returning sets are unsafe (point 1) */
3521 4309 [ + + ]: 23094 : if (subquery->hasTargetSRFs &&
1145 drowley@postgresql.o 4310 [ + - ]: 1200 : (safetyInfo->unsafeFlags[tle->resno] &
4311 [ + + ]: 1200 : UNSAFE_HAS_SET_FUNC) == 0 &&
3521 tgl@sss.pgh.pa.us 4312 : 1200 : expression_returns_set((Node *) tle->expr))
4313 : : {
1145 drowley@postgresql.o 4314 : 848 : safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_HAS_SET_FUNC;
4717 tgl@sss.pgh.pa.us 4315 : 848 : continue;
4316 : : }
4317 : :
4318 : : /* Volatile functions are unsafe (point 2) */
1145 drowley@postgresql.o 4319 [ + + ]: 22246 : if ((safetyInfo->unsafeFlags[tle->resno] &
4320 [ + + ]: 22236 : UNSAFE_HAS_VOLATILE_FUNC) == 0 &&
4321 : 22236 : contain_volatile_functions((Node *) tle->expr))
4322 : : {
4323 : 75 : safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_HAS_VOLATILE_FUNC;
4717 tgl@sss.pgh.pa.us 4324 : 75 : continue;
4325 : : }
4326 : :
4327 : : /* If subquery uses DISTINCT ON, check point 3 */
4328 [ - + ]: 22171 : if (subquery->hasDistinctOn &&
1145 drowley@postgresql.o 4329 [ # # ]:UBC 0 : (safetyInfo->unsafeFlags[tle->resno] &
4330 : 0 : UNSAFE_NOTIN_DISTINCTON_CLAUSE) == 0 &&
4717 tgl@sss.pgh.pa.us 4331 [ # # ]: 0 : !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
4332 : : {
4333 : : /* non-DISTINCT column, so mark it unsafe */
1145 drowley@postgresql.o 4334 : 0 : safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_NOTIN_DISTINCTON_CLAUSE;
4717 tgl@sss.pgh.pa.us 4335 : 0 : continue;
4336 : : }
4337 : :
4338 : : /* If subquery uses window functions, check point 4 */
4330 tgl@sss.pgh.pa.us 4339 [ + + ]:CBC 22171 : if (subquery->hasWindowFuncs &&
1145 drowley@postgresql.o 4340 [ + - ]: 897 : (safetyInfo->unsafeFlags[tle->resno] &
4341 : 1722 : UNSAFE_NOTIN_DISTINCTON_CLAUSE) == 0 &&
4330 tgl@sss.pgh.pa.us 4342 [ + + ]: 897 : !targetIsInAllPartitionLists(tle, subquery))
4343 : : {
4344 : : /* not present in all PARTITION BY clauses, so mark it unsafe */
1145 drowley@postgresql.o 4345 : 825 : safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_NOTIN_PARTITIONBY_CLAUSE;
4330 tgl@sss.pgh.pa.us 4346 : 825 : continue;
4347 : : }
4348 : : }
4717 4349 : 2200 : }
4350 : :
4351 : : /*
4352 : : * For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
4353 : : * push quals into each component query, but the quals can only reference
4354 : : * subquery columns that suffer no type coercions in the set operation.
4355 : : * Otherwise there are possible semantic gotchas. So, we check the
4356 : : * component queries to see if any of them have output types different from
4357 : : * the top-level setop outputs. We set the UNSAFE_TYPE_MISMATCH bit in
4358 : : * unsafeFlags[k] if column k has different type in any component.
4359 : : *
4360 : : * We don't have to care about typmods here: the only allowed difference
4361 : : * between set-op input and output typmods is input is a specific typmod
4362 : : * and output is -1, and that does not require a coercion.
4363 : : *
4364 : : * tlist is a subquery tlist.
4365 : : * colTypes is an OID list of the top-level setop's output column types.
4366 : : * safetyInfo is the pushdown_safety_info to set unsafeFlags[] for.
4367 : : */
4368 : : static void
8412 4369 : 264 : compare_tlist_datatypes(List *tlist, List *colTypes,
4370 : : pushdown_safety_info *safetyInfo)
4371 : : {
4372 : : ListCell *l;
8014 neilc@samurai.com 4373 : 264 : ListCell *colType = list_head(colTypes);
4374 : :
4375 [ + - + + : 816 : foreach(l, tlist)
+ + ]
4376 : : {
4377 : 552 : TargetEntry *tle = (TargetEntry *) lfirst(l);
4378 : :
7699 tgl@sss.pgh.pa.us 4379 [ - + ]: 552 : if (tle->resjunk)
8412 tgl@sss.pgh.pa.us 4380 :UBC 0 : continue; /* ignore resjunk columns */
8014 neilc@samurai.com 4381 [ - + ]:CBC 552 : if (colType == NULL)
8412 tgl@sss.pgh.pa.us 4382 [ # # ]:UBC 0 : elog(ERROR, "wrong number of tlist entries");
7699 tgl@sss.pgh.pa.us 4383 [ + + ]:CBC 552 : if (exprType((Node *) tle->expr) != lfirst_oid(colType))
1145 drowley@postgresql.o 4384 : 90 : safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_TYPE_MISMATCH;
2486 tgl@sss.pgh.pa.us 4385 : 552 : colType = lnext(colTypes, colType);
4386 : : }
8014 neilc@samurai.com 4387 [ - + ]: 264 : if (colType != NULL)
8412 tgl@sss.pgh.pa.us 4388 [ # # ]:UBC 0 : elog(ERROR, "wrong number of tlist entries");
8412 tgl@sss.pgh.pa.us 4389 :CBC 264 : }
4390 : :
4391 : : /*
4392 : : * targetIsInAllPartitionLists
4393 : : * True if the TargetEntry is listed in the PARTITION BY clause
4394 : : * of every window defined in the query.
4395 : : *
4396 : : * It would be safe to ignore windows not actually used by any window
4397 : : * function, but it's not easy to get that info at this stage; and it's
4398 : : * unlikely to be useful to spend any extra cycles getting it, since
4399 : : * unreferenced window definitions are probably infrequent in practice.
4400 : : */
4401 : : static bool
4330 4402 : 897 : targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
4403 : : {
4404 : : ListCell *lc;
4405 : :
4406 [ + - + + : 989 : foreach(lc, query->windowClause)
+ + ]
4407 : : {
4408 : 917 : WindowClause *wc = (WindowClause *) lfirst(lc);
4409 : :
4410 [ + + ]: 917 : if (!targetIsInSortList(tle, InvalidOid, wc->partitionClause))
4411 : 825 : return false;
4412 : : }
4413 : 72 : return true;
4414 : : }
4415 : :
4416 : : /*
4417 : : * qual_is_pushdown_safe - is a particular rinfo safe to push down?
4418 : : *
4419 : : * rinfo is a restriction clause applying to the given subquery (whose RTE
4420 : : * has index rti in the parent query).
4421 : : *
4422 : : * Conditions checked here:
4423 : : *
4424 : : * 1. rinfo's clause must not contain any SubPlans (mainly because it's
4425 : : * unclear that it will work correctly: SubLinks will already have been
4426 : : * transformed into SubPlans in the qual, but not in the subquery). Note that
4427 : : * SubLinks that transform to initplans are safe, and will be accepted here
4428 : : * because what we'll see in the qual is just a Param referencing the initplan
4429 : : * output.
4430 : : *
4431 : : * 2. If unsafeVolatile is set, rinfo's clause must not contain any volatile
4432 : : * functions.
4433 : : *
4434 : : * 3. If unsafeLeaky is set, rinfo's clause must not contain any leaky
4435 : : * functions that are passed Var nodes, and therefore might reveal values from
4436 : : * the subquery as side effects.
4437 : : *
4438 : : * 4. rinfo's clause must not refer to the whole-row output of the subquery
4439 : : * (since there is no easy way to name that within the subquery itself).
4440 : : *
4441 : : * 5. rinfo's clause must not refer to any subquery output columns that were
4442 : : * found to be unsafe to reference by subquery_is_pushdown_safe().
4443 : : */
4444 : : static pushdown_safe_type
1863 drowley@postgresql.o 4445 : 3540 : qual_is_pushdown_safe(Query *subquery, Index rti, RestrictInfo *rinfo,
4446 : : pushdown_safety_info *safetyInfo)
4447 : : {
1145 4448 : 3540 : pushdown_safe_type safe = PUSHDOWN_SAFE;
1863 4449 : 3540 : Node *qual = (Node *) rinfo->clause;
4450 : : List *vars;
4451 : : ListCell *vl;
4452 : :
4453 : : /* Refuse subselects (point 1) */
8445 tgl@sss.pgh.pa.us 4454 [ + + ]: 3540 : if (contain_subplans(qual))
1145 drowley@postgresql.o 4455 : 55 : return PUSHDOWN_UNSAFE;
4456 : :
4457 : : /* Refuse volatile quals if we found they'd be unsafe (point 2) */
4330 tgl@sss.pgh.pa.us 4458 [ + + + + ]: 4298 : if (safetyInfo->unsafeVolatile &&
1863 drowley@postgresql.o 4459 : 813 : contain_volatile_functions((Node *) rinfo))
1145 4460 : 15 : return PUSHDOWN_UNSAFE;
4461 : :
4462 : : /* Refuse leaky quals if told to (point 3) */
4330 tgl@sss.pgh.pa.us 4463 [ + + + + ]: 5408 : if (safetyInfo->unsafeLeaky &&
4026 sfrost@snowman.net 4464 : 1938 : contain_leaked_vars(qual))
1145 drowley@postgresql.o 4465 : 135 : return PUSHDOWN_UNSAFE;
4466 : :
4467 : : /*
4468 : : * Examine all Vars used in clause. Since it's a restriction clause, all
4469 : : * such Vars must refer to subselect output columns ... unless this is
4470 : : * part of a LATERAL subquery, in which case there could be lateral
4471 : : * references.
4472 : : *
4473 : : * By omitting the relevant flags, this also gives us a cheap sanity check
4474 : : * that no aggregates or window functions appear in the qual. Those would
4475 : : * be unsafe to push down, but at least for the moment we could never see
4476 : : * any in a qual anyhow.
4477 : : */
3708 tgl@sss.pgh.pa.us 4478 : 3335 : vars = pull_var_clause(qual, PVC_INCLUDE_PLACEHOLDERS);
8445 4479 [ + + + + : 6315 : foreach(vl, vars)
+ + ]
4480 : : {
8310 bruce@momjian.us 4481 : 3425 : Var *var = (Var *) lfirst(vl);
4482 : :
4483 : : /*
4484 : : * XXX Punt if we find any PlaceHolderVars in the restriction clause.
4485 : : * It's not clear whether a PHV could safely be pushed down, and even
4486 : : * less clear whether such a situation could arise in any cases of
4487 : : * practical interest anyway. So for the moment, just refuse to push
4488 : : * down.
4489 : : */
6405 tgl@sss.pgh.pa.us 4490 [ - + ]: 3425 : if (!IsA(var, Var))
4491 : : {
1145 drowley@postgresql.o 4492 :UBC 0 : safe = PUSHDOWN_UNSAFE;
6405 tgl@sss.pgh.pa.us 4493 : 0 : break;
4494 : : }
4495 : :
4496 : : /*
4497 : : * Punt if we find any lateral references. It would be safe to push
4498 : : * these down, but we'd have to convert them into outer references,
4499 : : * which subquery_push_qual lacks the infrastructure to do. The case
4500 : : * arises so seldom that it doesn't seem worth working hard on.
4501 : : */
2122 tgl@sss.pgh.pa.us 4502 [ + + ]:CBC 3425 : if (var->varno != rti)
4503 : : {
1145 drowley@postgresql.o 4504 : 10 : safe = PUSHDOWN_UNSAFE;
2122 tgl@sss.pgh.pa.us 4505 : 10 : break;
4506 : : }
4507 : :
4508 : : /* Subqueries have no system columns */
4717 4509 [ - + ]: 3415 : Assert(var->varattno >= 0);
4510 : :
4511 : : /* Check point 4 */
7386 4512 [ - + ]: 3415 : if (var->varattno == 0)
4513 : : {
1145 drowley@postgresql.o 4514 :UBC 0 : safe = PUSHDOWN_UNSAFE;
7386 tgl@sss.pgh.pa.us 4515 : 0 : break;
4516 : : }
4517 : :
4518 : : /* Check point 5 */
1145 drowley@postgresql.o 4519 [ + + ]:CBC 3415 : if (safetyInfo->unsafeFlags[var->varattno] != 0)
4520 : : {
4521 [ + + ]: 710 : if (safetyInfo->unsafeFlags[var->varattno] &
4522 : : (UNSAFE_HAS_VOLATILE_FUNC | UNSAFE_HAS_SET_FUNC |
4523 : : UNSAFE_NOTIN_DISTINCTON_CLAUSE | UNSAFE_TYPE_MISMATCH))
4524 : : {
4525 : 435 : safe = PUSHDOWN_UNSAFE;
4526 : 435 : break;
4527 : : }
4528 : : else
4529 : : {
4530 : : /* UNSAFE_NOTIN_PARTITIONBY_CLAUSE is ok for run conditions */
4531 : 275 : safe = PUSHDOWN_WINDOWCLAUSE_RUNCOND;
4532 : : /* don't break, we might find another Var that's unsafe */
4533 : : }
4534 : : }
4535 : : }
4536 : :
8010 neilc@samurai.com 4537 : 3335 : list_free(vars);
4538 : :
8445 tgl@sss.pgh.pa.us 4539 : 3335 : return safe;
4540 : : }
4541 : :
4542 : : /*
4543 : : * subquery_push_qual - push down a qual that we have determined is safe
4544 : : */
4545 : : static void
7640 4546 : 2884 : subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
4547 : : {
8650 4548 [ + + ]: 2884 : if (subquery->setOperations != NULL)
4549 : : {
4550 : : /* Recurse to push it separately to each component query */
7929 4551 : 102 : recurse_push_qual(subquery->setOperations, subquery,
4552 : : rte, rti, qual);
4553 : : }
4554 : : else
4555 : : {
4556 : : /*
4557 : : * We need to replace Vars in the qual (which must refer to outputs of
4558 : : * the subquery) with copies of the subquery's targetlist expressions.
4559 : : * Note that at this point, any uplevel Vars in the qual should have
4560 : : * been replaced with Params, so they need no work.
4561 : : *
4562 : : * This step also ensures that when we are pushing into a setop tree,
4563 : : * each component query gets its own copy of the qual.
4564 : : */
4926 4565 : 2782 : qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
4566 : : subquery->targetList,
4567 : : subquery->resultRelation,
4568 : : REPLACEVARS_REPORT_ERROR, 0,
4569 : : &subquery->hasSubLinks);
4570 : :
4571 : : /*
4572 : : * Now attach the qual to the proper place: normally WHERE, but if the
4573 : : * subquery uses grouping or aggregation, put it in HAVING (since the
4574 : : * qual really refers to the group-result rows).
4575 : : */
4007 andres@anarazel.de 4576 [ + + + - : 2782 : if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
+ - - + ]
7726 tgl@sss.pgh.pa.us 4577 : 312 : subquery->havingQual = make_and_qual(subquery->havingQual, qual);
4578 : : else
4579 : 2470 : subquery->jointree->quals =
4580 : 2470 : make_and_qual(subquery->jointree->quals, qual);
4581 : :
4582 : : /*
4583 : : * We need not change the subquery's hasAggs or hasSubLinks flags,
4584 : : * since we can't be pushing down any aggregates that weren't there
4585 : : * before, and we don't push down subselects at all.
4586 : : */
4587 : : }
8650 4588 : 2884 : }
4589 : :
4590 : : /*
4591 : : * Helper routine to recurse through setOperations tree
4592 : : */
4593 : : static void
4594 : 306 : recurse_push_qual(Node *setOp, Query *topquery,
4595 : : RangeTblEntry *rte, Index rti, Node *qual)
4596 : : {
4597 [ + + ]: 306 : if (IsA(setOp, RangeTblRef))
4598 : : {
4599 : 204 : RangeTblRef *rtr = (RangeTblRef *) setOp;
8030 4600 : 204 : RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
4601 : 204 : Query *subquery = subrte->subquery;
4602 : :
8650 4603 [ - + ]: 204 : Assert(subquery != NULL);
7640 4604 : 204 : subquery_push_qual(subquery, rte, rti, qual);
4605 : : }
8650 4606 [ + - ]: 102 : else if (IsA(setOp, SetOperationStmt))
4607 : : {
4608 : 102 : SetOperationStmt *op = (SetOperationStmt *) setOp;
4609 : :
7640 4610 : 102 : recurse_push_qual(op->larg, topquery, rte, rti, qual);
4611 : 102 : recurse_push_qual(op->rarg, topquery, rte, rti, qual);
4612 : : }
4613 : : else
4614 : : {
8320 tgl@sss.pgh.pa.us 4615 [ # # ]:UBC 0 : elog(ERROR, "unrecognized node type: %d",
4616 : : (int) nodeTag(setOp));
4617 : : }
8650 tgl@sss.pgh.pa.us 4618 :CBC 306 : }
4619 : :
4620 : : /*****************************************************************************
4621 : : * SIMPLIFYING SUBQUERY TARGETLISTS
4622 : : *****************************************************************************/
4623 : :
4624 : : /*
4625 : : * remove_unused_subquery_outputs
4626 : : * Remove subquery targetlist items we don't need
4627 : : *
4628 : : * It's possible, even likely, that the upper query does not read all the
4629 : : * output columns of the subquery. We can remove any such outputs that are
4630 : : * not needed by the subquery itself (e.g., as sort/group columns) and do not
4631 : : * affect semantics otherwise (e.g., volatile functions can't be removed).
4632 : : * This is useful not only because we might be able to remove expensive-to-
4633 : : * compute expressions, but because deletion of output columns might allow
4634 : : * optimizations such as join removal to occur within the subquery.
4635 : : *
4636 : : * extra_used_attrs can be passed as non-NULL to mark any columns (offset by
4637 : : * FirstLowInvalidHeapAttributeNumber) that we should not remove. This
4638 : : * parameter is modified by the function, so callers must make a copy if they
4639 : : * need to use the passed in Bitmapset after calling this function.
4640 : : *
4641 : : * To avoid affecting column numbering in the targetlist, we don't physically
4642 : : * remove unused tlist entries, but rather replace their expressions with NULL
4643 : : * constants. This is implemented by modifying subquery->targetList.
4644 : : */
4645 : : static void
1439 drowley@postgresql.o 4646 : 17487 : remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel,
4647 : : Bitmapset *extra_used_attrs)
4648 : : {
4649 : : Bitmapset *attrs_used;
4650 : : ListCell *lc;
4651 : :
4652 : : /*
4653 : : * Just point directly to extra_used_attrs. No need to bms_copy as none of
4654 : : * the current callers use the Bitmapset after calling this function.
4655 : : */
4656 : 17487 : attrs_used = extra_used_attrs;
4657 : :
4658 : : /*
4659 : : * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
4660 : : * could update all the child SELECTs' tlists, but it seems not worth the
4661 : : * trouble presently.
4662 : : */
4345 tgl@sss.pgh.pa.us 4663 [ + + ]: 17487 : if (subquery->setOperations)
4664 : 1429 : return;
4665 : :
4666 : : /*
4667 : : * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
4668 : : * time: all its output columns must be used in the distinctClause.
4669 : : */
4670 [ + + + + ]: 16862 : if (subquery->distinctClause && !subquery->hasDistinctOn)
4671 : 557 : return;
4672 : :
4673 : : /*
4674 : : * Collect a bitmap of all the output column numbers used by the upper
4675 : : * query.
4676 : : *
4677 : : * Add all the attributes needed for joins or final output. Note: we must
4678 : : * look at rel's targetlist, not the attr_needed data, because attr_needed
4679 : : * isn't computed for inheritance child rels, cf set_append_rel_size().
4680 : : * (XXX might be worth changing that sometime.)
4681 : : */
3704 4682 : 16305 : pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
4683 : :
4684 : : /* Add all the attributes used by un-pushed-down restriction clauses. */
4345 4685 [ + + + + : 17178 : foreach(lc, rel->baserestrictinfo)
+ + ]
4686 : : {
4687 : 873 : RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
4688 : :
4689 : 873 : pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
4690 : : }
4691 : :
4692 : : /*
4693 : : * If there's a whole-row reference to the subquery, we can't remove
4694 : : * anything.
4695 : : */
4696 [ + + ]: 16305 : if (bms_is_member(0 - FirstLowInvalidHeapAttributeNumber, attrs_used))
4697 : 247 : return;
4698 : :
4699 : : /*
4700 : : * Run through the tlist and zap entries we don't need. It's okay to
4701 : : * modify the tlist items in-place because set_subquery_pathlist made a
4702 : : * copy of the subquery.
4703 : : */
4704 [ + + + + : 89859 : foreach(lc, subquery->targetList)
+ + ]
4705 : : {
4706 : 73801 : TargetEntry *tle = (TargetEntry *) lfirst(lc);
4707 : 73801 : Node *texpr = (Node *) tle->expr;
4708 : :
4709 : : /*
4710 : : * If it has a sortgroupref number, it's used in some sort/group
4711 : : * clause so we'd better not remove it. Also, don't remove any
4712 : : * resjunk columns, since their reason for being has nothing to do
4713 : : * with anybody reading the subquery's output. (It's likely that
4714 : : * resjunk columns in a sub-SELECT would always have ressortgroupref
4715 : : * set, but even if they don't, it seems imprudent to remove them.)
4716 : : */
4717 [ + + - + ]: 73801 : if (tle->ressortgroupref || tle->resjunk)
4718 : 2475 : continue;
4719 : :
4720 : : /*
4721 : : * If it's used by the upper query, we can't remove it.
4722 : : */
4723 [ + + ]: 71326 : if (bms_is_member(tle->resno - FirstLowInvalidHeapAttributeNumber,
4724 : : attrs_used))
4725 : 41303 : continue;
4726 : :
4727 : : /*
4728 : : * If it contains a set-returning function, we can't remove it since
4729 : : * that could change the number of rows returned by the subquery.
4730 : : */
3521 4731 [ + + + + ]: 30727 : if (subquery->hasTargetSRFs &&
4732 : 704 : expression_returns_set(texpr))
4345 4733 : 474 : continue;
4734 : :
4735 : : /*
4736 : : * If it contains volatile functions, we daren't remove it for fear
4737 : : * that the user is expecting their side-effects to happen.
4738 : : */
4739 [ + + ]: 29549 : if (contain_volatile_functions(texpr))
4740 : 26 : continue;
4741 : :
4742 : : /*
4743 : : * OK, we don't need it. Replace the expression with a NULL constant.
4744 : : * Preserve the exposed type of the expression, in case something
4745 : : * looks at the rowtype of the subquery's result.
4746 : : */
4747 : 29523 : tle->expr = (Expr *) makeNullConst(exprType(texpr),
4748 : : exprTypmod(texpr),
4749 : : exprCollation(texpr));
4750 : : }
4751 : : }
4752 : :
4753 : : /*
4754 : : * create_partial_bitmap_paths
4755 : : * Build partial bitmap heap path for the relation
4756 : : */
4757 : : void
3345 rhaas@postgresql.org 4758 : 111941 : create_partial_bitmap_paths(PlannerInfo *root, RelOptInfo *rel,
4759 : : Path *bitmapqual)
4760 : : {
4761 : : int parallel_workers;
4762 : : double pages_fetched;
4763 : :
4764 : : /* Compute heap pages for bitmap heap scan */
4765 : 111941 : pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
4766 : : NULL, NULL);
4767 : :
3014 4768 : 111941 : parallel_workers = compute_parallel_worker(rel, pages_fetched, -1,
4769 : : max_parallel_workers_per_gather);
4770 : :
3345 4771 [ + + ]: 111941 : if (parallel_workers <= 0)
4772 : 108697 : return;
4773 : :
4774 : 3244 : add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
4775 : : bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
4776 : : }
4777 : :
4778 : : /*
4779 : : * Compute the number of parallel workers that should be used to scan a
4780 : : * relation. We compute the parallel workers based on the size of the heap to
4781 : : * be scanned and the size of the index to be scanned, then choose a minimum
4782 : : * of those.
4783 : : *
4784 : : * "heap_pages" is the number of pages from the table that we expect to scan, or
4785 : : * -1 if we don't expect to scan any.
4786 : : *
4787 : : * "index_pages" is the number of pages from the index that we expect to scan, or
4788 : : * -1 if we don't expect to scan any.
4789 : : *
4790 : : * "max_workers" is caller's limit on the number of workers. This typically
4791 : : * comes from a GUC.
4792 : : */
4793 : : int
3014 4794 : 606291 : compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages,
4795 : : int max_workers)
4796 : : {
3366 4797 : 606291 : int parallel_workers = 0;
4798 : :
4799 : : /*
4800 : : * If the user has set the parallel_workers reloption, use that; otherwise
4801 : : * select a default number of workers.
4802 : : */
3394 4803 [ + + ]: 606291 : if (rel->rel_parallel_workers != -1)
4804 : 2740 : parallel_workers = rel->rel_parallel_workers;
4805 : : else
4806 : : {
4807 : : /*
4808 : : * If the number of pages being scanned is insufficient to justify a
4809 : : * parallel scan, just return zero ... unless it's an inheritance
4810 : : * child. In that case, we want to generate a parallel path here
4811 : : * anyway. It might not be worthwhile just for this relation, but
4812 : : * when combined with all of its inheritance siblings it may well pay
4813 : : * off.
4814 : : */
3339 4815 [ + + + + ]: 603551 : if (rel->reloptkind == RELOPT_BASEREL &&
4816 [ + + + + ]: 571439 : ((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
3240 tgl@sss.pgh.pa.us 4817 [ + + ]: 17954 : (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
3394 rhaas@postgresql.org 4818 : 570690 : return 0;
4819 : :
3339 4820 [ + + ]: 32861 : if (heap_pages >= 0)
4821 : : {
4822 : : int heap_parallel_threshold;
4823 : 31306 : int heap_parallel_workers = 1;
4824 : :
4825 : : /*
4826 : : * Select the number of workers based on the log of the size of
4827 : : * the relation. This probably needs to be a good deal more
4828 : : * sophisticated, but we need something here for now. Note that
4829 : : * the upper limit of the min_parallel_table_scan_size GUC is
4830 : : * chosen to prevent overflow here.
4831 : : */
3366 4832 : 31306 : heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
4833 [ + + ]: 35786 : while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
4834 : : {
4835 : 4480 : heap_parallel_workers++;
4836 : 4480 : heap_parallel_threshold *= 3;
4837 [ - + ]: 4480 : if (heap_parallel_threshold > INT_MAX / 3)
3366 rhaas@postgresql.org 4838 :UBC 0 : break; /* avoid overflow */
4839 : : }
4840 : :
3366 rhaas@postgresql.org 4841 :CBC 31306 : parallel_workers = heap_parallel_workers;
4842 : : }
4843 : :
3339 4844 [ + + ]: 32861 : if (index_pages >= 0)
4845 : : {
4846 : 7406 : int index_parallel_workers = 1;
4847 : : int index_parallel_threshold;
4848 : :
4849 : : /* same calculation as for heap_pages above */
3366 4850 : 7406 : index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
4851 [ + + ]: 7636 : while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
4852 : : {
4853 : 230 : index_parallel_workers++;
4854 : 230 : index_parallel_threshold *= 3;
4855 [ - + ]: 230 : if (index_parallel_threshold > INT_MAX / 3)
3366 rhaas@postgresql.org 4856 :UBC 0 : break; /* avoid overflow */
4857 : : }
4858 : :
3366 rhaas@postgresql.org 4859 [ + + ]:CBC 7406 : if (parallel_workers > 0)
4860 : 5851 : parallel_workers = Min(parallel_workers, index_parallel_workers);
4861 : : else
4862 : 1555 : parallel_workers = index_parallel_workers;
4863 : : }
4864 : : }
4865 : :
4866 : : /* In no case use more than caller supplied maximum number of workers */
3014 4867 : 35601 : parallel_workers = Min(parallel_workers, max_workers);
4868 : :
3394 4869 : 35601 : return parallel_workers;
4870 : : }
4871 : :
4872 : : /*
4873 : : * generate_partitionwise_join_paths
4874 : : * Create paths representing partitionwise join for given partitioned
4875 : : * join relation.
4876 : : *
4877 : : * This must not be called until after we are done adding paths for all
4878 : : * child-joins. Otherwise, add_path might delete a path to which some path
4879 : : * generated here has a reference.
4880 : : */
4881 : : void
3000 peter_e@gmx.net 4882 : 194610 : generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
4883 : : {
3133 rhaas@postgresql.org 4884 : 194610 : List *live_children = NIL;
4885 : : int cnt_parts;
4886 : : int num_parts;
4887 : : RelOptInfo **part_rels;
4888 : :
4889 : : /* Handle only join relations here. */
4890 [ + + - + ]: 194610 : if (!IS_JOIN_REL(rel))
3133 rhaas@postgresql.org 4891 :UBC 0 : return;
4892 : :
4893 : : /* We've nothing to do if the relation is not partitioned. */
3011 rhaas@postgresql.org 4894 [ + + + + :CBC 194610 : if (!IS_PARTITIONED_REL(rel))
+ + + - +
+ ]
3133 4895 : 188643 : return;
4896 : :
4897 : : /* The relation should have consider_partitionwise_join set. */
2804 efujita@postgresql.o 4898 [ - + ]: 5967 : Assert(rel->consider_partitionwise_join);
4899 : :
4900 : : /* Guard against stack overflow due to overly deep partition hierarchy. */
3133 rhaas@postgresql.org 4901 : 5967 : check_stack_depth();
4902 : :
4903 : 5967 : num_parts = rel->nparts;
4904 : 5967 : part_rels = rel->part_rels;
4905 : :
4906 : : /* Collect non-dummy child-joins. */
4907 [ + + ]: 21264 : for (cnt_parts = 0; cnt_parts < num_parts; cnt_parts++)
4908 : : {
4909 : 15297 : RelOptInfo *child_rel = part_rels[cnt_parts];
4910 : :
4911 : : /* If it's been pruned entirely, it's certainly dummy. */
2593 tgl@sss.pgh.pa.us 4912 [ + + ]: 15297 : if (child_rel == NULL)
4913 : 52 : continue;
4914 : :
4915 : : /* Make partitionwise join paths for this partitioned child-join. */
3000 peter_e@gmx.net 4916 : 15245 : generate_partitionwise_join_paths(root, child_rel);
4917 : :
4918 : : /* If we failed to make any path for this child, we must give up. */
1248 tgl@sss.pgh.pa.us 4919 [ - + ]: 15245 : if (child_rel->pathlist == NIL)
4920 : : {
4921 : : /*
4922 : : * Mark the parent joinrel as unpartitioned so that later
4923 : : * functions treat it correctly.
4924 : : */
1248 tgl@sss.pgh.pa.us 4925 :UBC 0 : rel->nparts = 0;
4926 : 0 : return;
4927 : : }
4928 : :
4929 : : /* Else, identify the cheapest path for it. */
2616 tgl@sss.pgh.pa.us 4930 :CBC 15245 : set_cheapest(child_rel);
4931 : :
4932 : : /* Dummy children need not be scanned, so ignore those. */
3133 rhaas@postgresql.org 4933 [ - + ]: 15245 : if (IS_DUMMY_REL(child_rel))
3133 rhaas@postgresql.org 4934 :UBC 0 : continue;
4935 : :
4936 : : /*
4937 : : * Except for the topmost scan/join rel, consider generating partial
4938 : : * aggregation paths for the grouped relation on top of the paths of
4939 : : * this partitioned child-join. After that, we're done creating paths
4940 : : * for the grouped relation, so run set_cheapest().
4941 : : */
209 rguo@postgresql.org 4942 [ + + ]:GNC 15245 : if (child_rel->grouped_rel != NULL &&
4943 [ + + - + : 10730 : !bms_equal(IS_OTHER_REL(rel) ?
+ + ]
4944 : : rel->top_parent_relids : rel->relids,
4945 [ + - ]: 10730 : root->all_query_rels))
4946 : : {
4947 : 200 : RelOptInfo *grouped_rel = child_rel->grouped_rel;
4948 : :
4949 [ - + ]: 200 : Assert(IS_GROUPED_REL(grouped_rel));
4950 : :
4951 : 200 : generate_grouped_paths(root, grouped_rel, child_rel);
4952 : 200 : set_cheapest(grouped_rel);
4953 : : }
4954 : :
4955 : : #ifdef OPTIMIZER_DEBUG
4956 : : pprint(child_rel);
4957 : : #endif
4958 : :
3133 rhaas@postgresql.org 4959 :CBC 15245 : live_children = lappend(live_children, child_rel);
4960 : : }
4961 : :
4962 : : /* If all child-joins are dummy, parent join is also dummy. */
4963 [ - + ]: 5967 : if (!live_children)
4964 : : {
3133 rhaas@postgresql.org 4965 :UBC 0 : mark_dummy_rel(rel);
4966 : 0 : return;
4967 : : }
4968 : :
4969 : : /* Build additional paths for this rel from child-join paths. */
3133 rhaas@postgresql.org 4970 :CBC 5967 : add_paths_to_append_rel(root, rel, live_children);
4971 : 5967 : list_free(live_children);
4972 : : }
|