PostgreSQL
参考资料:postgresql-9.6-US manual,postgresql内核分析
索引
postgreSQL里面所有的索引都是“从属索引”,索引在物理上与原表文件相分离,作为一个新的数据库对象。
postgresql里面分为四种类型的索引,分别为:
- B-Tree
- Hash
- GiST通用搜索树
- GIN倒排序索引
索引相关系统表
为了管理各种索引类型,postgresql定义了相关系统表pg_am。每种索引为表中的一个元组,里面记录了索引的各种属性以及提供的访问函数。
官方手册的Chapter 59介绍如何定义一种新的索引类型以及索引基本的接口
所有索引类型作为新的数据类型存储,在pg_class中进行描述。物理上,所有索引被划分为标准大小的页面,以使用常规的存储管理函数。
Ordinarily the ambuild function will call IndexBuildHeapScan() to scan the table for existing tuples and compute the keys that need to be inserted into the index. – P2247
nbtree.c/btbuild() -> _bt_spoolinit() -> index.c/IndexBuildHeapScan() -> index.c/IndexBuildHeapRangeScan() //econtext应该含有自定义的key计算 -> EState *estate //executor state for evaluating any index expressions -> execUtils.c/CreateExecutorState() //创建EState —-重点 -> GetPerTupleExprContext() //与EState有关 -> heapam.h/heap_beginscan_strat() //传入的key数量为0,不会生成key -> heapam.h/heap_beginscan_internal() -> heapam.h/initscan() -> index.c/FormIndexDatum() //应该在这里生成key,可自定义计算 -> execQual.c/ExecEvalExprSwitchContext() //自定义计算