~ubuntu-branches/ubuntu/trusty/postgresql-9.3/trusty-proposed

« back to all changes in this revision

Viewing changes to src/backend/optimizer/path/allpaths.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2016-03-31 11:04:53 UTC
  • mfrom: (1.1.11) (18.1.4 trusty-security)
  • Revision ID: package-import@ubuntu.com-20160331110453-h6xfs9f11suj3mze
Tags: 9.3.12-0ubuntu0.14.04
* New upstream bug fix release. (LP: #1564268)
  - See http://www.postgresql.org/about/news/1656/ for details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
333
333
                                break;
334
334
                }
335
335
        }
 
336
 
 
337
        /*
 
338
         * We insist that all non-dummy rels have a nonzero rowcount estimate.
 
339
         */
 
340
        Assert(rel->rows > 0 || IS_DUMMY_REL(rel));
336
341
}
337
342
 
338
343
/*
462
467
 
463
468
        /* Let FDW adjust the size estimates, if it can */
464
469
        rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
 
470
 
 
471
        /* ... but do not let it set the rows estimate to zero */
 
472
        rel->rows = clamp_row_est(rel->rows);
465
473
}
466
474
 
467
475
/*
494
502
                                        Index rti, RangeTblEntry *rte)
495
503
{
496
504
        int                     parentRTindex = rti;
 
505
        bool            has_live_children;
497
506
        double          parent_rows;
498
507
        double          parent_size;
499
508
        double     *parent_attrsizes;
514
523
         * Note: if you consider changing this logic, beware that child rels could
515
524
         * have zero rows and/or width, if they were excluded by constraints.
516
525
         */
 
526
        has_live_children = false;
517
527
        parent_rows = 0;
518
528
        parent_size = 0;
519
529
        nattrs = rel->max_attr - rel->min_attr + 1;
641
651
                if (IS_DUMMY_REL(childrel))
642
652
                        continue;
643
653
 
 
654
                /* We have at least one live child. */
 
655
                has_live_children = true;
 
656
 
644
657
                /*
645
658
                 * Accumulate size information from each live child.
646
659
                 */
647
 
                if (childrel->rows > 0)
 
660
                Assert(childrel->rows > 0);
 
661
 
 
662
                parent_rows += childrel->rows;
 
663
                parent_size += childrel->width * childrel->rows;
 
664
 
 
665
                /*
 
666
                 * Accumulate per-column estimates too.  We need not do anything for
 
667
                 * PlaceHolderVars in the parent list.  If child expression isn't a
 
668
                 * Var, or we didn't record a width estimate for it, we have to fall
 
669
                 * back on a datatype-based estimate.
 
670
                 *
 
671
                 * By construction, child's reltargetlist is 1-to-1 with parent's.
 
672
                 */
 
673
                forboth(parentvars, rel->reltargetlist,
 
674
                                childvars, childrel->reltargetlist)
648
675
                {
649
 
                        parent_rows += childrel->rows;
650
 
                        parent_size += childrel->width * childrel->rows;
 
676
                        Var                *parentvar = (Var *) lfirst(parentvars);
 
677
                        Node       *childvar = (Node *) lfirst(childvars);
651
678
 
652
 
                        /*
653
 
                         * Accumulate per-column estimates too.  We need not do anything
654
 
                         * for PlaceHolderVars in the parent list.  If child expression
655
 
                         * isn't a Var, or we didn't record a width estimate for it, we
656
 
                         * have to fall back on a datatype-based estimate.
657
 
                         *
658
 
                         * By construction, child's reltargetlist is 1-to-1 with parent's.
659
 
                         */
660
 
                        forboth(parentvars, rel->reltargetlist,
661
 
                                        childvars, childrel->reltargetlist)
 
679
                        if (IsA(parentvar, Var))
662
680
                        {
663
 
                                Var                *parentvar = (Var *) lfirst(parentvars);
664
 
                                Node       *childvar = (Node *) lfirst(childvars);
 
681
                                int                     pndx = parentvar->varattno - rel->min_attr;
 
682
                                int32           child_width = 0;
665
683
 
666
 
                                if (IsA(parentvar, Var))
 
684
                                if (IsA(childvar, Var) &&
 
685
                                        ((Var *) childvar)->varno == childrel->relid)
667
686
                                {
668
 
                                        int                     pndx = parentvar->varattno - rel->min_attr;
669
 
                                        int32           child_width = 0;
670
 
 
671
 
                                        if (IsA(childvar, Var) &&
672
 
                                                ((Var *) childvar)->varno == childrel->relid)
673
 
                                        {
674
 
                                                int                     cndx = ((Var *) childvar)->varattno - childrel->min_attr;
675
 
 
676
 
                                                child_width = childrel->attr_widths[cndx];
677
 
                                        }
678
 
                                        if (child_width <= 0)
679
 
                                                child_width = get_typavgwidth(exprType(childvar),
680
 
                                                                                                          exprTypmod(childvar));
681
 
                                        Assert(child_width > 0);
682
 
                                        parent_attrsizes[pndx] += child_width * childrel->rows;
 
687
                                        int                     cndx = ((Var *) childvar)->varattno - childrel->min_attr;
 
688
 
 
689
                                        child_width = childrel->attr_widths[cndx];
683
690
                                }
 
691
                                if (child_width <= 0)
 
692
                                        child_width = get_typavgwidth(exprType(childvar),
 
693
                                                                                                  exprTypmod(childvar));
 
694
                                Assert(child_width > 0);
 
695
                                parent_attrsizes[pndx] += child_width * childrel->rows;
684
696
                        }
685
697
                }
686
698
        }
687
699
 
688
 
        /*
689
 
         * Save the finished size estimates.
690
 
         */
691
 
        rel->rows = parent_rows;
692
 
        if (parent_rows > 0)
 
700
        if (has_live_children)
693
701
        {
 
702
                /*
 
703
                 * Save the finished size estimates.
 
704
                 */
694
705
                int                     i;
695
706
 
 
707
                Assert(parent_rows > 0);
 
708
                rel->rows = parent_rows;
696
709
                rel->width = rint(parent_size / parent_rows);
697
710
                for (i = 0; i < nattrs; i++)
698
711
                        rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
 
712
 
 
713
                /*
 
714
                 * Set "raw tuples" count equal to "rows" for the appendrel; needed
 
715
                 * because some places assume rel->tuples is valid for any baserel.
 
716
                 */
 
717
                rel->tuples = parent_rows;
699
718
        }
700
719
        else
701
 
                rel->width = 0;                 /* attr_widths should be zero already */
702
 
 
703
 
        /*
704
 
         * Set "raw tuples" count equal to "rows" for the appendrel; needed
705
 
         * because some places assume rel->tuples is valid for any baserel.
706
 
         */
707
 
        rel->tuples = parent_rows;
 
720
        {
 
721
                /*
 
722
                 * All children were excluded by constraints, so mark the whole
 
723
                 * appendrel dummy.  We must do this in this phase so that the rel's
 
724
                 * dummy-ness is visible when we generate paths for other rels.
 
725
                 */
 
726
                set_dummy_rel_pathlist(rel);
 
727
        }
708
728
 
709
729
        pfree(parent_attrsizes);
710
730
}