---
title: " PostgreSQL服务过程中的那些事二：Pg服务进程处理简单查询六：执行器执行"
author: "BeiGang"
date: "2012-11-07T20:13:26Z"
tags: []
categories: ["数据库"]
slug: "postgresql-pg"
draft: false
lastmod: "2012-11-07T20:15:12Z"
id: 1718763
url: "https://beigang.iteye.com/blog/1718763"
---

话说 查询“ **select cname, comp from test1, test2 where test1.id=test2.id;** ” 发送到服务器端， 走查询分支 exec\_simple\_query ，先调用 start\_xact\_command 初始化了事务管理相关对象和资源，接着调用 pg\_parse\_query ，通过 Lex 和 Yacc 对传入 SQL 语句进行词法语法解析，生成解析树。下来调用 GetTransactionSnapshot 方法做内存快照，然后调用 pg\_analyze\_and\_rewrite 方法，进行语义分析把 parsetree 转换成 querytree ，然后对该 querytree 进行重写。接着调用 pg\_plan\_queries 方法，根据 querytree 做查询 规划，生成查询计划树 plantree 。然后调用了一系列方法 CreatePortal 、 PortalStart 、 PortalSetResultFormat 、 CreateDestReceiver 、 PortalRun 、 PortalDrop ，创建 portal 、初始化 portal 、 设置结果列格式、创建目的地接收者、运行portal执行查询并返回结果，然后释放 portal 相关资源，再调用 finish\_xact\_command 释放事务相关资源。这个例子终于执行完了。

 

1

下面是执行 plantree 的调用序列图。

