Commit 37c443ee authored by Tom Lane's avatar Tom Lane

Fix compare_fuzzy_path_costs() to behave a bit more sanely. The original

coding would ignore startup cost differences of less than 1% of the
estimated total cost; which was OK for normal planning but highly not OK
if a very small LIMIT was applied afterwards, so that startup cost becomes
the name of the game.  Instead, compare startup and total costs fuzzily
but independently.  This changes the plan selected for two queries in the
regression tests; adjust expected-output files for resulting changes in
row order.  Per reports from Dawid Kuroczko and Sam Mason.
parent 3758affc
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/util/pathnode.c,v 1.123 2005/07/15 17:09:25 tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/util/pathnode.c,v 1.124 2005/07/22 19:12:01 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -98,60 +98,41 @@ compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
static int
compare_fuzzy_path_costs(Path *path1, Path *path2, CostSelector criterion)
{
Cost fuzz;
/*
* The fuzz factor is set at one percent of the smaller total_cost,
* but not less than 0.01 cost units (just in case total cost is
* zero).
* We use a fuzz factor of 1% of the smaller cost.
*
* XXX does this percentage need to be user-configurable?
*/
fuzz = Min(path1->total_cost, path2->total_cost) * 0.01;
fuzz = Max(fuzz, 0.01);
if (criterion == STARTUP_COST)
{
if (Abs(path1->startup_cost - path2->startup_cost) > fuzz)
{
if (path1->startup_cost < path2->startup_cost)
return -1;
else
return +1;
}
if (path1->startup_cost > path2->startup_cost * 1.01)
return +1;
if (path2->startup_cost > path1->startup_cost * 1.01)
return -1;
/*
* If paths have the same startup cost (not at all unlikely),
* order them by total cost.
*/
if (Abs(path1->total_cost - path2->total_cost) > fuzz)
{
if (path1->total_cost < path2->total_cost)
return -1;
else
return +1;
}
if (path1->total_cost > path2->total_cost * 1.01)
return +1;
if (path2->total_cost > path1->total_cost * 1.01)
return -1;
}
else
{
if (Abs(path1->total_cost - path2->total_cost) > fuzz)
{
if (path1->total_cost < path2->total_cost)
return -1;
else
return +1;
}
if (path1->total_cost > path2->total_cost * 1.01)
return +1;
if (path2->total_cost > path1->total_cost * 1.01)
return -1;
/*
* If paths have the same total cost, order them by startup cost.
*/
if (Abs(path1->startup_cost - path2->startup_cost) > fuzz)
{
if (path1->startup_cost < path2->startup_cost)
return -1;
else
return +1;
}
if (path1->startup_cost > path2->startup_cost * 1.01)
return +1;
if (path2->startup_cost > path1->startup_cost * 1.01)
return -1;
}
return 0;
}
......
......@@ -258,24 +258,24 @@ SELECT '' AS twenty, b.f1 / p.f1 AS rotation
twenty | rotation
--------+----------------------------------------------------------------------
| (0,-0),(-0.2,-0.2)
| (0.08,-0),(0,-0.56)
| (0.0651176557644,0),(0,-0.0483449262493)
| (-0,0.0828402366864),(-0.201183431953,0)
| (0.2,0),(0,0)
| (-0.1,-0.1),(-0.3,-0.3)
| (0.12,-0.28),(0.04,-0.84)
| (0.0976764836466,-0.0241724631247),(0.0325588278822,-0.072517389374)
| (-0.100591715976,0.12426035503),(-0.301775147929,0.0414201183432)
| (0.3,0),(0.1,0)
| (-0.25,-0.25),(-0.25,-0.35)
| (0.26,-0.7),(0.1,-0.82)
| (0.109762715209,-0.0562379754329),(0.0813970697055,-0.0604311578117)
| (-0.251479289941,0.103550295858),(-0.322485207101,0.0739644970414)
| (0.3,0.05),(0.25,0)
| (-0.3,-0.3),(-0.3,-0.3)
| (0.08,-0),(0,-0.56)
| (0.12,-0.28),(0.04,-0.84)
| (0.26,-0.7),(0.1,-0.82)
| (0.12,-0.84),(0.12,-0.84)
| (0.0651176557644,0),(0,-0.0483449262493)
| (0.0976764836466,-0.0241724631247),(0.0325588278822,-0.072517389374)
| (0.109762715209,-0.0562379754329),(0.0813970697055,-0.0604311578117)
| (0.0976764836466,-0.072517389374),(0.0976764836466,-0.072517389374)
| (-0,0.0828402366864),(-0.201183431953,0)
| (-0.100591715976,0.12426035503),(-0.301775147929,0.0414201183432)
| (-0.251479289941,0.103550295858),(-0.322485207101,0.0739644970414)
| (-0.301775147929,0.12426035503),(-0.301775147929,0.12426035503)
| (0.2,0),(0,0)
| (0.3,0),(0.1,0)
| (0.3,0.05),(0.25,0)
| (0.3,0),(0.3,0)
(20 rows)
......
......@@ -258,24 +258,24 @@ SELECT '' AS twenty, b.f1 / p.f1 AS rotation
twenty | rotation
--------+----------------------------------------------------------------------
| (0,0),(-0.2,-0.2)
| (0.08,0),(0,-0.56)
| (0.0651176557644,0),(0,-0.0483449262493)
| (0,0.0828402366864),(-0.201183431953,0)
| (0.2,0),(0,0)
| (-0.1,-0.1),(-0.3,-0.3)
| (0.12,-0.28),(0.04,-0.84)
| (0.0976764836466,-0.0241724631247),(0.0325588278822,-0.072517389374)
| (-0.100591715976,0.12426035503),(-0.301775147929,0.0414201183432)
| (0.3,0),(0.1,0)
| (-0.25,-0.25),(-0.25,-0.35)
| (0.26,-0.7),(0.1,-0.82)
| (0.109762715209,-0.0562379754329),(0.0813970697055,-0.0604311578117)
| (-0.251479289941,0.103550295858),(-0.322485207101,0.0739644970414)
| (0.3,0.05),(0.25,0)
| (-0.3,-0.3),(-0.3,-0.3)
| (0.08,0),(0,-0.56)
| (0.12,-0.28),(0.04,-0.84)
| (0.26,-0.7),(0.1,-0.82)
| (0.12,-0.84),(0.12,-0.84)
| (0.0651176557644,0),(0,-0.0483449262493)
| (0.0976764836466,-0.0241724631247),(0.0325588278822,-0.072517389374)
| (0.109762715209,-0.0562379754329),(0.0813970697055,-0.0604311578117)
| (0.0976764836466,-0.072517389374),(0.0976764836466,-0.072517389374)
| (0,0.0828402366864),(-0.201183431953,0)
| (-0.100591715976,0.12426035503),(-0.301775147929,0.0414201183432)
| (-0.251479289941,0.103550295858),(-0.322485207101,0.0739644970414)
| (-0.301775147929,0.12426035503),(-0.301775147929,0.12426035503)
| (0.2,0),(0,0)
| (0.3,0),(0.1,0)
| (0.3,0.05),(0.25,0)
| (0.3,0),(0.3,0)
(20 rows)
......
......@@ -258,24 +258,24 @@ SELECT '' AS twenty, b.f1 / p.f1 AS rotation
twenty | rotation
--------+----------------------------------------------------------------------
| (0,-0),(-0.2,-0.2)
| (0.08,-0),(0,-0.56)
| (0.0651176557644,0),(0,-0.0483449262493)
| (-0,0.0828402366864),(-0.201183431953,0)
| (0.2,0),(0,0)
| (-0.1,-0.1),(-0.3,-0.3)
| (0.12,-0.28),(0.04,-0.84)
| (0.0976764836466,-0.0241724631247),(0.0325588278822,-0.072517389374)
| (-0.100591715976,0.12426035503),(-0.301775147929,0.0414201183432)
| (0.3,0),(0.1,0)
| (-0.25,-0.25),(-0.25,-0.35)
| (0.26,-0.7),(0.1,-0.82)
| (0.109762715209,-0.0562379754329),(0.0813970697055,-0.0604311578117)
| (-0.251479289941,0.103550295858),(-0.322485207101,0.0739644970414)
| (0.3,0.05),(0.25,0)
| (-0.3,-0.3),(-0.3,-0.3)
| (0.08,-0),(0,-0.56)
| (0.12,-0.28),(0.04,-0.84)
| (0.26,-0.7),(0.1,-0.82)
| (0.12,-0.84),(0.12,-0.84)
| (0.0651176557644,0),(0,-0.0483449262493)
| (0.0976764836466,-0.0241724631247),(0.0325588278822,-0.072517389374)
| (0.109762715209,-0.0562379754329),(0.0813970697055,-0.0604311578117)
| (0.0976764836466,-0.072517389374),(0.0976764836466,-0.072517389374)
| (-0,0.0828402366864),(-0.201183431953,0)
| (-0.100591715976,0.12426035503),(-0.301775147929,0.0414201183432)
| (-0.251479289941,0.103550295858),(-0.322485207101,0.0739644970414)
| (-0.301775147929,0.12426035503),(-0.301775147929,0.12426035503)
| (0.2,0),(0,0)
| (0.3,0),(0.1,0)
| (0.3,0.05),(0.25,0)
| (0.3,0),(0.3,0)
(20 rows)
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment