加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

【原】Postgresql中系统缓存相关代码(syscache和catcache)分析

(2017-04-05 20:30:47)
标签:

postgresql源码

数据库

系统缓存

syscache

catcache

分类: Postgres和数据库
前言:本文简单的讨论一下Postgresql的系统缓冲区这部分代码。这部分在彭智勇的《Postgresql数据库内核分析》中已经讲得比较明了了。本文只是做一些提炼和要点整理
 
一)系统缓存存储在syscache里,syscache 实际上是 catcache 组成的链表:
static CatCache *SysCache[lengthof(cacheinfo)];
其中 cacheinfo 中的内容形如:
        {AggregateRelationId,               
                AggregateFnoidIndexId,
                1,
                {
                        Anum_pg_aggregate_aggfnoid,
                        0,
                        0,
                        0
                },
                32
        },
AggregateFnoidIndexId 就是索引名称, 这部分东西参见之前撰写的《Postgresql新增系统表》的那篇博客

二) load_relmap_file  load_relcache_init_file 
load_relmap_file这个函数会把磁盘上的文件读取到内存里
其中系统表存在于global目录下
global/pg_filenode.map这个文件
这个部分内容在relmapper.c当中

load_relcache_init_file 这个函数会读这个文件: pg_internal.init
从这个文件中能获取到 RelationData 的信息
接着还会读一些 Form_pg_class 这里面的信息(因为每个OID都会在pg_class里面注册)
这个 RelationData  还可能是一个index

三)  cache中的hash桶和元素
cache->cc_bucket[hashIndex];
这个里面是hash桶, 桶里的元素使用dlist组织的
dlist中每一个元素为: CatCTup 结构
之所以用dlist 是因为就像dlist的说明中说到的:  dlist适合做预定类型的list 因为这样不浪费存储空间

四)在这个函数中,可以看到系统缓存查找的过程:
HeapTuple
SearchCatCache(CatCache *cache,
                           Datum v1,
                           Datum v2,
                           Datum v3,
                           Datum v4)

1) 调用 CatalogCacheInitializeCache 初始化cache相关信息(这个时候不会查数据的,但是会把字段类型, hash函数什么的设置好,所以这个函数中会调用 heap_open 读取字段类型)
2) CatalogCacheComputeHashValue  通过之前已经设定好的hash函数 计算hashvalue
3) 接着就会在dlist里面取查找, 看能不能找到
4) 如果找不到,就调用 systable_beginscan 扫描系统表索引(在这个函数中会调用 index_beginscan 方法,这个方法里,也注册了一堆回调函数在这个结构体中:RelationAmInfo 
      或者用 heap_beginscan_strat 扫描系统表文件
    typedef struct RelationAmInfo
    {
        FmgrInfo    aminsert;
        FmgrInfo    ambeginscan;
        FmgrInfo    amgettuple;
        FmgrInfo    amgetbitmap;
        FmgrInfo    amrescan;
        FmgrInfo    amendscan;
        FmgrInfo    ammarkpos;
        FmgrInfo    amrestrpos;
        FmgrInfo    amcanreturn;
    } RelationAmInfo;

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有