zoukankan      html  css  js  c++  java
  • PostgreSQL在何处处理 sql查询之十六

    接着分析:build_simple_rel 函数

    /*
     * build_simple_rel
     *      Construct a new RelOptInfo for a base relation or 'other' relation.
     */
    RelOptInfo *
    build_simple_rel(PlannerInfo *root, int relid, RelOptKind reloptkind)
    {
        ...
    
        /* Check type of rtable entry */
        switch (rte->rtekind)
        {
            case RTE_RELATION:
                /* Table --- retrieve statistics from the system catalogs */
                get_relation_info(root, rte->relid, rte->inh, rel);
                break;
            case RTE_SUBQUERY:
            case RTE_FUNCTION:
            case RTE_VALUES:
            case RTE_CTE:
    
                /*
                 * Subquery, function, or values list --- set up attr range and
                 * arrays
                 *
                 * Note: 0 is included in range to support whole-row Vars
                 */
                rel->min_attr = 0;
                rel->max_attr = list_length(rte->eref->colnames);
                rel->attr_needed = (Relids *)
                    palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
                rel->attr_widths = (int32 *)
                    palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
    
                break;
            default:
                elog(ERROR, "unrecognized RTE kind: %d",
                     (int) rte->rtekind);
                break;
        }
        ...
        return rel;
    }

    如果表对应的文件有问题,则 在  get_relation_info 处出错

    再看 get_relation_info(plancat.c)函数:

    /*
     * get_relation_info -
     *      Retrieves catalog information for a given relation.
     *
     * Given the Oid of the relation, return the following info into fields
     * of the RelOptInfo struct:
     *
     *    min_attr    lowest valid AttrNumber
     *    max_attr    highest valid AttrNumber
     *    indexlist    list of IndexOptInfos for relation's indexes
     *    pages        number of pages
     *    tuples        number of tuples
     *
     * Also, initialize the attr_needed[] and attr_widths[] arrays.  In most
     * cases these are left as zeroes, but sometimes we need to compute attr
     * widths here, and we may as well cache the results for costsize.c.
     *
     * If inhparent is true, all we need to do is set up the attr arrays:
     * the RelOptInfo actually represents the appendrel formed by an inheritance
     * tree, and so the parent rel's physical size and index information isn't
     * important for it.
     */
    void
    get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
                      RelOptInfo *rel)
    {
        Index        varno = rel->relid;
        Relation    relation;
        bool        hasindex;
        List       *indexinfos = NIL;
    
        /*
         * We need not lock the relation since it was already locked, either by
         * the rewriter or when expand_inherited_rtentry() added it to the query's
         * rangetable.
         */
        relation = heap_open(relationObjectId, NoLock);
    
        ...
    
        /*
         * Estimate relation size --- unless it's an inheritance parent, in which
         * case the size will be computed later in set_append_rel_pathlist, and we
         * must leave it zero for now to avoid bollixing the total_table_pages
         * calculation.
         */
        if (!inhparent)
            estimate_rel_size(relation, rel->attr_widths - rel->min_attr,
                              &rel->pages, &rel->tuples, &rel->allvisfrac);
    
        ...
    
        if (hasindex)
        {
           ...
        }
    
        ...
    }

    estimate_rel_size 要预估表的大小,此处开始了对表的物理信息的接触。

  • 相关阅读:
    MasterPage中找尋控件
    Win2003服务器发布的网站Session经常丢失
    Toolkits
    aspnet_regiis 命令格式說明
    SQL转换数字中文大写
    ASP.NET2.0实现无刷新客户端回调
    SQL的使用规范
    pku3207 2SAT问题入门
    unity3d打包资源
    Vector3.Lerp 插值
  • 原文地址:https://www.cnblogs.com/gaojian/p/3094922.html
Copyright © 2011-2022 走看看