]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/planner.c
New cost model for planning, incorporating a penalty for random page
[postgresql] / src / backend / optimizer / plan / planner.c
1 /*-------------------------------------------------------------------------
2  *
3  * planner.c
4  *        The query optimizer external interface.
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.75 2000/02/15 20:49:18 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include <sys/types.h>
16
17 #include "postgres.h"
18
19 #include "access/genam.h"
20 #include "access/heapam.h"
21 #include "catalog/pg_type.h"
22 #include "executor/executor.h"
23 #include "nodes/makefuncs.h"
24 #include "optimizer/clauses.h"
25 #include "optimizer/internal.h"
26 #include "optimizer/paths.h"
27 #include "optimizer/planmain.h"
28 #include "optimizer/planner.h"
29 #include "optimizer/prep.h"
30 #include "optimizer/subselect.h"
31 #include "optimizer/tlist.h"
32 #include "optimizer/var.h"
33 #include "parser/parse_expr.h"
34 #include "parser/parse_oper.h"
35 #include "utils/builtins.h"
36 #include "utils/lsyscache.h"
37 #include "utils/syscache.h"
38
39 static List *make_subplanTargetList(Query *parse, List *tlist,
40                                                                         AttrNumber **groupColIdx);
41 static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
42                                                         List *groupClause, AttrNumber *grpColIdx,
43                                                         bool is_presorted, Plan *subplan);
44 static Plan *make_sortplan(List *tlist, List *sortcls, Plan *plannode);
45
46 /*****************************************************************************
47  *
48  *         Query optimizer entry point
49  *
50  *****************************************************************************/
51 Plan *
52 planner(Query *parse)
53 {
54         Plan       *result_plan;
55
56         /* Initialize state for subselects */
57         PlannerQueryLevel = 1;
58         PlannerInitPlan = NULL;
59         PlannerParamVar = NULL;
60         PlannerPlanId = 0;
61
62         transformKeySetQuery(parse);
63
64         result_plan = union_planner(parse, -1.0 /* default case */);
65
66         Assert(PlannerQueryLevel == 1);
67         if (PlannerPlanId > 0)
68         {
69                 result_plan->initPlan = PlannerInitPlan;
70                 (void) SS_finalize_plan(result_plan);
71         }
72         result_plan->nParamExec = length(PlannerParamVar);
73
74         set_plan_references(result_plan);
75
76         return result_plan;
77 }
78
79 /*--------------------
80  * union_planner
81  *        Invokes the planner on union-type queries (both regular UNIONs and
82  *        appends produced by inheritance), recursing if necessary to get them
83  *        all, then processes normal plans.
84  *
85  * parse is the querytree produced by the parser & rewriter.
86  * tuple_fraction is the fraction of tuples we expect will be retrieved
87  *
88  * tuple_fraction is interpreted as follows:
89  *    < 0: determine fraction by inspection of query (normal case)
90  *    0: expect all tuples to be retrieved
91  *        0 < tuple_fraction < 1: expect the given fraction of tuples available
92  *              from the plan to be retrieved
93  *        tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
94  *              expected to be retrieved (ie, a LIMIT specification)
95  * The normal case is to pass -1, but some callers pass values >= 0 to
96  * override this routine's determination of the appropriate fraction.
97  *
98  * Returns a query plan.
99  *--------------------
100  */
101 Plan *
102 union_planner(Query *parse,
103                           double tuple_fraction)
104 {
105         List       *tlist = parse->targetList;
106         List       *rangetable = parse->rtable;
107         Plan       *result_plan = (Plan *) NULL;
108         AttrNumber *groupColIdx = NULL;
109         List       *current_pathkeys = NIL;
110         List       *group_pathkeys;
111         List       *sort_pathkeys;
112         Index           rt_index;
113
114         /*
115          * A HAVING clause without aggregates is equivalent to a WHERE clause
116          * (except it can only refer to grouped fields).  If there are no
117          * aggs anywhere in the query, then we don't want to create an Agg
118          * plan node, so merge the HAVING condition into WHERE.  (We used to
119          * consider this an error condition, but it seems to be legal SQL.)
120          */
121         if (parse->havingQual != NULL && ! parse->hasAggs)
122         {
123                 if (parse->qual == NULL)
124                         parse->qual = parse->havingQual;
125                 else
126                         parse->qual = (Node *) make_andclause(lappend(lcons(parse->qual,
127                                                                                                                                 NIL),
128                                                                                                                   parse->havingQual));
129                 parse->havingQual = NULL;
130         }
131
132         /*
133          * Simplify constant expressions in targetlist and quals.
134          *
135          * Note that at this point the qual has not yet been converted to
136          * implicit-AND form, so we can apply eval_const_expressions directly.
137          * Also note that we need to do this before SS_process_sublinks,
138          * because that routine inserts bogus "Const" nodes.
139          */
140         tlist = (List *) eval_const_expressions((Node *) tlist);
141         parse->qual = eval_const_expressions(parse->qual);
142         parse->havingQual = eval_const_expressions(parse->havingQual);
143
144
145         if (parse->unionClause)
146         {
147                 result_plan = (Plan *) plan_union_queries(parse);
148                 /* XXX do we need to do this? bjm 12/19/97 */
149                 tlist = preprocess_targetlist(tlist,
150                                                                           parse->commandType,
151                                                                           parse->resultRelation,
152                                                                           parse->rtable);
153                 /*
154                  * We leave current_pathkeys NIL indicating we do not know sort order.
155                  * Actually, for a normal UNION we have done an explicit sort; ought
156                  * to change interface to plan_union_queries to pass that info back!
157                  */
158
159                 /* Calculate pathkeys that represent grouping/ordering requirements */
160                 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
161                                                                                                            tlist);
162                 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
163                                                                                                           tlist);
164         }
165         else if ((rt_index = first_inherit_rt_entry(rangetable)) != -1)
166         {
167                 List       *sub_tlist;
168
169                 /*
170                  * Generate appropriate target list for subplan; may be different
171                  * from tlist if grouping or aggregation is needed.
172                  */
173                 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
174
175                 /*
176                  * Recursively plan the subqueries needed for inheritance
177                  */
178                 result_plan = (Plan *) plan_inherit_queries(parse, sub_tlist,
179                                                                                                         rt_index);
180
181                 /*
182                  * Fix up outer target list.  NOTE: unlike the case for non-inherited
183                  * query, we pass the unfixed tlist to subplans, which do their own
184                  * fixing.  But we still want to fix the outer target list afterwards.
185                  * I *think* this is correct --- doing the fix before recursing is
186                  * definitely wrong, because preprocess_targetlist() will do the
187                  * wrong thing if invoked twice on the same list. Maybe that is a bug?
188                  * tgl 6/6/99
189                  */
190                 tlist = preprocess_targetlist(tlist,
191                                                                           parse->commandType,
192                                                                           parse->resultRelation,
193                                                                           parse->rtable);
194
195                 if (parse->rowMark != NULL)
196                         elog(ERROR, "SELECT FOR UPDATE is not supported for inherit queries");
197                 /*
198                  * We leave current_pathkeys NIL indicating we do not know sort order
199                  * of the Append-ed results.
200                  */
201
202                 /* Calculate pathkeys that represent grouping/ordering requirements */
203                 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
204                                                                                                            tlist);
205                 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
206                                                                                                           tlist);
207         }
208         else
209         {
210                 List       *sub_tlist;
211
212                 /* Preprocess targetlist in case we are inside an INSERT/UPDATE. */
213                 tlist = preprocess_targetlist(tlist,
214                                                                           parse->commandType,
215                                                                           parse->resultRelation,
216                                                                           parse->rtable);
217
218                 /*
219                  * Add row-mark targets for UPDATE (should this be done in
220                  * preprocess_targetlist?)
221                  */
222                 if (parse->rowMark != NULL)
223                 {
224                         List       *l;
225
226                         foreach(l, parse->rowMark)
227                         {
228                                 RowMark    *rowmark = (RowMark *) lfirst(l);
229                                 TargetEntry *ctid;
230                                 Resdom     *resdom;
231                                 Var                *var;
232                                 char       *resname;
233
234                                 if (!(rowmark->info & ROW_MARK_FOR_UPDATE))
235                                         continue;
236
237                                 resname = (char *) palloc(32);
238                                 sprintf(resname, "ctid%u", rowmark->rti);
239                                 resdom = makeResdom(length(tlist) + 1,
240                                                                         TIDOID,
241                                                                         -1,
242                                                                         resname,
243                                                                         0,
244                                                                         0,
245                                                                         true);
246
247                                 var = makeVar(rowmark->rti, -1, TIDOID, -1, 0);
248
249                                 ctid = makeTargetEntry(resdom, (Node *) var);
250                                 tlist = lappend(tlist, ctid);
251                         }
252                 }
253
254                 /*
255                  * Generate appropriate target list for subplan; may be different
256                  * from tlist if grouping or aggregation is needed.
257                  */
258                 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
259
260                 /* Calculate pathkeys that represent grouping/ordering requirements */
261                 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
262                                                                                                            tlist);
263                 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
264                                                                                                           tlist);
265
266                 /*
267                  * Figure out whether we need a sorted result from query_planner.
268                  *
269                  * If we have a GROUP BY clause, then we want a result sorted
270                  * properly for grouping.  Otherwise, if there is an ORDER BY clause,
271                  * we want to sort by the ORDER BY clause.  (Note: if we have both,
272                  * and ORDER BY is a superset of GROUP BY, it would be tempting to
273                  * request sort by ORDER BY --- but that might just leave us failing
274                  * to exploit an available sort order at all.  Needs more thought...)
275                  */
276                 if (parse->groupClause)
277                         parse->query_pathkeys = group_pathkeys;
278                 else if (parse->sortClause)
279                         parse->query_pathkeys = sort_pathkeys;
280                 else
281                         parse->query_pathkeys = NIL;
282
283                 /*
284                  * Figure out whether we expect to retrieve all the tuples that the
285                  * plan can generate, or to stop early due to a LIMIT or other
286                  * factors.  If the caller passed a value >= 0, believe that value,
287                  * else do our own examination of the query context.
288                  */
289                 if (tuple_fraction < 0.0)
290                 {
291                         /* Initial assumption is we need all the tuples */
292                         tuple_fraction = 0.0;
293                         /*
294                          * Check for a LIMIT.
295                          *
296                          * For now, we deliberately ignore the OFFSET clause, so that
297                          * queries with the same LIMIT and different OFFSETs will get
298                          * the same queryplan and therefore generate consistent results
299                          * (to the extent the planner can guarantee that, anyway).
300                          * XXX Perhaps it would be better to use the OFFSET too, and tell
301                          * users to specify ORDER BY if they want consistent results
302                          * across different LIMIT queries.
303                          */
304                         if (parse->limitCount != NULL)
305                         {
306                                 if (IsA(parse->limitCount, Const))
307                                 {
308                                         Const      *ccount = (Const *) parse->limitCount;
309                                         tuple_fraction = (double) ((int) (ccount->constvalue));
310                                         /* the constant can legally be either 0 ("ALL") or a
311                                          * positive integer; either is consistent with our
312                                          * conventions for tuple_fraction.
313                                          */
314                                 }
315                                 else
316                                 {
317                                         /* It's a PARAM ... don't know exactly what the limit
318                                          * will be, but for lack of a better idea assume 10%
319                                          * of the plan's result is wanted.
320                                          */
321                                         tuple_fraction = 0.10;
322                                 }
323                         }
324                         /*
325                          * Check for a retrieve-into-portal, ie DECLARE CURSOR.
326                          *
327                          * We have no real idea how many tuples the user will ultimately
328                          * FETCH from a cursor, but it seems a good bet that he doesn't
329                          * want 'em all.  Optimize for 10% retrieval (you gotta better
330                          * number?)
331                          */
332                         if (parse->isPortal)
333                                 tuple_fraction = 0.10;
334                 }
335                 /*
336                  * Adjust tuple_fraction if we see that we are going to apply
337                  * grouping/aggregation/etc.  This is not overridable by the
338                  * caller, since it reflects plan actions that this routine
339                  * will certainly take, not assumptions about context.
340                  */
341                 if (parse->groupClause)
342                 {
343                         /*
344                          * In GROUP BY mode, we have the little problem that we don't
345                          * really know how many input tuples will be needed to make a
346                          * group, so we can't translate an output LIMIT count into an
347                          * input count.  For lack of a better idea, assume 10% of the
348                          * input data will be processed if there is any output limit.
349                          */
350                         if (tuple_fraction > 0.0)
351                                 tuple_fraction = 0.10;
352                         /*
353                          * If both GROUP BY and ORDER BY are specified, we will need
354                          * two levels of sort --- and, therefore, certainly need to
355                          * read all the input tuples --- unless ORDER BY is a subset
356                          * of GROUP BY.  (Although we are comparing non-canonicalized
357                          * pathkeys here, it should be OK since they will both contain
358                          * only single-element sublists at this point.  See pathkeys.c.)
359                          */
360                         if (parse->groupClause && parse->sortClause &&
361                                 ! pathkeys_contained_in(sort_pathkeys, group_pathkeys))
362                                 tuple_fraction = 0.0;
363                 }
364                 else if (parse->hasAggs)
365                 {
366                         /* Ungrouped aggregate will certainly want all the input tuples. */
367                         tuple_fraction = 0.0;
368                 }
369                 else if (parse->distinctClause)
370                 {
371                         /*
372                          * SELECT DISTINCT, like GROUP, will absorb an unpredictable
373                          * number of input tuples per output tuple.  So, fall back to
374                          * our same old 10% default...
375                          */
376                         if (tuple_fraction > 0.0)
377                                 tuple_fraction = 0.10;
378                 }
379
380                 /* Generate the (sub) plan */
381                 result_plan = query_planner(parse,
382                                                                         sub_tlist,
383                                                                         (List *) parse->qual,
384                                                                         tuple_fraction);
385
386                 /* query_planner returns actual sort order (which is not
387                  * necessarily what we requested) in query_pathkeys.
388                  */
389                 current_pathkeys = parse->query_pathkeys;
390         }
391
392         /* query_planner returns NULL if it thinks plan is bogus */
393         if (! result_plan)
394                 elog(ERROR, "union_planner: failed to create plan");
395
396         /*
397          * We couldn't canonicalize group_pathkeys and sort_pathkeys before
398          * running query_planner(), so do it now.
399          */
400         group_pathkeys = canonicalize_pathkeys(parse, group_pathkeys);
401         sort_pathkeys = canonicalize_pathkeys(parse, sort_pathkeys);
402
403         /*
404          * If we have a GROUP BY clause, insert a group node (plus the
405          * appropriate sort node, if necessary).
406          */
407         if (parse->groupClause)
408         {
409                 bool            tuplePerGroup;
410                 List       *group_tlist;
411                 bool            is_sorted;
412
413                 /*
414                  * Decide whether how many tuples per group the Group node needs
415                  * to return. (Needs only one tuple per group if no aggregate is
416                  * present. Otherwise, need every tuple from the group to do the
417                  * aggregation.)  Note tuplePerGroup is named backwards :-(
418                  */
419                 tuplePerGroup = parse->hasAggs;
420
421                 /*
422                  * If there are aggregates then the Group node should just return
423                  * the same set of vars as the subplan did (but we can exclude
424                  * any GROUP BY expressions).  If there are no aggregates
425                  * then the Group node had better compute the final tlist.
426                  */
427                 if (parse->hasAggs)
428                         group_tlist = flatten_tlist(result_plan->targetlist);
429                 else
430                         group_tlist = tlist;
431
432                 /*
433                  * Figure out whether the path result is already ordered the way we
434                  * need it --- if so, no need for an explicit sort step.
435                  */
436                 if (pathkeys_contained_in(group_pathkeys, current_pathkeys))
437                 {
438                         is_sorted = true;       /* no sort needed now */
439                         /* current_pathkeys remains unchanged */
440                 }
441                 else
442                 {
443                         /* We will need to do an explicit sort by the GROUP BY clause.
444                          * make_groupplan will do the work, but set current_pathkeys
445                          * to indicate the resulting order.
446                          */
447                         is_sorted = false;
448                         current_pathkeys = group_pathkeys;
449                 }
450
451                 result_plan = make_groupplan(group_tlist,
452                                                                          tuplePerGroup,
453                                                                          parse->groupClause,
454                                                                          groupColIdx,
455                                                                          is_sorted,
456                                                                          result_plan);
457         }
458
459         /*
460          * If we have a HAVING clause, do the necessary things with it.
461          * This code should parallel query_planner()'s initial processing
462          * of the WHERE clause.
463          */
464         if (parse->havingQual)
465         {
466                 /* Convert the havingQual to implicit-AND normal form */
467                 parse->havingQual = (Node *)
468                         canonicalize_qual((Expr *) parse->havingQual, true);
469
470                 /* Replace uplevel Vars with Params */
471                 if (PlannerQueryLevel > 1)
472                         parse->havingQual = SS_replace_correlation_vars(parse->havingQual);
473
474                 if (parse->hasSubLinks)
475                 {
476                         /* Expand SubLinks to SubPlans */
477                         parse->havingQual = SS_process_sublinks(parse->havingQual);
478                         /* Check for ungrouped variables passed to subplans */
479                         check_subplans_for_ungrouped_vars(parse->havingQual,
480                                                                                           parse,
481                                                                                           parse->targetList);
482                 }
483         }
484
485         /*
486          * If aggregate is present, insert the Agg node
487          *
488          * HAVING clause, if any, becomes qual of the Agg node
489          */
490         if (parse->hasAggs)
491         {
492                 result_plan = (Plan *) make_agg(tlist,
493                                                                                 (List *) parse->havingQual,
494                                                                                 result_plan);
495                 /* Note: Agg does not affect any existing sort order of the tuples */
496         }
497
498         /*
499          * If we were not able to make the plan come out in the right order,
500          * add an explicit sort step.
501          */
502         if (parse->sortClause)
503         {
504                 if (! pathkeys_contained_in(sort_pathkeys, current_pathkeys))
505                 {
506                         result_plan = make_sortplan(tlist, parse->sortClause, result_plan);
507                 }
508         }
509
510         /*
511          * Finally, if there is a DISTINCT clause, add the UNIQUE node.
512          */
513         if (parse->distinctClause)
514         {
515                 result_plan = (Plan *) make_unique(tlist, result_plan,
516                                                                                    parse->distinctClause);
517         }
518
519         return result_plan;
520 }
521
522 /*---------------
523  * make_subplanTargetList
524  *        Generate appropriate target list when grouping is required.
525  *
526  * When union_planner inserts Aggregate and/or Group plan nodes above
527  * the result of query_planner, we typically want to pass a different
528  * target list to query_planner than the outer plan nodes should have.
529  * This routine generates the correct target list for the subplan.
530  *
531  * The initial target list passed from the parser already contains entries
532  * for all ORDER BY and GROUP BY expressions, but it will not have entries
533  * for variables used only in HAVING clauses; so we need to add those
534  * variables to the subplan target list.  Also, if we are doing either
535  * grouping or aggregation, we flatten all expressions except GROUP BY items
536  * into their component variables; the other expressions will be computed by
537  * the inserted nodes rather than by the subplan.  For example,
538  * given a query like
539  *              SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
540  * we want to pass this targetlist to the subplan:
541  *              a,b,c,d,a+b
542  * where the a+b target will be used by the Sort/Group steps, and the
543  * other targets will be used for computing the final results.  (In the
544  * above example we could theoretically suppress the a and b targets and
545  * use only a+b, but it's not really worth the trouble.)
546  *
547  * 'parse' is the query being processed.
548  * 'tlist' is the query's target list.
549  * 'groupColIdx' receives an array of column numbers for the GROUP BY
550  * expressions (if there are any) in the subplan's target list.
551  *
552  * The result is the targetlist to be passed to the subplan.
553  *---------------
554  */
555 static List *
556 make_subplanTargetList(Query *parse,
557                                            List *tlist,
558                                            AttrNumber **groupColIdx)
559 {
560         List       *sub_tlist;
561         List       *extravars;
562         int                     numCols;
563
564         *groupColIdx = NULL;
565
566         /*
567          * If we're not grouping or aggregating, nothing to do here;
568          * query_planner should receive the unmodified target list.
569          */
570         if (!parse->hasAggs && !parse->groupClause && !parse->havingQual)
571                 return tlist;
572
573         /*
574          * Otherwise, start with a "flattened" tlist (having just the vars
575          * mentioned in the targetlist and HAVING qual --- but not upper-
576          * level Vars; they will be replaced by Params later on).
577          */
578         sub_tlist = flatten_tlist(tlist);
579         extravars = pull_var_clause(parse->havingQual, false);
580         sub_tlist = add_to_flat_tlist(sub_tlist, extravars);
581         freeList(extravars);
582
583         /*
584          * If grouping, create sub_tlist entries for all GROUP BY expressions
585          * (GROUP BY items that are simple Vars should be in the list already),
586          * and make an array showing where the group columns are in the sub_tlist.
587          */
588         numCols = length(parse->groupClause);
589         if (numCols > 0)
590         {
591                 int                     keyno = 0;
592                 AttrNumber *grpColIdx;
593                 List       *gl;
594
595                 grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
596                 *groupColIdx = grpColIdx;
597
598                 foreach(gl, parse->groupClause)
599                 {
600                         GroupClause        *grpcl = (GroupClause *) lfirst(gl);
601                         Node               *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
602                         TargetEntry        *te = NULL;
603                         List               *sl;
604
605                         /* Find or make a matching sub_tlist entry */
606                         foreach(sl, sub_tlist)
607                         {
608                                 te = (TargetEntry *) lfirst(sl);
609                                 if (equal(groupexpr, te->expr))
610                                         break;
611                         }
612                         if (! sl)
613                         {
614                                 te = makeTargetEntry(makeResdom(length(sub_tlist) + 1,
615                                                                                                 exprType(groupexpr),
616                                                                                                 exprTypmod(groupexpr),
617                                                                                                 NULL,
618                                                                                                 (Index) 0,
619                                                                                                 (Oid) 0,
620                                                                                                 false),
621                                                                          groupexpr);
622                                 sub_tlist = lappend(sub_tlist, te);
623                         }
624
625                         /* and save its resno */
626                         grpColIdx[keyno++] = te->resdom->resno;
627                 }
628         }
629
630         return sub_tlist;
631 }
632
633 /*
634  * make_groupplan
635  *              Add a Group node for GROUP BY processing.
636  *              If we couldn't make the subplan produce presorted output for grouping,
637  *              first add an explicit Sort node.
638  */
639 static Plan *
640 make_groupplan(List *group_tlist,
641                            bool tuplePerGroup,
642                            List *groupClause,
643                            AttrNumber *grpColIdx,
644                            bool is_presorted,
645                            Plan *subplan)
646 {
647         int                     numCols = length(groupClause);
648
649         if (! is_presorted)
650         {
651                 /*
652                  * The Sort node always just takes a copy of the subplan's tlist
653                  * plus ordering information.  (This might seem inefficient if the
654                  * subplan contains complex GROUP BY expressions, but in fact Sort
655                  * does not evaluate its targetlist --- it only outputs the same
656                  * tuples in a new order.  So the expressions we might be copying
657                  * are just dummies with no extra execution cost.)
658                  */
659                 List       *sort_tlist = new_unsorted_tlist(subplan->targetlist);
660                 int                     keyno = 0;
661                 List       *gl;
662
663                 foreach(gl, groupClause)
664                 {
665                         GroupClause        *grpcl = (GroupClause *) lfirst(gl);
666                         TargetEntry        *te = nth(grpColIdx[keyno]-1, sort_tlist);
667                         Resdom             *resdom = te->resdom;
668
669                         /*
670                          * Check for the possibility of duplicate group-by clauses --- the
671                          * parser should have removed 'em, but the Sort executor will get
672                          * terribly confused if any get through!
673                          */
674                         if (resdom->reskey == 0)
675                         {
676                                 /* OK, insert the ordering info needed by the executor. */
677                                 resdom->reskey = ++keyno;
678                                 resdom->reskeyop = get_opcode(grpcl->sortop);
679                         }
680                 }
681
682                 subplan = (Plan *) make_sort(sort_tlist,
683                                                                          _NONAME_RELATION_ID_,
684                                                                          subplan,
685                                                                          keyno);
686         }
687
688         return (Plan *) make_group(group_tlist, tuplePerGroup, numCols,
689                                                            grpColIdx, subplan);
690 }
691
692 /*
693  * make_sortplan
694  *        Add a Sort node to implement an explicit ORDER BY clause.
695  */
696 static Plan *
697 make_sortplan(List *tlist, List *sortcls, Plan *plannode)
698 {
699         List       *temp_tlist;
700         List       *i;
701         int                     keyno = 0;
702
703         /*
704          * First make a copy of the tlist so that we don't corrupt the
705          * original.
706          */
707
708         temp_tlist = new_unsorted_tlist(tlist);
709
710         foreach(i, sortcls)
711         {
712                 SortClause *sortcl = (SortClause *) lfirst(i);
713                 TargetEntry *tle = get_sortgroupclause_tle(sortcl, temp_tlist);
714                 Resdom     *resdom = tle->resdom;
715
716                 /*
717                  * Check for the possibility of duplicate order-by clauses --- the
718                  * parser should have removed 'em, but the executor will get terribly
719                  * confused if any get through!
720                  */
721                 if (resdom->reskey == 0)
722                 {
723                         /* OK, insert the ordering info needed by the executor. */
724                         resdom->reskey = ++keyno;
725                         resdom->reskeyop = get_opcode(sortcl->sortop);
726                 }
727         }
728
729         return (Plan *) make_sort(temp_tlist,
730                                                           _NONAME_RELATION_ID_,
731                                                           plannode,
732                                                           keyno);
733 }
734
735 /*
736  * pg_checkretval() -- check return value of a list of sql parse
737  *                                              trees.
738  *
739  * The return value of a sql function is the value returned by
740  * the final query in the function.  We do some ad-hoc define-time
741  * type checking here to be sure that the user is returning the
742  * type he claims.
743  *
744  * XXX Why is this function in this module?
745  */
746 void
747 pg_checkretval(Oid rettype, List *queryTreeList)
748 {
749         Query      *parse;
750         List       *tlist;
751         List       *rt;
752         int                     cmd;
753         Type            typ;
754         Resdom     *resnode;
755         Relation        reln;
756         Oid                     relid;
757         int                     relnatts;
758         int                     i;
759
760         /* find the final query */
761         parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);
762
763         /*
764          * test 1:      if the last query is a utility invocation, then there had
765          * better not be a return value declared.
766          */
767         if (parse->commandType == CMD_UTILITY)
768         {
769                 if (rettype == InvalidOid)
770                         return;
771                 else
772                         elog(ERROR, "return type mismatch in function decl: final query is a catalog utility");
773         }
774
775         /* okay, it's an ordinary query */
776         tlist = parse->targetList;
777         rt = parse->rtable;
778         cmd = parse->commandType;
779
780         /*
781          * test 2:      if the function is declared to return no value, then the
782          * final query had better not be a retrieve.
783          */
784         if (rettype == InvalidOid)
785         {
786                 if (cmd == CMD_SELECT)
787                         elog(ERROR,
788                                  "function declared with no return type, but final query is a retrieve");
789                 else
790                         return;
791         }
792
793         /* by here, the function is declared to return some type */
794         if ((typ = typeidType(rettype)) == NULL)
795                 elog(ERROR, "can't find return type %u for function\n", rettype);
796
797         /*
798          * test 3:      if the function is declared to return a value, then the
799          * final query had better be a retrieve.
800          */
801         if (cmd != CMD_SELECT)
802                 elog(ERROR, "function declared to return type %s, but final query is not a retrieve", typeTypeName(typ));
803
804         /*
805          * test 4:      for base type returns, the target list should have exactly
806          * one entry, and its type should agree with what the user declared.
807          */
808
809         if (typeTypeRelid(typ) == InvalidOid)
810         {
811                 if (ExecTargetListLength(tlist) > 1)
812                         elog(ERROR, "function declared to return %s returns multiple values in final retrieve", typeTypeName(typ));
813
814                 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
815                 if (resnode->restype != rettype)
816                         elog(ERROR, "return type mismatch in function: declared to return %s, returns %s", typeTypeName(typ), typeidTypeName(resnode->restype));
817
818                 /* by here, base return types match */
819                 return;
820         }
821
822         /*
823          * If the target list is of length 1, and the type of the varnode in
824          * the target list is the same as the declared return type, this is
825          * okay.  This can happen, for example, where the body of the function
826          * is 'retrieve (x = func2())', where func2 has the same return type
827          * as the function that's calling it.
828          */
829         if (ExecTargetListLength(tlist) == 1)
830         {
831                 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
832                 if (resnode->restype == rettype)
833                         return;
834         }
835
836         /*
837          * By here, the procedure returns a (set of) tuples.  This part of the
838          * typechecking is a hack.      We look up the relation that is the
839          * declared return type, and be sure that attributes 1 .. n in the
840          * target list match the declared types.
841          */
842         reln = heap_open(typeTypeRelid(typ), AccessShareLock);
843         relid = reln->rd_id;
844         relnatts = reln->rd_rel->relnatts;
845
846         if (ExecTargetListLength(tlist) != relnatts)
847                 elog(ERROR, "function declared to return type %s does not retrieve (%s.*)", typeTypeName(typ), typeTypeName(typ));
848
849         /* expect attributes 1 .. n in order */
850         for (i = 1; i <= relnatts; i++)
851         {
852                 TargetEntry *tle = lfirst(tlist);
853                 Node       *thenode = tle->expr;
854                 Oid                     tletype = exprType(thenode);
855
856                 if (tletype != reln->rd_att->attrs[i - 1]->atttypid)
857                         elog(ERROR, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
858                 tlist = lnext(tlist);
859         }
860
861         heap_close(reln, AccessShareLock);
862 }