~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-proposed

« back to all changes in this revision

Viewing changes to src/backend/optimizer/plan/createplan.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-11 16:59:35 UTC
  • mfrom: (5.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090711165935-jfwin6gfrxf0gfsi
Tags: 8.4.0-2
* debian/libpq-dev.install: Ship catalog/genbki.h. (Closes: #536139)
* debian/rules: Drop --enable-cassert for final release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 *
11
11
 *
12
12
 * IDENTIFICATION
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 $
14
14
 *
15
15
 *-------------------------------------------------------------------------
16
16
 */
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,
116
 
                                           Oid skewTable,
117
 
                                           AttrNumber skewColumn,
118
 
                                           Oid skewColType,
119
 
                                           int32 skewColTypmod);
 
116
                  Oid skewTable,
 
117
                  AttrNumber skewColumn,
 
118
                  Oid skewColType,
 
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)
330
330
        {
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));
333
333
 
334
334
                tlist = lappend(tlist, makeTargetEntry((Expr *) node,
335
335
                                                                                           resno,
657
657
                return subplan;
658
658
 
659
659
        /*
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.
664
664
         *
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.
674
674
         */
675
675
        in_operators = best_path->in_operators;
676
676
        uniq_exprs = best_path->uniq_exprs;
1063
1063
        qpqual = order_qual_clauses(root, qpqual);
1064
1064
 
1065
1065
        /*
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
1069
 
         * tests twice.
 
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
 
1069
         * twice.
1070
1070
         */
1071
1071
        bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1072
1072
 
1414
1414
create_ctescan_plan(PlannerInfo *root, Path *best_path,
1415
1415
                                        List *tlist, List *scan_clauses)
1416
1416
{
1417
 
        CteScan    *scan_plan;
 
1417
        CteScan    *scan_plan;
1418
1418
        Index           scan_relid = best_path->parent->relid;
1419
1419
        RangeTblEntry *rte;
1420
 
        SubPlan    *ctesplan = NULL;
 
1420
        SubPlan    *ctesplan = NULL;
1421
1421
        int                     plan_id;
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);
1443
1443
        }
 
1444
 
1444
1445
        /*
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);
1472
1473
 
1473
1474
        /*
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
 
1476
         * setParam list.
1476
1477
         */
1477
1478
        cte_param_id = linitial_int(ctesplan->setParam);
1478
1479
 
1512
1513
 
1513
1514
        /*
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.
1517
1518
         */
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);
1521
1522
        levelsup--;
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);
1528
1529
        }
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);
1531
1532
 
1532
1533
        /* Sort clauses into best execution order */
1563
1564
        NestLoop   *join_plan;
1564
1565
 
1565
1566
        /*
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
1569
 
         * are redundant.
 
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.
1570
1570
         */
1571
1571
        joinrestrictclauses =
1572
1572
                select_nonredundant_join_clauses(root,
1869
1869
                disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1870
1870
 
1871
1871
        /*
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.)
1878
1878
         */
1879
1879
        if (list_length(hashclauses) == 1)
1880
1880
        {
1887
1887
                        node = (Node *) ((RelabelType *) node)->arg;
1888
1888
                if (IsA(node, Var))
1889
1889
                {
1890
 
                        Var        *var = (Var *) node;
 
1890
                        Var                *var = (Var *) node;
1891
1891
                        RangeTblEntry *rte;
1892
1892
 
1893
1893
                        rte = root->simple_rte_array[var->varno];
2029
2029
                        /* Never need to commute... */
2030
2030
 
2031
2031
                        /*
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.
2034
2034
                         */
2035
2035
                        linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
2036
2036
                                                                                                                 index);
2506
2506
                         int ctePlanId,
2507
2507
                         int cteParam)
2508
2508
{
2509
 
        CteScan *node = makeNode(CteScan);
 
2509
        CteScan    *node = makeNode(CteScan);
2510
2510
        Plan       *plan = &node->scan.plan;
2511
2511
 
2512
2512
        /* cost should be inserted by caller */
3282
3282
{
3283
3283
        WindowAgg  *node = makeNode(WindowAgg);
3284
3284
        Plan       *plan = &node->plan;
3285
 
        Path            windowagg_path;         /* dummy for result of cost_windowagg */
 
3285
        Path            windowagg_path; /* dummy for result of cost_windowagg */
3286
3286
        QualCost        qual_cost;
3287
3287
 
3288
3288
        node->winref = winref;
3294
3294
        node->ordOperators = ordOperators;
3295
3295
        node->frameOptions = frameOptions;
3296
3296
 
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,