1 /*-------------------------------------------------------------------------
4 * Routines to find possible search paths for processing a query
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.175 2008/10/21 20:42:52 tgl Exp $
13 *-------------------------------------------------------------------------
20 #include "nodes/nodeFuncs.h"
21 #ifdef OPTIMIZER_DEBUG
22 #include "nodes/print.h"
24 #include "optimizer/clauses.h"
25 #include "optimizer/cost.h"
26 #include "optimizer/geqo.h"
27 #include "optimizer/pathnode.h"
28 #include "optimizer/paths.h"
29 #include "optimizer/plancat.h"
30 #include "optimizer/planner.h"
31 #include "optimizer/prep.h"
32 #include "optimizer/var.h"
33 #include "parser/parse_clause.h"
34 #include "parser/parsetree.h"
35 #include "rewrite/rewriteManip.h"
38 /* These parameters are set by GUC */
39 bool enable_geqo = false; /* just in case GUC doesn't set it */
42 /* Hook for plugins to replace standard_join_search() */
43 join_search_hook_type join_search_hook = NULL;
46 static void set_base_rel_pathlists(PlannerInfo *root);
47 static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
48 Index rti, RangeTblEntry *rte);
49 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
51 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
52 Index rti, RangeTblEntry *rte);
53 static void set_dummy_rel_pathlist(RelOptInfo *rel);
54 static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
55 Index rti, RangeTblEntry *rte);
56 static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
58 static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
60 static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
62 static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
64 static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
65 static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
66 bool *differentTypes);
67 static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
68 bool *differentTypes);
69 static void compare_tlist_datatypes(List *tlist, List *colTypes,
70 bool *differentTypes);
71 static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
72 bool *differentTypes);
73 static void subquery_push_qual(Query *subquery,
74 RangeTblEntry *rte, Index rti, Node *qual);
75 static void recurse_push_qual(Node *setOp, Query *topquery,
76 RangeTblEntry *rte, Index rti, Node *qual);
81 * Finds all possible access paths for executing a query, returning a
82 * single rel that represents the join of all base rels in the query.
85 make_one_rel(PlannerInfo *root, List *joinlist)
90 * Generate access paths for the base rels.
92 set_base_rel_pathlists(root);
95 * Generate access paths for the entire join tree.
97 rel = make_rel_from_joinlist(root, joinlist);
100 * The result should join all and only the query's base rels.
102 #ifdef USE_ASSERT_CHECKING
104 int num_base_rels = 0;
107 for (rti = 1; rti < root->simple_rel_array_size; rti++)
109 RelOptInfo *brel = root->simple_rel_array[rti];
114 Assert(brel->relid == rti); /* sanity check on array */
116 /* ignore RTEs that are "other rels" */
117 if (brel->reloptkind != RELOPT_BASEREL)
120 Assert(bms_is_member(rti, rel->relids));
124 Assert(bms_num_members(rel->relids) == num_base_rels);
132 * set_base_rel_pathlists
133 * Finds all paths available for scanning each base-relation entry.
134 * Sequential scan and any available indices are considered.
135 * Each useful path is attached to its relation's 'pathlist' field.
138 set_base_rel_pathlists(PlannerInfo *root)
142 for (rti = 1; rti < root->simple_rel_array_size; rti++)
144 RelOptInfo *rel = root->simple_rel_array[rti];
146 /* there may be empty slots corresponding to non-baserel RTEs */
150 Assert(rel->relid == rti); /* sanity check on array */
152 /* ignore RTEs that are "other rels" */
153 if (rel->reloptkind != RELOPT_BASEREL)
156 set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
162 * Build access paths for a base relation
165 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
166 Index rti, RangeTblEntry *rte)
170 /* It's an "append relation", process accordingly */
171 set_append_rel_pathlist(root, rel, rti, rte);
173 else if (rel->rtekind == RTE_SUBQUERY)
175 /* Subquery --- generate a separate plan for it */
176 set_subquery_pathlist(root, rel, rti, rte);
178 else if (rel->rtekind == RTE_FUNCTION)
180 /* RangeFunction --- generate a suitable path for it */
181 set_function_pathlist(root, rel, rte);
183 else if (rel->rtekind == RTE_VALUES)
185 /* Values list --- generate a suitable path for it */
186 set_values_pathlist(root, rel, rte);
188 else if (rel->rtekind == RTE_CTE)
190 /* CTE reference --- generate a suitable path for it */
191 if (rte->self_reference)
192 set_worktable_pathlist(root, rel, rte);
194 set_cte_pathlist(root, rel, rte);
199 Assert(rel->rtekind == RTE_RELATION);
200 set_plain_rel_pathlist(root, rel, rte);
203 #ifdef OPTIMIZER_DEBUG
204 debug_print_rel(root, rel);
209 * set_plain_rel_pathlist
210 * Build access paths for a plain relation (no subquery, no inheritance)
213 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
216 * If we can prove we don't need to scan the rel via constraint exclusion,
217 * set up a single dummy path for it. We only need to check for regular
218 * baserels; if it's an otherrel, CE was already checked in
219 * set_append_rel_pathlist().
221 if (rel->reloptkind == RELOPT_BASEREL &&
222 relation_excluded_by_constraints(root, rel, rte))
224 set_dummy_rel_pathlist(rel);
228 /* Mark rel with estimated output rows, width, etc */
229 set_baserel_size_estimates(root, rel);
231 /* Test any partial indexes of rel for applicability */
232 check_partial_indexes(root, rel);
235 * Check to see if we can extract any restriction conditions from join
236 * quals that are OR-of-AND structures. If so, add them to the rel's
237 * restriction list, and recompute the size estimates.
239 if (create_or_index_quals(root, rel))
240 set_baserel_size_estimates(root, rel);
243 * Generate paths and add them to the rel's pathlist.
245 * Note: add_path() will discard any paths that are dominated by another
246 * available path, keeping only those paths that are superior along at
247 * least one dimension of cost or sortedness.
250 /* Consider sequential scan */
251 add_path(rel, create_seqscan_path(root, rel));
253 /* Consider index scans */
254 create_index_paths(root, rel);
256 /* Consider TID scans */
257 create_tidscan_paths(root, rel);
259 /* Now find the cheapest of the paths for this rel */
264 * set_append_rel_pathlist
265 * Build access paths for an "append relation"
267 * The passed-in rel and RTE represent the entire append relation. The
268 * relation's contents are computed by appending together the output of
269 * the individual member relations. Note that in the inheritance case,
270 * the first member relation is actually the same table as is mentioned in
271 * the parent RTE ... but it has a different RTE and RelOptInfo. This is
272 * a good thing because their outputs are not the same size.
275 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
276 Index rti, RangeTblEntry *rte)
278 int parentRTindex = rti;
279 List *subpaths = NIL;
282 double *parent_attrsizes;
287 * XXX for now, can't handle inherited expansion of FOR UPDATE/SHARE; can
288 * we do better? (This will take some redesign because the executor
289 * currently supposes that every rowMark relation is involved in every row
290 * returned by the query.)
292 if (get_rowmark(root->parse, parentRTindex))
294 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
295 errmsg("SELECT FOR UPDATE/SHARE is not supported for inheritance queries")));
298 * Initialize to compute size estimates for whole append relation.
300 * We handle width estimates by weighting the widths of different
301 * child rels proportionally to their number of rows. This is sensible
302 * because the use of width estimates is mainly to compute the total
303 * relation "footprint" if we have to sort or hash it. To do this,
304 * we sum the total equivalent size (in "double" arithmetic) and then
305 * divide by the total rowcount estimate. This is done separately for
306 * the total rel width and each attribute.
308 * Note: if you consider changing this logic, beware that child rels could
309 * have zero rows and/or width, if they were excluded by constraints.
313 nattrs = rel->max_attr - rel->min_attr + 1;
314 parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
317 * Generate access paths for each member relation, and pick the cheapest
320 foreach(l, root->append_rel_list)
322 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
324 RangeTblEntry *childRTE;
325 RelOptInfo *childrel;
327 ListCell *parentvars;
330 /* append_rel_list contains all append rels; ignore others */
331 if (appinfo->parent_relid != parentRTindex)
334 childRTindex = appinfo->child_relid;
335 childRTE = root->simple_rte_array[childRTindex];
338 * The child rel's RelOptInfo was already created during
339 * add_base_rels_to_query.
341 childrel = find_base_rel(root, childRTindex);
342 Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
345 * We have to copy the parent's targetlist and quals to the child,
346 * with appropriate substitution of variables. However, only the
347 * baserestrictinfo quals are needed before we can check for
348 * constraint exclusion; so do that first and then check to see if we
349 * can disregard this child.
351 childrel->baserestrictinfo = (List *)
352 adjust_appendrel_attrs((Node *) rel->baserestrictinfo,
355 if (relation_excluded_by_constraints(root, childrel, childRTE))
358 * This child need not be scanned, so we can omit it from the
359 * appendrel. Mark it with a dummy cheapest-path though, in case
360 * best_appendrel_indexscan() looks at it later.
362 set_dummy_rel_pathlist(childrel);
366 /* CE failed, so finish copying targetlist and join quals */
367 childrel->joininfo = (List *)
368 adjust_appendrel_attrs((Node *) rel->joininfo,
370 childrel->reltargetlist = (List *)
371 adjust_appendrel_attrs((Node *) rel->reltargetlist,
375 * We have to make child entries in the EquivalenceClass data
376 * structures as well.
378 if (rel->has_eclass_joins)
380 add_child_rel_equivalences(root, appinfo, rel, childrel);
381 childrel->has_eclass_joins = true;
385 * Copy the parent's attr_needed data as well, with appropriate
386 * adjustment of relids and attribute numbers.
388 pfree(childrel->attr_needed);
389 childrel->attr_needed =
390 adjust_appendrel_attr_needed(rel, appinfo,
395 * Compute the child's access paths, and add the cheapest one to the
396 * Append path we are constructing for the parent.
398 * It's possible that the child is itself an appendrel, in which case
399 * we can "cut out the middleman" and just add its child paths to our
400 * own list. (We don't try to do this earlier because we need to
401 * apply both levels of transformation to the quals.)
403 set_rel_pathlist(root, childrel, childRTindex, childRTE);
405 childpath = childrel->cheapest_total_path;
406 if (IsA(childpath, AppendPath))
407 subpaths = list_concat(subpaths,
408 ((AppendPath *) childpath)->subpaths);
410 subpaths = lappend(subpaths, childpath);
413 * Accumulate size information from each child.
415 if (childrel->rows > 0)
417 parent_rows += childrel->rows;
418 parent_size += childrel->width * childrel->rows;
420 forboth(parentvars, rel->reltargetlist,
421 childvars, childrel->reltargetlist)
423 Var *parentvar = (Var *) lfirst(parentvars);
424 Var *childvar = (Var *) lfirst(childvars);
427 * Accumulate per-column estimates too. Whole-row Vars and
428 * PlaceHolderVars can be ignored here.
430 if (IsA(parentvar, Var) &&
433 int pndx = parentvar->varattno - rel->min_attr;
434 int cndx = childvar->varattno - childrel->min_attr;
436 parent_attrsizes[pndx] += childrel->attr_widths[cndx] * childrel->rows;
443 * Save the finished size estimates.
445 rel->rows = parent_rows;
450 rel->width = rint(parent_size / parent_rows);
451 for (i = 0; i < nattrs; i++)
452 rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
455 rel->width = 0; /* attr_widths should be zero already */
458 * Set "raw tuples" count equal to "rows" for the appendrel; needed
459 * because some places assume rel->tuples is valid for any baserel.
461 rel->tuples = parent_rows;
463 pfree(parent_attrsizes);
466 * Finally, build Append path and install it as the only access path for
467 * the parent rel. (Note: this is correct even if we have zero or one
468 * live subpath due to constraint exclusion.)
470 add_path(rel, (Path *) create_append_path(rel, subpaths));
472 /* Select cheapest path (pretty easy in this case...) */
477 * set_dummy_rel_pathlist
478 * Build a dummy path for a relation that's been excluded by constraints
480 * Rather than inventing a special "dummy" path type, we represent this as an
481 * AppendPath with no members (see also IS_DUMMY_PATH macro).
484 set_dummy_rel_pathlist(RelOptInfo *rel)
486 /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
490 add_path(rel, (Path *) create_append_path(rel, NIL));
492 /* Select cheapest path (pretty easy in this case...) */
496 /* quick-and-dirty test to see if any joining is needed */
498 has_multiple_baserels(PlannerInfo *root)
500 int num_base_rels = 0;
503 for (rti = 1; rti < root->simple_rel_array_size; rti++)
505 RelOptInfo *brel = root->simple_rel_array[rti];
510 /* ignore RTEs that are "other rels" */
511 if (brel->reloptkind == RELOPT_BASEREL)
512 if (++num_base_rels > 1)
519 * set_subquery_pathlist
520 * Build the (single) access path for a subquery RTE
523 set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
524 Index rti, RangeTblEntry *rte)
526 Query *parse = root->parse;
527 Query *subquery = rte->subquery;
528 bool *differentTypes;
529 double tuple_fraction;
530 PlannerInfo *subroot;
533 /* We need a workspace for keeping track of set-op type coercions */
534 differentTypes = (bool *)
535 palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
538 * If there are any restriction clauses that have been attached to the
539 * subquery relation, consider pushing them down to become WHERE or HAVING
540 * quals of the subquery itself. This transformation is useful because it
541 * may allow us to generate a better plan for the subquery than evaluating
542 * all the subquery output rows and then filtering them.
544 * There are several cases where we cannot push down clauses. Restrictions
545 * involving the subquery are checked by subquery_is_pushdown_safe().
546 * Restrictions on individual clauses are checked by
547 * qual_is_pushdown_safe(). Also, we don't want to push down
548 * pseudoconstant clauses; better to have the gating node above the
551 * Non-pushed-down clauses will get evaluated as qpquals of the
554 * XXX Are there any cases where we want to make a policy decision not to
555 * push down a pushable qual, because it'd result in a worse plan?
557 if (rel->baserestrictinfo != NIL &&
558 subquery_is_pushdown_safe(subquery, subquery, differentTypes))
560 /* OK to consider pushing down individual quals */
561 List *upperrestrictlist = NIL;
564 foreach(l, rel->baserestrictinfo)
566 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
567 Node *clause = (Node *) rinfo->clause;
569 if (!rinfo->pseudoconstant &&
570 qual_is_pushdown_safe(subquery, rti, clause, differentTypes))
573 subquery_push_qual(subquery, rte, rti, clause);
577 /* Keep it in the upper query */
578 upperrestrictlist = lappend(upperrestrictlist, rinfo);
581 rel->baserestrictinfo = upperrestrictlist;
584 pfree(differentTypes);
587 * We can safely pass the outer tuple_fraction down to the subquery if the
588 * outer level has no joining, aggregation, or sorting to do. Otherwise
589 * we'd better tell the subquery to plan for full retrieval. (XXX This
590 * could probably be made more intelligent ...)
592 if (parse->hasAggs ||
593 parse->groupClause ||
595 parse->distinctClause ||
597 has_multiple_baserels(root))
598 tuple_fraction = 0.0; /* default case */
600 tuple_fraction = root->tuple_fraction;
602 /* Generate the plan for the subquery */
603 rel->subplan = subquery_planner(root->glob, subquery,
605 false, tuple_fraction,
607 rel->subrtable = subroot->parse->rtable;
609 /* Copy number of output rows from subplan */
610 rel->tuples = rel->subplan->plan_rows;
612 /* Mark rel with estimated output rows, width, etc */
613 set_baserel_size_estimates(root, rel);
615 /* Convert subquery pathkeys to outer representation */
616 pathkeys = convert_subquery_pathkeys(root, rel, subroot->query_pathkeys);
618 /* Generate appropriate path */
619 add_path(rel, create_subqueryscan_path(rel, pathkeys));
621 /* Select cheapest path (pretty easy in this case...) */
626 * set_function_pathlist
627 * Build the (single) access path for a function RTE
630 set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
632 /* Mark rel with estimated output rows, width, etc */
633 set_function_size_estimates(root, rel);
635 /* Generate appropriate path */
636 add_path(rel, create_functionscan_path(root, rel));
638 /* Select cheapest path (pretty easy in this case...) */
643 * set_values_pathlist
644 * Build the (single) access path for a VALUES RTE
647 set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
649 /* Mark rel with estimated output rows, width, etc */
650 set_values_size_estimates(root, rel);
652 /* Generate appropriate path */
653 add_path(rel, create_valuesscan_path(root, rel));
655 /* Select cheapest path (pretty easy in this case...) */
661 * Build the (single) access path for a non-self-reference CTE RTE
664 set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
667 PlannerInfo *cteroot;
674 * Find the referenced CTE, and locate the plan previously made for it.
676 levelsup = rte->ctelevelsup;
678 while (levelsup-- > 0)
680 cteroot = cteroot->parent_root;
681 if (!cteroot) /* shouldn't happen */
682 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
685 * Note: cte_plan_ids can be shorter than cteList, if we are still working
686 * on planning the CTEs (ie, this is a side-reference from another CTE).
687 * So we mustn't use forboth here.
690 foreach(lc, cteroot->parse->cteList)
692 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
694 if (strcmp(cte->ctename, rte->ctename) == 0)
698 if (lc == NULL) /* shouldn't happen */
699 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
700 if (ndx >= list_length(cteroot->cte_plan_ids))
701 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
702 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
704 cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
706 /* Mark rel with estimated output rows, width, etc */
707 set_cte_size_estimates(root, rel, cteplan);
709 /* Generate appropriate path */
710 add_path(rel, create_ctescan_path(root, rel));
712 /* Select cheapest path (pretty easy in this case...) */
717 * set_worktable_pathlist
718 * Build the (single) access path for a self-reference CTE RTE
721 set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
724 PlannerInfo *cteroot;
728 * We need to find the non-recursive term's plan, which is in the plan
729 * level that's processing the recursive UNION, which is one level
730 * *below* where the CTE comes from.
732 levelsup = rte->ctelevelsup;
733 if (levelsup == 0) /* shouldn't happen */
734 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
737 while (levelsup-- > 0)
739 cteroot = cteroot->parent_root;
740 if (!cteroot) /* shouldn't happen */
741 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
743 cteplan = cteroot->non_recursive_plan;
744 if (!cteplan) /* shouldn't happen */
745 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
747 /* Mark rel with estimated output rows, width, etc */
748 set_cte_size_estimates(root, rel, cteplan);
750 /* Generate appropriate path */
751 add_path(rel, create_worktablescan_path(root, rel));
753 /* Select cheapest path (pretty easy in this case...) */
758 * make_rel_from_joinlist
759 * Build access paths using a "joinlist" to guide the join path search.
761 * See comments for deconstruct_jointree() for definition of the joinlist
765 make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
772 * Count the number of child joinlist nodes. This is the depth of the
773 * dynamic-programming algorithm we must employ to consider all ways of
774 * joining the child nodes.
776 levels_needed = list_length(joinlist);
778 if (levels_needed <= 0)
779 return NULL; /* nothing to do? */
782 * Construct a list of rels corresponding to the child joinlist nodes.
783 * This may contain both base rels and rels constructed according to
787 foreach(jl, joinlist)
789 Node *jlnode = (Node *) lfirst(jl);
792 if (IsA(jlnode, RangeTblRef))
794 int varno = ((RangeTblRef *) jlnode)->rtindex;
796 thisrel = find_base_rel(root, varno);
798 else if (IsA(jlnode, List))
800 /* Recurse to handle subproblem */
801 thisrel = make_rel_from_joinlist(root, (List *) jlnode);
805 elog(ERROR, "unrecognized joinlist node type: %d",
806 (int) nodeTag(jlnode));
807 thisrel = NULL; /* keep compiler quiet */
810 initial_rels = lappend(initial_rels, thisrel);
813 if (levels_needed == 1)
816 * Single joinlist node, so we're done.
818 return (RelOptInfo *) linitial(initial_rels);
823 * Consider the different orders in which we could join the rels,
824 * using a plugin, GEQO, or the regular join search code.
826 * We put the initial_rels list into a PlannerInfo field because
827 * has_legal_joinclause() needs to look at it (ugly :-().
829 root->initial_rels = initial_rels;
831 if (join_search_hook)
832 return (*join_search_hook) (root, levels_needed, initial_rels);
833 else if (enable_geqo && levels_needed >= geqo_threshold)
834 return geqo(root, levels_needed, initial_rels);
836 return standard_join_search(root, levels_needed, initial_rels);
841 * standard_join_search
842 * Find possible joinpaths for a query by successively finding ways
843 * to join component relations into join relations.
845 * 'levels_needed' is the number of iterations needed, ie, the number of
846 * independent jointree items in the query. This is > 1.
848 * 'initial_rels' is a list of RelOptInfo nodes for each independent
849 * jointree item. These are the components to be joined together.
850 * Note that levels_needed == list_length(initial_rels).
852 * Returns the final level of join relations, i.e., the relation that is
853 * the result of joining all the original relations together.
854 * At least one implementation path must be provided for this relation and
855 * all required sub-relations.
857 * To support loadable plugins that modify planner behavior by changing the
858 * join searching algorithm, we provide a hook variable that lets a plugin
859 * replace or supplement this function. Any such hook must return the same
860 * final join relation as the standard code would, but it might have a
861 * different set of implementation paths attached, and only the sub-joinrels
862 * needed for these paths need have been instantiated.
864 * Note to plugin authors: the functions invoked during standard_join_search()
865 * modify root->join_rel_list and root->join_rel_hash. If you want to do more
866 * than one join-order search, you'll probably need to save and restore the
867 * original states of those data structures. See geqo_eval() for an example.
870 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
877 * We employ a simple "dynamic programming" algorithm: we first find all
878 * ways to build joins of two jointree items, then all ways to build joins
879 * of three items (from two-item joins and single items), then four-item
880 * joins, and so on until we have considered all ways to join all the
881 * items into one rel.
883 * joinitems[j] is a list of all the j-item rels. Initially we set
884 * joinitems[1] to represent all the single-jointree-item relations.
886 joinitems = (List **) palloc0((levels_needed + 1) * sizeof(List *));
888 joinitems[1] = initial_rels;
890 for (lev = 2; lev <= levels_needed; lev++)
895 * Determine all possible pairs of relations to be joined at this
896 * level, and build paths for making each one from every available
897 * pair of lower-level relations.
899 joinitems[lev] = join_search_one_level(root, lev, joinitems);
902 * Do cleanup work on each just-processed rel.
904 foreach(x, joinitems[lev])
906 rel = (RelOptInfo *) lfirst(x);
908 /* Find and save the cheapest paths for this rel */
911 #ifdef OPTIMIZER_DEBUG
912 debug_print_rel(root, rel);
918 * We should have a single rel at the final level.
920 if (joinitems[levels_needed] == NIL)
921 elog(ERROR, "failed to build any %d-way joins", levels_needed);
922 Assert(list_length(joinitems[levels_needed]) == 1);
924 rel = (RelOptInfo *) linitial(joinitems[levels_needed]);
929 /*****************************************************************************
930 * PUSHING QUALS DOWN INTO SUBQUERIES
931 *****************************************************************************/
934 * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
936 * subquery is the particular component query being checked. topquery
937 * is the top component of a set-operations tree (the same Query if no
938 * set-op is involved).
940 * Conditions checked here:
942 * 1. If the subquery has a LIMIT clause, we must not push down any quals,
943 * since that could change the set of rows returned.
945 * 2. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
946 * quals into it, because that would change the results.
948 * 3. For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
949 * push quals into each component query, but the quals can only reference
950 * subquery columns that suffer no type coercions in the set operation.
951 * Otherwise there are possible semantic gotchas. So, we check the
952 * component queries to see if any of them have different output types;
953 * differentTypes[k] is set true if column k has different type in any
957 subquery_is_pushdown_safe(Query *subquery, Query *topquery,
958 bool *differentTypes)
960 SetOperationStmt *topop;
963 if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
966 /* Are we at top level, or looking at a setop component? */
967 if (subquery == topquery)
969 /* Top level, so check any component queries */
970 if (subquery->setOperations != NULL)
971 if (!recurse_pushdown_safe(subquery->setOperations, topquery,
977 /* Setop component must not have more components (too weird) */
978 if (subquery->setOperations != NULL)
980 /* Check whether setop component output types match top level */
981 topop = (SetOperationStmt *) topquery->setOperations;
982 Assert(topop && IsA(topop, SetOperationStmt));
983 compare_tlist_datatypes(subquery->targetList,
991 * Helper routine to recurse through setOperations tree
994 recurse_pushdown_safe(Node *setOp, Query *topquery,
995 bool *differentTypes)
997 if (IsA(setOp, RangeTblRef))
999 RangeTblRef *rtr = (RangeTblRef *) setOp;
1000 RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
1001 Query *subquery = rte->subquery;
1003 Assert(subquery != NULL);
1004 return subquery_is_pushdown_safe(subquery, topquery, differentTypes);
1006 else if (IsA(setOp, SetOperationStmt))
1008 SetOperationStmt *op = (SetOperationStmt *) setOp;
1010 /* EXCEPT is no good */
1011 if (op->op == SETOP_EXCEPT)
1014 if (!recurse_pushdown_safe(op->larg, topquery, differentTypes))
1016 if (!recurse_pushdown_safe(op->rarg, topquery, differentTypes))
1021 elog(ERROR, "unrecognized node type: %d",
1022 (int) nodeTag(setOp));
1028 * Compare tlist's datatypes against the list of set-operation result types.
1029 * For any items that are different, mark the appropriate element of
1030 * differentTypes[] to show that this column will have type conversions.
1032 * We don't have to care about typmods here: the only allowed difference
1033 * between set-op input and output typmods is input is a specific typmod
1034 * and output is -1, and that does not require a coercion.
1037 compare_tlist_datatypes(List *tlist, List *colTypes,
1038 bool *differentTypes)
1041 ListCell *colType = list_head(colTypes);
1045 TargetEntry *tle = (TargetEntry *) lfirst(l);
1048 continue; /* ignore resjunk columns */
1049 if (colType == NULL)
1050 elog(ERROR, "wrong number of tlist entries");
1051 if (exprType((Node *) tle->expr) != lfirst_oid(colType))
1052 differentTypes[tle->resno] = true;
1053 colType = lnext(colType);
1055 if (colType != NULL)
1056 elog(ERROR, "wrong number of tlist entries");
1060 * qual_is_pushdown_safe - is a particular qual safe to push down?
1062 * qual is a restriction clause applying to the given subquery (whose RTE
1063 * has index rti in the parent query).
1065 * Conditions checked here:
1067 * 1. The qual must not contain any subselects (mainly because I'm not sure
1068 * it will work correctly: sublinks will already have been transformed into
1069 * subplans in the qual, but not in the subquery).
1071 * 2. The qual must not refer to the whole-row output of the subquery
1072 * (since there is no easy way to name that within the subquery itself).
1074 * 3. The qual must not refer to any subquery output columns that were
1075 * found to have inconsistent types across a set operation tree by
1076 * subquery_is_pushdown_safe().
1078 * 4. If the subquery uses DISTINCT ON, we must not push down any quals that
1079 * refer to non-DISTINCT output columns, because that could change the set
1080 * of rows returned. (This condition is vacuous for DISTINCT, because then
1081 * there are no non-DISTINCT output columns, so we needn't check. But note
1082 * we are assuming that the qual can't distinguish values that the DISTINCT
1083 * operator sees as equal. This is a bit shaky but we have no way to test
1084 * for the case, and it's unlikely enough that we shouldn't refuse the
1085 * optimization just because it could theoretically happen.)
1087 * 5. We must not push down any quals that refer to subselect outputs that
1088 * return sets, else we'd introduce functions-returning-sets into the
1089 * subquery's WHERE/HAVING quals.
1091 * 6. We must not push down any quals that refer to subselect outputs that
1092 * contain volatile functions, for fear of introducing strange results due
1093 * to multiple evaluation of a volatile function.
1096 qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
1097 bool *differentTypes)
1102 Bitmapset *tested = NULL;
1104 /* Refuse subselects (point 1) */
1105 if (contain_subplans(qual))
1109 * Examine all Vars used in clause; since it's a restriction clause, all
1110 * such Vars must refer to subselect output columns.
1112 vars = pull_var_clause(qual, true);
1115 Var *var = (Var *) lfirst(vl);
1119 * XXX Punt if we find any PlaceHolderVars in the restriction clause.
1120 * It's not clear whether a PHV could safely be pushed down, and even
1121 * less clear whether such a situation could arise in any cases of
1122 * practical interest anyway. So for the moment, just refuse to push
1131 Assert(var->varno == rti);
1134 if (var->varattno == 0)
1141 * We use a bitmapset to avoid testing the same attno more than once.
1142 * (NB: this only works because subquery outputs can't have negative
1145 if (bms_is_member(var->varattno, tested))
1147 tested = bms_add_member(tested, var->varattno);
1150 if (differentTypes[var->varattno])
1156 /* Must find the tlist element referenced by the Var */
1157 tle = get_tle_by_resno(subquery->targetList, var->varattno);
1158 Assert(tle != NULL);
1159 Assert(!tle->resjunk);
1161 /* If subquery uses DISTINCT ON, check point 4 */
1162 if (subquery->hasDistinctOn &&
1163 !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
1165 /* non-DISTINCT column, so fail */
1170 /* Refuse functions returning sets (point 5) */
1171 if (expression_returns_set((Node *) tle->expr))
1177 /* Refuse volatile functions (point 6) */
1178 if (contain_volatile_functions((Node *) tle->expr))
1192 * subquery_push_qual - push down a qual that we have determined is safe
1195 subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
1197 if (subquery->setOperations != NULL)
1199 /* Recurse to push it separately to each component query */
1200 recurse_push_qual(subquery->setOperations, subquery,
1206 * We need to replace Vars in the qual (which must refer to outputs of
1207 * the subquery) with copies of the subquery's targetlist expressions.
1208 * Note that at this point, any uplevel Vars in the qual should have
1209 * been replaced with Params, so they need no work.
1211 * This step also ensures that when we are pushing into a setop tree,
1212 * each component query gets its own copy of the qual.
1214 qual = ResolveNew(qual, rti, 0, rte,
1215 subquery->targetList,
1219 * Now attach the qual to the proper place: normally WHERE, but if the
1220 * subquery uses grouping or aggregation, put it in HAVING (since the
1221 * qual really refers to the group-result rows).
1223 if (subquery->hasAggs || subquery->groupClause || subquery->havingQual)
1224 subquery->havingQual = make_and_qual(subquery->havingQual, qual);
1226 subquery->jointree->quals =
1227 make_and_qual(subquery->jointree->quals, qual);
1230 * We need not change the subquery's hasAggs or hasSublinks flags,
1231 * since we can't be pushing down any aggregates that weren't there
1232 * before, and we don't push down subselects at all.
1238 * Helper routine to recurse through setOperations tree
1241 recurse_push_qual(Node *setOp, Query *topquery,
1242 RangeTblEntry *rte, Index rti, Node *qual)
1244 if (IsA(setOp, RangeTblRef))
1246 RangeTblRef *rtr = (RangeTblRef *) setOp;
1247 RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
1248 Query *subquery = subrte->subquery;
1250 Assert(subquery != NULL);
1251 subquery_push_qual(subquery, rte, rti, qual);
1253 else if (IsA(setOp, SetOperationStmt))
1255 SetOperationStmt *op = (SetOperationStmt *) setOp;
1257 recurse_push_qual(op->larg, topquery, rte, rti, qual);
1258 recurse_push_qual(op->rarg, topquery, rte, rti, qual);
1262 elog(ERROR, "unrecognized node type: %d",
1263 (int) nodeTag(setOp));
1267 /*****************************************************************************
1269 *****************************************************************************/
1271 #ifdef OPTIMIZER_DEBUG
1274 print_relids(Relids relids)
1280 tmprelids = bms_copy(relids);
1281 while ((x = bms_first_member(tmprelids)) >= 0)
1288 bms_free(tmprelids);
1292 print_restrictclauses(PlannerInfo *root, List *clauses)
1298 RestrictInfo *c = lfirst(l);
1300 print_expr((Node *) c->clause, root->parse->rtable);
1307 print_path(PlannerInfo *root, Path *path, int indent)
1311 Path *subpath = NULL;
1314 switch (nodeTag(path))
1322 case T_BitmapHeapPath:
1323 ptype = "BitmapHeapScan";
1325 case T_BitmapAndPath:
1326 ptype = "BitmapAndPath";
1328 case T_BitmapOrPath:
1329 ptype = "BitmapOrPath";
1340 case T_MaterialPath:
1342 subpath = ((MaterialPath *) path)->subpath;
1346 subpath = ((UniquePath *) path)->subpath;
1353 ptype = "MergeJoin";
1365 for (i = 0; i < indent; i++)
1367 printf("%s", ptype);
1372 print_relids(path->parent->relids);
1373 printf(") rows=%.0f", path->parent->rows);
1375 printf(" cost=%.2f..%.2f\n", path->startup_cost, path->total_cost);
1379 for (i = 0; i < indent; i++)
1381 printf(" pathkeys: ");
1382 print_pathkeys(path->pathkeys, root->parse->rtable);
1387 JoinPath *jp = (JoinPath *) path;
1389 for (i = 0; i < indent; i++)
1391 printf(" clauses: ");
1392 print_restrictclauses(root, jp->joinrestrictinfo);
1395 if (IsA(path, MergePath))
1397 MergePath *mp = (MergePath *) path;
1399 if (mp->outersortkeys || mp->innersortkeys)
1401 for (i = 0; i < indent; i++)
1403 printf(" sortouter=%d sortinner=%d\n",
1404 ((mp->outersortkeys) ? 1 : 0),
1405 ((mp->innersortkeys) ? 1 : 0));
1409 print_path(root, jp->outerjoinpath, indent + 1);
1410 print_path(root, jp->innerjoinpath, indent + 1);
1414 print_path(root, subpath, indent + 1);
1418 debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
1422 printf("RELOPTINFO (");
1423 print_relids(rel->relids);
1424 printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
1426 if (rel->baserestrictinfo)
1428 printf("\tbaserestrictinfo: ");
1429 print_restrictclauses(root, rel->baserestrictinfo);
1435 printf("\tjoininfo: ");
1436 print_restrictclauses(root, rel->joininfo);
1440 printf("\tpath list:\n");
1441 foreach(l, rel->pathlist)
1442 print_path(root, lfirst(l), 1);
1443 printf("\n\tcheapest startup path:\n");
1444 print_path(root, rel->cheapest_startup_path, 1);
1445 printf("\n\tcheapest total path:\n");
1446 print_path(root, rel->cheapest_total_path, 1);
1451 #endif /* OPTIMIZER_DEBUG */