![](http://dl.iteye.com/upload/attachment/0076/3005/2a323704-fef5-3546-aa9a-a576448b4d83.png)  

 

执行 plantree 的调用序列图

 

上图红色方框中显示了执行 plantree 的过程，主要分为 portal 创建和初始化（ CreatePortal 、 PortalStart ）、执行（ PortalRun ）、释放资源（ PortalDrop ）三个部分。 portal 是个抽象概念，它表示一个正在运行或可运行 query 的执行状态。 Portal 支持 SQL 层的 CURSOR 和协议层的 portal 。从上图中可以看出，在各个部分， portal 调用了执行器 executor 的对应方法 Executor Start 、 Executor Run 、 Executor Drop 完成了相关操作。

简单描述一下执行过程，方法 CreatePortal 在内存上下文 PortalMemory 里创建 portal ，并在 portal 管理器 "Portal hash" 中注册（参见《 PostgreSQL 服务过程中的那些事一：启动 postgres 服务进程一 . 七：初始化 portal 管理环境》）；调用 PortalDefineQuery 为新建的 portal 设置 sourceText （就是例子里的源 SQL 语句）、 stmts （就是上一节讨论的 plantree 列表）等字段，将 portal 状态设置为 PORTAL\_DEFINED ；再调用 PortalStart 初始化 portal ，在该例子里选择 PORTAL\_ONE\_SELECT 策略，调用 CreateQueryDesc 方法，为 portal 创建查询描述符，将 portal 状态设置为 PORTAL\_READY ；调用方法 PortalRun ，根据选择策略调用相应方法，根据 portal 相关由 plantree 转化来的各节点结构由下至上逐层进行处理，涉及扫描数据、进行投射、结果缓存等；最后调用 PortalDrop 释放 portal 相关资源。

portal 有多个执行策略，依赖于执行什么样的 query （其实就是根据不同的 SQL 语句调用不同的方法以完成相应操作）。（注意：在所有情况下，一个 portal 仅执行一个源 SQL query ，并且从用户的视点，仅产生一个结果。但是，规则重写器可以扩展一个源 query 成 0 或多个实际的 query 。）下面是 portal 的四种策略

PORTAL\_ONE\_SELECT ：这个 portal 包含一个 SELECT 查询。我们增加运行一个执行器作为被要求的结果。这个策略还支持可持有的 cursor （为了事务结束后访问结果，执行器结果可以被转储到一个 tuplestore 里）。

         PORTAL\_ONE\_RETURNING ：这个 portal 包含一个伴有 RETURNING 子句的 INSERT/UPDATE/DELETE 查询（由规则重写器重写增加的可能的辅助查询 /query ）。在第一次执行时，我们运行 portal 以完成和转储主 query 的结果到 portal 的 tuplestore 里；然后这个结果按要求返回给用户。（我们不支持 query 的部分遍历悬挂，因为 AFTER 触发器代码不能处理，并且还因为我们不想在执行所有辅助 query 时冒失败风险。）

         PORTAL\_ONE\_MOD\_WITH ： portal 包含一个 SELECT qiery ，但它包含数据修改 CTE 。这个目前和处理 PORTAL\_ONE\_RETURNING 的情况一样，因为需要触发触发器的可能性。将来处理这种情况的行为可以更像 PORTAL\_ONE\_SELECT 。

         PORTAL\_UTIL\_SELECT ： portal 包含一个 utility 语句，其返回一个象 SELECT 那样的结果（例如， WXPLAIN 或者 SHOW ）。在第一次执行时，我们运行这个语句任何转储其结果到 portal 的 tuplestore 里；然后这个结果按要求的返回给客户端。

         PORTAL\_MULTI\_QUERY ：所有其它情况。这儿，我们不支持 portal 执行： portal 的 query 会被运行以完成第一次调用。

 

下面是 portal 和其执行状态、策略的类型定义及执行器状态结构定义，其他涉及到节点结构定义略去。

**typedef** **enum** PortalStrategy

{

    *PORTAL\_ONE\_SELECT* ,

    *PORTAL\_ONE\_RETURNING* ,

    *PORTAL\_ONE\_MOD\_WITH* ,

    *PORTAL\_UTIL\_SELECT* ,

    *PORTAL\_MULTI\_QUERY*

} PortalStrategy ;

 

**typedef** **enum** PortalStatus

{

    *PORTAL\_NEW* ,                 /* freshly created \*/

    *PORTAL\_DEFINED* ,             /* PortalDefineQuery done \*/

    *PORTAL\_READY* ,            /* PortalStart complete, can run it \*/

    *PORTAL\_ACTIVE* ,              /* portal is running (can't delete it) \*/

    *PORTAL\_DONE* ,             /* portal is finished (don't re-run it) \*/

    *PORTAL\_FAILED*             /* portal got error (can't re-run it) \*/

} PortalStatus ;

 

 

**typedef** **struct** PortalData * Portal ;

 

**typedef** **struct** PortalData

{

    /* Bookkeeping data \*/

    **const** **char** * name ;           /* portal's name \*/

    **const** **char** * prepStmtName ;   /* source prepared statement (NULL if none) \*/

    MemoryContext heap ;         /* subsidiary memory for portal \*/

    ResourceOwner resowner ;     /* resources owned by portal \*/

    **void**         (* cleanup ) ( Portal portal);     /* cleanup hook \*/

    SubTransactionId createSubid ;       /* the ID of the creating subxact \*/

 

    /*

      * if createSubid is InvalidSubTransactionId, the portal is held over from

      * a previous transaction

      \*/

 

    /* The query or queries the portal will execute \*/

    **const** **char** * sourceText ;     /* text of query (as of 8.4, never NULL) \*/

    **const** **char** * commandTag ;     /* command tag for original query \*/

    List        * stmts ;          /* PlannedStmts and/or utility statements \*/

    CachedPlan * cplan ;          /* CachedPlan, if stmts are from one \*/

 

    ParamListInfo portalParams ; /* params to pass to query \*/

 

    /* Features/options \*/

    PortalStrategy strategy ;    /* see above \*/

    **int**          cursorOptions ;  /* DECLARE CURSOR option bits \*/

 

    /* Status data \*/

    PortalStatus status ;        /* see above \*/

    bool         portalPinned ;   /* a pinned portal can't be dropped \*/

 

    /* If not NULL, Executor is active; call ExecutorEnd eventually: \*/

    QueryDesc   * queryDesc ;      /* info needed for executor invocation \*/

 

    /* If portal returns tuples, this is their tupdesc : \*/

    TupleDesc    tupDesc ;        /* descriptor for result tuples \*/

    /* and these are the format codes to use for the columns: \*/

    int16       * formats ;    /* a format code for each column \*/

 

    /*

      * Where we store tuples for a held cursor or a PORTAL\_ONE\_RETURNING or

      * PORTAL\_UTIL\_SELECT query.  (A cursor held past the end of its

      * transaction no longer has any active executor state.)

      \*/

    Tuplestorestate * holdStore ; /* store for holdable cursors \*/

    MemoryContext holdContext ;  /* memory containing holdStore \*/

 

    /*

      * atStart, atEnd and portalPos indicate the current cursor position.

      * portalPos is zero before the first row, N after fetching N'th row of

      * query.  After we run off the end, portalPos = # of rows in query, and

      * atEnd is true.  If portalPos overflows, set posOverflow (this causes us

      * to stop relying on its value for navigation).  Note that atStart

      * implies portalPos == 0, but not the reverse (portalPos could have

      * overflowed).

      \*/

    bool         atStart ;

    bool         atEnd ;

    bool         posOverflow ;

    **long**         portalPos ;

 

    /* Presentation data, primarily used by the pg\_cursors system view \*/

    TimestampTz creation\_time ;  /* time at which this portal was defined \*/

    bool         visible ;        /* include this portal in pg\_cursors? \*/

}   PortalData ;

 

执行器调用的主工作状态

**typedef** **struct** EState

{

    NodeTag      type ;

 

    /* Basic state for all query types: \*/

    ScanDirection es\_direction ; /* current scan direction \*/

    Snapshot     es\_snapshot ;    /* time qual to use \*/

    Snapshot     es\_crosscheck\_snapshot ; /* crosscheck time qual for RI \*/

    List        * es\_range\_table ; /* List of RangeTblEntry \*/

    PlannedStmt * es\_plannedstmt ;    /* link to top of plan tree \*/

 

    JunkFilter * es\_junkFilter ;  /* top-level junk filter, if any \*/

 

    /* If query can insert/delete tuples, the command ID to mark them with \*/

    CommandId    es\_output\_cid ;

 

    /* Info about target table(s) for insert/update/delete queries: \*/

    ResultRelInfo * es\_result\_relations ; /* array of ResultRelInfos \*/

    **int**          es\_num\_result\_relations ;        /* length of array \*/

    ResultRelInfo * es\_result\_relation\_info ;     /* currently active array elt \*/

 

    /* Stuff used for firing triggers: \*/

    List        * es\_trig\_target\_relations ;       /* trigger-only ResultRelInfos \*/

    TupleTableSlot * es\_trig\_tuple\_slot ; /* for trigger output tuples \*/

    TupleTableSlot * es\_trig\_oldtup\_slot ;        /* for TriggerEnabled \*/

 

    /* Parameter info: \*/

    ParamListInfo es\_param\_list\_info ;   /* values of external params \*/

    ParamExecData * es\_param\_exec\_vals ;  /* values of internal params \*/

 

    /* Other working state: \*/

    MemoryContext es\_query\_cxt ; /* per-query context in which EState lives \*/

 

    List        * es\_tupleTable ;  /* List of TupleTableSlots \*/

 

    List        * es\_rowMarks ; /* List of ExecRowMarks \*/

 

    uint32       es\_processed ;   /* # of tuples processed \*/

    Oid          es\_lastoid ;     /* last oid processed (by INSERT) \*/

 

    **int**          es\_top\_eflags ;  /* eflags passed to ExecutorStart \*/

    **int**          es\_instrument ;  /* OR of InstrumentOption flags \*/

    bool         es\_select\_into ; /* true if doing SELECT INTO \*/

    bool         es\_into\_oids ;   /* true to generate OIDs in SELECT INTO \*/

    bool         es\_finished ;    /* true when ExecutorFinish is done \*/

 

    List        * es\_exprcontexts ; /* List of ExprContexts within EState \*/

 

    List        * es\_subplanstates ;       /* List of PlanState for SubPlans \*/

 

    List        * es\_auxmodifytables ;     /* List of secondary ModifyTableStates \*/

 

    /*

      * this ExprContext is for per-output-tuple operations, such as constraint

      * checks and index-value computations.  It will be reset for each output

      * tuple.  Note that it will be created only if needed.

      \*/

    ExprContext * es\_per\_tuple\_exprcontext ;

 

    /*

      * These fields are for re-evaluating plan quals when an updated tuple is

      * substituted in READ COMMITTED mode.  es\_epqTuple\[] contains tuples that

      * scan plan nodes should return instead of whatever they'd normally

      * return, or NULL if nothing to return; es\_epqTupleSet\[] is true if a

      * particular array entry is valid; and es\_epqScanDone\[] is state to

      * remember if the tuple has been returned already.  Arrays are of size

      * list\_length(es\_range\_table) and are indexed by scan node scanrelid - 1.

      \*/

    HeapTuple   * es\_epqTuple ; /* array of EPQ substitute tuples \*/

    bool        * es\_epqTupleSet ; /* true if EPQ tuple is provided \*/

    bool        * es\_epqScanDone ; /* true if EPQ tuple has been fetched \*/

 

    /*

      * this field added at end of struct to avoid post-release ABI breakage in

      * existing release branches.  It'll be in a more logical place in 9.2.

      \*/

    TupleTableSlot * es\_trig\_newtup\_slot ;        /* for TriggerEnabled \*/

} EState ;

 

         下面是执行这个查询的 portal 相关内存结构图：

   
![](http://dl.iteye.com/upload/attachment/0076/3007/f38432ff-795b-32a1-bbc1-5d40c6fd00ad.png)

 

portal 相关内存结构图

 

 

就到这儿吧。

 

\------------  
转载请注明出处，来自博客：  
blog.csdn.net/beiigang  
beigang.iteye.com