]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/planmain.c
Re-implement LIMIT/OFFSET as a plan node type, instead of a hack in
[postgresql] / src / backend / optimizer / plan / planmain.c
1 /*-------------------------------------------------------------------------
2  *
3  * planmain.c
4  *        Routines to plan a single query
5  *
6  * What's in a name, anyway?  The top-level entry point of the planner/
7  * optimizer is over in planner.c, not here as you might think from the
8  * file name.  But this is the main code for planning a basic join operation,
9  * shorn of features like subselects, inheritance, aggregates, grouping,
10  * and so on.  (Those are the things planner.c deals with.)
11  *
12  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  *
16  * IDENTIFICATION
17  *        $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.61 2000/10/05 19:11:29 tgl Exp $
18  *
19  *-------------------------------------------------------------------------
20  */
21 #include "postgres.h"
22
23 #include <sys/types.h>
24
25 #include "optimizer/clauses.h"
26 #include "optimizer/cost.h"
27 #include "optimizer/pathnode.h"
28 #include "optimizer/paths.h"
29 #include "optimizer/planmain.h"
30 #include "optimizer/tlist.h"
31 #include "parser/parsetree.h"
32 #include "utils/memutils.h"
33
34
35 static Plan *subplanner(Query *root, List *flat_tlist,
36                                                 double tuple_fraction);
37
38
39 /*--------------------
40  * query_planner
41  *        Generate a plan for a basic query, which may involve joins but
42  *        not any fancier features.
43  *
44  * tlist is the target list the query should produce (NOT root->targetList!)
45  * tuple_fraction is the fraction of tuples we expect will be retrieved
46  *
47  * Note: the Query node now also includes a query_pathkeys field, which
48  * is both an input and an output of query_planner().  The input value
49  * signals query_planner that the indicated sort order is wanted in the
50  * final output plan.  The output value is the actual pathkeys of the
51  * selected path.  This might not be the same as what the caller requested;
52  * the caller must do pathkeys_contained_in() to decide whether an
53  * explicit sort is still needed.  (The main reason query_pathkeys is a
54  * Query field and not a passed parameter is that the low-level routines
55  * in indxpath.c need to see it.)  The pathkeys value passed to query_planner
56  * has not yet been "canonicalized", since the necessary info does not get
57  * computed until subplanner() scans the qual clauses.  We canonicalize it
58  * inside subplanner() as soon as that task is done.  The output value
59  * will be in canonical form as well.
60  *
61  * tuple_fraction is interpreted as follows:
62  *        0 (or less): expect all tuples to be retrieved (normal case)
63  *        0 < tuple_fraction < 1: expect the given fraction of tuples available
64  *              from the plan to be retrieved
65  *        tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
66  *              expected to be retrieved (ie, a LIMIT specification)
67  * Note that while this routine and its subroutines treat a negative
68  * tuple_fraction the same as 0, union_planner has a different interpretation.
69  *
70  * Returns a query plan.
71  *--------------------
72  */
73 Plan *
74 query_planner(Query *root,
75                           List *tlist,
76                           double tuple_fraction)
77 {
78         List       *constant_quals;
79         List       *var_only_tlist;
80         Plan       *subplan;
81
82         /*
83          * If the query has an empty join tree, then it's something easy like
84          * "SELECT 2+2;" or "INSERT ... VALUES()".  Fall through quickly.
85          */
86         if (root->jointree->fromlist == NIL)
87         {
88                 root->query_pathkeys = NIL;             /* signal unordered result */
89
90                 /* Make childless Result node to evaluate given tlist. */
91                 return (Plan *) make_result(tlist, root->jointree->quals,
92                                                                         (Plan *) NULL);
93         }
94
95         /*
96          * Pull out any non-variable WHERE clauses so these can be put in a
97          * toplevel "Result" node, where they will gate execution of the whole
98          * plan (the Result will not invoke its descendant plan unless the
99          * quals are true).  Note that any *really* non-variable quals will
100          * have been optimized away by eval_const_expressions().  What we're
101          * mostly interested in here is quals that depend only on outer-level
102          * vars, although if the qual reduces to "WHERE FALSE" this path will
103          * also be taken.
104          */
105         root->jointree->quals = (Node *)
106                 pull_constant_clauses((List *) root->jointree->quals,
107                                                           &constant_quals);
108
109         /*
110          * Create a target list that consists solely of (resdom var) target
111          * list entries, i.e., contains no arbitrary expressions.
112          *
113          * All subplan nodes will have "flat" (var-only) tlists.
114          *
115          * This implies that all expression evaluations are done at the root of
116          * the plan tree.  Once upon a time there was code to try to push
117          * expensive function calls down to lower plan nodes, but that's dead
118          * code and has been for a long time...
119          */
120         var_only_tlist = flatten_tlist(tlist);
121
122         /*
123          * Choose the best access path and build a plan for it.
124          */
125         subplan = subplanner(root, var_only_tlist, tuple_fraction);
126
127         /*
128          * Build a result node to control the plan if we have constant quals.
129          */
130         if (constant_quals)
131         {
132
133                 /*
134                  * The result node will also be responsible for evaluating the
135                  * originally requested tlist.
136                  */
137                 subplan = (Plan *) make_result(tlist,
138                                                                            (Node *) constant_quals,
139                                                                            subplan);
140         }
141         else
142         {
143
144                 /*
145                  * Replace the toplevel plan node's flattened target list with the
146                  * targetlist given by my caller, so that expressions are
147                  * evaluated.
148                  */
149                 subplan->targetlist = tlist;
150         }
151
152         return subplan;
153 }
154
155 /*
156  * subplanner
157  *
158  *       Subplanner creates an entire plan consisting of joins and scans
159  *       for processing a single level of attributes.
160  *
161  * flat_tlist is the flattened target list
162  * tuple_fraction is the fraction of tuples we expect will be retrieved
163  *
164  * See query_planner() comments about the interpretation of tuple_fraction.
165  *
166  * Returns a subplan.
167  */
168 static Plan *
169 subplanner(Query *root,
170                    List *flat_tlist,
171                    double tuple_fraction)
172 {
173         List       *joined_rels;
174         List       *brel;
175         RelOptInfo *final_rel;
176         Plan       *resultplan;
177         Path       *cheapestpath;
178         Path       *presortedpath;
179
180         /*
181          * Examine the targetlist and qualifications, adding entries to
182          * base_rel_list as relation references are found (e.g., in the
183          * qualification, the targetlist, etc.).  Restrict and join clauses
184          * are added to appropriate lists belonging to the mentioned
185          * relations.  We also build lists of equijoined keys for pathkey
186          * construction.
187          */
188         root->base_rel_list = NIL;
189         root->join_rel_list = NIL;
190         root->equi_key_list = NIL;
191
192         build_base_rel_tlists(root, flat_tlist);
193
194         (void) distribute_quals_to_rels(root, (Node *) root->jointree);
195
196         /*
197          * Make sure we have RelOptInfo nodes for all relations to be joined.
198          */
199         joined_rels = add_missing_rels_to_query(root, (Node *) root->jointree);
200
201         /*
202          * Check that the join tree includes all the base relations used in
203          * the query --- otherwise, the parser or rewriter messed up.
204          */
205         foreach(brel, root->base_rel_list)
206         {
207                 RelOptInfo *baserel = (RelOptInfo *) lfirst(brel);
208                 int             relid = lfirsti(baserel->relids);
209
210                 if (! ptrMember(baserel, joined_rels))
211                         elog(ERROR, "Internal error: no jointree entry for rel %s (%d)",
212                                  rt_fetch(relid, root->rtable)->eref->relname, relid);
213         }
214
215         /*
216          * Use the completed lists of equijoined keys to deduce any implied
217          * but unstated equalities (for example, A=B and B=C imply A=C).
218          */
219         generate_implied_equalities(root);
220
221         /*
222          * We should now have all the pathkey equivalence sets built, so it's
223          * now possible to convert the requested query_pathkeys to canonical
224          * form.
225          */
226         root->query_pathkeys = canonicalize_pathkeys(root, root->query_pathkeys);
227
228         /*
229          * Ready to do the primary planning.
230          */
231         final_rel = make_one_rel(root);
232
233         if (!final_rel)
234                 elog(ERROR, "subplanner: failed to construct a relation");
235
236 #ifdef NOT_USED                                 /* fix xfunc */
237
238         /*
239          * Perform Predicate Migration on each path, to optimize and correctly
240          * assess the cost of each before choosing the cheapest one. -- JMH,
241          * 11/16/92
242          *
243          * Needn't do so if the top rel is pruneable: that means there's no
244          * expensive functions left to pull up.  -- JMH, 11/22/92
245          */
246         if (XfuncMode != XFUNC_OFF && XfuncMode != XFUNC_NOPM &&
247                 XfuncMode != XFUNC_NOPULL && !final_rel->pruneable)
248         {
249                 List       *pathnode;
250
251                 foreach(pathnode, final_rel->pathlist)
252                 {
253                         if (xfunc_do_predmig((Path *) lfirst(pathnode)))
254                                 set_cheapest(final_rel);
255                 }
256         }
257 #endif
258
259         /*
260          * Now that we have an estimate of the final rel's size, we can
261          * convert a tuple_fraction specified as an absolute count (ie, a
262          * LIMIT option) into a fraction of the total tuples.
263          */
264         if (tuple_fraction >= 1.0)
265                 tuple_fraction /= final_rel->rows;
266
267         /*
268          * Determine the cheapest path, independently of any ordering
269          * considerations.      We do, however, take into account whether the
270          * whole plan is expected to be evaluated or not.
271          */
272         if (tuple_fraction <= 0.0 || tuple_fraction >= 1.0)
273                 cheapestpath = final_rel->cheapest_total_path;
274         else
275                 cheapestpath =
276                         get_cheapest_fractional_path_for_pathkeys(final_rel->pathlist,
277                                                                                                           NIL,
278                                                                                                           tuple_fraction);
279
280         Assert(cheapestpath != NULL);
281
282         /*
283          * Select the best path and create a subplan to execute it.
284          *
285          * If no special sort order is wanted, or if the cheapest path is already
286          * appropriately ordered, we use the cheapest path found above.
287          */
288         if (root->query_pathkeys == NIL ||
289                 pathkeys_contained_in(root->query_pathkeys,
290                                                           cheapestpath->pathkeys))
291         {
292                 root->query_pathkeys = cheapestpath->pathkeys;
293                 resultplan = create_plan(root, cheapestpath);
294                 goto plan_built;
295         }
296
297         /*
298          * Otherwise, look to see if we have an already-ordered path that is
299          * cheaper than doing an explicit sort on the cheapest-total-cost
300          * path.
301          */
302         cheapestpath = final_rel->cheapest_total_path;
303         presortedpath =
304                 get_cheapest_fractional_path_for_pathkeys(final_rel->pathlist,
305                                                                                                   root->query_pathkeys,
306                                                                                                   tuple_fraction);
307         if (presortedpath)
308         {
309                 Path            sort_path;      /* dummy for result of cost_sort */
310
311                 cost_sort(&sort_path, root->query_pathkeys,
312                                   final_rel->rows, final_rel->width);
313                 sort_path.startup_cost += cheapestpath->total_cost;
314                 sort_path.total_cost += cheapestpath->total_cost;
315                 if (compare_fractional_path_costs(presortedpath, &sort_path,
316                                                                                   tuple_fraction) <= 0)
317                 {
318                         /* Presorted path is cheaper, use it */
319                         root->query_pathkeys = presortedpath->pathkeys;
320                         resultplan = create_plan(root, presortedpath);
321                         goto plan_built;
322                 }
323                 /* otherwise, doing it the hard way is still cheaper */
324         }
325
326         /*
327          * Nothing for it but to sort the cheapest-total-cost path --- but we
328          * let the caller do that.      union_planner has to be able to add a sort
329          * node anyway, so no need for extra code here.  (Furthermore, the
330          * given pathkeys might involve something we can't compute here, such
331          * as an aggregate function...)
332          */
333         root->query_pathkeys = cheapestpath->pathkeys;
334         resultplan = create_plan(root, cheapestpath);
335
336 plan_built:
337
338         return resultplan;
339 }