1010 *
1111 *
1212 * IDENTIFICATION
13- * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.128 2002/12/12 15:49:32 tgl Exp $
13+ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.129 2003/01/13 00:29:25 tgl Exp $
1414 *
1515 *-------------------------------------------------------------------------
1616 */
@@ -1366,6 +1366,7 @@ make_subqueryscan(List *qptlist,
13661366SubqueryScan * node = makeNode (SubqueryScan );
13671367Plan * plan = & node -> scan .plan ;
13681368
1369+ /* cost is figured here for the convenience of prepunion.c */
13691370copy_plan_costsize (plan ,subplan );
13701371plan -> targetlist = qptlist ;
13711372plan -> qual = qpqual ;
@@ -1488,7 +1489,7 @@ make_hash(List *tlist, List *hashkeys, Plan *lefttree)
14881489 */
14891490plan -> startup_cost = plan -> total_cost ;
14901491plan -> targetlist = tlist ;
1491- plan -> qual = NULL ;
1492+ plan -> qual = NIL ;
14921493plan -> lefttree = lefttree ;
14931494plan -> righttree = NULL ;
14941495node -> hashkeys = hashkeys ;
@@ -1646,6 +1647,7 @@ make_agg(Query *root, List *tlist, List *qual,
16461647Agg * node = makeNode (Agg );
16471648Plan * plan = & node -> plan ;
16481649Path agg_path ;/* dummy for result of cost_agg */
1650+ QualCost qual_cost ;
16491651
16501652node -> aggstrategy = aggstrategy ;
16511653node -> numCols = numGroupCols ;
@@ -1671,6 +1673,27 @@ make_agg(Query *root, List *tlist, List *qual,
16711673else
16721674plan -> plan_rows = numGroups ;
16731675
1676+ /*
1677+ * We also need to account for the cost of evaluation of the qual
1678+ * (ie, the HAVING clause) and the tlist. Note that cost_qual_eval
1679+ * doesn't charge anything for Aggref nodes; this is okay since
1680+ * they are really comparable to Vars.
1681+ *
1682+ * See notes in grouping_planner about why this routine and make_group
1683+ * are the only ones in this file that worry about tlist eval cost.
1684+ */
1685+ if (qual )
1686+ {
1687+ cost_qual_eval (& qual_cost ,qual );
1688+ plan -> startup_cost += qual_cost .startup ;
1689+ plan -> total_cost += qual_cost .startup ;
1690+ plan -> total_cost += qual_cost .per_tuple * plan -> plan_rows ;
1691+ }
1692+ cost_qual_eval (& qual_cost ,tlist );
1693+ plan -> startup_cost += qual_cost .startup ;
1694+ plan -> total_cost += qual_cost .startup ;
1695+ plan -> total_cost += qual_cost .per_tuple * plan -> plan_rows ;
1696+
16741697plan -> qual = qual ;
16751698plan -> targetlist = tlist ;
16761699plan -> lefttree = lefttree ;
@@ -1690,6 +1713,7 @@ make_group(Query *root,
16901713Group * node = makeNode (Group );
16911714Plan * plan = & node -> plan ;
16921715Path group_path ;/* dummy for result of cost_group */
1716+ QualCost qual_cost ;
16931717
16941718node -> numCols = numGroupCols ;
16951719node -> grpColIdx = grpColIdx ;
@@ -1706,7 +1730,23 @@ make_group(Query *root,
17061730/* One output tuple per estimated result group */
17071731plan -> plan_rows = numGroups ;
17081732
1709- plan -> qual = NULL ;
1733+ /*
1734+ * We also need to account for the cost of evaluation of the tlist.
1735+ *
1736+ * XXX this double-counts the cost of evaluation of any expressions
1737+ * used for grouping, since in reality those will have been evaluated
1738+ * at a lower plan level and will only be copied by the Group node.
1739+ * Worth fixing?
1740+ *
1741+ * See notes in grouping_planner about why this routine and make_agg
1742+ * are the only ones in this file that worry about tlist eval cost.
1743+ */
1744+ cost_qual_eval (& qual_cost ,tlist );
1745+ plan -> startup_cost += qual_cost .startup ;
1746+ plan -> total_cost += qual_cost .startup ;
1747+ plan -> total_cost += qual_cost .per_tuple * plan -> plan_rows ;
1748+
1749+ plan -> qual = NIL ;
17101750plan -> targetlist = tlist ;
17111751plan -> lefttree = lefttree ;
17121752plan -> righttree = (Plan * )NULL ;
@@ -1718,7 +1758,6 @@ make_group(Query *root,
17181758 * distinctList is a list of SortClauses, identifying the targetlist items
17191759 * that should be considered by the Unique filter.
17201760 */
1721-
17221761Unique *
17231762make_unique (List * tlist ,Plan * lefttree ,List * distinctList )
17241763{
@@ -1911,6 +1950,16 @@ make_result(List *tlist,
19111950plan -> plan_width = 0 ;/* XXX try to be smarter? */
19121951}
19131952
1953+ if (resconstantqual )
1954+ {
1955+ QualCost qual_cost ;
1956+
1957+ cost_qual_eval (& qual_cost , (List * )resconstantqual );
1958+ /* resconstantqual is evaluated once at startup */
1959+ plan -> startup_cost += qual_cost .startup + qual_cost .per_tuple ;
1960+ plan -> total_cost += qual_cost .startup + qual_cost .per_tuple ;
1961+ }
1962+
19141963plan -> targetlist = tlist ;
19151964plan -> qual = NIL ;
19161965plan -> lefttree = subplan ;