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

Commit0d7abfe

Browse files
committed
Marginal tweaks to make sure that roundoff error won't cause us to make
a bad choice between sorted and hashed aggregation.
1 parent056467e commit0d7abfe

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

‎src/backend/optimizer/path/costsize.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* Portions Copyright (c) 1994, Regents of the University of California
5050
*
5151
* IDENTIFICATION
52-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.105 2003/02/08 20:20:54 tgl Exp $
52+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.106 2003/02/15 21:39:58 tgl Exp $
5353
*
5454
*-------------------------------------------------------------------------
5555
*/
@@ -601,6 +601,15 @@ cost_agg(Path *path, Query *root,
601601
*
602602
* We will produce a single output tuple if not grouping,
603603
* and a tuple per group otherwise.
604+
*
605+
* Note: in this cost model, AGG_SORTED and AGG_HASHED have exactly the
606+
* same total CPU cost, but AGG_SORTED has lower startup cost. If the
607+
* input path is already sorted appropriately, AGG_SORTED should be
608+
* preferred (since it has no risk of memory overflow). This will happen
609+
* as long as the computed total costs are indeed exactly equal --- but
610+
* if there's roundoff error we might do the wrong thing. So be sure
611+
* that the computations below form the same intermediate values in the
612+
* same order.
604613
*/
605614
if (aggstrategy==AGG_PLAIN)
606615
{
@@ -614,15 +623,17 @@ cost_agg(Path *path, Query *root,
614623
/* Here we are able to deliver output on-the-fly */
615624
startup_cost=input_startup_cost;
616625
total_cost=input_total_cost;
617-
total_cost+=cpu_operator_cost* (input_tuples+numGroups)*numAggs;
626+
/* calcs phrased this way to match HASHED case, see note above */
618627
total_cost+=cpu_operator_cost*input_tuples*numGroupCols;
628+
total_cost+=cpu_operator_cost*input_tuples*numAggs;
629+
total_cost+=cpu_operator_cost*numGroups*numAggs;
619630
}
620631
else
621632
{
622633
/* must be AGG_HASHED */
623634
startup_cost=input_total_cost;
624-
startup_cost+=cpu_operator_cost*input_tuples*numAggs;
625635
startup_cost+=cpu_operator_cost*input_tuples*numGroupCols;
636+
startup_cost+=cpu_operator_cost*input_tuples*numAggs;
626637
total_cost=startup_cost;
627638
total_cost+=cpu_operator_cost*numGroups*numAggs;
628639
}

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

Lines changed: 2 additions & 2 deletions
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.147 2003/02/1520:12:40 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.148 2003/02/1521:39:58 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1003,7 +1003,7 @@ grouping_planner(Query *parse, double tuple_fraction)
10031003
tuple_fraction /=dNumGroups;
10041004

10051005
if (compare_fractional_path_costs(&hashed_p,&sorted_p,
1006-
tuple_fraction) <=0)
1006+
tuple_fraction)<0)
10071007
{
10081008
/* Hashed is cheaper, so use it */
10091009
use_hashed_grouping= true;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp