mirror of
https://github.com/postgres/postgres.git
synced 2025-07-07 00:36:50 +03:00
Turn the rangetable used by the executor into a flat list, and avoid storing
useless substructure for its RangeTblEntry nodes. (I chose to keep using the same struct node type and just zero out the link fields for unneeded info, rather than making a separate ExecRangeTblEntry type --- it seemed too fragile to have two different rangetable representations.) Along the way, put subplans into a list in the toplevel PlannedStmt node, and have SubPlan nodes refer to them by list index instead of direct pointers. Vadim wanted to do that years ago, but I never understood what he was on about until now. It makes things a *whole* lot more robust, because we can stop worrying about duplicate processing of subplans during expression tree traversals. That's been a constant source of bugs, and it's finally gone. There are some consequent simplifications yet to be made, like not using a separate EState for subplans in the executor, but I'll tackle that later.
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.160 2007/02/20 17:32:15 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.161 2007/02/22 22:00:23 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -521,6 +521,7 @@ set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
|
||||
root->query_level + 1,
|
||||
tuple_fraction,
|
||||
&subroot);
|
||||
rel->subrtable = subroot->parse->rtable;
|
||||
|
||||
/* Copy number of output rows from subplan */
|
||||
rel->tuples = rel->subplan->plan_rows;
|
||||
|
@ -54,7 +54,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.177 2007/01/22 20:00:39 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.178 2007/02/22 22:00:24 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -107,11 +107,16 @@ bool enable_nestloop = true;
|
||||
bool enable_mergejoin = true;
|
||||
bool enable_hashjoin = true;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
PlannerInfo *root;
|
||||
QualCost total;
|
||||
} cost_qual_eval_context;
|
||||
|
||||
static MergeScanSelCache *cached_scansel(PlannerInfo *root,
|
||||
RestrictInfo *rinfo,
|
||||
PathKey *pathkey);
|
||||
static bool cost_qual_eval_walker(Node *node, QualCost *total);
|
||||
static bool cost_qual_eval_walker(Node *node, cost_qual_eval_context *context);
|
||||
static Selectivity approx_selectivity(PlannerInfo *root, List *quals,
|
||||
JoinType jointype);
|
||||
static Selectivity join_in_selectivity(JoinPath *path, PlannerInfo *root);
|
||||
@ -362,7 +367,7 @@ cost_index(IndexPath *path, PlannerInfo *root,
|
||||
{
|
||||
QualCost index_qual_cost;
|
||||
|
||||
cost_qual_eval(&index_qual_cost, indexQuals);
|
||||
cost_qual_eval(&index_qual_cost, indexQuals, root);
|
||||
/* any startup cost still has to be paid ... */
|
||||
cpu_per_tuple -= index_qual_cost.per_tuple;
|
||||
}
|
||||
@ -855,7 +860,7 @@ cost_functionscan(Path *path, PlannerInfo *root, RelOptInfo *baserel)
|
||||
Assert(rte->rtekind == RTE_FUNCTION);
|
||||
|
||||
/* Estimate costs of executing the function expression */
|
||||
cost_qual_eval_node(&exprcost, rte->funcexpr);
|
||||
cost_qual_eval_node(&exprcost, rte->funcexpr, root);
|
||||
|
||||
startup_cost += exprcost.startup;
|
||||
cpu_per_tuple = exprcost.per_tuple;
|
||||
@ -1241,7 +1246,7 @@ cost_nestloop(NestPath *path, PlannerInfo *root)
|
||||
ntuples = outer_path_rows * inner_path_rows * joininfactor;
|
||||
|
||||
/* CPU costs */
|
||||
cost_qual_eval(&restrict_qual_cost, path->joinrestrictinfo);
|
||||
cost_qual_eval(&restrict_qual_cost, path->joinrestrictinfo, root);
|
||||
startup_cost += restrict_qual_cost.startup;
|
||||
cpu_per_tuple = cpu_tuple_cost + restrict_qual_cost.per_tuple;
|
||||
run_cost += cpu_per_tuple * ntuples;
|
||||
@ -1301,8 +1306,8 @@ cost_mergejoin(MergePath *path, PlannerInfo *root)
|
||||
*/
|
||||
merge_selec = approx_selectivity(root, mergeclauses,
|
||||
path->jpath.jointype);
|
||||
cost_qual_eval(&merge_qual_cost, mergeclauses);
|
||||
cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo);
|
||||
cost_qual_eval(&merge_qual_cost, mergeclauses, root);
|
||||
cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo, root);
|
||||
qp_qual_cost.startup -= merge_qual_cost.startup;
|
||||
qp_qual_cost.per_tuple -= merge_qual_cost.per_tuple;
|
||||
|
||||
@ -1587,8 +1592,8 @@ cost_hashjoin(HashPath *path, PlannerInfo *root)
|
||||
*/
|
||||
hash_selec = approx_selectivity(root, hashclauses,
|
||||
path->jpath.jointype);
|
||||
cost_qual_eval(&hash_qual_cost, hashclauses);
|
||||
cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo);
|
||||
cost_qual_eval(&hash_qual_cost, hashclauses, root);
|
||||
cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo, root);
|
||||
qp_qual_cost.startup -= hash_qual_cost.startup;
|
||||
qp_qual_cost.per_tuple -= hash_qual_cost.per_tuple;
|
||||
|
||||
@ -1756,12 +1761,14 @@ cost_hashjoin(HashPath *path, PlannerInfo *root)
|
||||
* and a per-evaluation component.
|
||||
*/
|
||||
void
|
||||
cost_qual_eval(QualCost *cost, List *quals)
|
||||
cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
|
||||
{
|
||||
cost_qual_eval_context context;
|
||||
ListCell *l;
|
||||
|
||||
cost->startup = 0;
|
||||
cost->per_tuple = 0;
|
||||
context.root = root;
|
||||
context.total.startup = 0;
|
||||
context.total.per_tuple = 0;
|
||||
|
||||
/* We don't charge any cost for the implicit ANDing at top level ... */
|
||||
|
||||
@ -1769,8 +1776,10 @@ cost_qual_eval(QualCost *cost, List *quals)
|
||||
{
|
||||
Node *qual = (Node *) lfirst(l);
|
||||
|
||||
cost_qual_eval_walker(qual, cost);
|
||||
cost_qual_eval_walker(qual, &context);
|
||||
}
|
||||
|
||||
*cost = context.total;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -1778,15 +1787,21 @@ cost_qual_eval(QualCost *cost, List *quals)
|
||||
* As above, for a single RestrictInfo or expression.
|
||||
*/
|
||||
void
|
||||
cost_qual_eval_node(QualCost *cost, Node *qual)
|
||||
cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
|
||||
{
|
||||
cost->startup = 0;
|
||||
cost->per_tuple = 0;
|
||||
cost_qual_eval_walker(qual, cost);
|
||||
cost_qual_eval_context context;
|
||||
|
||||
context.root = root;
|
||||
context.total.startup = 0;
|
||||
context.total.per_tuple = 0;
|
||||
|
||||
cost_qual_eval_walker(qual, &context);
|
||||
|
||||
*cost = context.total;
|
||||
}
|
||||
|
||||
static bool
|
||||
cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
cost_qual_eval_walker(Node *node, cost_qual_eval_context *context)
|
||||
{
|
||||
if (node == NULL)
|
||||
return false;
|
||||
@ -1803,18 +1818,19 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
|
||||
if (rinfo->eval_cost.startup < 0)
|
||||
{
|
||||
rinfo->eval_cost.startup = 0;
|
||||
rinfo->eval_cost.per_tuple = 0;
|
||||
cost_qual_eval_context locContext;
|
||||
|
||||
locContext.root = context->root;
|
||||
locContext.total.startup = 0;
|
||||
locContext.total.per_tuple = 0;
|
||||
/*
|
||||
* For an OR clause, recurse into the marked-up tree so that
|
||||
* we set the eval_cost for contained RestrictInfos too.
|
||||
*/
|
||||
if (rinfo->orclause)
|
||||
cost_qual_eval_walker((Node *) rinfo->orclause,
|
||||
&rinfo->eval_cost);
|
||||
cost_qual_eval_walker((Node *) rinfo->orclause, &locContext);
|
||||
else
|
||||
cost_qual_eval_walker((Node *) rinfo->clause,
|
||||
&rinfo->eval_cost);
|
||||
cost_qual_eval_walker((Node *) rinfo->clause, &locContext);
|
||||
/*
|
||||
* If the RestrictInfo is marked pseudoconstant, it will be tested
|
||||
* only once, so treat its cost as all startup cost.
|
||||
@ -1822,12 +1838,13 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
if (rinfo->pseudoconstant)
|
||||
{
|
||||
/* count one execution during startup */
|
||||
rinfo->eval_cost.startup += rinfo->eval_cost.per_tuple;
|
||||
rinfo->eval_cost.per_tuple = 0;
|
||||
locContext.total.startup += locContext.total.per_tuple;
|
||||
locContext.total.per_tuple = 0;
|
||||
}
|
||||
rinfo->eval_cost = locContext.total;
|
||||
}
|
||||
total->startup += rinfo->eval_cost.startup;
|
||||
total->per_tuple += rinfo->eval_cost.per_tuple;
|
||||
context->total.startup += rinfo->eval_cost.startup;
|
||||
context->total.per_tuple += rinfo->eval_cost.per_tuple;
|
||||
/* do NOT recurse into children */
|
||||
return false;
|
||||
}
|
||||
@ -1849,8 +1866,8 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
*/
|
||||
if (IsA(node, FuncExpr))
|
||||
{
|
||||
total->per_tuple += get_func_cost(((FuncExpr *) node)->funcid) *
|
||||
cpu_operator_cost;
|
||||
context->total.per_tuple +=
|
||||
get_func_cost(((FuncExpr *) node)->funcid) * cpu_operator_cost;
|
||||
}
|
||||
else if (IsA(node, OpExpr) ||
|
||||
IsA(node, DistinctExpr) ||
|
||||
@ -1858,8 +1875,8 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
{
|
||||
/* rely on struct equivalence to treat these all alike */
|
||||
set_opfuncid((OpExpr *) node);
|
||||
total->per_tuple += get_func_cost(((OpExpr *) node)->opfuncid) *
|
||||
cpu_operator_cost;
|
||||
context->total.per_tuple +=
|
||||
get_func_cost(((OpExpr *) node)->opfuncid) * cpu_operator_cost;
|
||||
}
|
||||
else if (IsA(node, ScalarArrayOpExpr))
|
||||
{
|
||||
@ -1871,7 +1888,7 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
Node *arraynode = (Node *) lsecond(saop->args);
|
||||
|
||||
set_sa_opfuncid(saop);
|
||||
total->per_tuple += get_func_cost(saop->opfuncid) *
|
||||
context->total.per_tuple += get_func_cost(saop->opfuncid) *
|
||||
cpu_operator_cost * estimate_array_length(arraynode) * 0.5;
|
||||
}
|
||||
else if (IsA(node, RowCompareExpr))
|
||||
@ -1884,7 +1901,7 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
{
|
||||
Oid opid = lfirst_oid(lc);
|
||||
|
||||
total->per_tuple += get_func_cost(get_opcode(opid)) *
|
||||
context->total.per_tuple += get_func_cost(get_opcode(opid)) *
|
||||
cpu_operator_cost;
|
||||
}
|
||||
}
|
||||
@ -1905,7 +1922,7 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
* subplan by hashing.
|
||||
*/
|
||||
SubPlan *subplan = (SubPlan *) node;
|
||||
Plan *plan = subplan->plan;
|
||||
Plan *plan = planner_subplan_get_plan(context->root, subplan);
|
||||
|
||||
if (subplan->useHashTable)
|
||||
{
|
||||
@ -1915,7 +1932,7 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
* cpu_operator_cost per tuple for the work of loading the
|
||||
* hashtable, too.
|
||||
*/
|
||||
total->startup += plan->total_cost +
|
||||
context->total.startup += plan->total_cost +
|
||||
cpu_operator_cost * plan->plan_rows;
|
||||
|
||||
/*
|
||||
@ -1941,20 +1958,21 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
if (subplan->subLinkType == EXISTS_SUBLINK)
|
||||
{
|
||||
/* we only need to fetch 1 tuple */
|
||||
total->per_tuple += plan_run_cost / plan->plan_rows;
|
||||
context->total.per_tuple += plan_run_cost / plan->plan_rows;
|
||||
}
|
||||
else if (subplan->subLinkType == ALL_SUBLINK ||
|
||||
subplan->subLinkType == ANY_SUBLINK)
|
||||
{
|
||||
/* assume we need 50% of the tuples */
|
||||
total->per_tuple += 0.50 * plan_run_cost;
|
||||
context->total.per_tuple += 0.50 * plan_run_cost;
|
||||
/* also charge a cpu_operator_cost per row examined */
|
||||
total->per_tuple += 0.50 * plan->plan_rows * cpu_operator_cost;
|
||||
context->total.per_tuple +=
|
||||
0.50 * plan->plan_rows * cpu_operator_cost;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* assume we need all tuples */
|
||||
total->per_tuple += plan_run_cost;
|
||||
context->total.per_tuple += plan_run_cost;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -1967,15 +1985,15 @@ cost_qual_eval_walker(Node *node, QualCost *total)
|
||||
if (subplan->parParam == NIL &&
|
||||
(IsA(plan, Sort) ||
|
||||
IsA(plan, Material)))
|
||||
total->startup += plan->startup_cost;
|
||||
context->total.startup += plan->startup_cost;
|
||||
else
|
||||
total->per_tuple += plan->startup_cost;
|
||||
context->total.per_tuple += plan->startup_cost;
|
||||
}
|
||||
}
|
||||
|
||||
/* recurse into children */
|
||||
return expression_tree_walker(node, cost_qual_eval_walker,
|
||||
(void *) total);
|
||||
(void *) context);
|
||||
}
|
||||
|
||||
|
||||
@ -2042,7 +2060,7 @@ set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
|
||||
|
||||
rel->rows = clamp_row_est(nrows);
|
||||
|
||||
cost_qual_eval(&rel->baserestrictcost, rel->baserestrictinfo);
|
||||
cost_qual_eval(&rel->baserestrictcost, rel->baserestrictinfo, root);
|
||||
|
||||
set_rel_width(root, rel);
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.225 2007/02/19 02:23:12 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.226 2007/02/22 22:00:24 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -128,12 +128,12 @@ static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
|
||||
* create_plan
|
||||
* Creates the access plan for a query by tracing backwards through the
|
||||
* desired chain of pathnodes, starting at the node 'best_path'. For
|
||||
* every pathnode found:
|
||||
* (1) Create a corresponding plan node containing appropriate id,
|
||||
* target list, and qualification information.
|
||||
* (2) Modify qual clauses of join nodes so that subplan attributes are
|
||||
* referenced using relative values.
|
||||
* (3) Target lists are not modified, but will be in setrefs.c.
|
||||
* every pathnode found, we create a corresponding plan node containing
|
||||
* appropriate id, target list, and qualification information.
|
||||
*
|
||||
* The tlists and quals in the plan tree are still in planner format,
|
||||
* ie, Vars still correspond to the parser's numbering. This will be
|
||||
* fixed later by setrefs.c.
|
||||
*
|
||||
* best_path is the best access path
|
||||
*
|
||||
@ -421,7 +421,8 @@ create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
|
||||
if (!pseudoconstants)
|
||||
return plan;
|
||||
|
||||
return (Plan *) make_result((List *) copyObject(plan->targetlist),
|
||||
return (Plan *) make_result(root,
|
||||
plan->targetlist,
|
||||
(Node *) pseudoconstants,
|
||||
plan);
|
||||
}
|
||||
@ -519,7 +520,8 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path)
|
||||
if (best_path->subpaths == NIL)
|
||||
{
|
||||
/* Generate a Result plan with constant-FALSE gating qual */
|
||||
return (Plan *) make_result(tlist,
|
||||
return (Plan *) make_result(root,
|
||||
tlist,
|
||||
(Node *) list_make1(makeBoolConst(false,
|
||||
false)),
|
||||
NULL);
|
||||
@ -559,7 +561,7 @@ create_result_plan(PlannerInfo *root, ResultPath *best_path)
|
||||
|
||||
quals = order_qual_clauses(root, best_path->quals);
|
||||
|
||||
return make_result(tlist, (Node *) quals, NULL);
|
||||
return make_result(root, tlist, (Node *) quals, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -682,7 +684,7 @@ create_unique_plan(PlannerInfo *root, UniquePath *best_path)
|
||||
* node to help it along.
|
||||
*/
|
||||
if (!is_projection_capable_plan(subplan))
|
||||
subplan = (Plan *) make_result(newtlist, NULL, subplan);
|
||||
subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
|
||||
else
|
||||
subplan->targetlist = newtlist;
|
||||
}
|
||||
@ -1065,12 +1067,6 @@ create_bitmap_scan_plan(PlannerInfo *root,
|
||||
*/
|
||||
bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
|
||||
|
||||
/*
|
||||
* Copy the finished bitmapqualorig to make sure we have an independent
|
||||
* copy --- needed in case there are subplans in the index quals
|
||||
*/
|
||||
bitmapqualorig = copyObject(bitmapqualorig);
|
||||
|
||||
/* Finally ready to build the plan node */
|
||||
scan_plan = make_bitmap_heapscan(tlist,
|
||||
qpqual,
|
||||
@ -1333,7 +1329,8 @@ create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
|
||||
scan_plan = make_subqueryscan(tlist,
|
||||
scan_clauses,
|
||||
scan_relid,
|
||||
best_path->parent->subplan);
|
||||
best_path->parent->subplan,
|
||||
best_path->parent->subrtable);
|
||||
|
||||
copy_path_costsize(&scan_plan->scan.plan, best_path);
|
||||
|
||||
@ -2115,7 +2112,7 @@ order_qual_clauses(PlannerInfo *root, List *clauses)
|
||||
Node *clause = (Node *) lfirst(lc);
|
||||
QualCost qcost;
|
||||
|
||||
cost_qual_eval_node(&qcost, clause);
|
||||
cost_qual_eval_node(&qcost, clause, root);
|
||||
items[i].clause = clause;
|
||||
items[i].cost = qcost.per_tuple;
|
||||
i++;
|
||||
@ -2326,7 +2323,8 @@ SubqueryScan *
|
||||
make_subqueryscan(List *qptlist,
|
||||
List *qpqual,
|
||||
Index scanrelid,
|
||||
Plan *subplan)
|
||||
Plan *subplan,
|
||||
List *subrtable)
|
||||
{
|
||||
SubqueryScan *node = makeNode(SubqueryScan);
|
||||
Plan *plan = &node->scan.plan;
|
||||
@ -2345,6 +2343,7 @@ make_subqueryscan(List *qptlist,
|
||||
plan->righttree = NULL;
|
||||
node->scan.scanrelid = scanrelid;
|
||||
node->subplan = subplan;
|
||||
node->subrtable = subrtable;
|
||||
|
||||
return node;
|
||||
}
|
||||
@ -2524,7 +2523,7 @@ make_hash(Plan *lefttree)
|
||||
* plan; this only affects EXPLAIN display not decisions.
|
||||
*/
|
||||
plan->startup_cost = plan->total_cost;
|
||||
plan->targetlist = copyObject(lefttree->targetlist);
|
||||
plan->targetlist = lefttree->targetlist;
|
||||
plan->qual = NIL;
|
||||
plan->lefttree = lefttree;
|
||||
plan->righttree = NULL;
|
||||
@ -2583,7 +2582,7 @@ make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
|
||||
lefttree->plan_width);
|
||||
plan->startup_cost = sort_path.startup_cost;
|
||||
plan->total_cost = sort_path.total_cost;
|
||||
plan->targetlist = copyObject(lefttree->targetlist);
|
||||
plan->targetlist = lefttree->targetlist;
|
||||
plan->qual = NIL;
|
||||
plan->lefttree = lefttree;
|
||||
plan->righttree = NULL;
|
||||
@ -2741,10 +2740,7 @@ make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys)
|
||||
* Do we need to insert a Result node?
|
||||
*/
|
||||
if (!is_projection_capable_plan(lefttree))
|
||||
{
|
||||
tlist = copyObject(tlist);
|
||||
lefttree = (Plan *) make_result(tlist, NULL, lefttree);
|
||||
}
|
||||
lefttree = (Plan *) make_result(root, tlist, NULL, lefttree);
|
||||
|
||||
/*
|
||||
* Add resjunk entry to input's tlist
|
||||
@ -2905,7 +2901,7 @@ make_material(Plan *lefttree)
|
||||
Plan *plan = &node->plan;
|
||||
|
||||
/* cost should be inserted by caller */
|
||||
plan->targetlist = copyObject(lefttree->targetlist);
|
||||
plan->targetlist = lefttree->targetlist;
|
||||
plan->qual = NIL;
|
||||
plan->lefttree = lefttree;
|
||||
plan->righttree = NULL;
|
||||
@ -2996,12 +2992,12 @@ make_agg(PlannerInfo *root, List *tlist, List *qual,
|
||||
*/
|
||||
if (qual)
|
||||
{
|
||||
cost_qual_eval(&qual_cost, qual);
|
||||
cost_qual_eval(&qual_cost, qual, root);
|
||||
plan->startup_cost += qual_cost.startup;
|
||||
plan->total_cost += qual_cost.startup;
|
||||
plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
|
||||
}
|
||||
cost_qual_eval(&qual_cost, tlist);
|
||||
cost_qual_eval(&qual_cost, tlist, root);
|
||||
plan->startup_cost += qual_cost.startup;
|
||||
plan->total_cost += qual_cost.startup;
|
||||
plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
|
||||
@ -3059,12 +3055,12 @@ make_group(PlannerInfo *root,
|
||||
*/
|
||||
if (qual)
|
||||
{
|
||||
cost_qual_eval(&qual_cost, qual);
|
||||
cost_qual_eval(&qual_cost, qual, root);
|
||||
plan->startup_cost += qual_cost.startup;
|
||||
plan->total_cost += qual_cost.startup;
|
||||
plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
|
||||
}
|
||||
cost_qual_eval(&qual_cost, tlist);
|
||||
cost_qual_eval(&qual_cost, tlist, root);
|
||||
plan->startup_cost += qual_cost.startup;
|
||||
plan->total_cost += qual_cost.startup;
|
||||
plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
|
||||
@ -3108,7 +3104,7 @@ make_unique(Plan *lefttree, List *distinctList)
|
||||
* has a better idea.
|
||||
*/
|
||||
|
||||
plan->targetlist = copyObject(lefttree->targetlist);
|
||||
plan->targetlist = lefttree->targetlist;
|
||||
plan->qual = NIL;
|
||||
plan->lefttree = lefttree;
|
||||
plan->righttree = NULL;
|
||||
@ -3174,7 +3170,7 @@ make_setop(SetOpCmd cmd, Plan *lefttree,
|
||||
if (plan->plan_rows < 1)
|
||||
plan->plan_rows = 1;
|
||||
|
||||
plan->targetlist = copyObject(lefttree->targetlist);
|
||||
plan->targetlist = lefttree->targetlist;
|
||||
plan->qual = NIL;
|
||||
plan->lefttree = lefttree;
|
||||
plan->righttree = NULL;
|
||||
@ -3272,7 +3268,7 @@ make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
|
||||
plan->plan_rows = 1;
|
||||
}
|
||||
|
||||
plan->targetlist = copyObject(lefttree->targetlist);
|
||||
plan->targetlist = lefttree->targetlist;
|
||||
plan->qual = NIL;
|
||||
plan->lefttree = lefttree;
|
||||
plan->righttree = NULL;
|
||||
@ -3293,7 +3289,8 @@ make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
|
||||
* cost. In either case, tlist eval cost is not to be included here.
|
||||
*/
|
||||
Result *
|
||||
make_result(List *tlist,
|
||||
make_result(PlannerInfo *root,
|
||||
List *tlist,
|
||||
Node *resconstantqual,
|
||||
Plan *subplan)
|
||||
{
|
||||
@ -3312,7 +3309,7 @@ make_result(List *tlist,
|
||||
{
|
||||
QualCost qual_cost;
|
||||
|
||||
cost_qual_eval(&qual_cost, (List *) resconstantqual);
|
||||
cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
|
||||
/* resconstantqual is evaluated once at startup */
|
||||
plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
|
||||
plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planagg.c,v 1.28 2007/02/20 17:32:15 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planagg.c,v 1.29 2007/02/22 22:00:24 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -188,10 +188,10 @@ optimize_minmax_aggregates(PlannerInfo *root, List *tlist, Path *best_path)
|
||||
/*
|
||||
* Generate the output plan --- basically just a Result
|
||||
*/
|
||||
plan = (Plan *) make_result(tlist, hqual, NULL);
|
||||
plan = (Plan *) make_result(root, tlist, hqual, NULL);
|
||||
|
||||
/* Account for evaluation cost of the tlist (make_result did the rest) */
|
||||
cost_qual_eval(&tlist_cost, tlist);
|
||||
cost_qual_eval(&tlist_cost, tlist, root);
|
||||
plan->startup_cost += tlist_cost.startup;
|
||||
plan->total_cost += tlist_cost.startup + tlist_cost.per_tuple;
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.214 2007/02/20 17:32:15 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.215 2007/02/22 22:00:24 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -88,6 +88,8 @@ planner(Query *parse, bool isCursor, int cursorOptions,
|
||||
double tuple_fraction;
|
||||
PlannerInfo *root;
|
||||
Plan *top_plan;
|
||||
ListCell *lp,
|
||||
*lr;
|
||||
|
||||
/*
|
||||
* Set up global state for this planner invocation. This data is needed
|
||||
@ -99,7 +101,9 @@ planner(Query *parse, bool isCursor, int cursorOptions,
|
||||
|
||||
glob->boundParams = boundParams;
|
||||
glob->paramlist = NIL;
|
||||
glob->next_plan_id = 0;
|
||||
glob->subplans = NIL;
|
||||
glob->subrtables = NIL;
|
||||
glob->finalrtable = NIL;
|
||||
|
||||
/* Determine what fraction of the plan is likely to be scanned */
|
||||
if (isCursor)
|
||||
@ -132,7 +136,17 @@ planner(Query *parse, bool isCursor, int cursorOptions,
|
||||
}
|
||||
|
||||
/* final cleanup of the plan */
|
||||
top_plan = set_plan_references(top_plan, parse->rtable);
|
||||
Assert(glob->finalrtable == NIL);
|
||||
top_plan = set_plan_references(glob, top_plan, root->parse->rtable);
|
||||
/* ... and the subplans (both regular subplans and initplans) */
|
||||
Assert(list_length(glob->subplans) == list_length(glob->subrtables));
|
||||
forboth(lp, glob->subplans, lr, glob->subrtables)
|
||||
{
|
||||
Plan *subplan = (Plan *) lfirst(lp);
|
||||
List *subrtable = (List *) lfirst(lr);
|
||||
|
||||
lfirst(lp) = set_plan_references(glob, subplan, subrtable);
|
||||
}
|
||||
|
||||
/* build the PlannedStmt result */
|
||||
result = makeNode(PlannedStmt);
|
||||
@ -140,9 +154,10 @@ planner(Query *parse, bool isCursor, int cursorOptions,
|
||||
result->commandType = parse->commandType;
|
||||
result->canSetTag = parse->canSetTag;
|
||||
result->planTree = top_plan;
|
||||
result->rtable = parse->rtable;
|
||||
result->rtable = glob->finalrtable;
|
||||
result->resultRelations = root->resultRelations;
|
||||
result->into = parse->into;
|
||||
result->subplans = glob->subplans;
|
||||
result->returningLists = root->returningLists;
|
||||
result->rowMarks = parse->rowMarks;
|
||||
result->nParamExec = list_length(glob->paramlist);
|
||||
@ -182,7 +197,7 @@ subquery_planner(PlannerGlobal *glob, Query *parse,
|
||||
Index level, double tuple_fraction,
|
||||
PlannerInfo **subroot)
|
||||
{
|
||||
int saved_plan_id = glob->next_plan_id;
|
||||
int num_old_subplans = list_length(glob->subplans);
|
||||
PlannerInfo *root;
|
||||
Plan *plan;
|
||||
List *newHaving;
|
||||
@ -384,7 +399,8 @@ subquery_planner(PlannerGlobal *glob, Query *parse,
|
||||
* initPlan list and extParam/allParam sets for plan nodes, and attach the
|
||||
* initPlans to the top plan node.
|
||||
*/
|
||||
if (root->glob->next_plan_id != saved_plan_id || root->query_level > 1)
|
||||
if (list_length(glob->subplans) != num_old_subplans ||
|
||||
root->query_level > 1)
|
||||
SS_finalize_plan(root, plan);
|
||||
|
||||
/* Return internal info if caller wants it */
|
||||
@ -621,7 +637,8 @@ inheritance_planner(PlannerInfo *root)
|
||||
* If we managed to exclude every child rel, return a dummy plan
|
||||
*/
|
||||
if (subplans == NIL)
|
||||
return (Plan *) make_result(tlist,
|
||||
return (Plan *) make_result(root,
|
||||
tlist,
|
||||
(Node *) list_make1(makeBoolConst(false,
|
||||
false)),
|
||||
NULL);
|
||||
@ -639,6 +656,10 @@ inheritance_planner(PlannerInfo *root)
|
||||
*/
|
||||
parse->rtable = rtable;
|
||||
|
||||
/* Suppress Append if there's only one surviving child rel */
|
||||
if (list_length(subplans) == 1)
|
||||
return (Plan *) linitial(subplans);
|
||||
|
||||
return (Plan *) make_append(subplans, true, tlist);
|
||||
}
|
||||
|
||||
@ -897,7 +918,9 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
|
||||
*/
|
||||
if (!is_projection_capable_plan(result_plan))
|
||||
{
|
||||
result_plan = (Plan *) make_result(sub_tlist, NULL,
|
||||
result_plan = (Plan *) make_result(root,
|
||||
sub_tlist,
|
||||
NULL,
|
||||
result_plan);
|
||||
}
|
||||
else
|
||||
@ -928,7 +951,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
|
||||
* tuples) --- so make_agg() and make_group() are responsible
|
||||
* for computing the added cost.
|
||||
*/
|
||||
cost_qual_eval(&tlist_cost, sub_tlist);
|
||||
cost_qual_eval(&tlist_cost, sub_tlist, root);
|
||||
result_plan->startup_cost += tlist_cost.startup;
|
||||
result_plan->total_cost += tlist_cost.startup +
|
||||
tlist_cost.per_tuple * result_plan->plan_rows;
|
||||
@ -1051,7 +1074,8 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
|
||||
* this routine to avoid having to generate the plan in the
|
||||
* first place.
|
||||
*/
|
||||
result_plan = (Plan *) make_result(tlist,
|
||||
result_plan = (Plan *) make_result(root,
|
||||
tlist,
|
||||
parse->havingQual,
|
||||
NULL);
|
||||
}
|
||||
@ -1110,6 +1134,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
|
||||
{
|
||||
List *rlist;
|
||||
|
||||
Assert(parse->resultRelation);
|
||||
rlist = set_returning_clause_references(parse->returningList,
|
||||
result_plan,
|
||||
parse->resultRelation);
|
||||
@ -1544,7 +1569,7 @@ choose_hashed_grouping(PlannerInfo *root, double tuple_fraction,
|
||||
* pass down only c,d,a+b, but it's not really worth the trouble to
|
||||
* eliminate simple var references from the subplan. We will avoid doing
|
||||
* the extra computation to recompute a+b at the outer level; see
|
||||
* replace_vars_with_subplan_refs() in setrefs.c.)
|
||||
* fix_upper_expr() in setrefs.c.)
|
||||
*
|
||||
* If we are grouping or aggregating, *and* there are no non-Var grouping
|
||||
* expressions, then the returned tlist is effectively dummy; we do not
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -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.120 2007/02/19 07:03:30 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.121 2007/02/22 22:00:24 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -46,6 +46,7 @@ typedef struct process_sublinks_context
|
||||
|
||||
typedef struct finalize_primnode_context
|
||||
{
|
||||
PlannerInfo *root;
|
||||
Bitmapset *paramids; /* Set of PARAM_EXEC paramids found */
|
||||
Bitmapset *outer_params; /* Set of accessible outer paramids */
|
||||
} finalize_primnode_context;
|
||||
@ -57,12 +58,13 @@ static Node *convert_testexpr(PlannerInfo *root,
|
||||
List **righthandIds);
|
||||
static Node *convert_testexpr_mutator(Node *node,
|
||||
convert_testexpr_context *context);
|
||||
static bool subplan_is_hashable(SubLink *slink, SubPlan *node);
|
||||
static bool subplan_is_hashable(SubLink *slink, SubPlan *node, Plan *plan);
|
||||
static bool hash_ok_operator(OpExpr *expr);
|
||||
static Node *replace_correlation_vars_mutator(Node *node, PlannerInfo *root);
|
||||
static Node *process_sublinks_mutator(Node *node,
|
||||
process_sublinks_context *context);
|
||||
static Bitmapset *finalize_plan(Plan *plan, List *rtable,
|
||||
static Bitmapset *finalize_plan(PlannerInfo *root,
|
||||
Plan *plan,
|
||||
Bitmapset *outer_params,
|
||||
Bitmapset *valid_params);
|
||||
static bool finalize_primnode(Node *node, finalize_primnode_context *context);
|
||||
@ -203,6 +205,24 @@ generate_new_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod)
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the datatype of the first column of the plan's output.
|
||||
*
|
||||
* This is a hack to support exprType(), which doesn't have any way to get
|
||||
* at the plan associated with a SubPlan node. We really only need the value
|
||||
* for EXPR_SUBLINK and ARRAY_SUBLINK subplans, but for consistency we set
|
||||
* it always.
|
||||
*/
|
||||
static Oid
|
||||
get_first_col_type(Plan *plan)
|
||||
{
|
||||
TargetEntry *tent = (TargetEntry *) linitial(plan->targetlist);
|
||||
|
||||
Assert(IsA(tent, TargetEntry));
|
||||
Assert(!tent->resjunk);
|
||||
return exprType((Node *) tent->expr);
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert a SubLink (as created by the parser) into a SubPlan.
|
||||
*
|
||||
@ -219,10 +239,11 @@ generate_new_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod)
|
||||
static Node *
|
||||
make_subplan(PlannerInfo *root, SubLink *slink, Node *testexpr, bool isTopQual)
|
||||
{
|
||||
SubPlan *node = makeNode(SubPlan);
|
||||
Query *subquery = (Query *) (slink->subselect);
|
||||
double tuple_fraction;
|
||||
SubPlan *node;
|
||||
Plan *plan;
|
||||
PlannerInfo *subroot;
|
||||
Bitmapset *tmpset;
|
||||
int paramid;
|
||||
Node *result;
|
||||
@ -266,22 +287,19 @@ make_subplan(PlannerInfo *root, SubLink *slink, Node *testexpr, bool isTopQual)
|
||||
/*
|
||||
* Generate the plan for the subquery.
|
||||
*/
|
||||
node->plan = plan = subquery_planner(root->glob, subquery,
|
||||
root->query_level + 1,
|
||||
tuple_fraction,
|
||||
NULL);
|
||||
|
||||
/* Assign quasi-unique ID to this SubPlan */
|
||||
node->plan_id = root->glob->next_plan_id++;
|
||||
|
||||
node->rtable = subquery->rtable;
|
||||
plan = subquery_planner(root->glob, subquery,
|
||||
root->query_level + 1,
|
||||
tuple_fraction,
|
||||
&subroot);
|
||||
|
||||
/*
|
||||
* Initialize other fields of the SubPlan node.
|
||||
* Initialize the SubPlan node. Note plan_id isn't set yet.
|
||||
*/
|
||||
node = makeNode(SubPlan);
|
||||
node->subLinkType = slink->subLinkType;
|
||||
node->testexpr = NULL;
|
||||
node->paramIds = NIL;
|
||||
node->firstColType = get_first_col_type(plan);
|
||||
node->useHashTable = false;
|
||||
/* At top level of a qual, can treat UNKNOWN the same as FALSE */
|
||||
node->unknownEqFalse = isTopQual;
|
||||
@ -384,7 +402,7 @@ make_subplan(PlannerInfo *root, SubLink *slink, Node *testexpr, bool isTopQual)
|
||||
* tuple. But if it's an IN (= ANY) test, we might be able to use a
|
||||
* hashtable to avoid comparing all the tuples.
|
||||
*/
|
||||
if (subplan_is_hashable(slink, node))
|
||||
if (subplan_is_hashable(slink, node, plan))
|
||||
node->useHashTable = true;
|
||||
|
||||
/*
|
||||
@ -411,7 +429,7 @@ make_subplan(PlannerInfo *root, SubLink *slink, Node *testexpr, bool isTopQual)
|
||||
break;
|
||||
}
|
||||
if (use_material)
|
||||
node->plan = plan = materialize_finished_plan(plan);
|
||||
plan = materialize_finished_plan(plan);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -435,6 +453,15 @@ make_subplan(PlannerInfo *root, SubLink *slink, Node *testexpr, bool isTopQual)
|
||||
result = (Node *) node;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add the subplan and its rtable to the global lists.
|
||||
*/
|
||||
root->glob->subplans = lappend(root->glob->subplans,
|
||||
plan);
|
||||
root->glob->subrtables = lappend(root->glob->subrtables,
|
||||
subroot->parse->rtable);
|
||||
node->plan_id = list_length(root->glob->subplans);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -539,7 +566,7 @@ convert_testexpr_mutator(Node *node,
|
||||
* on its plan and parParam fields, however.
|
||||
*/
|
||||
static bool
|
||||
subplan_is_hashable(SubLink *slink, SubPlan *node)
|
||||
subplan_is_hashable(SubLink *slink, SubPlan *node, Plan *plan)
|
||||
{
|
||||
double subquery_size;
|
||||
ListCell *l;
|
||||
@ -574,8 +601,8 @@ subplan_is_hashable(SubLink *slink, SubPlan *node)
|
||||
* actually be stored as MinimalTuples; this provides some fudge factor
|
||||
* for hashtable overhead.)
|
||||
*/
|
||||
subquery_size = node->plan->plan_rows *
|
||||
(MAXALIGN(node->plan->plan_width) + MAXALIGN(sizeof(HeapTupleHeaderData)));
|
||||
subquery_size = plan->plan_rows *
|
||||
(MAXALIGN(plan->plan_width) + MAXALIGN(sizeof(HeapTupleHeaderData)));
|
||||
if (subquery_size > work_mem * 1024L)
|
||||
return false;
|
||||
|
||||
@ -964,7 +991,7 @@ SS_finalize_plan(PlannerInfo *root, Plan *plan)
|
||||
/*
|
||||
* Now recurse through plan tree.
|
||||
*/
|
||||
(void) finalize_plan(plan, root->parse->rtable, outer_params, valid_params);
|
||||
(void) finalize_plan(root, plan, outer_params, valid_params);
|
||||
|
||||
bms_free(outer_params);
|
||||
bms_free(valid_params);
|
||||
@ -988,16 +1015,16 @@ SS_finalize_plan(PlannerInfo *root, Plan *plan)
|
||||
initplan_cost = 0;
|
||||
foreach(l, plan->initPlan)
|
||||
{
|
||||
SubPlan *initplan = (SubPlan *) lfirst(l);
|
||||
SubPlan *initsubplan = (SubPlan *) lfirst(l);
|
||||
Plan *initplan = planner_subplan_get_plan(root, initsubplan);
|
||||
ListCell *l2;
|
||||
|
||||
initExtParam = bms_add_members(initExtParam,
|
||||
initplan->plan->extParam);
|
||||
foreach(l2, initplan->setParam)
|
||||
initExtParam = bms_add_members(initExtParam, initplan->extParam);
|
||||
foreach(l2, initsubplan->setParam)
|
||||
{
|
||||
initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
|
||||
}
|
||||
initplan_cost += initplan->plan->total_cost;
|
||||
initplan_cost += initplan->total_cost;
|
||||
}
|
||||
/* allParam must include all these params */
|
||||
plan->allParam = bms_add_members(plan->allParam, initExtParam);
|
||||
@ -1019,7 +1046,7 @@ SS_finalize_plan(PlannerInfo *root, Plan *plan)
|
||||
* This is just an internal notational convenience.
|
||||
*/
|
||||
static Bitmapset *
|
||||
finalize_plan(Plan *plan, List *rtable,
|
||||
finalize_plan(PlannerInfo *root, Plan *plan,
|
||||
Bitmapset *outer_params, Bitmapset *valid_params)
|
||||
{
|
||||
finalize_primnode_context context;
|
||||
@ -1027,6 +1054,7 @@ finalize_plan(Plan *plan, List *rtable,
|
||||
if (plan == NULL)
|
||||
return NULL;
|
||||
|
||||
context.root = root;
|
||||
context.paramids = NULL; /* initialize set to empty */
|
||||
context.outer_params = outer_params;
|
||||
|
||||
@ -1110,8 +1138,8 @@ finalize_plan(Plan *plan, List *rtable,
|
||||
{
|
||||
context.paramids =
|
||||
bms_add_members(context.paramids,
|
||||
finalize_plan((Plan *) lfirst(l),
|
||||
rtable,
|
||||
finalize_plan(root,
|
||||
(Plan *) lfirst(l),
|
||||
outer_params,
|
||||
valid_params));
|
||||
}
|
||||
@ -1126,8 +1154,8 @@ finalize_plan(Plan *plan, List *rtable,
|
||||
{
|
||||
context.paramids =
|
||||
bms_add_members(context.paramids,
|
||||
finalize_plan((Plan *) lfirst(l),
|
||||
rtable,
|
||||
finalize_plan(root,
|
||||
(Plan *) lfirst(l),
|
||||
outer_params,
|
||||
valid_params));
|
||||
}
|
||||
@ -1142,8 +1170,8 @@ finalize_plan(Plan *plan, List *rtable,
|
||||
{
|
||||
context.paramids =
|
||||
bms_add_members(context.paramids,
|
||||
finalize_plan((Plan *) lfirst(l),
|
||||
rtable,
|
||||
finalize_plan(root,
|
||||
(Plan *) lfirst(l),
|
||||
outer_params,
|
||||
valid_params));
|
||||
}
|
||||
@ -1193,14 +1221,14 @@ finalize_plan(Plan *plan, List *rtable,
|
||||
|
||||
/* Process left and right child plans, if any */
|
||||
context.paramids = bms_add_members(context.paramids,
|
||||
finalize_plan(plan->lefttree,
|
||||
rtable,
|
||||
finalize_plan(root,
|
||||
plan->lefttree,
|
||||
outer_params,
|
||||
valid_params));
|
||||
|
||||
context.paramids = bms_add_members(context.paramids,
|
||||
finalize_plan(plan->righttree,
|
||||
rtable,
|
||||
finalize_plan(root,
|
||||
plan->righttree,
|
||||
outer_params,
|
||||
valid_params));
|
||||
|
||||
@ -1252,10 +1280,11 @@ finalize_primnode(Node *node, finalize_primnode_context *context)
|
||||
if (is_subplan(node))
|
||||
{
|
||||
SubPlan *subplan = (SubPlan *) node;
|
||||
Plan *plan = planner_subplan_get_plan(context->root, subplan);
|
||||
|
||||
/* Add outer-level params needed by the subplan to paramids */
|
||||
context->paramids = bms_join(context->paramids,
|
||||
bms_intersect(subplan->plan->extParam,
|
||||
bms_intersect(plan->extParam,
|
||||
context->outer_params));
|
||||
/* fall through to recurse into subplan args */
|
||||
}
|
||||
@ -1299,16 +1328,21 @@ SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
|
||||
root->query_level--;
|
||||
root->init_plans = saved_init_plans;
|
||||
|
||||
/*
|
||||
* Add the subplan and its rtable to the global lists.
|
||||
*/
|
||||
root->glob->subplans = lappend(root->glob->subplans,
|
||||
plan);
|
||||
root->glob->subrtables = lappend(root->glob->subrtables,
|
||||
root->parse->rtable);
|
||||
|
||||
/*
|
||||
* Create a SubPlan node and add it to the outer list of InitPlans.
|
||||
*/
|
||||
node = makeNode(SubPlan);
|
||||
node->subLinkType = EXPR_SUBLINK;
|
||||
node->plan = plan;
|
||||
/* Assign quasi-unique ID to this SubPlan */
|
||||
node->plan_id = root->glob->next_plan_id++;
|
||||
|
||||
node->rtable = root->parse->rtable;
|
||||
node->firstColType = get_first_col_type(plan);
|
||||
node->plan_id = list_length(root->glob->subplans);
|
||||
|
||||
root->init_plans = lappend(root->init_plans, node);
|
||||
|
||||
|
@ -22,7 +22,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.138 2007/02/19 07:03:30 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.139 2007/02/22 22:00:24 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -169,6 +169,7 @@ recurse_set_operations(Node *setOp, PlannerInfo *root,
|
||||
RangeTblRef *rtr = (RangeTblRef *) setOp;
|
||||
RangeTblEntry *rte = rt_fetch(rtr->rtindex, root->parse->rtable);
|
||||
Query *subquery = rte->subquery;
|
||||
PlannerInfo *subroot;
|
||||
Plan *subplan,
|
||||
*plan;
|
||||
|
||||
@ -180,7 +181,7 @@ recurse_set_operations(Node *setOp, PlannerInfo *root,
|
||||
subplan = subquery_planner(root->glob, subquery,
|
||||
root->query_level + 1,
|
||||
tuple_fraction,
|
||||
NULL);
|
||||
&subroot);
|
||||
|
||||
/*
|
||||
* Add a SubqueryScan with the caller-requested targetlist
|
||||
@ -193,7 +194,8 @@ recurse_set_operations(Node *setOp, PlannerInfo *root,
|
||||
refnames_tlist),
|
||||
NIL,
|
||||
rtr->rtindex,
|
||||
subplan);
|
||||
subplan,
|
||||
subroot->parse->rtable);
|
||||
|
||||
/*
|
||||
* We don't bother to determine the subquery's output ordering since
|
||||
@ -223,7 +225,7 @@ recurse_set_operations(Node *setOp, PlannerInfo *root,
|
||||
* output columns.
|
||||
*
|
||||
* XXX you don't really want to know about this: setrefs.c will apply
|
||||
* replace_vars_with_subplan_refs() to the Result node's tlist. This
|
||||
* fix_upper_expr() to the Result node's tlist. This
|
||||
* would fail if the Vars generated by generate_setop_tlist() were not
|
||||
* exactly equal() to the corresponding tlist entries of the subplan.
|
||||
* However, since the subplan was generated by generate_union_plan()
|
||||
@ -235,7 +237,8 @@ recurse_set_operations(Node *setOp, PlannerInfo *root,
|
||||
!tlist_same_datatypes(plan->targetlist, colTypes, junkOK))
|
||||
{
|
||||
plan = (Plan *)
|
||||
make_result(generate_setop_tlist(colTypes, flag,
|
||||
make_result(root,
|
||||
generate_setop_tlist(colTypes, flag,
|
||||
0,
|
||||
false,
|
||||
plan->targetlist,
|
||||
@ -1216,28 +1219,6 @@ adjust_appendrel_attrs_mutator(Node *node, AppendRelInfo *context)
|
||||
Assert(!IsA(node, SubLink));
|
||||
Assert(!IsA(node, Query));
|
||||
|
||||
/*
|
||||
* BUT: although we don't need to recurse into subplans, we do need to
|
||||
* make sure that they are copied, not just referenced as
|
||||
* expression_tree_mutator will do by default. Otherwise we'll have the
|
||||
* same subplan node referenced from each arm of the finished APPEND plan,
|
||||
* which will cause trouble in the executor. This is a kluge that should
|
||||
* go away when we redesign querytrees.
|
||||
*/
|
||||
if (is_subplan(node))
|
||||
{
|
||||
SubPlan *subplan;
|
||||
|
||||
/* Copy the node and process subplan args */
|
||||
node = expression_tree_mutator(node, adjust_appendrel_attrs_mutator,
|
||||
(void *) context);
|
||||
/* Make sure we have separate copies of subplan and its rtable */
|
||||
subplan = (SubPlan *) node;
|
||||
subplan->plan = copyObject(subplan->plan);
|
||||
subplan->rtable = copyObject(subplan->rtable);
|
||||
return node;
|
||||
}
|
||||
|
||||
return expression_tree_mutator(node, adjust_appendrel_attrs_mutator,
|
||||
(void *) context);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.235 2007/02/19 07:03:30 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.236 2007/02/22 22:00:24 tgl Exp $
|
||||
*
|
||||
* HISTORY
|
||||
* AUTHOR DATE MAJOR EVENT
|
||||
@ -2989,7 +2989,7 @@ inline_function(Oid funcid, Oid result_type, List *args,
|
||||
*/
|
||||
if (contain_subplans(param))
|
||||
goto fail;
|
||||
cost_qual_eval(&eval_cost, list_make1(param));
|
||||
cost_qual_eval(&eval_cost, list_make1(param), NULL);
|
||||
if (eval_cost.startup + eval_cost.per_tuple >
|
||||
10 * cpu_operator_cost)
|
||||
goto fail;
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/util/relnode.c,v 1.85 2007/01/20 20:45:40 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/util/relnode.c,v 1.86 2007/02/22 22:00:25 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -82,6 +82,7 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptKind reloptkind)
|
||||
rel->pages = 0;
|
||||
rel->tuples = 0;
|
||||
rel->subplan = NULL;
|
||||
rel->subrtable = NIL;
|
||||
rel->baserestrictinfo = NIL;
|
||||
rel->baserestrictcost.startup = 0;
|
||||
rel->baserestrictcost.per_tuple = 0;
|
||||
@ -333,6 +334,7 @@ build_join_rel(PlannerInfo *root,
|
||||
joinrel->pages = 0;
|
||||
joinrel->tuples = 0;
|
||||
joinrel->subplan = NULL;
|
||||
joinrel->subrtable = NIL;
|
||||
joinrel->baserestrictinfo = NIL;
|
||||
joinrel->baserestrictcost.startup = 0;
|
||||
joinrel->baserestrictcost.per_tuple = 0;
|
||||
|
Reference in New Issue
Block a user