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