且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PostgreSQL在何处处理 sql查询之十六

更新时间:2022-09-18 13:35:08

接着分析:build_simple_rel 函数

PostgreSQL在何处处理 sql查询之十六
/*
 * 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;
}
PostgreSQL在何处处理 sql查询之十六

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

再看 get_relation_info(plancat.c)函数:

PostgreSQL在何处处理 sql查询之十六
/*
 * 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)
    {
       ...
    }

    ...
}
PostgreSQL在何处处理 sql查询之十六

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






本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2013/05/23/3094922.html,如需转载请自行联系原作者