1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* planmain.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routines to plan a single query
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-03-21 06:12:12 +01:00
|
|
|
* What's in a name, anyway? The top-level entry point of the planner/
|
|
|
|
* optimizer is over in planner.c, not here as you might think from the
|
|
|
|
* file name. But this is the main code for planning a basic join operation,
|
|
|
|
* shorn of features like subselects, inheritance, aggregates, grouping,
|
|
|
|
* and so on. (Those are the things planner.c deals with.)
|
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-03-21 06:12:12 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.53 2000/03/21 05:11:58 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2000-03-21 06:12:12 +01:00
|
|
|
#include <sys/types.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/clauses.h"
|
1999-08-21 05:49:17 +02:00
|
|
|
#include "optimizer/cost.h"
|
2000-02-15 21:49:31 +01:00
|
|
|
#include "optimizer/pathnode.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/paths.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "optimizer/planmain.h"
|
|
|
|
#include "optimizer/tlist.h"
|
|
|
|
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
static Plan *subplanner(Query *root, List *flat_tlist, List *qual,
|
|
|
|
double tuple_fraction);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-10-07 06:23:24 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*--------------------
|
1999-02-14 00:22:53 +01:00
|
|
|
* query_planner
|
2000-03-21 06:12:12 +01:00
|
|
|
* Generate a plan for a basic query, which may involve joins but
|
|
|
|
* not any fancier features.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-03-21 06:12:12 +01:00
|
|
|
* tlist is the target list the query should produce (NOT root->targetList!)
|
2000-02-15 21:49:31 +01:00
|
|
|
* qual is the qualification of the query (likewise!)
|
|
|
|
* tuple_fraction is the fraction of tuples we expect will be retrieved
|
|
|
|
*
|
2000-03-21 06:12:12 +01:00
|
|
|
* qual must already have been converted to implicit-AND form.
|
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* Note: the Query node now also includes a query_pathkeys field, which
|
|
|
|
* is both an input and an output of query_planner(). The input value
|
|
|
|
* signals query_planner that the indicated sort order is wanted in the
|
|
|
|
* final output plan. The output value is the actual pathkeys of the
|
|
|
|
* selected path. This might not be the same as what the caller requested;
|
|
|
|
* the caller must do pathkeys_contained_in() to decide whether an
|
|
|
|
* explicit sort is still needed. (The main reason query_pathkeys is a
|
|
|
|
* Query field and not a passed parameter is that the low-level routines
|
|
|
|
* in indxpath.c need to see it.) The pathkeys value passed to query_planner
|
|
|
|
* has not yet been "canonicalized", since the necessary info does not get
|
|
|
|
* computed until subplanner() scans the qual clauses. We canonicalize it
|
|
|
|
* inside subplanner() as soon as that task is done. The output value
|
|
|
|
* will be in canonical form as well.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* tuple_fraction is interpreted as follows:
|
|
|
|
* 0 (or less): expect all tuples to be retrieved (normal case)
|
|
|
|
* 0 < tuple_fraction < 1: expect the given fraction of tuples available
|
|
|
|
* from the plan to be retrieved
|
|
|
|
* tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
|
|
|
|
* expected to be retrieved (ie, a LIMIT specification)
|
|
|
|
* Note that while this routine and its subroutines treat a negative
|
|
|
|
* tuple_fraction the same as 0, union_planner has a different interpretation.
|
1999-08-21 05:49:17 +02:00
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* Returns a query plan.
|
|
|
|
*--------------------
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
Plan *
|
1997-09-08 23:56:23 +02:00
|
|
|
query_planner(Query *root,
|
|
|
|
List *tlist,
|
2000-02-15 21:49:31 +01:00
|
|
|
List *qual,
|
|
|
|
double tuple_fraction)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *constant_qual = NIL;
|
1999-06-12 21:38:30 +02:00
|
|
|
List *var_only_tlist;
|
|
|
|
Plan *subplan;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-10-07 06:23:24 +02:00
|
|
|
* If the query contains no relation references at all, it must be
|
|
|
|
* something like "SELECT 2+2;". Build a trivial "Result" plan.
|
|
|
|
*/
|
|
|
|
if (root->rtable == NIL)
|
|
|
|
{
|
|
|
|
/* If it's not a select, it should have had a target relation... */
|
|
|
|
if (root->commandType != CMD_SELECT)
|
|
|
|
elog(ERROR, "Empty range table for non-SELECT query");
|
|
|
|
|
|
|
|
root->query_pathkeys = NIL; /* signal unordered result */
|
|
|
|
|
|
|
|
/* Make childless Result node to evaluate given tlist. */
|
|
|
|
return (Plan *) make_result(tlist, (Node *) qual, (Plan *) NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pull out any non-variable qual clauses so these can be put in a
|
|
|
|
* toplevel "Result" node, where they will gate execution of the whole
|
|
|
|
* plan (the Result will not invoke its descendant plan unless the
|
|
|
|
* quals are true). Note that any *really* non-variable quals will
|
1999-09-26 04:28:44 +02:00
|
|
|
* have been optimized away by eval_const_expressions(). What we're
|
1999-10-07 06:23:24 +02:00
|
|
|
* mostly interested in here is quals that depend only on outer-level
|
|
|
|
* vars, although if the qual reduces to "WHERE FALSE" this path will
|
|
|
|
* also be taken.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
qual = pull_constant_clauses(qual, &constant_qual);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a target list that consists solely of (resdom var) target
|
|
|
|
* list entries, i.e., contains no arbitrary expressions.
|
1999-10-07 06:23:24 +02:00
|
|
|
*
|
|
|
|
* All subplan nodes will have "flat" (var-only) tlists.
|
|
|
|
*
|
|
|
|
* This implies that all expression evaluations are done at the root
|
|
|
|
* of the plan tree. Once upon a time there was code to try to push
|
|
|
|
* expensive function calls down to lower plan nodes, but that's dead
|
|
|
|
* code and has been for a long time...
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1997-12-22 06:42:25 +01:00
|
|
|
var_only_tlist = flatten_tlist(tlist);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1999-08-22 22:15:04 +02:00
|
|
|
* Choose the best access path and build a plan for it.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
subplan = subplanner(root, var_only_tlist, qual, tuple_fraction);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1999-10-07 06:23:24 +02:00
|
|
|
* Build a result node to control the plan if we have constant quals.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (constant_qual)
|
|
|
|
{
|
1999-10-07 06:23:24 +02:00
|
|
|
/*
|
|
|
|
* The result node will also be responsible for evaluating
|
|
|
|
* the originally requested tlist.
|
|
|
|
*/
|
1999-05-03 02:38:44 +02:00
|
|
|
subplan = (Plan *) make_result(tlist,
|
1998-02-26 05:46:47 +01:00
|
|
|
(Node *) constant_qual,
|
|
|
|
subplan);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
else
|
|
|
|
{
|
1999-10-07 06:23:24 +02:00
|
|
|
/*
|
|
|
|
* Replace the toplevel plan node's flattened target list with the
|
|
|
|
* targetlist given by my caller, so that expressions are evaluated.
|
|
|
|
*/
|
1999-08-22 22:15:04 +02:00
|
|
|
subplan->targetlist = tlist;
|
1998-02-26 05:46:47 +01:00
|
|
|
}
|
|
|
|
|
1999-10-07 06:23:24 +02:00
|
|
|
return subplan;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* subplanner
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
|
|
|
* Subplanner creates an entire plan consisting of joins and scans
|
|
|
|
* for processing a single level of attributes.
|
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* flat_tlist is the flattened target list
|
|
|
|
* qual is the qualification to be satisfied
|
|
|
|
* tuple_fraction is the fraction of tuples we expect will be retrieved
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* See query_planner() comments about the interpretation of tuple_fraction.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* Returns a subplan.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static Plan *
|
1997-09-08 23:56:23 +02:00
|
|
|
subplanner(Query *root,
|
|
|
|
List *flat_tlist,
|
2000-02-15 21:49:31 +01:00
|
|
|
List *qual,
|
|
|
|
double tuple_fraction)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *final_rel;
|
2000-02-15 21:49:31 +01:00
|
|
|
Path *cheapestpath;
|
|
|
|
Path sort_path; /* dummy for result of cost_sort */
|
|
|
|
Path *presortedpath;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the targetlist and qualification, adding entries to
|
1998-09-01 06:40:42 +02:00
|
|
|
* base_rel_list as relation references are found (e.g., in the
|
2000-02-15 21:49:31 +01:00
|
|
|
* qualification, the targetlist, etc.). Restrict and join clauses
|
|
|
|
* are added to appropriate lists belonging to the mentioned relations,
|
|
|
|
* and we also build lists of equijoined keys for pathkey construction.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1998-08-10 04:26:40 +02:00
|
|
|
root->base_rel_list = NIL;
|
|
|
|
root->join_rel_list = NIL;
|
2000-02-15 21:49:31 +01:00
|
|
|
root->equi_key_list = NIL;
|
1998-08-10 04:26:40 +02:00
|
|
|
|
1999-02-14 00:22:53 +01:00
|
|
|
make_var_only_tlist(root, flat_tlist);
|
|
|
|
add_restrict_and_join_to_rels(root, qual);
|
1999-10-07 06:23:24 +02:00
|
|
|
add_missing_rels_to_query(root);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
|
|
|
* We should now have all the pathkey equivalence sets built,
|
|
|
|
* so it's now possible to convert the requested query_pathkeys
|
|
|
|
* to canonical form.
|
|
|
|
*/
|
|
|
|
root->query_pathkeys = canonicalize_pathkeys(root, root->query_pathkeys);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ready to do the primary planning.
|
|
|
|
*/
|
2000-02-07 05:41:04 +01:00
|
|
|
final_rel = make_one_rel(root);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-10-07 06:23:24 +02:00
|
|
|
if (! final_rel)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We expect to end up here for a trivial INSERT ... VALUES query
|
|
|
|
* (which will have a target relation, so it gets past query_planner's
|
|
|
|
* check for empty range table; but the target rel is unreferenced
|
|
|
|
* and not marked inJoinSet, so we find there is nothing to join).
|
|
|
|
*
|
|
|
|
* It's also possible to get here if the query was rewritten by the
|
|
|
|
* rule processor (creating rangetable entries not marked inJoinSet)
|
|
|
|
* but the rules either did nothing or were simplified to nothing
|
|
|
|
* by constant-expression folding. So, don't complain.
|
|
|
|
*/
|
|
|
|
root->query_pathkeys = NIL; /* signal unordered result */
|
|
|
|
|
|
|
|
/* Make childless Result node to evaluate given tlist. */
|
|
|
|
return (Plan *) make_result(flat_tlist, (Node *) qual, (Plan *) NULL);
|
|
|
|
}
|
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
#ifdef NOT_USED /* fix xfunc */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform Predicate Migration on each path, to optimize and correctly
|
|
|
|
* assess the cost of each before choosing the cheapest one. -- JMH,
|
|
|
|
* 11/16/92
|
|
|
|
*
|
|
|
|
* Needn't do so if the top rel is pruneable: that means there's no
|
|
|
|
* expensive functions left to pull up. -- JMH, 11/22/92
|
|
|
|
*/
|
|
|
|
if (XfuncMode != XFUNC_OFF && XfuncMode != XFUNC_NOPM &&
|
1998-08-10 04:26:40 +02:00
|
|
|
XfuncMode != XFUNC_NOPULL && !final_rel->pruneable)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *pathnode;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-10 04:26:40 +02:00
|
|
|
foreach(pathnode, final_rel->pathlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (xfunc_do_predmig((Path *) lfirst(pathnode)))
|
2000-02-15 21:49:31 +01:00
|
|
|
set_cheapest(final_rel);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-08-21 05:49:17 +02:00
|
|
|
/*
|
2000-02-15 21:49:31 +01:00
|
|
|
* Now that we have an estimate of the final rel's size, we can convert
|
|
|
|
* a tuple_fraction specified as an absolute count (ie, a LIMIT option)
|
|
|
|
* into a fraction of the total tuples.
|
|
|
|
*/
|
|
|
|
if (tuple_fraction >= 1.0)
|
|
|
|
tuple_fraction /= final_rel->rows;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the cheapest path, independently of any ordering
|
|
|
|
* considerations. We do, however, take into account whether the
|
|
|
|
* whole plan is expected to be evaluated or not.
|
|
|
|
*/
|
|
|
|
if (tuple_fraction <= 0.0 || tuple_fraction >= 1.0)
|
|
|
|
cheapestpath = final_rel->cheapest_total_path;
|
|
|
|
else
|
|
|
|
cheapestpath =
|
|
|
|
get_cheapest_fractional_path_for_pathkeys(final_rel->pathlist,
|
|
|
|
NIL,
|
|
|
|
tuple_fraction);
|
|
|
|
|
|
|
|
Assert(cheapestpath != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Select the best path and create a subplan to execute it.
|
1999-08-21 05:49:17 +02:00
|
|
|
*
|
|
|
|
* If no special sort order is wanted, or if the cheapest path is
|
2000-02-15 21:49:31 +01:00
|
|
|
* already appropriately ordered, we use the cheapest path found above.
|
1999-08-21 05:49:17 +02:00
|
|
|
*/
|
|
|
|
if (root->query_pathkeys == NIL ||
|
|
|
|
pathkeys_contained_in(root->query_pathkeys,
|
2000-02-15 21:49:31 +01:00
|
|
|
cheapestpath->pathkeys))
|
1999-08-23 01:56:45 +02:00
|
|
|
{
|
2000-02-15 21:49:31 +01:00
|
|
|
root->query_pathkeys = cheapestpath->pathkeys;
|
|
|
|
return create_plan(root, cheapestpath);
|
1999-08-23 01:56:45 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-08-21 05:49:17 +02:00
|
|
|
* Otherwise, look to see if we have an already-ordered path that is
|
2000-02-15 21:49:31 +01:00
|
|
|
* cheaper than doing an explicit sort on the cheapest-total-cost path.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
cheapestpath = final_rel->cheapest_total_path;
|
|
|
|
cost_sort(&sort_path, root->query_pathkeys,
|
|
|
|
final_rel->rows, final_rel->width);
|
|
|
|
sort_path.startup_cost += cheapestpath->total_cost;
|
|
|
|
sort_path.total_cost += cheapestpath->total_cost;
|
|
|
|
|
|
|
|
presortedpath =
|
|
|
|
get_cheapest_fractional_path_for_pathkeys(final_rel->pathlist,
|
|
|
|
root->query_pathkeys,
|
|
|
|
tuple_fraction);
|
|
|
|
if (presortedpath)
|
1999-08-21 05:49:17 +02:00
|
|
|
{
|
2000-02-15 21:49:31 +01:00
|
|
|
if (compare_fractional_path_costs(presortedpath, &sort_path,
|
|
|
|
tuple_fraction) <= 0)
|
1999-08-21 05:49:17 +02:00
|
|
|
{
|
|
|
|
/* Found a better presorted path, use it */
|
2000-02-15 21:49:31 +01:00
|
|
|
root->query_pathkeys = presortedpath->pathkeys;
|
|
|
|
return create_plan(root, presortedpath);
|
1999-08-21 05:49:17 +02:00
|
|
|
}
|
|
|
|
/* otherwise, doing it the hard way is still cheaper */
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-10-07 06:23:24 +02:00
|
|
|
/*
|
2000-02-15 21:49:31 +01:00
|
|
|
* Nothing for it but to sort the cheapest-total-cost path --- but we let
|
|
|
|
* the caller do that. union_planner has to be able to add a sort node
|
1999-08-23 01:56:45 +02:00
|
|
|
* anyway, so no need for extra code here. (Furthermore, the given
|
2000-02-15 21:49:31 +01:00
|
|
|
* pathkeys might involve something we can't compute here, such as an
|
|
|
|
* aggregate function...)
|
1999-08-21 05:49:17 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
root->query_pathkeys = cheapestpath->pathkeys;
|
|
|
|
return create_plan(root, cheapestpath);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|