微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL服务过程中的那些事一:启动postgres服务进程一.六:初始化系统表缓存catcache

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

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

下面讨论第(6)步,InitCatalogCache方法初始化relcache相关对象。Relcache是存放系统表元组的地方。为了图能大一点,PostgresMain以前的调用流程序列就从下面的图中省略了,要回顾可以参考上面的“InitPostgres方法调用序列梗概图”。


相关方法调用序列图

InitCatalogCache -> InitCatCache ->MemoryContextSwitchTo将内存上下文切换到"CacheMemoryContext",然后InitCatalogCache ->InitCatCache -> palloc在内存上下文"CacheMemoryContext"里分配管理catcache的相关结构。相关结构和图形见下面。

定义一个syscache的信息

struct cachedesc

{

Oid reloid; /* OID of the relation being cached */

Oid indoid; /* OID of index relation for this cache */

int nkeys; /* # of keys needed for cache lookup */

int key[4]; /* attribute numbers of key attrs */

int nbuckets; /* numberof hash buckets for this cache */

};

staticconststruct cachedesc cacheinfo[] = {

{AggregateRelationId,/* AGGFNOID */

AggregateFnoidindexId,

1,

{

Anum_pg_aggregate_aggfnoid,

0,

0

},

32

},

……,

……,

{UserMappingrelationId,/* USERMAPPINGOID */

UserMappingOidindexId,

{

ObjectIdAttributeNumber,

128

},/* USERMAPPINGUSERSERVER */

UserMappingUserServerIndexId,

2,

{

Anum_pg_user_mapping_umuser,

Anum_pg_user_mapping_umserver,

128

}

};

typedefstruct catcache

{

int id; /* cache identifier --- see syscache.h */

struct catcache *cc_next; /* link to next catcache */

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) */

int cc_ntup; /* # oftuples currently in this cache */

int cc_nbuckets; /* # of hashbuckets in this cache */

int cc_nkeys; /* # ofkeys (1..CATCACHE_MAXKEYS) */

int cc_key[CATCACHE_MAXKEYS]; /* AttrNumberof each key */

PGFunctioncc_hashfunc[CATCACHE_MAXKEYS]; /* hash functionfor each key */

ScanKeyDatacc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for

* heap scans */

bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */

Dllist cc_lists; /* list of CatCList structs */

#ifdef CATCACHE_STATS

long cc_searches; /*total # searches against this cache */

long cc_hits; /* # of matchesagainst existing entry */

long cc_neg_hits; /* #of matches against negative entry */

long cc_newloads; /* #of successful loads of new entry */

/*

* cc_searches - (cc_hits + cc_neg_hits +cc_newloads) is number of Failed

* searches,each of which will result inloading a negative entry

*/

long cc_invals; /* #of entries invalidated from cache */

long cc_lsearches; /*total # list-searches */

long cc_lhits; /* # of matchesagainst existing lists */

#endif

Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */

} CatCache; /* VARIABLE LENGTH STRUCT */

structcatcacheheader: 管理所有cache的信息.

typedefstruct catcacheheader

{

CatCache *ch_caches; /* head of list of CatCache structs */

int ch_ntup; /* # oftuples in all caches */

} CatCacheHeader;


catcache和管理它的相关结构图

系统表和SysCache中的catcache一一对应。静态数组cacheinfo的元素cachedesc描述系统表的catcache信息。系统表中的元组存放在catcache的双向链表数组cc_bucket的元素Dllist里。


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

原文地址:https://www.jb51.cc/postgresql/196421.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