PostgreSQL服务过程中的那些事一:启动postgres服务进程一.七:初始化portal管理环境

      话说调用 InitPostgres 方法给 portgres 服务进程做相关初始化,这个方法里初始化了 relcache catcache ,初始化了执行查询计划的 portal 的管理器,填充本进程 PGPROC 结构相关部分成员等,上一节讨论了 relcache 管理环境的初始化,这一节继续讨论执行 sql portal 管理器的初始化。

 

1

先看 InitPostgres 方法的调用序列梗概图

 

InitPostgres方法的调用序列梗概图

         InitPostgres方法为初始化这个postgres服务进程做了一系列的工作,具体如下:

(1)调用InitProcessPhas e2 方 法,把本进程的PGPROC结构注册到PGPROC数组,就是让共享内存里的PGPROC数组(初始化PGPROC数组的文章见《PostgreSQL启 动过程中的那些事七:初始化共享内存和信号十一:shmem中初始化SharedProcArray》)的第一个空元素指向这个PGPROC结构,并注册 退出时做内存清理的函数。

(2)调用SharedInvalBackendInit 方法,在该后台进程数据的共享失效管理器数组获取一个ProcState结构(相关数据结果见《PostgreSQL启动过程中的那些事七:初始化共享内 存和信号十三:shmem中初始化SharedInvalidationState》)给该进程并初始化相关项,并注册退出方法以在退出时标记本进程的项 非活跃。

(3)调用 ProcSignalInit 方法, ProcSignalSlot 结构 数组(关于 ProcSignalSlot 结构 数组参见《PostgreSQL启动过程中的那些事七:初始化共享内存和信号十四:shmem中初始化PMSignal》) ProcSignalSlot s 里给当前进程获取一个元素,元素下标是MyBackendId-1,并注册以在进程退出时释放这个槽。

(4)为访问XLOG,调用RecoveryInProgress方法做共享内存相关初始化。

(5)调用RelationCacheInitlisze 方法做管理relcache的环境的初始化。

(6)调用InitCatalogCache方法做管理catcache的环境的初始化。

(7)调用EnablePortalManager方法初始化portal管理器。

(8)调用RelationCacheInitializePhase2方法初始化共享系统表。

(9)调用 GetTransactionSnapshot 方法获取一个事务快照。这个方法在后面讨论简单查询时再讨论。

10 )调用 PerformAuthentication 方法根据 hba 文件设置进行客户端认证。

11 )调用 GetDatabaseTuple 方法根据数据库名字从 pg_database 系统表获取要访问的数据库对应的元组。

12 )调用 RelationCacheInitializePhase3 方法完成 relcache 初始化。

13 )调用 CheckMyDatabase 方法检查当前用户的数据库访问权限,从 cache 里的 pg_database 取当前数据库的相关属性字段。

14 )调用 InitializeClientEncoding 方法初始化客户端字符编码。

15 )调用 pgstat_bestart 方法在 PgBackendStatus 设置本进程状态。

2

    下面讨论第( 7 )步, EnablePortalManager 方法初始化 portal 的管理环境。 portal 表示一个正在运行或可运行 query 的执行状态的抽象,具体情况到后续《 pg 服务过程中的那些事二》讨论 pg 处理简单查询时再具体讨论。 EnablePortalManager 方法先调用 AllocSetContextCreate 方法创建内存上下文 "PortalMemory" ,然后调用 hash_create 在这个内存上下文里创建 哈西表 "Portal hash" 。“ EnablePortalManager 调用序列图”见下面,为了图能大一点, PostgresMain 以前的调用流程序列就从下面的图中省略了,要回顾可以参考上面的“ InitPostgres 方法的调用序列梗概图 ”。

 

 

EnablePortalManager 调用序列图

 

Portal 是指向 PortalData 结构的指针类型。相关定义和结构图见下面。

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 ;

 

#define NAMEDATALEN 64

 

#define MAX_PORTALNAME_LEN       NAMEDATALEN

 

typedef struct portalhashent

{

    char        portalname [MAX_PORTALNAME_LEN ];

    Portal      portal ;

} PortalHashEnt ;

 

static HTAB *PortalHashTable = NULL;



管理 portal PostalHashTable

 

 

 

 

 

------------
转载请著明出处,来自博客:
blog.csdn.net/beiigang
beigang.iteye.com

猜你喜欢

转载自beigang.iteye.com/blog/1676136
今日推荐