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.60 1999/07/16 04:59:19 momjian Exp $
12 *-------------------------------------------------------------------------
14 #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/planmain.h"
27 #include "optimizer/planner.h"
28 #include "optimizer/prep.h"
29 #include "optimizer/subselect.h"
30 #include "optimizer/tlist.h"
31 #include "optimizer/var.h"
32 #include "parser/parse_expr.h"
33 #include "parser/parse_oper.h"
34 #include "utils/builtins.h"
35 #include "utils/lsyscache.h"
36 #include "utils/syscache.h"
38 static List *make_subplanTargetList(Query *parse, List *tlist,
39 AttrNumber **groupColIdx);
40 static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
41 List *groupClause, AttrNumber *grpColIdx,
43 static bool need_sortplan(List *sortcls, Plan *plan);
44 static Plan *make_sortplan(List *tlist, List *sortcls, Plan *plannode);
46 /*****************************************************************************
48 * Query optimizer entry point
50 *****************************************************************************/
56 /* Initialize state for subselects */
57 PlannerQueryLevel = 1;
58 PlannerInitPlan = NULL;
59 PlannerParamVar = NULL;
62 transformKeySetQuery(parse);
63 result_plan = union_planner(parse);
65 Assert(PlannerQueryLevel == 1);
66 if (PlannerPlanId > 0)
68 result_plan->initPlan = PlannerInitPlan;
69 (void) SS_finalize_plan(result_plan);
71 result_plan->nParamExec = length(PlannerParamVar);
79 * Invokes the planner on union queries if there are any left,
80 * recursing if necessary to get them all, then processes normal plans.
82 * Returns a query plan.
86 union_planner(Query *parse)
88 List *tlist = parse->targetList;
89 List *rangetable = parse->rtable;
90 Plan *result_plan = (Plan *) NULL;
91 AttrNumber *groupColIdx = NULL;
94 if (parse->unionClause)
96 result_plan = (Plan *) plan_union_queries(parse);
97 /* XXX do we need to do this? bjm 12/19/97 */
98 tlist = preprocess_targetlist(tlist,
100 parse->resultRelation,
103 else if ((rt_index = first_inherit_rt_entry(rangetable)) != -1)
108 * Generate appropriate target list for subplan; may be different
109 * from tlist if grouping or aggregation is needed.
111 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
114 * Recursively plan the subqueries needed for inheritance
116 result_plan = (Plan *) plan_inherit_queries(parse, sub_tlist,
120 * Fix up outer target list. NOTE: unlike the case for non-inherited
121 * query, we pass the unfixed tlist to subplans, which do their own
122 * fixing. But we still want to fix the outer target list afterwards.
123 * I *think* this is correct --- doing the fix before recursing is
124 * definitely wrong, because preprocess_targetlist() will do the
125 * wrong thing if invoked twice on the same list. Maybe that is a bug?
128 tlist = preprocess_targetlist(tlist,
130 parse->resultRelation,
133 if (parse->rowMark != NULL)
134 elog(ERROR, "SELECT FOR UPDATE is not supported for inherit queries");
140 /* Preprocess targetlist in case we are inside an INSERT/UPDATE. */
141 tlist = preprocess_targetlist(tlist,
143 parse->resultRelation,
147 * Add row-mark targets for UPDATE (should this be done in
148 * preprocess_targetlist?)
150 if (parse->rowMark != NULL)
154 foreach(l, parse->rowMark)
156 RowMark *rowmark = (RowMark *) lfirst(l);
162 if (!(rowmark->info & ROW_MARK_FOR_UPDATE))
165 resname = (char *) palloc(32);
166 sprintf(resname, "ctid%u", rowmark->rti);
167 resdom = makeResdom(length(tlist) + 1,
175 var = makeVar(rowmark->rti, -1, TIDOID,
176 -1, 0, rowmark->rti, -1);
178 ctid = makeTargetEntry(resdom, (Node *) var);
179 tlist = lappend(tlist, ctid);
184 * Generate appropriate target list for subplan; may be different
185 * from tlist if grouping or aggregation is needed.
187 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
189 /* Generate the (sub) plan */
190 result_plan = query_planner(parse,
193 (List *) parse->qual);
196 /* query_planner returns NULL if it thinks plan is bogus */
198 elog(ERROR, "union_planner: failed to create plan");
201 * If we have a GROUP BY clause, insert a group node (with the
202 * appropriate sort node.)
204 if (parse->groupClause)
210 * Decide whether how many tuples per group the Group node needs
211 * to return. (Needs only one tuple per group if no aggregate is
212 * present. Otherwise, need every tuple from the group to do the
213 * aggregation.) Note tuplePerGroup is named backwards :-(
215 tuplePerGroup = parse->hasAggs;
218 * If there are aggregates then the Group node should just return
219 * the same (simplified) tlist as the subplan, which we indicate
220 * to make_groupplan by passing NIL. If there are no aggregates
221 * then the Group node had better compute the final tlist.
223 group_tlist = parse->hasAggs ? NIL : tlist;
225 result_plan = make_groupplan(group_tlist,
233 * If we have a HAVING clause, do the necessary things with it.
235 if (parse->havingQual)
237 /* convert the havingQual to conjunctive normal form (cnf) */
238 parse->havingQual = (Node *) cnfify((Expr *) parse->havingQual, true);
240 if (parse->hasSubLinks)
243 * There may be a subselect in the havingQual, so we have to
244 * process it using the same function as for a subselect in
247 parse->havingQual = SS_process_sublinks(parse->havingQual);
250 * Check for ungrouped variables passed to subplans. (Probably
251 * this should be done for the targetlist as well???)
253 check_having_for_ungrouped_vars(parse->havingQual,
258 /* Calculate the opfids from the opnos */
259 parse->havingQual = (Node *) fix_opids((List *) parse->havingQual);
263 * If aggregate is present, insert the agg node
267 result_plan = (Plan *) make_agg(tlist, result_plan);
269 /* HAVING clause, if any, becomes qual of the Agg node */
270 result_plan->qual = (List *) parse->havingQual;
273 * Update vars to refer to subplan result tuples, find Aggrefs,
274 * make sure there is an Aggref in every HAVING clause.
276 if (!set_agg_tlist_references((Agg *) result_plan))
277 elog(ERROR, "SELECT/HAVING requires aggregates to be valid");
280 * Check that we actually found some aggregates, else executor
281 * will die unpleasantly. (This defends against possible bugs in
282 * parser or rewrite that might cause hasAggs to be incorrectly
283 * set 'true'. It's not easy to recover here, since we've already
284 * made decisions assuming there will be an Agg node.)
286 if (((Agg *) result_plan)->aggs == NIL)
287 elog(ERROR, "union_planner: query is marked hasAggs, but I don't see any");
291 * For now, before we hand back the plan, check to see if there is a
292 * user-specified sort that needs to be done. Eventually, this will
293 * be moved into the guts of the planner s.t. user specified sorts
294 * will be considered as part of the planning process. Since we can
295 * only make use of user-specified sorts in special cases, we can do
296 * the optimization step later.
299 if (parse->uniqueFlag)
301 Plan *sortplan = make_sortplan(tlist, parse->sortClause, result_plan);
303 return ((Plan *) make_unique(tlist, sortplan, parse->uniqueFlag));
307 if (parse->sortClause && need_sortplan(parse->sortClause, result_plan))
308 return (make_sortplan(tlist, parse->sortClause, result_plan));
310 return ((Plan *) result_plan);
316 * make_subplanTargetList
317 * Generate appropriate target lists when grouping is required.
319 * When union_planner inserts Aggregate and/or Group/Sort plan nodes above
320 * the result of query_planner, we typically need to pass a different
321 * target list to query_planner than the outer plan nodes should have.
322 * This routine generates the correct target list for the subplan, and
323 * if necessary modifies the target list for the inserted nodes as well.
325 * The initial target list passed from the parser already contains entries
326 * for all ORDER BY and GROUP BY expressions, but it will not have entries
327 * for variables used only in HAVING clauses; so we need to add those
328 * variables to the subplan target list. Also, if we are doing either
329 * grouping or aggregation, we flatten all expressions except GROUP BY items
330 * into their component variables; the other expressions will be computed by
331 * the inserted nodes rather than by the subplan. For example,
333 * SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
334 * we want to pass this targetlist to the subplan:
336 * where the a+b target will be used by the Sort/Group steps, and the
337 * c and d targets will be needed to compute the aggregate results.
339 * 'parse' is the query being processed.
340 * 'tlist' is the query's target list. CAUTION: list elements may be
341 * modified by this routine!
342 * 'groupColIdx' receives an array of column numbers for the GROUP BY
343 * expressions (if there are any) in the subplan's target list.
345 * The result is the targetlist to be passed to the subplan. Also,
346 * the parent tlist is modified so that any nontrivial targetlist items that
347 * exactly match GROUP BY items are replaced by simple Var nodes referencing
348 * those outputs of the subplan. This avoids redundant recalculations in
350 * SELECT a+1, ... GROUP BY a+1
351 * Note, however, that other varnodes in the parent's targetlist (and
352 * havingQual, if any) will still need to be updated to refer to outputs
353 * of the subplan. This routine is quite large enough already, so we do
358 make_subplanTargetList(Query *parse,
360 AttrNumber **groupColIdx)
367 List *extravars = NIL;
369 AttrNumber *grpColIdx = NULL;
375 * If we're not grouping or aggregating, nothing to do here;
376 * query_planner should receive the unmodified target list.
378 if (!parse->hasAggs && !parse->groupClause && !parse->havingQual)
382 * If grouping, make a working copy of groupClause list (which we use
383 * just to verify that we found all the groupClause items in tlist).
384 * Also allocate space to remember where the group columns are in the
387 numCols = length(parse->groupClause);
390 glc = listCopy(parse->groupClause);
391 grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
392 *groupColIdx = grpColIdx;
395 sub_tlist = new_unsorted_tlist(tlist); /* make a modifiable copy */
398 * Step 1: build grpColIdx by finding targetlist items that match
399 * GroupBy entries. If there are aggregates, remove non-GroupBy items
400 * from sub_tlist, and reset its resnos accordingly. When we leave an
401 * expression in the subplan tlist, modify the parent tlist to copy
402 * the value from the subplan output rather than re-evaluating it.
404 prnt_tlist = tlist; /* scans parent tlist in sync with sl */
405 foreach(sl, sub_tlist)
407 TargetEntry *te = (TargetEntry *) lfirst(sl);
408 TargetEntry *parentte = (TargetEntry *) lfirst(prnt_tlist);
409 Resdom *resdom = te->resdom;
410 bool keepInSubPlan = true;
411 bool foundGroupClause = false;
414 foreach(gl, parse->groupClause)
416 GroupClause *grpcl = (GroupClause *) lfirst(gl);
418 keyno++; /* sort key # for this GroupClause */
419 if (grpcl->tleGroupref == resdom->resgroupref)
421 /* Found a matching groupclause; record info for sorting */
422 foundGroupClause = true;
423 resdom->reskey = keyno;
424 resdom->reskeyop = get_opcode(grpcl->grpOpoid);
425 grpColIdx[keyno - 1] = next_resno;
428 * Remove groupclause from our list of unmatched
429 * groupclauses. NB: this depends on having used a shallow
432 glc = lremove((void *) grpcl, glc);
437 if (!foundGroupClause)
441 * Non-GroupBy entry: remove it from subplan if there are
442 * aggregates in query - it will be evaluated by Aggregate
443 * plan. But do not remove simple-Var entries; we'd just have
444 * to add them back anyway, and we risk confusing
447 if (parse->hasAggs && !IsA(te->expr, Var))
448 keepInSubPlan = false;
453 /* Assign new sequential resnos to subplan tlist items */
454 resdom->resno = next_resno++;
455 if (!IsA(parentte->expr, Var))
459 * Since the item is being computed in the subplan, we can
460 * just make a Var node to reference it in the outer plan,
461 * rather than recomputing it there. Note we use varnoold
462 * = -1 as a flag to let replace_vars_with_subplan_refs
463 * know it needn't change this Var node. If it's only a
464 * Var anyway, we leave it alone for now;
465 * replace_vars_with_subplan_refs will fix it later.
467 parentte->expr = (Node *) makeVar(1, resdom->resno,
470 0, -1, resdom->resno);
477 * Remove this tlist item from the subplan, but remember the
478 * vars it needs. The outer tlist item probably needs
479 * changes, but that will happen later.
481 sub_tlist = lremove(te, sub_tlist);
482 extravars = nconc(extravars, pull_var_clause(te->expr));
485 prnt_tlist = lnext(prnt_tlist);
488 /* We should have found all the GROUP BY clauses in the tlist. */
489 if (length(glc) != 0)
490 elog(ERROR, "make_subplanTargetList: GROUP BY attribute not found in target list");
493 * Add subplan targets for any variables needed by removed tlist
494 * entries that aren't otherwise mentioned in the subplan target list.
495 * We'll also need targets for any variables seen only in HAVING.
497 extravars = nconc(extravars, pull_var_clause(parse->havingQual));
499 foreach(gl, extravars)
501 Var *v = (Var *) lfirst(gl);
503 if (tlist_member(v, sub_tlist) == NULL)
507 * Make sure sub_tlist element is a fresh object not shared
508 * with any other structure; not sure if anything will break
509 * if it is shared, but better to be safe...
511 sub_tlist = lappend(sub_tlist,
512 create_tl_element((Var *) copyObject(v),
522 make_groupplan(List *group_tlist,
525 AttrNumber *grpColIdx,
532 int numCols = length(groupClause);
535 * Make the targetlist for the Sort node; it always just references
536 * each of the corresponding target items of the subplan. We need to
537 * ensure that simple Vars in the subplan's target list are
538 * recognizable by replace_vars_with_subplan_refs when it's applied to
539 * the Sort/Group target list, so copy up their varnoold/varoattno.
542 foreach(sl, subplan->targetlist)
544 TargetEntry *te = (TargetEntry *) lfirst(sl);
545 Resdom *resdom = te->resdom;
548 if (IsA(te->expr, Var))
550 Var *subvar = (Var *) te->expr;
552 newvar = makeVar(1, resdom->resno,
553 resdom->restype, resdom->restypmod,
554 0, subvar->varnoold, subvar->varoattno);
558 newvar = makeVar(1, resdom->resno,
559 resdom->restype, resdom->restypmod,
560 0, -1, resdom->resno);
563 sort_tlist = lappend(sort_tlist,
564 makeTargetEntry((Resdom *) copyObject(resdom),
571 sortplan = make_sort(sort_tlist,
572 _NONAME_RELATION_ID_,
575 sortplan->plan.cost = subplan->cost; /* XXX assume no cost */
578 * If the caller gave us a target list, use it after fixing the
579 * variables. If not, we need the same sort of "repeater" tlist as for
584 group_tlist = copyObject(group_tlist); /* necessary?? */
585 replace_tlist_with_subplan_refs(group_tlist,
587 subplan->targetlist);
590 group_tlist = copyObject(sort_tlist);
593 * Make the Group node
595 grpplan = make_group(group_tlist, tuplePerGroup, numCols,
596 grpColIdx, sortplan);
598 return (Plan *) grpplan;
603 * Returns a sortplan which is basically a SORT node attached to the
604 * top of the plan returned from the planner. It also adds the
605 * cost of sorting into the plan.
607 * sortkeys: ( resdom1 resdom2 resdom3 ...)
608 * sortops: (sortop1 sortop2 sortop3 ...)
611 make_sortplan(List *tlist, List *sortcls, Plan *plannode)
613 Plan *sortplan = (Plan *) NULL;
614 List *temp_tlist = NIL;
616 Resdom *resnode = (Resdom *) NULL;
617 Resdom *resdom = (Resdom *) NULL;
621 * First make a copy of the tlist so that we don't corrupt the the
625 temp_tlist = new_unsorted_tlist(tlist);
629 SortClause *sortcl = (SortClause *) lfirst(i);
631 resnode = sortcl->resdom;
632 resdom = tlist_resdom(temp_tlist, resnode);
635 * Order the resdom keys and replace the operator OID for each key
636 * with the regproc OID.
638 resdom->reskey = keyno;
639 resdom->reskeyop = get_opcode(sortcl->opoid);
643 sortplan = (Plan *) make_sort(temp_tlist,
644 _NONAME_RELATION_ID_,
649 * XXX Assuming that an internal sort has no. cost. This is wrong, but
650 * given that at this point, we don't know the no. of tuples returned,
651 * etc, we can't do better than to add a constant cost. This will be
652 * fixed once we move the sort further into the planner, but for now
653 * ... functionality....
656 sortplan->cost = plannode->cost;
662 * pg_checkretval() -- check return value of a list of sql parse
665 * The return value of a sql function is the value returned by
666 * the final query in the function. We do some ad-hoc define-time
667 * type checking here to be sure that the user is returning the
670 * XXX Why is this function in this module?
673 pg_checkretval(Oid rettype, List *queryTreeList)
687 /* find the final query */
688 parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);
691 * test 1: if the last query is a utility invocation, then there had
692 * better not be a return value declared.
694 if (parse->commandType == CMD_UTILITY)
696 if (rettype == InvalidOid)
699 elog(ERROR, "return type mismatch in function decl: final query is a catalog utility");
702 /* okay, it's an ordinary query */
703 tlist = parse->targetList;
705 cmd = parse->commandType;
708 * test 2: if the function is declared to return no value, then the
709 * final query had better not be a retrieve.
711 if (rettype == InvalidOid)
713 if (cmd == CMD_SELECT)
715 "function declared with no return type, but final query is a retrieve");
720 /* by here, the function is declared to return some type */
721 if ((typ = typeidType(rettype)) == NULL)
722 elog(ERROR, "can't find return type %u for function\n", rettype);
725 * test 3: if the function is declared to return a value, then the
726 * final query had better be a retrieve.
728 if (cmd != CMD_SELECT)
729 elog(ERROR, "function declared to return type %s, but final query is not a retrieve", typeTypeName(typ));
732 * test 4: for base type returns, the target list should have exactly
733 * one entry, and its type should agree with what the user declared.
736 if (typeTypeRelid(typ) == InvalidOid)
738 if (ExecTargetListLength(tlist) > 1)
739 elog(ERROR, "function declared to return %s returns multiple values in final retrieve", typeTypeName(typ));
741 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
742 if (resnode->restype != rettype)
743 elog(ERROR, "return type mismatch in function: declared to return %s, returns %s", typeTypeName(typ), typeidTypeName(resnode->restype));
745 /* by here, base return types match */
750 * If the target list is of length 1, and the type of the varnode in
751 * the target list is the same as the declared return type, this is
752 * okay. This can happen, for example, where the body of the function
753 * is 'retrieve (x = func2())', where func2 has the same return type
754 * as the function that's calling it.
756 if (ExecTargetListLength(tlist) == 1)
758 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
759 if (resnode->restype == rettype)
764 * By here, the procedure returns a (set of) tuples. This part of the
765 * typechecking is a hack. We look up the relation that is the
766 * declared return type, and be sure that attributes 1 .. n in the
767 * target list match the declared types.
769 reln = heap_open(typeTypeRelid(typ));
771 if (!RelationIsValid(reln))
772 elog(ERROR, "cannot open relation relid %u", typeTypeRelid(typ));
775 relnatts = reln->rd_rel->relnatts;
777 if (ExecTargetListLength(tlist) != relnatts)
778 elog(ERROR, "function declared to return type %s does not retrieve (%s.*)", typeTypeName(typ), typeTypeName(typ));
780 /* expect attributes 1 .. n in order */
781 for (i = 1; i <= relnatts; i++)
783 TargetEntry *tle = lfirst(tlist);
784 Node *thenode = tle->expr;
786 tlist = lnext(tlist);
787 tletype = exprType(thenode);
789 #ifdef NOT_USED /* fix me */
790 /* this is tedious */
791 if (IsA(thenode, Var))
792 tletype = (Oid) ((Var *) thenode)->vartype;
793 else if (IsA(thenode, Const))
794 tletype = (Oid) ((Const *) thenode)->consttype;
795 else if (IsA(thenode, Param))
796 tletype = (Oid) ((Param *) thenode)->paramtype;
797 else if (IsA(thenode, Expr))
800 else if (IsA(thenode, LispList))
802 thenode = lfirst(thenode);
803 if (IsA(thenode, Oper))
804 tletype = (Oid) get_opresulttype((Oper *) thenode);
805 else if (IsA(thenode, Func))
806 tletype = (Oid) get_functype((Func *) thenode);
808 elog(ERROR, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
811 elog(ERROR, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
813 /* reach right in there, why don't you? */
814 if (tletype != reln->rd_att->attrs[i - 1]->atttypid)
815 elog(ERROR, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
826 * Support function for need_sortplan
830 get_matching_tle(Plan *plan, Resdom *resdom)
835 foreach(i, plan->targetlist)
837 tle = (TargetEntry *) lfirst(i);
838 if (tle->resdom->resno == resdom->resno)
846 * Check if a user requested ORDER BY is already satisfied by
847 * the choosen index scan.
849 * Returns TRUE if sort is required, FALSE if can be omitted.
853 need_sortplan(List *sortcls, Plan *plan)
856 IndexScan *indexScan;
860 Form_pg_index index_tup;
864 * Must be an IndexScan
867 if (nodeTag(plan) != T_IndexScan)
870 indexScan = (IndexScan *) plan;
873 * Should not have left- or righttree
876 if (plan->lefttree != NULL)
878 if (plan->righttree != NULL)
882 * Must be a single index scan
885 if (length(indexScan->indxid) != 1)
889 * Indices can only have up to 8 attributes. So an ORDER BY using
890 * more that 8 attributes could never be satisfied by an index.
893 if (length(sortcls) > 8)
897 * The choosen Index must be a btree
900 indexId = lfirsti(indexScan->indxid);
902 indexRel = index_open(indexId);
903 if (strcmp(nameout(&(indexRel->rd_am->amname)), "btree") != 0)
905 heap_close(indexRel);
908 heap_close(indexRel);
911 * Fetch the index tuple
914 htup = SearchSysCacheTuple(INDEXRELID,
915 ObjectIdGetDatum(indexId), 0, 0, 0);
916 if (!HeapTupleIsValid(htup))
917 elog(ERROR, "cache lookup for index %u failed", indexId);
918 index_tup = (Form_pg_index) GETSTRUCT(htup);
921 * Check if all the sort clauses match the attributes in the index
931 sortcl = (SortClause *) lfirst(i);
933 resdom = sortcl->resdom;
934 tle = get_matching_tle(plan, resdom);
943 if (nodeTag(tle->expr) != T_Var)
946 * The target list expression isn't a var, so it
947 * cannot be the indexed attribute
952 var = (Var *) (tle->expr);
954 if (var->varno != indexScan->scan.scanrelid)
957 * This Var isn't from the scan relation. So it isn't
964 if (var->varattno != index_tup->indkey[key_no])
967 * It isn't the indexed attribute.
973 if (oprid(oper("<", resdom->restype, resdom->restype, FALSE)) != sortcl->opoid)
976 * Sort order isn't in ascending order.
986 * Index matches ORDER BY - sort not required