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

本文详细解读了SQL查询中如何通过遍历连接树来构建基本关系RelOptInfo,确保每个非连接RTE在查询中都有对应的基本关系记录,强调了build_simple_rel函数在这一过程中的关键作用。

如此:

接前面,看 add_base_rels_to_query函数:

/*
 * add_base_rels_to_query
 *
 *      Scan the query's jointree and create baserel RelOptInfos for all
 *      the base relations (ie, table, subquery, and function RTEs)
 *      appearing in the jointree.
 *
 * The initial invocation must pass root->parse->jointree as the value of
 * jtnode.    Internally, the function recurses through the jointree.
 *
 * At the end of this process, there should be one baserel RelOptInfo for
 * every non-join RTE that is used in the query.  Therefore, this routine
 * is the only place that should call build_simple_rel with reloptkind
 * RELOPT_BASEREL.    (Note: build_simple_rel recurses internally to build
 * "other rel" RelOptInfos for the members of any appendrels we find here.)
 */
void
add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
{
    if (jtnode == NULL)
        return;
    if (IsA(jtnode, RangeTblRef))
    {
        int            varno = ((RangeTblRef *) jtnode)->rtindex;

        (void) build_simple_rel(root, varno, RELOPT_BASEREL);
    }
    else if (IsA(jtnode, FromExpr))
    {
        FromExpr   *f = (FromExpr *) jtnode;
        ListCell   *l;

        foreach(l, f->fromlist)
            add_base_rels_to_query(root, lfirst(l));
    }
    else if (IsA(jtnode, JoinExpr))
    {
        JoinExpr   *j = (JoinExpr *) jtnode;

        add_base_rels_to_query(root, j->larg);
        add_base_rels_to_query(root, j->rarg);
    }
    else
        elog(ERROR, "unrecognized node type: %d",
             (int) nodeTag(jtnode));
}

如果表所对应的文件出现问题,会在 build_simple_rel处报错。

转载于:https://www.cnblogs.com/gaojian/archive/2013/05/23/3094890.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值