1 /*-------------------------------------------------------------------------
4 * The query optimizer external interface.
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.11 1997/11/25 21:59:59 momjian Exp $
12 *-------------------------------------------------------------------------
14 #include <sys/types.h>
18 #include "nodes/pg_list.h"
19 #include "nodes/plannodes.h"
20 #include "nodes/parsenodes.h"
21 #include "nodes/relation.h"
22 #include "parser/parse_expr.h"
24 #include "utils/elog.h"
25 #include "utils/lsyscache.h"
26 #include "access/heapam.h"
28 #include "optimizer/internal.h"
29 #include "optimizer/planner.h"
30 #include "optimizer/plancat.h"
31 #include "optimizer/prep.h"
32 #include "optimizer/planmain.h"
33 #include "optimizer/paths.h"
34 #include "optimizer/cost.h"
36 /* DATA STRUCTURE CREATION/MANIPULATION ROUTINES */
37 #include "nodes/relation.h"
38 #include "optimizer/clauseinfo.h"
39 #include "optimizer/joininfo.h"
40 #include "optimizer/keys.h"
41 #include "optimizer/ordering.h"
42 #include "optimizer/pathnode.h"
43 #include "optimizer/clauses.h"
44 #include "optimizer/tlist.h"
45 #include "optimizer/var.h"
47 #include "executor/executor.h"
49 static Plan *make_sortplan(List *tlist, List *sortcls, Plan *plannode);
50 static Plan *init_query_planner(Query *parse);
51 static Existential *make_existential(Plan *left, Plan *right);
53 /*****************************************************************************
55 * Query optimizer entry point
57 *****************************************************************************/
62 * Main query optimizer routine.
64 * Invokes the planner on union queries if there are any left,
65 * recursing if necessary to get them all, then processes normal plans.
67 * Returns a query plan.
73 List *tlist = parse->targetList;
74 List *rangetable = parse->rtable;
75 char *uniqueflag = parse->uniqueFlag;
76 List *sortclause = parse->sortClause;
77 Plan *special_plans = (Plan *) NULL;
79 Plan *result_plan = (Plan *) NULL;
86 rt_index = first_matching_rt_entry(rangetable, INHERITS_FLAG);
89 special_plans = (Plan *) plan_union_queries((Index) rt_index,
95 result_plan = special_plans;
97 result_plan = init_query_planner(parse); /* regular plans */
100 * For now, before we hand back the plan, check to see if there is a
101 * user-specified sort that needs to be done. Eventually, this will
102 * be moved into the guts of the planner s.t. user specified sorts
103 * will be considered as part of the planning process. Since we can
104 * only make use of user-specified sorts in special cases, we can do
105 * the optimization step later.
110 Plan *sortplan = make_sortplan(tlist, sortclause, result_plan);
112 return ((Plan *) make_unique(tlist, sortplan, uniqueflag));
117 return (make_sortplan(tlist, sortclause, result_plan));
119 return ((Plan *) result_plan);
126 * Returns a sortplan which is basically a SORT node attached to the
127 * top of the plan returned from the planner. It also adds the
128 * cost of sorting into the plan.
130 * sortkeys: ( resdom1 resdom2 resdom3 ...)
131 * sortops: (sortop1 sortop2 sortop3 ...)
134 make_sortplan(List *tlist, List *sortcls, Plan *plannode)
136 Plan *sortplan = (Plan *) NULL;
137 List *temp_tlist = NIL;
139 Resdom *resnode = (Resdom *) NULL;
140 Resdom *resdom = (Resdom *) NULL;
144 * First make a copy of the tlist so that we don't corrupt the the
148 temp_tlist = new_unsorted_tlist(tlist);
152 SortClause *sortcl = (SortClause *) lfirst(i);
154 resnode = sortcl->resdom;
155 resdom = tlist_resdom(temp_tlist, resnode);
158 * Order the resdom keys and replace the operator OID for each key
159 * with the regproc OID.
161 resdom->reskey = keyno;
162 resdom->reskeyop = get_opcode(sortcl->opoid);
166 sortplan = (Plan *) make_sort(temp_tlist,
172 * XXX Assuming that an internal sort has no. cost. This is wrong, but
173 * given that at this point, we don't know the no. of tuples returned,
174 * etc, we can't do better than to add a constant cost. This will be
175 * fixed once we move the sort further into the planner, but for now
176 * ... functionality....
179 sortplan->cost = plannode->cost;
186 * init-query-planner--
187 * Deals with all non-union preprocessing, including existential
188 * qualifications and CNFifying the qualifications.
190 * Returns a query plan.
191 * MODIFIES: tlist,qual
195 init_query_planner(Query *root)
198 List *existential_qual;
199 Existential *exist_plan;
200 List *tlist = root->targetList;
202 tlist = preprocess_targetlist(tlist,
204 root->resultRelation,
208 preprocess_qualification((Expr *) root->qual,
212 if (existential_qual == NULL)
214 return (query_planner(root,
221 int temp = root->commandType;
222 Plan *existential_plan;
224 root->commandType = CMD_SELECT;
225 existential_plan = query_planner(root,
230 exist_plan = make_existential(existential_plan,
235 return ((Plan *) exist_plan);
241 * Instantiates an existential plan node and fills in
242 * the left and right subtree slots.
245 make_existential(Plan *left, Plan *right)
247 Existential *node = makeNode(Existential);
249 node->lefttree = left;
250 node->righttree = left;
255 * pg_checkretval() -- check return value of a list of sql parse
258 * The return value of a sql function is the value returned by
259 * the final query in the function. We do some ad-hoc define-time
260 * type checking here to be sure that the user is returning the
264 pg_checkretval(Oid rettype, QueryTreeList *queryTreeList)
278 /* find the final query */
279 parse = queryTreeList->qtrees[queryTreeList->len - 1];
282 * test 1: if the last query is a utility invocation, then there had
283 * better not be a return value declared.
285 if (parse->commandType == CMD_UTILITY)
287 if (rettype == InvalidOid)
290 elog(WARN, "return type mismatch in function decl: final query is a catalog utility");
293 /* okay, it's an ordinary query */
294 tlist = parse->targetList;
296 cmd = parse->commandType;
299 * test 2: if the function is declared to return no value, then the
300 * final query had better not be a retrieve.
302 if (rettype == InvalidOid)
304 if (cmd == CMD_SELECT)
306 "function declared with no return type, but final query is a retrieve");
311 /* by here, the function is declared to return some type */
312 if ((typ = typeidType(rettype)) == NULL)
313 elog(WARN, "can't find return type %d for function\n", rettype);
316 * test 3: if the function is declared to return a value, then the
317 * final query had better be a retrieve.
319 if (cmd != CMD_SELECT)
320 elog(WARN, "function declared to return type %s, but final query is not a retrieve", typeTypeName(typ));
323 * test 4: for base type returns, the target list should have exactly
324 * one entry, and its type should agree with what the user declared.
327 if (typeTypeRelid(typ) == InvalidOid)
329 if (exec_tlist_length(tlist) > 1)
330 elog(WARN, "function declared to return %s returns multiple values in final retrieve", typeTypeName(typ));
332 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
333 if (resnode->restype != rettype)
334 elog(WARN, "return type mismatch in function: declared to return %s, returns %s", typeTypeName(typ), typeidTypeName(resnode->restype));
336 /* by here, base return types match */
341 * If the target list is of length 1, and the type of the varnode in
342 * the target list is the same as the declared return type, this is
343 * okay. This can happen, for example, where the body of the function
344 * is 'retrieve (x = func2())', where func2 has the same return type
345 * as the function that's calling it.
347 if (exec_tlist_length(tlist) == 1)
349 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
350 if (resnode->restype == rettype)
355 * By here, the procedure returns a (set of) tuples. This part of the
356 * typechecking is a hack. We look up the relation that is the
357 * declared return type, and be sure that attributes 1 .. n in the
358 * target list match the declared types.
360 reln = heap_open(typeTypeRelid(typ));
362 if (!RelationIsValid(reln))
363 elog(WARN, "cannot open relation relid %d", typeTypeRelid(typ));
366 relnatts = reln->rd_rel->relnatts;
368 if (exec_tlist_length(tlist) != relnatts)
369 elog(WARN, "function declared to return type %s does not retrieve (%s.*)", typeTypeName(typ), typeTypeName(typ));
371 /* expect attributes 1 .. n in order */
372 for (i = 1; i <= relnatts; i++)
374 TargetEntry *tle = lfirst(tlist);
375 Node *thenode = tle->expr;
377 tlist = lnext(tlist);
378 tletype = exprType(thenode);
381 /* this is tedious */
382 if (IsA(thenode, Var))
383 tletype = (Oid) ((Var *) thenode)->vartype;
384 else if (IsA(thenode, Const))
385 tletype = (Oid) ((Const *) thenode)->consttype;
386 else if (IsA(thenode, Param))
387 tletype = (Oid) ((Param *) thenode)->paramtype;
388 else if (IsA(thenode, Expr))
391 else if (IsA(thenode, LispList))
393 thenode = lfirst(thenode);
394 if (IsA(thenode, Oper))
395 tletype = (Oid) get_opresulttype((Oper *) thenode);
396 else if (IsA(thenode, Func))
397 tletype = (Oid) get_functype((Func *) thenode);
399 elog(WARN, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
402 elog(WARN, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
404 /* reach right in there, why don't you? */
405 if (tletype != reln->rd_att->attrs[i - 1]->atttypid)
406 elog(WARN, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));