13
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.259 2009/05/09 22:51:41 tgl Exp $
13
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.260 2009/06/11 14:48:59 momjian Exp $
15
15
*-------------------------------------------------------------------------
63
63
static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
64
64
List *tlist, List *scan_clauses);
65
65
static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
66
List *tlist, List *scan_clauses);
66
List *tlist, List *scan_clauses);
67
67
static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
68
List *tlist, List *scan_clauses);
68
List *tlist, List *scan_clauses);
69
69
static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
70
70
Plan *outer_plan, Plan *inner_plan);
71
71
static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
98
98
static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
99
99
Index scanrelid, List *values_lists);
100
100
static CteScan *make_ctescan(List *qptlist, List *qpqual,
101
Index scanrelid, int ctePlanId, int cteParam);
101
Index scanrelid, int ctePlanId, int cteParam);
102
102
static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
103
Index scanrelid, int wtParam);
103
Index scanrelid, int wtParam);
104
104
static BitmapAnd *make_bitmap_and(List *bitmapplans);
105
105
static BitmapOr *make_bitmap_or(List *bitmapplans);
106
106
static NestLoop *make_nestloop(List *tlist,
113
113
Plan *lefttree, Plan *righttree,
114
114
JoinType jointype);
115
115
static Hash *make_hash(Plan *lefttree,
117
AttrNumber skewColumn,
119
int32 skewColTypmod);
117
AttrNumber skewColumn,
119
int32 skewColTypmod);
120
120
static MergeJoin *make_mergejoin(List *tlist,
121
121
List *joinclauses, List *otherclauses,
122
122
List *mergeclauses,
329
329
foreach(v, rel->reltargetlist)
331
331
/* Do we really need to copy here? Not sure */
332
Node *node = (Node *) copyObject(lfirst(v));
332
Node *node = (Node *) copyObject(lfirst(v));
334
334
tlist = lappend(tlist, makeTargetEntry((Expr *) node,
660
* As constructed, the subplan has a "flat" tlist containing just the
661
* Vars needed here and at upper levels. The values we are supposed
662
* to unique-ify may be expressions in these variables. We have to
663
* add any such expressions to the subplan's tlist.
660
* As constructed, the subplan has a "flat" tlist containing just the Vars
661
* needed here and at upper levels. The values we are supposed to
662
* unique-ify may be expressions in these variables. We have to add any
663
* such expressions to the subplan's tlist.
665
* The subplan may have a "physical" tlist if it is a simple scan plan.
666
* If we're going to sort, this should be reduced to the regular tlist,
667
* so that we don't sort more data than we need to. For hashing, the
668
* tlist should be left as-is if we don't need to add any expressions;
669
* but if we do have to add expressions, then a projection step will be
670
* needed at runtime anyway, so we may as well remove unneeded items.
671
* Therefore newtlist starts from build_relation_tlist() not just a
672
* copy of the subplan's tlist; and we don't install it into the subplan
673
* unless we are sorting or stuff has to be added.
665
* The subplan may have a "physical" tlist if it is a simple scan plan. If
666
* we're going to sort, this should be reduced to the regular tlist, so
667
* that we don't sort more data than we need to. For hashing, the tlist
668
* should be left as-is if we don't need to add any expressions; but if we
669
* do have to add expressions, then a projection step will be needed at
670
* runtime anyway, so we may as well remove unneeded items. Therefore
671
* newtlist starts from build_relation_tlist() not just a copy of the
672
* subplan's tlist; and we don't install it into the subplan unless we are
673
* sorting or stuff has to be added.
675
675
in_operators = best_path->in_operators;
676
676
uniq_exprs = best_path->uniq_exprs;
1063
1063
qpqual = order_qual_clauses(root, qpqual);
1066
* When dealing with special operators, we will at this point
1067
* have duplicate clauses in qpqual and bitmapqualorig. We may as well
1068
* drop 'em from bitmapqualorig, since there's no point in making the
1066
* When dealing with special operators, we will at this point have
1067
* duplicate clauses in qpqual and bitmapqualorig. We may as well drop
1068
* 'em from bitmapqualorig, since there's no point in making the tests
1071
1071
bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1414
1414
create_ctescan_plan(PlannerInfo *root, Path *best_path,
1415
1415
List *tlist, List *scan_clauses)
1418
1418
Index scan_relid = best_path->parent->relid;
1419
1419
RangeTblEntry *rte;
1420
SubPlan *ctesplan = NULL;
1420
SubPlan *ctesplan = NULL;
1422
1422
int cte_param_id;
1423
1423
PlannerInfo *cteroot;
1441
1441
if (!cteroot) /* shouldn't happen */
1442
1442
elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1445
1446
* Note: cte_plan_ids can be shorter than cteList, if we are still working
1446
1447
* on planning the CTEs (ie, this is a side-reference from another CTE).
1471
1472
elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1474
* We need the CTE param ID, which is the sole member of the
1475
* SubPlan's setParam list.
1475
* We need the CTE param ID, which is the sole member of the SubPlan's
1477
1478
cte_param_id = linitial_int(ctesplan->setParam);
1514
1515
* We need to find the worktable param ID, which is in the plan level
1515
* that's processing the recursive UNION, which is one level *below*
1516
* where the CTE comes from.
1516
* that's processing the recursive UNION, which is one level *below* where
1517
* the CTE comes from.
1518
1519
levelsup = rte->ctelevelsup;
1519
1520
if (levelsup == 0) /* shouldn't happen */
1520
elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1521
elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1522
1523
cteroot = root;
1523
1524
while (levelsup-- > 0)
1526
1527
if (!cteroot) /* shouldn't happen */
1527
1528
elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1529
if (cteroot->wt_param_id < 0) /* shouldn't happen */
1530
if (cteroot->wt_param_id < 0) /* shouldn't happen */
1530
1531
elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
1532
1533
/* Sort clauses into best execution order */
1563
1564
NestLoop *join_plan;
1566
* If the inner path is a nestloop inner indexscan, it might be using
1567
* some of the join quals as index quals, in which case we don't have
1568
* to check them again at the join node. Remove any join quals that
1567
* If the inner path is a nestloop inner indexscan, it might be using some
1568
* of the join quals as index quals, in which case we don't have to check
1569
* them again at the join node. Remove any join quals that are redundant.
1571
1571
joinrestrictclauses =
1572
1572
select_nonredundant_join_clauses(root,
1869
1869
disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1872
* If there is a single join clause and we can identify the outer
1873
* variable as a simple column reference, supply its identity for
1874
* possible use in skew optimization. (Note: in principle we could
1875
* do skew optimization with multiple join clauses, but we'd have to
1876
* be able to determine the most common combinations of outer values,
1877
* which we don't currently have enough stats for.)
1872
* If there is a single join clause and we can identify the outer variable
1873
* as a simple column reference, supply its identity for possible use in
1874
* skew optimization. (Note: in principle we could do skew optimization
1875
* with multiple join clauses, but we'd have to be able to determine the
1876
* most common combinations of outer values, which we don't currently have
1877
* enough stats for.)
1879
1879
if (list_length(hashclauses) == 1)
2029
2029
/* Never need to commute... */
2032
* Determine which index attribute this is and change the
2033
* indexkey operand as needed.
2032
* Determine which index attribute this is and change the indexkey
2033
* operand as needed.
2035
2035
linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
3294
3294
node->ordOperators = ordOperators;
3295
3295
node->frameOptions = frameOptions;
3297
copy_plan_costsize(plan, lefttree); /* only care about copying size */
3297
copy_plan_costsize(plan, lefttree); /* only care about copying size */
3298
3298
cost_windowagg(&windowagg_path, root,
3299
3299
numWindowFuncs, partNumCols, ordNumCols,
3300
3300
lefttree->startup_cost,