Commit 9bb34281 authored by Tom Lane's avatar Tom Lane

Rewrite the planner's handling of materialized plan types so that there is

an explicit model of rescan costs being different from first-time costs.
The costing of Material nodes in particular now has some visible relationship
to the actual runtime behavior, where before it was essentially fantasy.
This also fixes up a couple of places where different materialized plan types
were treated differently for no very good reason (probably just oversights).

A couple of the regression tests are affected, because the planner now chooses
to put the other relation on the inside of a nestloop-with-materialize.
So far as I can see both changes are sane, and the planner is now more
consistently following the expectation that it should prefer to materialize
the smaller of two relations.

Per a recent discussion with Robert Haas.
parent 5f1b32dd
......@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/backend/executor/execAmi.c,v 1.103 2009/01/01 17:23:41 momjian Exp $
* $PostgreSQL: pgsql/src/backend/executor/execAmi.c,v 1.104 2009/09/12 22:12:03 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -496,3 +496,30 @@ IndexSupportsBackwardScan(Oid indexid)
return result;
}
/*
* ExecMaterializesOutput - does a plan type materialize its output?
*
* Returns true if the plan node type is one that automatically materializes
* its output (typically by keeping it in a tuplestore). For such plans,
* a rescan without any parameter change will have zero startup cost and
* very low per-tuple cost.
*/
bool
ExecMaterializesOutput(NodeTag plantype)
{
switch (plantype)
{
case T_Material:
case T_FunctionScan:
case T_CteScan:
case T_WorkTableScan:
case T_Sort:
return true;
default:
break;
}
return false;
}
This diff is collapsed.
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.122 2009/06/11 14:48:59 momjian Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.123 2009/09/12 22:12:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -16,6 +16,7 @@
#include <math.h>
#include "executor/executor.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
......@@ -405,18 +406,10 @@ match_unsorted_outer(PlannerInfo *root,
else if (nestjoinOK)
{
/*
* If the cheapest inner path is a join or seqscan, we should consider
* materializing it. (This is a heuristic: we could consider it
* always, but for inner indexscans it's probably a waste of time.)
* Also skip it if the inner path materializes its output anyway.
* Consider materializing the cheapest inner path, unless it is one
* that materializes its output anyway.
*/
if (!(inner_cheapest_total->pathtype == T_IndexScan ||
inner_cheapest_total->pathtype == T_BitmapHeapScan ||
inner_cheapest_total->pathtype == T_TidScan ||
inner_cheapest_total->pathtype == T_Material ||
inner_cheapest_total->pathtype == T_FunctionScan ||
inner_cheapest_total->pathtype == T_CteScan ||
inner_cheapest_total->pathtype == T_WorkTableScan))
if (!ExecMaterializesOutput(inner_cheapest_total->pathtype))
matpath = (Path *)
create_material_path(innerrel, inner_cheapest_total);
......
......@@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.261 2009/07/17 23:19:34 tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.262 2009/09/12 22:12:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -3266,6 +3266,7 @@ materialize_finished_plan(Plan *subplan)
/* Set cost data */
cost_material(&matpath,
subplan->startup_cost,
subplan->total_cost,
subplan->plan_rows,
subplan->plan_width);
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.152 2009/07/16 06:33:43 petere Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.153 2009/09/12 22:12:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -15,6 +15,7 @@
#include "catalog/pg_operator.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
......@@ -564,33 +565,16 @@ build_subplan(PlannerInfo *root, Plan *plan, List *rtable,
splan->useHashTable = true;
/*
* Otherwise, we have the option to tack a MATERIAL node onto the top
* Otherwise, we have the option to tack a Material node onto the top
* of the subplan, to reduce the cost of reading it repeatedly. This
* is pointless for a direct-correlated subplan, since we'd have to
* recompute its results each time anyway. For uncorrelated/undirect
* correlated subplans, we add MATERIAL unless the subplan's top plan
* correlated subplans, we add Material unless the subplan's top plan
* node would materialize its output anyway.
*/
else if (splan->parParam == NIL)
{
bool use_material;
switch (nodeTag(plan))
{
case T_Material:
case T_FunctionScan:
case T_CteScan:
case T_WorkTableScan:
case T_Sort:
use_material = false;
break;
default:
use_material = true;
break;
}
if (use_material)
plan = materialize_finished_plan(plan);
}
else if (splan->parParam == NIL &&
!ExecMaterializesOutput(nodeTag(plan)))
plan = materialize_finished_plan(plan);
result = (Node *) splan;
isInitPlan = false;
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/util/pathnode.c,v 1.152 2009/06/11 14:48:59 momjian Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/util/pathnode.c,v 1.153 2009/09/12 22:12:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -711,6 +711,7 @@ create_material_path(RelOptInfo *rel, Path *subpath)
pathnode->subpath = subpath;
cost_material(&pathnode->path,
subpath->startup_cost,
subpath->total_cost,
rel->rows,
rel->width);
......@@ -1424,7 +1425,8 @@ create_mergejoin_path(PlannerInfo *root,
* cost_mergejoin will avoid choosing anyway). Therefore
* cost_material's cost estimate is bogus and we should charge just
* cpu_tuple_cost per tuple. (Keep this estimate in sync with similar
* ones in cost_mergejoin and create_mergejoin_plan.)
* ones in cost_mergejoin and create_mergejoin_plan; also see
* cost_rescan.)
*/
mpath->startup_cost = inner_path->startup_cost;
mpath->total_cost = inner_path->total_cost;
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/executor/executor.h,v 1.158 2009/07/29 20:56:20 tgl Exp $
* $PostgreSQL: pgsql/src/include/executor/executor.h,v 1.159 2009/09/12 22:12:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -83,6 +83,7 @@ extern void ExecMarkPos(PlanState *node);
extern void ExecRestrPos(PlanState *node);
extern bool ExecSupportsMarkRestore(NodeTag plantype);
extern bool ExecSupportsBackwardScan(Plan *node);
extern bool ExecMaterializesOutput(NodeTag plantype);
/*
* prototypes from functions in execCurrent.c
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/optimizer/cost.h,v 1.97 2009/06/11 14:49:11 momjian Exp $
* $PostgreSQL: pgsql/src/include/optimizer/cost.h,v 1.98 2009/09/12 22:12:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -86,7 +86,8 @@ extern void cost_sort(Path *path, PlannerInfo *root,
double limit_tuples);
extern bool sort_exceeds_work_mem(Sort *sort);
extern void cost_material(Path *path,
Cost input_cost, double tuples, int width);
Cost input_startup_cost, Cost input_total_cost,
double tuples, int width);
extern void cost_agg(Path *path, PlannerInfo *root,
AggStrategy aggstrategy, int numAggs,
int numGroupCols, double numGroups,
......
......@@ -260,24 +260,24 @@ SELECT '' AS twenty, b.f1 / p.f1 AS rotation
twenty | rotation
--------+----------------------------------------------------------------------
| (0,-0),(-0.2,-0.2)
| (-0.1,-0.1),(-0.3,-0.3)
| (-0.25,-0.25),(-0.25,-0.35)
| (-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.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.12,-0.84),(0.12,-0.84)
| (0.0976764836466,-0.072517389374),(0.0976764836466,-0.072517389374)
| (-0.301775147929,0.12426035503),(-0.301775147929,0.12426035503)
| (0.3,0),(0.3,0)
(20 rows)
......
......@@ -260,24 +260,24 @@ SELECT '' AS twenty, b.f1 / p.f1 AS rotation
twenty | rotation
--------+----------------------------------------------------------------------
| (0,0),(-0.2,-0.2)
| (-0.1,-0.1),(-0.3,-0.3)
| (-0.25,-0.25),(-0.25,-0.35)
| (-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.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.12,-0.84),(0.12,-0.84)
| (0.0976764836466,-0.072517389374),(0.0976764836466,-0.072517389374)
| (-0.301775147929,0.12426035503),(-0.301775147929,0.12426035503)
| (0.3,0),(0.3,0)
(20 rows)
......
......@@ -260,24 +260,24 @@ SELECT '' AS twenty, b.f1 / p.f1 AS rotation
twenty | rotation
--------+----------------------------------------------------------------------
| (0,-0),(-0.2,-0.2)
| (-0.1,-0.1),(-0.3,-0.3)
| (-0.25,-0.25),(-0.25,-0.35)
| (-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.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.12,-0.84),(0.12,-0.84)
| (0.0976764836466,-0.072517389374),(0.0976764836466,-0.072517389374)
| (-0.301775147929,0.12426035503),(-0.301775147929,0.12426035503)
| (0.3,0),(0.3,0)
(20 rows)
......
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