博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL在何处处理 sql查询之三十六
阅读量:4449 次
发布时间:2019-06-07

本文共 7322 字,大约阅读时间需要 24 分钟。

接前面:

/* ---------------------------------------------------------------- *        ExecutorRun * *        This is the main routine of the executor module. It accepts *        the query descriptor from the traffic cop and executes the *        query plan. * *        ExecutorStart must have been called already. * *        If direction is NoMovementScanDirection then nothing is done *        except to start up/shut down the destination.  Otherwise, *        we retrieve up to 'count' tuples in the specified direction. * *        Note: count = 0 is interpreted as no portal limit, i.e., run to *        completion. * *        There is no return value, but output tuples (if any) are sent to *        the destination receiver specified in the QueryDesc; and the number *        of tuples processed at the top level can be found in *        estate->es_processed. * *        We provide a function hook variable that lets loadable plugins *        get control when ExecutorRun is called.  Such a plugin would *        normally call standard_ExecutorRun(). * * ---------------------------------------------------------------- */voidExecutorRun(QueryDesc *queryDesc,            ScanDirection direction, long count){    if (ExecutorRun_hook)        (*ExecutorRun_hook) (queryDesc, direction, count);    else        standard_ExecutorRun(queryDesc, direction, count);}voidstandard_ExecutorRun(QueryDesc *queryDesc,                     ScanDirection direction, long count){    EState       *estate;    CmdType        operation;    DestReceiver *dest;    bool        sendTuples;    MemoryContext oldcontext;    /* sanity checks */    Assert(queryDesc != NULL);    estate = queryDesc->estate;    Assert(estate != NULL);    Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));    /*     * Switch into per-query memory context     */    oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);    /* Allow instrumentation of Executor overall runtime */    if (queryDesc->totaltime)        InstrStartNode(queryDesc->totaltime);    /*     * extract information from the query descriptor and the query feature.     */    operation = queryDesc->operation;    dest = queryDesc->dest;    /*     * startup tuple receiver, if we will be emitting tuples     */    estate->es_processed = 0;    estate->es_lastoid = InvalidOid;    sendTuples = (operation == CMD_SELECT ||                  queryDesc->plannedstmt->hasReturning);    if (sendTuples)        (*dest->rStartup) (dest, operation, queryDesc->tupDesc);    /*     * run plan     */    if (!ScanDirectionIsNoMovement(direction))        ExecutePlan(estate,                    queryDesc->planstate,                    operation,                    sendTuples,                    count,                    direction,                    dest);    /*     * shutdown tuple receiver, if we started it     */    if (sendTuples)        (*dest->rShutdown) (dest);    if (queryDesc->totaltime)        InstrStopNode(queryDesc->totaltime, estate->es_processed);    MemoryContextSwitchTo(oldcontext);}

其中,最为核心的,也就是这一段了:

ExecutePlan(estate,                    queryDesc->planstate,                    operation,                    sendTuples,                    count,                    direction,                    dest);

展开 ExecutePlan的源代码:

static voidExecutePlan(EState *estate,            PlanState *planstate,            CmdType operation,            bool sendTuples,            long numberTuples,            ScanDirection direction,            DestReceiver *dest){    TupleTableSlot *slot;    long        current_tuple_count;    /*     * initialize local variables     */    current_tuple_count = 0;    /*     * Set the direction.     */    estate->es_direction = direction;    /*     * Loop until we've processed the proper number of tuples from the plan.     */    for (;;)    {        fprintf(stderr,"In ExecutePlan ...for loop...\n");        /* Reset the per-output-tuple exprcontext */        ResetPerTupleExprContext(estate);        /*         * Execute the plan and obtain a tuple         */        slot = ExecProcNode(planstate);        /*         * if the tuple is null, then we assume there is nothing more to         * process so we just end the loop...         */        if (TupIsNull(slot))            break;        /*         * If we have a junk filter, then project a new tuple with the junk         * removed.         *         * Store this new "clean" tuple in the junkfilter's resultSlot.         * (Formerly, we stored it back over the "dirty" tuple, which is WRONG         * because that tuple slot has the wrong descriptor.)         */        if (estate->es_junkFilter != NULL)            slot = ExecFilterJunk(estate->es_junkFilter, slot);        /*         * If we are supposed to send the tuple somewhere, do so. (In         * practice, this is probably always the case at this point.)         */        if (sendTuples)            (*dest->receiveSlot) (slot, dest);        /*         * Count tuples processed, if this is a SELECT.  (For other operation         * types, the ModifyTable plan node must count the appropriate         * events.)         */        if (operation == CMD_SELECT)            (estate->es_processed)++;        /*         * check our tuple count.. if we've processed the proper number then         * quit, else loop again and process more tuples.  Zero numberTuples         * means no limit.         */        current_tuple_count++;        if (numberTuples && numberTuples == current_tuple_count)            break;    }}

把它缩略一下,得到的是:

static voidExecutePlan(EState *estate,            PlanState *planstate,            CmdType operation,            bool sendTuples,            long numberTuples,            ScanDirection direction,            DestReceiver *dest){    ...    /*     * Loop until we've processed the proper number of tuples from the plan.     */    for (;;)    {        ...        /*         * check our tuple count.. if we've processed the proper number then         * quit, else loop again and process more tuples.  Zero numberTuples         * means no limit.         */        current_tuple_count++;        if (numberTuples && numberTuples == current_tuple_count)            break;    }}

可以发现:对于有很多条记录第表tst01而言,select * from tst01 where id<10 ,执行循环10次。

下面,仔细探究其for 循环的内容,从其循环退出条件的角度来看看:

static voidExecutePlan(EState *estate,            PlanState *planstate,            CmdType operation,            bool sendTuples,            long numberTuples,            ScanDirection direction,            DestReceiver *dest){    TupleTableSlot *slot;    long        current_tuple_count;    /*     * initialize local variables     */    current_tuple_count = 0;    ...    for (;;)    {
...
/*         * if the tuple is null, then we assume there is nothing more to         * process so we just end the loop...         */        if (TupIsNull(slot))            break;
...        /*         * check our tuple count.. if we've processed the proper number then         * quit, else loop again and process more tuples.  Zero numberTuples         * means no limit.         */        current_tuple_count++;                if (numberTuples && numberTuples == current_tuple_count)            break;    }}

可以说,应当查询多少条记录,如果表中有10条记录符合条件,那么执行10循环后,从  if (TupIsNull(slot)) 跳出去。

转载于:https://www.cnblogs.com/gaojian/archive/2013/05/30/3108292.html

你可能感兴趣的文章
递归--二叉树上的相同点
查看>>
Math数学函数
查看>>
1316 文化之旅 2012年NOIP全国联赛普及组
查看>>
左移运算符
查看>>
svn的分支
查看>>
爬虫必备—性能相关(异步非阻塞)
查看>>
H5 65-清除浮动方式一
查看>>
四旋翼姿态解算——基础理论及推导
查看>>
通过用户模型,对数据库进行增删改查操作
查看>>
redis安装使用配置
查看>>
C++数据文件存储与加载(利用opencv)
查看>>
[TensorFlow 2] [Keras] fit()、fit_generator() 和 train_on_batch() 分析与应用
查看>>
Java 编译错误:缺少返回语句
查看>>
leanote使用本地账户时,去掉待同步的小红点
查看>>
整理pandas操作
查看>>
REST-assured 2发送消息代码重构
查看>>
python-面向对象
查看>>
11 go并发编程-上
查看>>
操作DOM树
查看>>
WPF——TargetNullValue(如何在绑定空值显示默认字符)
查看>>