1 /*-------------------------------------------------------------------------
4 * The query optimizer external interface.
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.78 2000/03/21 05:12:01 tgl Exp $
13 *-------------------------------------------------------------------------
15 #include <sys/types.h>
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"
40 static List *make_subplanTargetList(Query *parse, List *tlist,
41 AttrNumber **groupColIdx);
42 static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
43 List *groupClause, AttrNumber *grpColIdx,
44 bool is_presorted, Plan *subplan);
45 static Plan *make_sortplan(List *tlist, List *sortcls, Plan *plannode);
47 /*****************************************************************************
49 * Query optimizer entry point
51 *****************************************************************************/
57 /* Initialize state for subselects */
58 PlannerQueryLevel = 1;
59 PlannerInitPlan = NULL;
60 PlannerParamVar = NULL;
63 /* this should go away sometime soon */
64 transformKeySetQuery(parse);
66 /* primary planning entry point (may recurse for subplans) */
67 result_plan = subquery_planner(parse, -1.0 /* default case */);
69 Assert(PlannerQueryLevel == 1);
71 /* if top-level query had subqueries, do housekeeping for them */
72 if (PlannerPlanId > 0)
74 (void) SS_finalize_plan(result_plan);
75 result_plan->initPlan = PlannerInitPlan;
78 /* executor wants to know total number of Params used overall */
79 result_plan->nParamExec = length(PlannerParamVar);
81 /* final cleanup of the plan */
82 set_plan_references(result_plan);
88 /*--------------------
90 * Invokes the planner on a subquery. We recurse to here for each
91 * sub-SELECT found in the query tree.
93 * parse is the querytree produced by the parser & rewriter.
94 * tuple_fraction is the fraction of tuples we expect will be retrieved.
95 * tuple_fraction is interpreted as explained for union_planner, below.
97 * Basically, this routine does the stuff that should only be done once
98 * per Query object. It then calls union_planner, which may be called
99 * recursively on the same Query node in order to handle UNIONs and/or
100 * inheritance. subquery_planner is called recursively from subselect.c.
102 * prepunion.c uses an unholy combination of calling union_planner when
103 * recursing on the primary Query node, or subquery_planner when recursing
104 * on a UNION'd Query node that hasn't previously been seen by
105 * subquery_planner. That whole chunk of code needs rewritten from scratch.
107 * Returns a query plan.
108 *--------------------
111 subquery_planner(Query *parse, double tuple_fraction)
114 * A HAVING clause without aggregates is equivalent to a WHERE clause
115 * (except it can only refer to grouped fields). If there are no
116 * aggs anywhere in the query, then we don't want to create an Agg
117 * plan node, so merge the HAVING condition into WHERE. (We used to
118 * consider this an error condition, but it seems to be legal SQL.)
120 if (parse->havingQual != NULL && ! parse->hasAggs)
122 if (parse->qual == NULL)
123 parse->qual = parse->havingQual;
125 parse->qual = (Node *) make_andclause(lappend(lcons(parse->qual,
128 parse->havingQual = NULL;
132 * Simplify constant expressions in targetlist and quals.
134 * Note that at this point the qual has not yet been converted to
135 * implicit-AND form, so we can apply eval_const_expressions directly.
136 * Also note that we need to do this before SS_process_sublinks,
137 * because that routine inserts bogus "Const" nodes.
139 parse->targetList = (List *)
140 eval_const_expressions((Node *) parse->targetList);
141 parse->qual = eval_const_expressions(parse->qual);
142 parse->havingQual = eval_const_expressions(parse->havingQual);
145 * Canonicalize the qual, and convert it to implicit-AND format.
147 * XXX Is there any value in re-applying eval_const_expressions
148 * after canonicalize_qual?
150 parse->qual = (Node *) canonicalize_qual((Expr *) parse->qual, true);
151 #ifdef OPTIMIZER_DEBUG
152 printf("After canonicalize_qual()\n");
157 * Ditto for the havingQual
159 parse->havingQual = (Node *) canonicalize_qual((Expr *) parse->havingQual,
162 /* Expand SubLinks to SubPlans */
163 if (parse->hasSubLinks)
165 parse->targetList = (List *)
166 SS_process_sublinks((Node *) parse->targetList);
167 parse->qual = SS_process_sublinks(parse->qual);
168 parse->havingQual = SS_process_sublinks(parse->havingQual);
170 if (parse->groupClause != NIL)
173 * Check for ungrouped variables passed to subplans.
174 * Note we do NOT do this for subplans in WHERE; it's legal
175 * there because WHERE is evaluated pre-GROUP.
177 * An interesting fine point: if we reassigned a HAVING qual
178 * into WHERE above, then we will accept references to ungrouped
179 * vars from subplans in the HAVING qual. This is not entirely
180 * consistent, but it doesn't seem particularly harmful...
182 check_subplans_for_ungrouped_vars((Node *) parse->targetList,
184 check_subplans_for_ungrouped_vars(parse->havingQual, parse);
188 /* Replace uplevel vars with Param nodes */
189 if (PlannerQueryLevel > 1)
191 parse->targetList = (List *)
192 SS_replace_correlation_vars((Node *) parse->targetList);
193 parse->qual = SS_replace_correlation_vars(parse->qual);
194 parse->havingQual = SS_replace_correlation_vars(parse->havingQual);
197 /* Do the main planning (potentially recursive) */
199 return union_planner(parse, tuple_fraction);
202 * XXX should any more of union_planner's activity be moved here?
204 * That would take careful study of the interactions with prepunion.c,
205 * but I suspect it would pay off in simplicity and avoidance of
211 /*--------------------
213 * Invokes the planner on union-type queries (both regular UNIONs and
214 * appends produced by inheritance), recursing if necessary to get them
215 * all, then processes normal plans.
217 * parse is the querytree produced by the parser & rewriter.
218 * tuple_fraction is the fraction of tuples we expect will be retrieved
220 * tuple_fraction is interpreted as follows:
221 * < 0: determine fraction by inspection of query (normal case)
222 * 0: expect all tuples to be retrieved
223 * 0 < tuple_fraction < 1: expect the given fraction of tuples available
224 * from the plan to be retrieved
225 * tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
226 * expected to be retrieved (ie, a LIMIT specification)
227 * The normal case is to pass -1, but some callers pass values >= 0 to
228 * override this routine's determination of the appropriate fraction.
230 * Returns a query plan.
231 *--------------------
234 union_planner(Query *parse,
235 double tuple_fraction)
237 List *tlist = parse->targetList;
238 List *rangetable = parse->rtable;
239 Plan *result_plan = (Plan *) NULL;
240 AttrNumber *groupColIdx = NULL;
241 List *current_pathkeys = NIL;
242 List *group_pathkeys;
246 if (parse->unionClause)
248 result_plan = (Plan *) plan_union_queries(parse);
249 /* XXX do we need to do this? bjm 12/19/97 */
250 tlist = preprocess_targetlist(tlist,
252 parse->resultRelation,
255 * We leave current_pathkeys NIL indicating we do not know sort order.
256 * Actually, for a normal UNION we have done an explicit sort; ought
257 * to change interface to plan_union_queries to pass that info back!
260 /* Calculate pathkeys that represent grouping/ordering requirements */
261 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
263 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
266 else if ((rt_index = first_inherit_rt_entry(rangetable)) != -1)
271 * Generate appropriate target list for subplan; may be different
272 * from tlist if grouping or aggregation is needed.
274 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
277 * Recursively plan the subqueries needed for inheritance
279 result_plan = (Plan *) plan_inherit_queries(parse, sub_tlist,
283 * Fix up outer target list. NOTE: unlike the case for non-inherited
284 * query, we pass the unfixed tlist to subplans, which do their own
285 * fixing. But we still want to fix the outer target list afterwards.
286 * I *think* this is correct --- doing the fix before recursing is
287 * definitely wrong, because preprocess_targetlist() will do the
288 * wrong thing if invoked twice on the same list. Maybe that is a bug?
291 tlist = preprocess_targetlist(tlist,
293 parse->resultRelation,
296 if (parse->rowMark != NULL)
297 elog(ERROR, "SELECT FOR UPDATE is not supported for inherit queries");
299 * We leave current_pathkeys NIL indicating we do not know sort order
300 * of the Append-ed results.
303 /* Calculate pathkeys that represent grouping/ordering requirements */
304 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
306 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
313 /* Preprocess targetlist in case we are inside an INSERT/UPDATE. */
314 tlist = preprocess_targetlist(tlist,
316 parse->resultRelation,
320 * Add row-mark targets for UPDATE (should this be done in
321 * preprocess_targetlist?)
323 if (parse->rowMark != NULL)
327 foreach(l, parse->rowMark)
329 RowMark *rowmark = (RowMark *) lfirst(l);
335 if (!(rowmark->info & ROW_MARK_FOR_UPDATE))
338 resname = (char *) palloc(32);
339 sprintf(resname, "ctid%u", rowmark->rti);
340 resdom = makeResdom(length(tlist) + 1,
348 var = makeVar(rowmark->rti, -1, TIDOID, -1, 0);
350 ctid = makeTargetEntry(resdom, (Node *) var);
351 tlist = lappend(tlist, ctid);
356 * Generate appropriate target list for subplan; may be different
357 * from tlist if grouping or aggregation is needed.
359 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
361 /* Calculate pathkeys that represent grouping/ordering requirements */
362 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
364 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
368 * Figure out whether we need a sorted result from query_planner.
370 * If we have a GROUP BY clause, then we want a result sorted
371 * properly for grouping. Otherwise, if there is an ORDER BY clause,
372 * we want to sort by the ORDER BY clause. (Note: if we have both,
373 * and ORDER BY is a superset of GROUP BY, it would be tempting to
374 * request sort by ORDER BY --- but that might just leave us failing
375 * to exploit an available sort order at all. Needs more thought...)
377 if (parse->groupClause)
378 parse->query_pathkeys = group_pathkeys;
379 else if (parse->sortClause)
380 parse->query_pathkeys = sort_pathkeys;
382 parse->query_pathkeys = NIL;
385 * Figure out whether we expect to retrieve all the tuples that the
386 * plan can generate, or to stop early due to a LIMIT or other
387 * factors. If the caller passed a value >= 0, believe that value,
388 * else do our own examination of the query context.
390 if (tuple_fraction < 0.0)
392 /* Initial assumption is we need all the tuples */
393 tuple_fraction = 0.0;
395 * Check for a LIMIT clause.
397 if (parse->limitCount != NULL)
399 if (IsA(parse->limitCount, Const))
401 Const *limitc = (Const *) parse->limitCount;
402 int count = (int) (limitc->constvalue);
405 * The constant can legally be either 0 ("ALL") or a
406 * positive integer. If it is not ALL, we also need
407 * to consider the OFFSET part of LIMIT.
411 tuple_fraction = (double) count;
412 if (parse->limitOffset != NULL)
414 if (IsA(parse->limitOffset, Const))
418 limitc = (Const *) parse->limitOffset;
419 offset = (int) (limitc->constvalue);
421 tuple_fraction += (double) offset;
425 /* It's a PARAM ... punt ... */
426 tuple_fraction = 0.10;
434 * COUNT is a PARAM ... don't know exactly what the limit
435 * will be, but for lack of a better idea assume 10%
436 * of the plan's result is wanted.
438 tuple_fraction = 0.10;
442 * Check for a retrieve-into-portal, ie DECLARE CURSOR.
444 * We have no real idea how many tuples the user will ultimately
445 * FETCH from a cursor, but it seems a good bet that he doesn't
446 * want 'em all. Optimize for 10% retrieval (you gotta better
450 tuple_fraction = 0.10;
453 * Adjust tuple_fraction if we see that we are going to apply
454 * grouping/aggregation/etc. This is not overridable by the
455 * caller, since it reflects plan actions that this routine
456 * will certainly take, not assumptions about context.
458 if (parse->groupClause)
461 * In GROUP BY mode, we have the little problem that we don't
462 * really know how many input tuples will be needed to make a
463 * group, so we can't translate an output LIMIT count into an
464 * input count. For lack of a better idea, assume 25% of the
465 * input data will be processed if there is any output limit.
466 * However, if the caller gave us a fraction rather than an
467 * absolute count, we can keep using that fraction (which amounts
468 * to assuming that all the groups are about the same size).
470 if (tuple_fraction >= 1.0)
471 tuple_fraction = 0.25;
473 * If both GROUP BY and ORDER BY are specified, we will need
474 * two levels of sort --- and, therefore, certainly need to
475 * read all the input tuples --- unless ORDER BY is a subset
476 * of GROUP BY. (Although we are comparing non-canonicalized
477 * pathkeys here, it should be OK since they will both contain
478 * only single-element sublists at this point. See pathkeys.c.)
480 if (parse->groupClause && parse->sortClause &&
481 ! pathkeys_contained_in(sort_pathkeys, group_pathkeys))
482 tuple_fraction = 0.0;
484 else if (parse->hasAggs)
486 /* Ungrouped aggregate will certainly want all the input tuples. */
487 tuple_fraction = 0.0;
489 else if (parse->distinctClause)
492 * SELECT DISTINCT, like GROUP, will absorb an unpredictable
493 * number of input tuples per output tuple. Handle the same way.
495 if (tuple_fraction >= 1.0)
496 tuple_fraction = 0.25;
499 /* Generate the (sub) plan */
500 result_plan = query_planner(parse,
502 (List *) parse->qual,
505 /* query_planner returns actual sort order (which is not
506 * necessarily what we requested) in query_pathkeys.
508 current_pathkeys = parse->query_pathkeys;
511 /* query_planner returns NULL if it thinks plan is bogus */
513 elog(ERROR, "union_planner: failed to create plan");
516 * We couldn't canonicalize group_pathkeys and sort_pathkeys before
517 * running query_planner(), so do it now.
519 group_pathkeys = canonicalize_pathkeys(parse, group_pathkeys);
520 sort_pathkeys = canonicalize_pathkeys(parse, sort_pathkeys);
523 * If we have a GROUP BY clause, insert a group node (plus the
524 * appropriate sort node, if necessary).
526 if (parse->groupClause)
533 * Decide whether how many tuples per group the Group node needs
534 * to return. (Needs only one tuple per group if no aggregate is
535 * present. Otherwise, need every tuple from the group to do the
536 * aggregation.) Note tuplePerGroup is named backwards :-(
538 tuplePerGroup = parse->hasAggs;
541 * If there are aggregates then the Group node should just return
542 * the same set of vars as the subplan did (but we can exclude
543 * any GROUP BY expressions). If there are no aggregates
544 * then the Group node had better compute the final tlist.
547 group_tlist = flatten_tlist(result_plan->targetlist);
552 * Figure out whether the path result is already ordered the way we
553 * need it --- if so, no need for an explicit sort step.
555 if (pathkeys_contained_in(group_pathkeys, current_pathkeys))
557 is_sorted = true; /* no sort needed now */
558 /* current_pathkeys remains unchanged */
562 /* We will need to do an explicit sort by the GROUP BY clause.
563 * make_groupplan will do the work, but set current_pathkeys
564 * to indicate the resulting order.
567 current_pathkeys = group_pathkeys;
570 result_plan = make_groupplan(group_tlist,
579 * If aggregate is present, insert the Agg node
581 * HAVING clause, if any, becomes qual of the Agg node
585 result_plan = (Plan *) make_agg(tlist,
586 (List *) parse->havingQual,
588 /* Note: Agg does not affect any existing sort order of the tuples */
592 * If we were not able to make the plan come out in the right order,
593 * add an explicit sort step.
595 if (parse->sortClause)
597 if (! pathkeys_contained_in(sort_pathkeys, current_pathkeys))
599 result_plan = make_sortplan(tlist, parse->sortClause, result_plan);
604 * Finally, if there is a DISTINCT clause, add the UNIQUE node.
606 if (parse->distinctClause)
608 result_plan = (Plan *) make_unique(tlist, result_plan,
609 parse->distinctClause);
616 * make_subplanTargetList
617 * Generate appropriate target list when grouping is required.
619 * When union_planner inserts Aggregate and/or Group plan nodes above
620 * the result of query_planner, we typically want to pass a different
621 * target list to query_planner than the outer plan nodes should have.
622 * This routine generates the correct target list for the subplan.
624 * The initial target list passed from the parser already contains entries
625 * for all ORDER BY and GROUP BY expressions, but it will not have entries
626 * for variables used only in HAVING clauses; so we need to add those
627 * variables to the subplan target list. Also, if we are doing either
628 * grouping or aggregation, we flatten all expressions except GROUP BY items
629 * into their component variables; the other expressions will be computed by
630 * the inserted nodes rather than by the subplan. For example,
632 * SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
633 * we want to pass this targetlist to the subplan:
635 * where the a+b target will be used by the Sort/Group steps, and the
636 * other targets will be used for computing the final results. (In the
637 * above example we could theoretically suppress the a and b targets and
638 * use only a+b, but it's not really worth the trouble.)
640 * 'parse' is the query being processed.
641 * 'tlist' is the query's target list.
642 * 'groupColIdx' receives an array of column numbers for the GROUP BY
643 * expressions (if there are any) in the subplan's target list.
645 * The result is the targetlist to be passed to the subplan.
649 make_subplanTargetList(Query *parse,
651 AttrNumber **groupColIdx)
660 * If we're not grouping or aggregating, nothing to do here;
661 * query_planner should receive the unmodified target list.
663 if (!parse->hasAggs && !parse->groupClause && !parse->havingQual)
667 * Otherwise, start with a "flattened" tlist (having just the vars
668 * mentioned in the targetlist and HAVING qual --- but not upper-
669 * level Vars; they will be replaced by Params later on).
671 sub_tlist = flatten_tlist(tlist);
672 extravars = pull_var_clause(parse->havingQual, false);
673 sub_tlist = add_to_flat_tlist(sub_tlist, extravars);
677 * If grouping, create sub_tlist entries for all GROUP BY expressions
678 * (GROUP BY items that are simple Vars should be in the list already),
679 * and make an array showing where the group columns are in the sub_tlist.
681 numCols = length(parse->groupClause);
685 AttrNumber *grpColIdx;
688 grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
689 *groupColIdx = grpColIdx;
691 foreach(gl, parse->groupClause)
693 GroupClause *grpcl = (GroupClause *) lfirst(gl);
694 Node *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
695 TargetEntry *te = NULL;
698 /* Find or make a matching sub_tlist entry */
699 foreach(sl, sub_tlist)
701 te = (TargetEntry *) lfirst(sl);
702 if (equal(groupexpr, te->expr))
707 te = makeTargetEntry(makeResdom(length(sub_tlist) + 1,
709 exprTypmod(groupexpr),
715 sub_tlist = lappend(sub_tlist, te);
718 /* and save its resno */
719 grpColIdx[keyno++] = te->resdom->resno;
728 * Add a Group node for GROUP BY processing.
729 * If we couldn't make the subplan produce presorted output for grouping,
730 * first add an explicit Sort node.
733 make_groupplan(List *group_tlist,
736 AttrNumber *grpColIdx,
740 int numCols = length(groupClause);
745 * The Sort node always just takes a copy of the subplan's tlist
746 * plus ordering information. (This might seem inefficient if the
747 * subplan contains complex GROUP BY expressions, but in fact Sort
748 * does not evaluate its targetlist --- it only outputs the same
749 * tuples in a new order. So the expressions we might be copying
750 * are just dummies with no extra execution cost.)
752 List *sort_tlist = new_unsorted_tlist(subplan->targetlist);
756 foreach(gl, groupClause)
758 GroupClause *grpcl = (GroupClause *) lfirst(gl);
759 TargetEntry *te = nth(grpColIdx[keyno]-1, sort_tlist);
760 Resdom *resdom = te->resdom;
763 * Check for the possibility of duplicate group-by clauses --- the
764 * parser should have removed 'em, but the Sort executor will get
765 * terribly confused if any get through!
767 if (resdom->reskey == 0)
769 /* OK, insert the ordering info needed by the executor. */
770 resdom->reskey = ++keyno;
771 resdom->reskeyop = get_opcode(grpcl->sortop);
775 subplan = (Plan *) make_sort(sort_tlist,
776 _NONAME_RELATION_ID_,
781 return (Plan *) make_group(group_tlist, tuplePerGroup, numCols,
787 * Add a Sort node to implement an explicit ORDER BY clause.
790 make_sortplan(List *tlist, List *sortcls, Plan *plannode)
797 * First make a copy of the tlist so that we don't corrupt the
801 temp_tlist = new_unsorted_tlist(tlist);
805 SortClause *sortcl = (SortClause *) lfirst(i);
806 TargetEntry *tle = get_sortgroupclause_tle(sortcl, temp_tlist);
807 Resdom *resdom = tle->resdom;
810 * Check for the possibility of duplicate order-by clauses --- the
811 * parser should have removed 'em, but the executor will get terribly
812 * confused if any get through!
814 if (resdom->reskey == 0)
816 /* OK, insert the ordering info needed by the executor. */
817 resdom->reskey = ++keyno;
818 resdom->reskeyop = get_opcode(sortcl->sortop);
822 return (Plan *) make_sort(temp_tlist,
823 _NONAME_RELATION_ID_,
829 * pg_checkretval() -- check return value of a list of sql parse
832 * The return value of a sql function is the value returned by
833 * the final query in the function. We do some ad-hoc define-time
834 * type checking here to be sure that the user is returning the
837 * XXX Why is this function in this module?
840 pg_checkretval(Oid rettype, List *queryTreeList)
853 /* find the final query */
854 parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);
857 * test 1: if the last query is a utility invocation, then there had
858 * better not be a return value declared.
860 if (parse->commandType == CMD_UTILITY)
862 if (rettype == InvalidOid)
865 elog(ERROR, "return type mismatch in function decl: final query is a catalog utility");
868 /* okay, it's an ordinary query */
869 tlist = parse->targetList;
871 cmd = parse->commandType;
874 * test 2: if the function is declared to return no value, then the
875 * final query had better not be a retrieve.
877 if (rettype == InvalidOid)
879 if (cmd == CMD_SELECT)
881 "function declared with no return type, but final query is a retrieve");
886 /* by here, the function is declared to return some type */
887 if ((typ = typeidType(rettype)) == NULL)
888 elog(ERROR, "can't find return type %u for function\n", rettype);
891 * test 3: if the function is declared to return a value, then the
892 * final query had better be a retrieve.
894 if (cmd != CMD_SELECT)
895 elog(ERROR, "function declared to return type %s, but final query is not a retrieve", typeTypeName(typ));
898 * test 4: for base type returns, the target list should have exactly
899 * one entry, and its type should agree with what the user declared.
902 if (typeTypeRelid(typ) == InvalidOid)
904 if (ExecTargetListLength(tlist) > 1)
905 elog(ERROR, "function declared to return %s returns multiple values in final retrieve", typeTypeName(typ));
907 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
908 if (resnode->restype != rettype)
909 elog(ERROR, "return type mismatch in function: declared to return %s, returns %s", typeTypeName(typ), typeidTypeName(resnode->restype));
911 /* by here, base return types match */
916 * If the target list is of length 1, and the type of the varnode in
917 * the target list is the same as the declared return type, this is
918 * okay. This can happen, for example, where the body of the function
919 * is 'retrieve (x = func2())', where func2 has the same return type
920 * as the function that's calling it.
922 if (ExecTargetListLength(tlist) == 1)
924 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
925 if (resnode->restype == rettype)
930 * By here, the procedure returns a (set of) tuples. This part of the
931 * typechecking is a hack. We look up the relation that is the
932 * declared return type, and be sure that attributes 1 .. n in the
933 * target list match the declared types.
935 reln = heap_open(typeTypeRelid(typ), AccessShareLock);
937 relnatts = reln->rd_rel->relnatts;
939 if (ExecTargetListLength(tlist) != relnatts)
940 elog(ERROR, "function declared to return type %s does not retrieve (%s.*)", typeTypeName(typ), typeTypeName(typ));
942 /* expect attributes 1 .. n in order */
943 for (i = 1; i <= relnatts; i++)
945 TargetEntry *tle = lfirst(tlist);
946 Node *thenode = tle->expr;
947 Oid tletype = exprType(thenode);
949 if (tletype != reln->rd_att->attrs[i - 1]->atttypid)
950 elog(ERROR, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
951 tlist = lnext(tlist);
954 heap_close(reln, AccessShareLock);