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.59 1999/07/15 22:39:27 momjian Exp $
12 *-------------------------------------------------------------------------
14 #include <sys/types.h>
19 #include "nodes/makefuncs.h"
20 #include "catalog/pg_type.h"
21 #include "parser/parse_expr.h"
23 #include "utils/lsyscache.h"
24 #include "access/heapam.h"
26 #include "optimizer/internal.h"
27 #include "optimizer/planner.h"
28 #include "optimizer/prep.h"
29 #include "optimizer/planmain.h"
30 #include "optimizer/subselect.h"
32 /* DATA STRUCTURE CREATION/MANIPULATION ROUTINES */
33 #include "optimizer/clauses.h"
34 #include "optimizer/tlist.h"
35 #include "optimizer/var.h"
37 #include "executor/executor.h"
39 #include "utils/builtins.h"
40 #include "utils/syscache.h"
41 #include "access/genam.h"
42 #include "parser/parse_oper.h"
44 static List *make_subplanTargetList(Query *parse, List *tlist,
45 AttrNumber **groupColIdx);
46 static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
47 List *groupClause, AttrNumber *grpColIdx,
49 static bool need_sortplan(List *sortcls, Plan *plan);
50 static Plan *make_sortplan(List *tlist, List *sortcls, Plan *plannode);
52 /*****************************************************************************
54 * Query optimizer entry point
56 *****************************************************************************/
62 /* Initialize state for subselects */
63 PlannerQueryLevel = 1;
64 PlannerInitPlan = NULL;
65 PlannerParamVar = NULL;
68 transformKeySetQuery(parse);
69 result_plan = union_planner(parse);
71 Assert(PlannerQueryLevel == 1);
72 if (PlannerPlanId > 0)
74 result_plan->initPlan = PlannerInitPlan;
75 (void) SS_finalize_plan(result_plan);
77 result_plan->nParamExec = length(PlannerParamVar);
85 * Invokes the planner on union queries if there are any left,
86 * recursing if necessary to get them all, then processes normal plans.
88 * Returns a query plan.
92 union_planner(Query *parse)
94 List *tlist = parse->targetList;
95 List *rangetable = parse->rtable;
96 Plan *result_plan = (Plan *) NULL;
97 AttrNumber *groupColIdx = NULL;
100 if (parse->unionClause)
102 result_plan = (Plan *) plan_union_queries(parse);
103 /* XXX do we need to do this? bjm 12/19/97 */
104 tlist = preprocess_targetlist(tlist,
106 parse->resultRelation,
109 else if ((rt_index = first_inherit_rt_entry(rangetable)) != -1)
114 * Generate appropriate target list for subplan; may be different
115 * from tlist if grouping or aggregation is needed.
117 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
120 * Recursively plan the subqueries needed for inheritance
122 result_plan = (Plan *) plan_inherit_queries(parse, sub_tlist,
126 * Fix up outer target list. NOTE: unlike the case for non-inherited
127 * query, we pass the unfixed tlist to subplans, which do their own
128 * fixing. But we still want to fix the outer target list afterwards.
129 * I *think* this is correct --- doing the fix before recursing is
130 * definitely wrong, because preprocess_targetlist() will do the
131 * wrong thing if invoked twice on the same list. Maybe that is a bug?
134 tlist = preprocess_targetlist(tlist,
136 parse->resultRelation,
139 if (parse->rowMark != NULL)
140 elog(ERROR, "SELECT FOR UPDATE is not supported for inherit queries");
146 /* Preprocess targetlist in case we are inside an INSERT/UPDATE. */
147 tlist = preprocess_targetlist(tlist,
149 parse->resultRelation,
153 * Add row-mark targets for UPDATE (should this be done in
154 * preprocess_targetlist?)
156 if (parse->rowMark != NULL)
160 foreach(l, parse->rowMark)
162 RowMark *rowmark = (RowMark *) lfirst(l);
168 if (!(rowmark->info & ROW_MARK_FOR_UPDATE))
171 resname = (char *) palloc(32);
172 sprintf(resname, "ctid%u", rowmark->rti);
173 resdom = makeResdom(length(tlist) + 1,
181 var = makeVar(rowmark->rti, -1, TIDOID,
182 -1, 0, rowmark->rti, -1);
184 ctid = makeTargetEntry(resdom, (Node *) var);
185 tlist = lappend(tlist, ctid);
190 * Generate appropriate target list for subplan; may be different
191 * from tlist if grouping or aggregation is needed.
193 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
195 /* Generate the (sub) plan */
196 result_plan = query_planner(parse,
199 (List *) parse->qual);
202 /* query_planner returns NULL if it thinks plan is bogus */
204 elog(ERROR, "union_planner: failed to create plan");
207 * If we have a GROUP BY clause, insert a group node (with the
208 * appropriate sort node.)
210 if (parse->groupClause)
216 * Decide whether how many tuples per group the Group node needs
217 * to return. (Needs only one tuple per group if no aggregate is
218 * present. Otherwise, need every tuple from the group to do the
219 * aggregation.) Note tuplePerGroup is named backwards :-(
221 tuplePerGroup = parse->hasAggs;
224 * If there are aggregates then the Group node should just return
225 * the same (simplified) tlist as the subplan, which we indicate
226 * to make_groupplan by passing NIL. If there are no aggregates
227 * then the Group node had better compute the final tlist.
229 group_tlist = parse->hasAggs ? NIL : tlist;
231 result_plan = make_groupplan(group_tlist,
239 * If we have a HAVING clause, do the necessary things with it.
241 if (parse->havingQual)
243 /* convert the havingQual to conjunctive normal form (cnf) */
244 parse->havingQual = (Node *) cnfify((Expr *) parse->havingQual, true);
246 if (parse->hasSubLinks)
249 * There may be a subselect in the havingQual, so we have to
250 * process it using the same function as for a subselect in
253 parse->havingQual = SS_process_sublinks(parse->havingQual);
256 * Check for ungrouped variables passed to subplans. (Probably
257 * this should be done for the targetlist as well???)
259 check_having_for_ungrouped_vars(parse->havingQual,
264 /* Calculate the opfids from the opnos */
265 parse->havingQual = (Node *) fix_opids((List *) parse->havingQual);
269 * If aggregate is present, insert the agg node
273 result_plan = (Plan *) make_agg(tlist, result_plan);
275 /* HAVING clause, if any, becomes qual of the Agg node */
276 result_plan->qual = (List *) parse->havingQual;
279 * Update vars to refer to subplan result tuples, find Aggrefs,
280 * make sure there is an Aggref in every HAVING clause.
282 if (!set_agg_tlist_references((Agg *) result_plan))
283 elog(ERROR, "SELECT/HAVING requires aggregates to be valid");
286 * Check that we actually found some aggregates, else executor
287 * will die unpleasantly. (This defends against possible bugs in
288 * parser or rewrite that might cause hasAggs to be incorrectly
289 * set 'true'. It's not easy to recover here, since we've already
290 * made decisions assuming there will be an Agg node.)
292 if (((Agg *) result_plan)->aggs == NIL)
293 elog(ERROR, "union_planner: query is marked hasAggs, but I don't see any");
297 * For now, before we hand back the plan, check to see if there is a
298 * user-specified sort that needs to be done. Eventually, this will
299 * be moved into the guts of the planner s.t. user specified sorts
300 * will be considered as part of the planning process. Since we can
301 * only make use of user-specified sorts in special cases, we can do
302 * the optimization step later.
305 if (parse->uniqueFlag)
307 Plan *sortplan = make_sortplan(tlist, parse->sortClause, result_plan);
309 return ((Plan *) make_unique(tlist, sortplan, parse->uniqueFlag));
313 if (parse->sortClause && need_sortplan(parse->sortClause, result_plan))
314 return (make_sortplan(tlist, parse->sortClause, result_plan));
316 return ((Plan *) result_plan);
322 * make_subplanTargetList
323 * Generate appropriate target lists when grouping is required.
325 * When union_planner inserts Aggregate and/or Group/Sort plan nodes above
326 * the result of query_planner, we typically need to pass a different
327 * target list to query_planner than the outer plan nodes should have.
328 * This routine generates the correct target list for the subplan, and
329 * if necessary modifies the target list for the inserted nodes as well.
331 * The initial target list passed from the parser already contains entries
332 * for all ORDER BY and GROUP BY expressions, but it will not have entries
333 * for variables used only in HAVING clauses; so we need to add those
334 * variables to the subplan target list. Also, if we are doing either
335 * grouping or aggregation, we flatten all expressions except GROUP BY items
336 * into their component variables; the other expressions will be computed by
337 * the inserted nodes rather than by the subplan. For example,
339 * SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
340 * we want to pass this targetlist to the subplan:
342 * where the a+b target will be used by the Sort/Group steps, and the
343 * c and d targets will be needed to compute the aggregate results.
345 * 'parse' is the query being processed.
346 * 'tlist' is the query's target list. CAUTION: list elements may be
347 * modified by this routine!
348 * 'groupColIdx' receives an array of column numbers for the GROUP BY
349 * expressions (if there are any) in the subplan's target list.
351 * The result is the targetlist to be passed to the subplan. Also,
352 * the parent tlist is modified so that any nontrivial targetlist items that
353 * exactly match GROUP BY items are replaced by simple Var nodes referencing
354 * those outputs of the subplan. This avoids redundant recalculations in
356 * SELECT a+1, ... GROUP BY a+1
357 * Note, however, that other varnodes in the parent's targetlist (and
358 * havingQual, if any) will still need to be updated to refer to outputs
359 * of the subplan. This routine is quite large enough already, so we do
364 make_subplanTargetList(Query *parse,
366 AttrNumber **groupColIdx)
373 List *extravars = NIL;
375 AttrNumber *grpColIdx = NULL;
381 * If we're not grouping or aggregating, nothing to do here;
382 * query_planner should receive the unmodified target list.
384 if (!parse->hasAggs && !parse->groupClause && !parse->havingQual)
388 * If grouping, make a working copy of groupClause list (which we use
389 * just to verify that we found all the groupClause items in tlist).
390 * Also allocate space to remember where the group columns are in the
393 numCols = length(parse->groupClause);
396 glc = listCopy(parse->groupClause);
397 grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
398 *groupColIdx = grpColIdx;
401 sub_tlist = new_unsorted_tlist(tlist); /* make a modifiable copy */
404 * Step 1: build grpColIdx by finding targetlist items that match
405 * GroupBy entries. If there are aggregates, remove non-GroupBy items
406 * from sub_tlist, and reset its resnos accordingly. When we leave an
407 * expression in the subplan tlist, modify the parent tlist to copy
408 * the value from the subplan output rather than re-evaluating it.
410 prnt_tlist = tlist; /* scans parent tlist in sync with sl */
411 foreach(sl, sub_tlist)
413 TargetEntry *te = (TargetEntry *) lfirst(sl);
414 TargetEntry *parentte = (TargetEntry *) lfirst(prnt_tlist);
415 Resdom *resdom = te->resdom;
416 bool keepInSubPlan = true;
417 bool foundGroupClause = false;
420 foreach(gl, parse->groupClause)
422 GroupClause *grpcl = (GroupClause *) lfirst(gl);
424 keyno++; /* sort key # for this GroupClause */
425 if (grpcl->tleGroupref == resdom->resgroupref)
427 /* Found a matching groupclause; record info for sorting */
428 foundGroupClause = true;
429 resdom->reskey = keyno;
430 resdom->reskeyop = get_opcode(grpcl->grpOpoid);
431 grpColIdx[keyno - 1] = next_resno;
434 * Remove groupclause from our list of unmatched
435 * groupclauses. NB: this depends on having used a shallow
438 glc = lremove((void *) grpcl, glc);
443 if (!foundGroupClause)
447 * Non-GroupBy entry: remove it from subplan if there are
448 * aggregates in query - it will be evaluated by Aggregate
449 * plan. But do not remove simple-Var entries; we'd just have
450 * to add them back anyway, and we risk confusing
453 if (parse->hasAggs && !IsA(te->expr, Var))
454 keepInSubPlan = false;
459 /* Assign new sequential resnos to subplan tlist items */
460 resdom->resno = next_resno++;
461 if (!IsA(parentte->expr, Var))
465 * Since the item is being computed in the subplan, we can
466 * just make a Var node to reference it in the outer plan,
467 * rather than recomputing it there. Note we use varnoold
468 * = -1 as a flag to let replace_vars_with_subplan_refs
469 * know it needn't change this Var node. If it's only a
470 * Var anyway, we leave it alone for now;
471 * replace_vars_with_subplan_refs will fix it later.
473 parentte->expr = (Node *) makeVar(1, resdom->resno,
476 0, -1, resdom->resno);
483 * Remove this tlist item from the subplan, but remember the
484 * vars it needs. The outer tlist item probably needs
485 * changes, but that will happen later.
487 sub_tlist = lremove(te, sub_tlist);
488 extravars = nconc(extravars, pull_var_clause(te->expr));
491 prnt_tlist = lnext(prnt_tlist);
494 /* We should have found all the GROUP BY clauses in the tlist. */
495 if (length(glc) != 0)
496 elog(ERROR, "make_subplanTargetList: GROUP BY attribute not found in target list");
499 * Add subplan targets for any variables needed by removed tlist
500 * entries that aren't otherwise mentioned in the subplan target list.
501 * We'll also need targets for any variables seen only in HAVING.
503 extravars = nconc(extravars, pull_var_clause(parse->havingQual));
505 foreach(gl, extravars)
507 Var *v = (Var *) lfirst(gl);
509 if (tlist_member(v, sub_tlist) == NULL)
513 * Make sure sub_tlist element is a fresh object not shared
514 * with any other structure; not sure if anything will break
515 * if it is shared, but better to be safe...
517 sub_tlist = lappend(sub_tlist,
518 create_tl_element((Var *) copyObject(v),
528 make_groupplan(List *group_tlist,
531 AttrNumber *grpColIdx,
538 int numCols = length(groupClause);
541 * Make the targetlist for the Sort node; it always just references
542 * each of the corresponding target items of the subplan. We need to
543 * ensure that simple Vars in the subplan's target list are
544 * recognizable by replace_vars_with_subplan_refs when it's applied to
545 * the Sort/Group target list, so copy up their varnoold/varoattno.
548 foreach(sl, subplan->targetlist)
550 TargetEntry *te = (TargetEntry *) lfirst(sl);
551 Resdom *resdom = te->resdom;
554 if (IsA(te->expr, Var))
556 Var *subvar = (Var *) te->expr;
558 newvar = makeVar(1, resdom->resno,
559 resdom->restype, resdom->restypmod,
560 0, subvar->varnoold, subvar->varoattno);
564 newvar = makeVar(1, resdom->resno,
565 resdom->restype, resdom->restypmod,
566 0, -1, resdom->resno);
569 sort_tlist = lappend(sort_tlist,
570 makeTargetEntry((Resdom *) copyObject(resdom),
577 sortplan = make_sort(sort_tlist,
578 _NONAME_RELATION_ID_,
581 sortplan->plan.cost = subplan->cost; /* XXX assume no cost */
584 * If the caller gave us a target list, use it after fixing the
585 * variables. If not, we need the same sort of "repeater" tlist as for
590 group_tlist = copyObject(group_tlist); /* necessary?? */
591 replace_tlist_with_subplan_refs(group_tlist,
593 subplan->targetlist);
596 group_tlist = copyObject(sort_tlist);
599 * Make the Group node
601 grpplan = make_group(group_tlist, tuplePerGroup, numCols,
602 grpColIdx, sortplan);
604 return (Plan *) grpplan;
609 * Returns a sortplan which is basically a SORT node attached to the
610 * top of the plan returned from the planner. It also adds the
611 * cost of sorting into the plan.
613 * sortkeys: ( resdom1 resdom2 resdom3 ...)
614 * sortops: (sortop1 sortop2 sortop3 ...)
617 make_sortplan(List *tlist, List *sortcls, Plan *plannode)
619 Plan *sortplan = (Plan *) NULL;
620 List *temp_tlist = NIL;
622 Resdom *resnode = (Resdom *) NULL;
623 Resdom *resdom = (Resdom *) NULL;
627 * First make a copy of the tlist so that we don't corrupt the the
631 temp_tlist = new_unsorted_tlist(tlist);
635 SortClause *sortcl = (SortClause *) lfirst(i);
637 resnode = sortcl->resdom;
638 resdom = tlist_resdom(temp_tlist, resnode);
641 * Order the resdom keys and replace the operator OID for each key
642 * with the regproc OID.
644 resdom->reskey = keyno;
645 resdom->reskeyop = get_opcode(sortcl->opoid);
649 sortplan = (Plan *) make_sort(temp_tlist,
650 _NONAME_RELATION_ID_,
655 * XXX Assuming that an internal sort has no. cost. This is wrong, but
656 * given that at this point, we don't know the no. of tuples returned,
657 * etc, we can't do better than to add a constant cost. This will be
658 * fixed once we move the sort further into the planner, but for now
659 * ... functionality....
662 sortplan->cost = plannode->cost;
668 * pg_checkretval() -- check return value of a list of sql parse
671 * The return value of a sql function is the value returned by
672 * the final query in the function. We do some ad-hoc define-time
673 * type checking here to be sure that the user is returning the
676 * XXX Why is this function in this module?
679 pg_checkretval(Oid rettype, List *queryTreeList)
693 /* find the final query */
694 parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);
697 * test 1: if the last query is a utility invocation, then there had
698 * better not be a return value declared.
700 if (parse->commandType == CMD_UTILITY)
702 if (rettype == InvalidOid)
705 elog(ERROR, "return type mismatch in function decl: final query is a catalog utility");
708 /* okay, it's an ordinary query */
709 tlist = parse->targetList;
711 cmd = parse->commandType;
714 * test 2: if the function is declared to return no value, then the
715 * final query had better not be a retrieve.
717 if (rettype == InvalidOid)
719 if (cmd == CMD_SELECT)
721 "function declared with no return type, but final query is a retrieve");
726 /* by here, the function is declared to return some type */
727 if ((typ = typeidType(rettype)) == NULL)
728 elog(ERROR, "can't find return type %u for function\n", rettype);
731 * test 3: if the function is declared to return a value, then the
732 * final query had better be a retrieve.
734 if (cmd != CMD_SELECT)
735 elog(ERROR, "function declared to return type %s, but final query is not a retrieve", typeTypeName(typ));
738 * test 4: for base type returns, the target list should have exactly
739 * one entry, and its type should agree with what the user declared.
742 if (typeTypeRelid(typ) == InvalidOid)
744 if (ExecTargetListLength(tlist) > 1)
745 elog(ERROR, "function declared to return %s returns multiple values in final retrieve", typeTypeName(typ));
747 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
748 if (resnode->restype != rettype)
749 elog(ERROR, "return type mismatch in function: declared to return %s, returns %s", typeTypeName(typ), typeidTypeName(resnode->restype));
751 /* by here, base return types match */
756 * If the target list is of length 1, and the type of the varnode in
757 * the target list is the same as the declared return type, this is
758 * okay. This can happen, for example, where the body of the function
759 * is 'retrieve (x = func2())', where func2 has the same return type
760 * as the function that's calling it.
762 if (ExecTargetListLength(tlist) == 1)
764 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
765 if (resnode->restype == rettype)
770 * By here, the procedure returns a (set of) tuples. This part of the
771 * typechecking is a hack. We look up the relation that is the
772 * declared return type, and be sure that attributes 1 .. n in the
773 * target list match the declared types.
775 reln = heap_open(typeTypeRelid(typ));
777 if (!RelationIsValid(reln))
778 elog(ERROR, "cannot open relation relid %u", typeTypeRelid(typ));
781 relnatts = reln->rd_rel->relnatts;
783 if (ExecTargetListLength(tlist) != relnatts)
784 elog(ERROR, "function declared to return type %s does not retrieve (%s.*)", typeTypeName(typ), typeTypeName(typ));
786 /* expect attributes 1 .. n in order */
787 for (i = 1; i <= relnatts; i++)
789 TargetEntry *tle = lfirst(tlist);
790 Node *thenode = tle->expr;
792 tlist = lnext(tlist);
793 tletype = exprType(thenode);
795 #ifdef NOT_USED /* fix me */
796 /* this is tedious */
797 if (IsA(thenode, Var))
798 tletype = (Oid) ((Var *) thenode)->vartype;
799 else if (IsA(thenode, Const))
800 tletype = (Oid) ((Const *) thenode)->consttype;
801 else if (IsA(thenode, Param))
802 tletype = (Oid) ((Param *) thenode)->paramtype;
803 else if (IsA(thenode, Expr))
806 else if (IsA(thenode, LispList))
808 thenode = lfirst(thenode);
809 if (IsA(thenode, Oper))
810 tletype = (Oid) get_opresulttype((Oper *) thenode);
811 else if (IsA(thenode, Func))
812 tletype = (Oid) get_functype((Func *) thenode);
814 elog(ERROR, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
817 elog(ERROR, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
819 /* reach right in there, why don't you? */
820 if (tletype != reln->rd_att->attrs[i - 1]->atttypid)
821 elog(ERROR, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
832 * Support function for need_sortplan
836 get_matching_tle(Plan *plan, Resdom *resdom)
841 foreach(i, plan->targetlist)
843 tle = (TargetEntry *) lfirst(i);
844 if (tle->resdom->resno == resdom->resno)
852 * Check if a user requested ORDER BY is already satisfied by
853 * the choosen index scan.
855 * Returns TRUE if sort is required, FALSE if can be omitted.
859 need_sortplan(List *sortcls, Plan *plan)
862 IndexScan *indexScan;
866 Form_pg_index index_tup;
870 * Must be an IndexScan
873 if (nodeTag(plan) != T_IndexScan)
876 indexScan = (IndexScan *) plan;
879 * Should not have left- or righttree
882 if (plan->lefttree != NULL)
884 if (plan->righttree != NULL)
888 * Must be a single index scan
891 if (length(indexScan->indxid) != 1)
895 * Indices can only have up to 8 attributes. So an ORDER BY using
896 * more that 8 attributes could never be satisfied by an index.
899 if (length(sortcls) > 8)
903 * The choosen Index must be a btree
906 indexId = lfirsti(indexScan->indxid);
908 indexRel = index_open(indexId);
909 if (strcmp(nameout(&(indexRel->rd_am->amname)), "btree") != 0)
911 heap_close(indexRel);
914 heap_close(indexRel);
917 * Fetch the index tuple
920 htup = SearchSysCacheTuple(INDEXRELID,
921 ObjectIdGetDatum(indexId), 0, 0, 0);
922 if (!HeapTupleIsValid(htup))
923 elog(ERROR, "cache lookup for index %u failed", indexId);
924 index_tup = (Form_pg_index) GETSTRUCT(htup);
927 * Check if all the sort clauses match the attributes in the index
937 sortcl = (SortClause *) lfirst(i);
939 resdom = sortcl->resdom;
940 tle = get_matching_tle(plan, resdom);
949 if (nodeTag(tle->expr) != T_Var)
952 * The target list expression isn't a var, so it
953 * cannot be the indexed attribute
958 var = (Var *) (tle->expr);
960 if (var->varno != indexScan->scan.scanrelid)
963 * This Var isn't from the scan relation. So it isn't
970 if (var->varattno != index_tup->indkey[key_no])
973 * It isn't the indexed attribute.
979 if (oprid(oper("<", resdom->restype, resdom->restype, FALSE)) != sortcl->opoid)
982 * Sort order isn't in ascending order.
992 * Index matches ORDER BY - sort not required