Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8ac6d95

Browse files
committed
Cause planner to account for evaluation costs in targetlists and
HAVING quals. Normally this is an insignificant effect --- but itwill not be insignificant when these clauses contain sub-selects.The added costs cannot affect the planning of the query containingthem, but they might have an impact when the query is a sub-queryof a larger one.
1 parentd1686b4 commit8ac6d95

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

‎src/backend/optimizer/plan/createplan.c

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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,
13661366
SubqueryScan*node=makeNode(SubqueryScan);
13671367
Plan*plan=&node->scan.plan;
13681368

1369+
/* cost is figured here for the convenience of prepunion.c */
13691370
copy_plan_costsize(plan,subplan);
13701371
plan->targetlist=qptlist;
13711372
plan->qual=qpqual;
@@ -1488,7 +1489,7 @@ make_hash(List *tlist, List *hashkeys, Plan *lefttree)
14881489
*/
14891490
plan->startup_cost=plan->total_cost;
14901491
plan->targetlist=tlist;
1491-
plan->qual=NULL;
1492+
plan->qual=NIL;
14921493
plan->lefttree=lefttree;
14931494
plan->righttree=NULL;
14941495
node->hashkeys=hashkeys;
@@ -1646,6 +1647,7 @@ make_agg(Query *root, List *tlist, List *qual,
16461647
Agg*node=makeNode(Agg);
16471648
Plan*plan=&node->plan;
16481649
Pathagg_path;/* dummy for result of cost_agg */
1650+
QualCostqual_cost;
16491651

16501652
node->aggstrategy=aggstrategy;
16511653
node->numCols=numGroupCols;
@@ -1671,6 +1673,27 @@ make_agg(Query *root, List *tlist, List *qual,
16711673
else
16721674
plan->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+
16741697
plan->qual=qual;
16751698
plan->targetlist=tlist;
16761699
plan->lefttree=lefttree;
@@ -1690,6 +1713,7 @@ make_group(Query *root,
16901713
Group*node=makeNode(Group);
16911714
Plan*plan=&node->plan;
16921715
Pathgroup_path;/* dummy for result of cost_group */
1716+
QualCostqual_cost;
16931717

16941718
node->numCols=numGroupCols;
16951719
node->grpColIdx=grpColIdx;
@@ -1706,7 +1730,23 @@ make_group(Query *root,
17061730
/* One output tuple per estimated result group */
17071731
plan->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;
17101750
plan->targetlist=tlist;
17111751
plan->lefttree=lefttree;
17121752
plan->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-
17221761
Unique*
17231762
make_unique(List*tlist,Plan*lefttree,List*distinctList)
17241763
{
@@ -1911,6 +1950,16 @@ make_result(List *tlist,
19111950
plan->plan_width=0;/* XXX try to be smarter? */
19121951
}
19131952

1953+
if (resconstantqual)
1954+
{
1955+
QualCostqual_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+
19141963
plan->targetlist=tlist;
19151964
plan->qual=NIL;
19161965
plan->lefttree=subplan;

‎src/backend/optimizer/plan/planner.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.136 2002/12/19 23:25:01 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.137 2003/01/13 00:29:25 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -966,6 +966,7 @@ grouping_planner(Query *parse, double tuple_fraction)
966966
List*sub_tlist;
967967
List*group_pathkeys;
968968
AttrNumber*groupColIdx=NULL;
969+
QualCosttlist_cost;
969970
doublesub_tuple_fraction;
970971
Path*cheapest_path;
971972
Path*sorted_path;
@@ -1452,6 +1453,27 @@ grouping_planner(Query *parse, double tuple_fraction)
14521453
*/
14531454
result_plan->targetlist=sub_tlist;
14541455
}
1456+
/*
1457+
* Also, account for the cost of evaluation of the sub_tlist.
1458+
*
1459+
* Up to now, we have only been dealing with "flat" tlists, containing
1460+
* just Vars. So their evaluation cost is zero according to the
1461+
* model used by cost_qual_eval() (or if you prefer, the cost is
1462+
* factored into cpu_tuple_cost). Thus we can avoid accounting for
1463+
* tlist cost throughout query_planner() and subroutines.
1464+
* But now we've inserted a tlist that might contain actual operators,
1465+
* sub-selects, etc --- so we'd better account for its cost.
1466+
*
1467+
* Below this point, any tlist eval cost for added-on nodes should
1468+
* be accounted for as we create those nodes. Presently, of the
1469+
* node types we can add on, only Agg and Group project new tlists
1470+
* (the rest just copy their input tuples) --- so make_agg() and
1471+
* make_group() are responsible for computing the added cost.
1472+
*/
1473+
cost_qual_eval(&tlist_cost,sub_tlist);
1474+
result_plan->startup_cost+=tlist_cost.startup;
1475+
result_plan->total_cost+=tlist_cost.startup+
1476+
tlist_cost.per_tuple*result_plan->plan_rows;
14551477

14561478
/*
14571479
* Insert AGG or GROUP node if needed, plus an explicit sort step

‎src/backend/optimizer/plan/subselect.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.64 2003/01/12 04:03:34 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.65 2003/01/13 00:29:26 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -378,6 +378,8 @@ make_subplan(SubLink *slink, List *lefthand)
378378
plan->plan_width);
379379
matplan->startup_cost=matpath.startup_cost;
380380
matplan->total_cost=matpath.total_cost;
381+
matplan->plan_rows=plan->plan_rows;
382+
matplan->plan_width=plan->plan_width;
381383
/* parameter kluge --- see comments above */
382384
matplan->extParam=listCopy(plan->extParam);
383385
matplan->locParam=listCopy(plan->locParam);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp