PostgreSQL服务过程中的那些事一:启动postgres服务进程一.七:
话说调用InitPostgres方法给portgres服务进程做相关初始化,这个方法里初始化了relcache和catcache,初始化了执行查询计划的portal的管理器,填充本进程PGPROC结构相关部分成员等,上一节讨论了relcache管理环境的初始化,这一节继续讨论执行sql的portal管理器的初始化。
1 先看InitPostgres方法的调用序列梗概图
InitPostgres方法的调用序列梗概图 InitPostgres方法为初始化这个postgres服务进程做了一系列的工作,具体如下: (1)调用InitProcessPhase2方法,把本进程的PGPROC结构注册到PGPROC数组,就是让共享内存里的PGPROC数组(初始化PGPROC数组的文章见《PostgreSQL启动过程中的那些事七:初始化共享内存和信号十一:shmem中初始化SharedProcArray》)的第一个空元素指向这个PGPROC结构,并注册退出时做内存清理的函数。 (2)调用SharedInvalBackendInit方法,在该后台进程数据的共享失效管理器数组获取一个ProcState结构(相关数据结果见《PostgreSQL启动过程中的那些事七:初始化共享内存和信号十三:shmem中初始化SharedInvalidationState》)给该进程并初始化相关项,并注册退出方法以在退出时标记本进程的项非活跃。 (3)调用ProcSignalInit方法,在ProcSignalSlot结构数组(关于ProcSignalSlot结构数组参见《PostgreSQL启动过程中的那些事七:初始化共享内存和信号十四:shmem中初始化PMSignal》)ProcSignalSlots里给当前进程获取一个元素,元素下标是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在这个内存上下文里创建哈西表"Portalhash"。“EnablePortalManager调用序列图”见下面,为了图能大一点,PostgresMain以前的调用流程序列就从下面的图中省略了,要回顾可以参考上面的“InitPostgres方法的调用序列梗概图”。
EnablePortalManager调用序列图
Portal是指向PortalData结构的指针类型。相关定义和结构图见下面。 typedefstruct PortalData *Portal;
typedefstruct PortalData { /* Bookkeeping data */ constchar *name; /* portal's name */ constchar *prepStmtName; /* source prepared statement (NULL if none) */ MemoryContextheap; /* subsidiary memory for portal */ ResourceOwnerresowner; /* resources owned by portal */ void (*cleanup) (Portal portal); /* cleanuphook */ SubTransactionIdcreateSubid; /* the ID of the creating subxact */
/* * if createSubid isInvalidSubTransactionId,the portal is held over from * a previous transaction */
/* The query or queries the portal willexecute */ constchar *sourceText; /* text of query (as of 8.4,never NULL) */ constchar *commandTag; /* command tag for original query */ List *stmts; /* PlannedStmts and/or utility statements */ CachedPlan *cplan; /* CachedPlan,if stmts are from one */
ParamListInfoportalParams; /* params to pass to query */
/* Features/options */ PortalStrategystrategy; /* see above */ int cursorOptions; /* DECLARECURSOR option bits */
/* Status data */ PortalStatusstatus; /* see above */ bool portalPinned; /* a pinned portal can't be dropped */
/* If not NULL,Executor is active; callExecutorEnd 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 forthe columns: */ int16 *formats; /* a format code for each column */
/* * Where we store tuplesfor a held cursor or a PORTAL_ONE_RETURNING or * PORTAL_UTIL_SELECTquery. (A cursor held past the end ofits * transaction no longerhas any active executor state.) */ Tuplestorestate *holdStore; /* store for holdable cursors */ MemoryContextholdContext; /* memory containing holdStore */
/* * atStart,atEnd andportalPos indicate the current cursor position. * portalPos is zero beforethe first row,N after fetching N'th row of * query. After we run off the end,portalPos = # ofrows in query,and * atEnd is true. If portalPos overflows,set posOverflow (thiscauses us * to stop relying on itsvalue 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 thepg_cursors system view */ TimestampTzcreation_time; /* time at which this portal was defined */ bool visible; /* include this portal in pg_cursors? */ } PortalData;
#define NAMEDATALEN64
#define MAX_PORTALNAME_LEN NAMEDATALEN
typedefstruct portalhashent { char portalname[MAX_PORTALNAME_LEN]; Portal portal; } PortalHashEnt;
staticHTAB *PortalHashTable = NULL;
管理portal的PostalHashTable
------------ 转载请著明出处,来自博客: blog.csdn.net/beiigang beigang.iteye.com (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |