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,

0

},

32

},

……,

……,

……,

{UserMappingRelationId, /* USERMAPPINGOID */

UserMappingOidIndexId,

1,

{

ObjectIdAttributeNumber,

0,

0,

0

},

128

},

{UserMappingRelationId, /* USERMAPPINGUSERSERVER */

UserMappingUserServerIndexId,

2,

{

Anum_pg_user_mapping_umuser,

Anum_pg_user_mapping_umserver,

0,

0

},

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值