@H_403_1@ 话说调用InitPostgres方法给portgres服务进程做相关初始化,这个方法里初始化了relcache和catcache,初始化了执行查询计划的portal的管理器,填充本进程PGPROC结构相关部分成员等,上一节讨论了relcache管理环境的初始化,这一节继续讨论catcache的初始化。
@H_403_1@
@H_403_1@1
@H_403_1@先看InitPostgres方法的调用序列梗概图
@H_403_1@
@H_403_1@
@H_403_1@InitPostgres方法的调用序列梗概图
@H_403_1@ InitPostgres方法为初始化这个postgres服务进程做了一系列的工作,具体如下:
@H_403_1@(1)调用InitProcessPhase2方法,把本进程的PGPROC结构注册到PGPROC数组,就是让共享内存里的PGPROC数组(初始化PGPROC数组的文章见《Postgresql启动过程中的那些事七:初始化共享内存和信号十一:shmem中初始化SharedProcArray》)的第一个空元素指向这个PGPROC结构,并注册退出时做内存清理的函数。
@H_403_1@(2)调用SharedInvalBackendInit方法,在该后台进程数据的共享失效管理器数组获取一个ProcState结构(相关数据结果见《Postgresql启动过程中的那些事七:初始化共享内存和信号十三:shmem中初始化SharedInvalidationState》)给该进程并初始化相关项,并注册退出方法以在退出时标记本进程的项非活跃。
@H_403_1@(3)调用ProcSignalInit方法,@H_403_1@在ProcSignalSlot结构@H_403_1@数组(关于ProcSignalSlot结构@H_403_1@数组参见《Postgresql启动过程中的那些事七:初始化共享内存和信号十四:shmem中初始化PMSignal》)ProcSignalSlots@H_403_1@里给当前进程获取一个元素,元素下标是MyBackendId-1,并注册以在进程退出时释放这个槽。
@H_403_1@(4)为访问XLOG,调用RecoveryInProgress方法做共享内存相关初始化。
@H_403_1@(5)调用RelationCacheInitlisze方法做管理relcache的环境的初始化。
@H_403_1@(6)调用InitCatalogCache方法做管理catcache的环境的初始化。
@H_403_1@(7)调用EnablePortalManager方法初始化portal管理器。
@H_403_1@(8)调用RelationCacheInitializePhase2方法初始化共享系统表。
@H_403_1@(9)调用GetTransactionSnapshot方法获取一个事务快照。这个方法在后面讨论简单查询时再讨论。
(10)调用PerformAuthentication方法根据hba文件设置进行客户端认证。
(11)调用GetDatabaseTuple方法根据数据库名字从pg_database系统表获取要访问的数据库对应的元组。
(12)调用RelationCacheInitializePhase3方法完成relcache初始化。
(13)调用CheckMyDatabase方法检查当前用户的数据库访问权限,从cache里的pg_database取当前数据库的相关属性字段。
(14)调用InitializeClientEncoding方法初始化客户端字符编码。
(15)调用pgstat_bestart方法在PgBackendStatus设置本进程状态。
@H_403_1@
2
下面讨论第(6)步,@H_403_1@InitCatalogCache方法初始化relcache相关对象。Relcache是存放系统表元组的地方。为了图能大一点,PostgresMain以前的调用流程序列就从下面的图中省略了,要回顾可以参考上面的“@H_403_1@InitPostgres方法的调用序列梗概图”。
@H_403_1@
@H_403_1@
@H_403_1@
@H_403_1@ InitCatalogCache -> InitCatCache ->MemoryContextSwitchTo将内存上下文切换到"CacheMemoryContext"@H_403_1@,然后InitCatalogCache ->InitCatCache -> palloc在内存上下文"CacheMemoryContext"@H_403_1@里分配管理catcache的相关结构。相关结构和图形见下面。
@H_403_1@
定义一个syscache的信息
@H_403_1@struct cachedesc
{
Oid reloid; /* OID of the relation being cached */
Oid indoid; /* OID of index relation for this cache */
@H_403_1@int nkeys; /* # of keys needed for cache lookup */
@H_403_1@int key[4]; /* attribute numbers of key attrs */
@H_403_1@int nbuckets; /* numberof hash buckets for this cache */
};
@H_403_1@
@H_403_1@staticconststruct cachedesc cacheinfo[] = {
@H_403_1@{AggregateRelationId,/* AGGFNOID */
@H_403_1@AggregateFnoidIndexId,
@H_403_1@1,
@H_403_1@{
@H_403_1@Anum_pg_aggregate_aggfnoid,
@H_403_1@0,
@H_403_1@0
@H_403_1@},
@H_403_1@32
@H_403_1@},
……,
……,
@H_403_1@{UserMappingRelationId,/* USERMAPPINGOID */
@H_403_1@UserMappingOidIndexId,
@H_403_1@{
@H_403_1@ObjectIdAttributeNumber,
@H_403_1@128
@H_403_1@},/* USERMAPPINGUSERSERVER */
@H_403_1@UserMappingUserServerIndexId,
@H_403_1@2,
@H_403_1@{
@H_403_1@Anum_pg_user_mapping_umuser,
@H_403_1@Anum_pg_user_mapping_umserver,
@H_403_1@128
@H_403_1@}
};
@H_403_1@
@H_403_1@
@H_403_1@typedefstruct catcache
{
@H_403_1@int id; /* cache identifier --- see syscache.h */
@H_403_1@struct catcache *cc_next; /* link to next catcache */
@H_403_1@constchar *cc_relname; /* name of relation the tuples come from */
Oid cc_reloid; /* OID of relation the tuples come from */
Oid cc_indexoid; /* OID of index matching cache keys */
bool cc_relisshared; /* is relation shared across databases? */
TupleDesc cc_tupdesc; /* tuple descriptor (copiedfrom reldesc) */
@H_403_1@int cc_ntup; /* # oftuples currently in this cache */
@H_403_1@int cc_nbuckets; /* # of hashbuckets in this cache */
@H_403_1@int cc_nkeys; /* # ofkeys (1..CATCACHE_MAXKEYS) */
@H_403_1@int cc_key[CATCACHE_MAXKEYS]; /* AttrNumberof each key */
PGFunctioncc_hashfunc[CATCACHE_MAXKEYS]; /* hash functionfor each key */
ScanKeyDatacc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
@H_403_1@* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
Dllist cc_lists; /* list of CatCList structs */
@H_403_1@#ifdef CATCACHE_STATS
@H_403_1@long @H_403_1@cc_searches; /*total # searches against this cache */
@H_403_1@long @H_403_1@cc_hits; /* # of matchesagainst existing entry */
@H_403_1@long @H_403_1@cc_neg_hits; /* #of matches against negative entry */
@H_403_1@long @H_403_1@cc_newloads; /* #of successful loads of new entry */
@H_403_1@
/*
@H_403_1@* cc_searches - (cc_hits + cc_neg_hits +cc_newloads) is number of Failed
@H_403_1@* searches,each of which will result inloading a negative entry
@H_403_1@*/
@H_403_1@long @H_403_1@cc_invals; /* #of entries invalidated from cache */
@H_403_1@long @H_403_1@cc_lsearches; /*total # list-searches */
@H_403_1@long @H_403_1@cc_lhits; /* # of matchesagainst existing lists */
@H_403_1@#endif
Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
@H_403_1@
@H_403_1@structcatcacheheader: 管理所有cache的信息.
@H_403_1@typedefstruct catcacheheader
{
CatCache @H_403_1@*ch_caches; /* head of list of CatCache structs */
@H_403_1@int ch_ntup; /* # oftuples in all caches */
} CatCacheHeader;
@H_403_1@
@H_403_1@
@H_403_1@
@H_403_1@catcache和管理它的相关结构图
@H_403_1@系统表和SysCache@H_403_1@中的catcache@H_403_1@一一对应。静态数组cacheinfo@H_403_1@的元素cachedesc@H_403_1@描述系统表的catcache@H_403_1@信息。系统表中的元组存放在catcache@H_403_1@的双向链表数组cc_bucket@H_403_1@的元素Dllist@H_403_1@里。
@H_403_1@
@H_403_1@------------ 转载请著明出处,来自博客: blog.csdn.net/beiigang beigang.iteye.com