1 /*-------------------------------------------------------------------------
4 * Routines to find possible search paths for processing a query
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.82 2001/11/05 17:46:25 momjian Exp $
13 *-------------------------------------------------------------------------
18 #ifdef OPTIMIZER_DEBUG
19 #include "nodes/print.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/geqo.h"
24 #include "optimizer/pathnode.h"
25 #include "optimizer/paths.h"
26 #include "optimizer/plancat.h"
27 #include "optimizer/planner.h"
28 #include "optimizer/prep.h"
29 #include "parser/parsetree.h"
30 #include "rewrite/rewriteManip.h"
33 bool enable_geqo = true;
34 int geqo_rels = DEFAULT_GEQO_RELS;
37 static void set_base_rel_pathlists(Query *root);
38 static void set_plain_rel_pathlist(Query *root, RelOptInfo *rel,
40 static void set_inherited_rel_pathlist(Query *root, RelOptInfo *rel,
41 Index rti, RangeTblEntry *rte,
43 static void set_subquery_pathlist(Query *root, RelOptInfo *rel,
44 Index rti, RangeTblEntry *rte);
45 static RelOptInfo *make_one_rel_by_joins(Query *root, int levels_needed,
51 * Finds all possible access paths for executing a query, returning a
52 * single rel that represents the join of all base rels in the query.
55 make_one_rel(Query *root)
60 * Generate access paths for the base rels.
62 set_base_rel_pathlists(root);
65 * Generate access paths for the entire join tree.
67 Assert(root->jointree != NULL && IsA(root->jointree, FromExpr));
69 rel = make_fromexpr_rel(root, root->jointree);
72 * The result should join all the query's base rels.
74 Assert(length(rel->relids) == length(root->base_rel_list));
80 * set_base_rel_pathlists
81 * Finds all paths available for scanning each base-relation entry.
82 * Sequential scan and any available indices are considered.
83 * Each useful path is attached to its relation's 'pathlist' field.
86 set_base_rel_pathlists(Query *root)
90 foreach(rellist, root->base_rel_list)
92 RelOptInfo *rel = (RelOptInfo *) lfirst(rellist);
97 Assert(length(rel->relids) == 1); /* better be base rel */
98 rti = lfirsti(rel->relids);
99 rte = rt_fetch(rti, root->rtable);
103 /* Subquery --- generate a separate plan for it */
104 set_subquery_pathlist(root, rel, rti, rte);
106 else if ((inheritlist = expand_inherted_rtentry(root, rti, true))
109 /* Relation is root of an inheritance tree, process specially */
110 set_inherited_rel_pathlist(root, rel, rti, rte, inheritlist);
115 set_plain_rel_pathlist(root, rel, rte);
118 #ifdef OPTIMIZER_DEBUG
119 debug_print_rel(root, rel);
125 * set_plain_rel_pathlist
126 * Build access paths for a plain relation (no subquery, no inheritance)
129 set_plain_rel_pathlist(Query *root, RelOptInfo *rel, RangeTblEntry *rte)
131 /* Mark rel with estimated output rows, width, etc */
132 set_baserel_size_estimates(root, rel);
135 * Generate paths and add them to the rel's pathlist.
137 * Note: add_path() will discard any paths that are dominated by another
138 * available path, keeping only those paths that are superior along at
139 * least one dimension of cost or sortedness.
142 /* Consider sequential scan */
143 add_path(rel, create_seqscan_path(root, rel));
145 /* Consider TID scans */
146 create_tidscan_paths(root, rel);
148 /* Consider index paths for both simple and OR index clauses */
149 create_index_paths(root, rel);
151 /* create_index_paths must be done before create_or_index_paths */
152 create_or_index_paths(root, rel);
154 /* Now find the cheapest of the paths for this rel */
159 * set_inherited_rel_pathlist
160 * Build access paths for a inheritance tree rooted at rel
162 * inheritlist is a list of RT indexes of all tables in the inheritance tree,
163 * including a duplicate of the parent itself. Note we will not come here
164 * unless there's at least one child in addition to the parent.
166 * NOTE: the passed-in rel and RTE will henceforth represent the appended
167 * result of the whole inheritance tree. The members of inheritlist represent
168 * the individual tables --- in particular, the inheritlist member that is a
169 * duplicate of the parent RTE represents the parent table alone.
170 * We will generate plans to scan the individual tables that refer to
171 * the inheritlist RTEs, whereas Vars elsewhere in the plan tree that
172 * refer to the original RTE are taken to refer to the append output.
173 * In particular, this means we have separate RelOptInfos for the parent
174 * table and for the append output, which is a good thing because they're
178 set_inherited_rel_pathlist(Query *root, RelOptInfo *rel,
179 Index rti, RangeTblEntry *rte,
182 int parentRTindex = rti;
183 Oid parentOID = rte->relid;
184 List *subpaths = NIL;
188 * XXX for now, can't handle inherited expansion of FOR UPDATE; can we
191 if (intMember(parentRTindex, root->rowMarks))
192 elog(ERROR, "SELECT FOR UPDATE is not supported for inherit queries");
195 * The executor will check the parent table's access permissions when
196 * it examines the parent's inheritlist entry. There's no need to
197 * check twice, so turn off access check bits in the original RTE.
199 rte->checkForRead = false;
200 rte->checkForWrite = false;
203 * Initialize to compute size estimates for whole inheritance tree
209 * Generate access paths for each table in the tree (parent AND
210 * children), and pick the cheapest path for each table.
212 foreach(il, inheritlist)
214 int childRTindex = lfirsti(il);
215 RangeTblEntry *childrte;
217 RelOptInfo *childrel;
219 childrte = rt_fetch(childRTindex, root->rtable);
220 childOID = childrte->relid;
223 * Make a RelOptInfo for the child so we can do planning. Do NOT
224 * attach the RelOptInfo to the query's base_rel_list, however,
225 * since the child is not part of the main join tree. Instead,
226 * the child RelOptInfo is added to other_rel_list.
228 childrel = build_other_rel(root, childRTindex);
231 * Copy the parent's targetlist and restriction quals to the
232 * child, with attribute-number adjustment as needed. We don't
233 * bother to copy the join quals, since we can't do any joining of
234 * the individual tables.
236 childrel->targetlist = (List *)
237 adjust_inherited_attrs((Node *) rel->targetlist,
242 childrel->baserestrictinfo = (List *)
243 adjust_inherited_attrs((Node *) rel->baserestrictinfo,
248 childrel->baserestrictcost = rel->baserestrictcost;
251 * Now compute child access paths, and save the cheapest.
253 set_plain_rel_pathlist(root, childrel, childrte);
255 subpaths = lappend(subpaths, childrel->cheapest_total_path);
257 /* Also update total size estimates */
258 rel->rows += childrel->rows;
259 if (childrel->width > rel->width)
260 rel->width = childrel->width;
264 * Finally, build Append path and install it as the only access path
265 * for the parent rel.
267 add_path(rel, (Path *) create_append_path(rel, subpaths));
269 /* Select cheapest path (pretty easy in this case...) */
274 * set_subquery_pathlist
275 * Build the (single) access path for a subquery RTE
278 set_subquery_pathlist(Query *root, RelOptInfo *rel,
279 Index rti, RangeTblEntry *rte)
281 Query *subquery = rte->subquery;
284 * If there are any restriction clauses that have been attached to the
285 * subquery relation, consider pushing them down to become HAVING
286 * quals of the subquery itself. (Not WHERE clauses, since they may
287 * refer to subquery outputs that are aggregate results. But
288 * planner.c will transfer them into the subquery's WHERE if they do
289 * not.) This transformation is useful because it may allow us to
290 * generate a better plan for the subquery than evaluating all the
291 * subquery output rows and then filtering them.
293 * There are several cases where we cannot push down clauses:
295 * 1. If the subquery contains set ops (UNION/INTERSECT/EXCEPT) we do not
296 * push down any qual clauses, since the planner doesn't support quals
297 * at the top level of a setop. (With suitable analysis we could try
298 * to push the quals down into the component queries of the setop, but
299 * getting it right seems nontrivial. Work on this later.)
301 * 2. If the subquery has a LIMIT clause or a DISTINCT ON clause, we must
302 * not push down any quals, since that could change the set of rows
303 * returned. (Actually, we could push down quals into a DISTINCT ON
304 * subquery if they refer only to DISTINCT-ed output columns, but
305 * checking that seems more work than it's worth. In any case, a
306 * plain DISTINCT is safe to push down past.)
308 * 3. We do not push down clauses that contain subselects, mainly because
309 * I'm not sure it will work correctly (the subplan hasn't yet
310 * transformed sublinks to subselects).
312 * Non-pushed-down clauses will get evaluated as qpquals of the
315 * XXX Are there any cases where we want to make a policy decision not to
316 * push down, because it'd result in a worse plan?
318 if (subquery->setOperations == NULL &&
319 subquery->limitOffset == NULL &&
320 subquery->limitCount == NULL &&
321 !has_distinct_on_clause(subquery))
323 /* OK to consider pushing down individual quals */
324 List *upperrestrictlist = NIL;
327 foreach(lst, rel->baserestrictinfo)
329 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lst);
330 Node *clause = (Node *) rinfo->clause;
332 if (contain_subplans(clause))
334 /* Keep it in the upper query */
335 upperrestrictlist = lappend(upperrestrictlist, rinfo);
340 * We need to replace Vars in the clause (which must refer
341 * to outputs of the subquery) with copies of the
342 * subquery's targetlist expressions. Note that at this
343 * point, any uplevel Vars in the clause should have been
344 * replaced with Params, so they need no work.
346 clause = ResolveNew(clause, rti, 0,
347 subquery->targetList,
349 subquery->havingQual = make_and_qual(subquery->havingQual,
353 * We need not change the subquery's hasAggs or
354 * hasSublinks flags, since we can't be pushing down any
355 * aggregates that weren't there before, and we don't push
356 * down subselects at all.
360 rel->baserestrictinfo = upperrestrictlist;
363 /* Generate the plan for the subquery */
364 rel->subplan = subquery_planner(subquery,
365 -1.0 /* default case */ );
367 /* Copy number of output rows from subplan */
368 rel->tuples = rel->subplan->plan_rows;
370 /* Mark rel with estimated output rows, width, etc */
371 set_baserel_size_estimates(root, rel);
373 /* Generate appropriate path */
374 add_path(rel, create_subqueryscan_path(rel));
376 /* Select cheapest path (pretty easy in this case...) */
382 * Build access paths for a FromExpr jointree node.
385 make_fromexpr_rel(Query *root, FromExpr *from)
388 List *initial_rels = NIL;
392 * Count the number of child jointree nodes. This is the depth of the
393 * dynamic-programming algorithm we must employ to consider all ways
394 * of joining the child nodes.
396 levels_needed = length(from->fromlist);
398 if (levels_needed <= 0)
399 return NULL; /* nothing to do? */
402 * Construct a list of rels corresponding to the child jointree nodes.
403 * This may contain both base rels and rels constructed according to
404 * explicit JOIN directives.
406 foreach(jt, from->fromlist)
408 Node *jtnode = (Node *) lfirst(jt);
410 initial_rels = lappend(initial_rels,
411 make_jointree_rel(root, jtnode));
414 if (levels_needed == 1)
417 * Single jointree node, so we're done.
419 return (RelOptInfo *) lfirst(initial_rels);
424 * Consider the different orders in which we could join the rels,
425 * using either GEQO or regular optimizer.
427 if (enable_geqo && levels_needed >= geqo_rels)
428 return geqo(root, levels_needed, initial_rels);
430 return make_one_rel_by_joins(root, levels_needed, initial_rels);
435 * make_one_rel_by_joins
436 * Find all possible joinpaths for a query by successively finding ways
437 * to join component relations into join relations.
439 * 'levels_needed' is the number of iterations needed, ie, the number of
440 * independent jointree items in the query. This is > 1.
442 * 'initial_rels' is a list of RelOptInfo nodes for each independent
443 * jointree item. These are the components to be joined together.
445 * Returns the final level of join relations, i.e., the relation that is
446 * the result of joining all the original relations together.
449 make_one_rel_by_joins(Query *root, int levels_needed, List *initial_rels)
456 * We employ a simple "dynamic programming" algorithm: we first find
457 * all ways to build joins of two jointree items, then all ways to
458 * build joins of three items (from two-item joins and single items),
459 * then four-item joins, and so on until we have considered all ways
460 * to join all the items into one rel.
462 * joinitems[j] is a list of all the j-item rels. Initially we set
463 * joinitems[1] to represent all the single-jointree-item relations.
465 joinitems = (List **) palloc((levels_needed + 1) * sizeof(List *));
466 MemSet(joinitems, 0, (levels_needed + 1) * sizeof(List *));
468 joinitems[1] = initial_rels;
470 for (lev = 2; lev <= levels_needed; lev++)
475 * Determine all possible pairs of relations to be joined at this
476 * level, and build paths for making each one from every available
477 * pair of lower-level relations.
479 joinitems[lev] = make_rels_by_joins(root, lev, joinitems);
482 * Do cleanup work on each just-processed rel.
484 foreach(x, joinitems[lev])
486 rel = (RelOptInfo *) lfirst(x);
491 * * for each expensive predicate in each path in each
492 * distinct rel, * consider doing pullup -- JMH
494 if (XfuncMode != XFUNC_NOPULL && XfuncMode != XFUNC_OFF)
495 xfunc_trypullup(rel);
498 /* Find and save the cheapest paths for this rel */
501 #ifdef OPTIMIZER_DEBUG
502 debug_print_rel(root, rel);
508 * We should have a single rel at the final level.
510 Assert(length(joinitems[levels_needed]) == 1);
512 rel = (RelOptInfo *) lfirst(joinitems[levels_needed]);
517 /*****************************************************************************
519 *****************************************************************************/
521 #ifdef OPTIMIZER_DEBUG
524 print_relids(Relids relids)
530 printf("%d", lfirsti(l));
537 print_restrictclauses(Query *root, List *clauses)
543 RestrictInfo *c = lfirst(l);
545 print_expr((Node *) c->clause, root->rtable);
552 print_path(Query *root, Path *path, int indent)
558 switch (nodeTag(path))
590 for (i = 0; i < indent; i++)
592 printf("%s(", ptype);
593 print_relids(path->parent->relids);
594 printf(") rows=%.0f cost=%.2f..%.2f\n",
595 path->parent->rows, path->startup_cost, path->total_cost);
599 for (i = 0; i < indent; i++)
601 printf(" pathkeys: ");
602 print_pathkeys(path->pathkeys, root->rtable);
607 JoinPath *jp = (JoinPath *) path;
609 for (i = 0; i < indent; i++)
611 printf(" clauses: ");
612 print_restrictclauses(root, jp->joinrestrictinfo);
615 if (nodeTag(path) == T_MergePath)
617 MergePath *mp = (MergePath *) path;
619 if (mp->outersortkeys || mp->innersortkeys)
621 for (i = 0; i < indent; i++)
623 printf(" sortouter=%d sortinner=%d\n",
624 ((mp->outersortkeys) ? 1 : 0),
625 ((mp->innersortkeys) ? 1 : 0));
629 print_path(root, jp->outerjoinpath, indent + 1);
630 print_path(root, jp->innerjoinpath, indent + 1);
635 debug_print_rel(Query *root, RelOptInfo *rel)
639 printf("RELOPTINFO (");
640 print_relids(rel->relids);
641 printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
643 if (rel->baserestrictinfo)
645 printf("\tbaserestrictinfo: ");
646 print_restrictclauses(root, rel->baserestrictinfo);
650 foreach(l, rel->joininfo)
652 JoinInfo *j = (JoinInfo *) lfirst(l);
654 printf("\tjoininfo (");
655 print_relids(j->unjoined_relids);
657 print_restrictclauses(root, j->jinfo_restrictinfo);
661 printf("\tpath list:\n");
662 foreach(l, rel->pathlist)
663 print_path(root, lfirst(l), 1);
664 printf("\n\tcheapest startup path:\n");
665 print_path(root, rel->cheapest_startup_path, 1);
666 printf("\n\tcheapest total path:\n");
667 print_path(root, rel->cheapest_total_path, 1);
672 #endif /* OPTIMIZER_DEBUG */