【openGauss数据库内核分析系列】:数据库索引的创建过程_opengauss 视图索引

本文详细介绍了PostgreSQL中创建索引的关键函数如DefineIndex和Index_create,涉及索引类型(如btree、hash、gin等)、创建过程、并发控制以及存储类型。还提及了系统表pg_class和pg_index的使用,以及index_build函数在构建索引中的作用。
摘要由CSDN通过智能技术生成


#define DEFAULT_INDEX_TYPE “btree”
#define DEFAULT_HASH_INDEX_TYPE “hash”
#define DEFAULT_CSTORE_INDEX_TYPE “psort”
#define DEFAULT_GIST_INDEX_TYPE “gist”
#define CSTORE_BTREE_INDEX_TYPE “cbtree”
#define DEFAULT_GIN_INDEX_TYPE “gin”
#define CSTORE_GINBTREE_INDEX_TYPE “cgin”
#define DEFAULT_USTORE_INDEX_TYPE “ubtree”


## 普通表索引


### DefineInde


DefineIndex为创建索引主入口函数。通常创建索引以Share锁锁定表,允许并发查询,但禁上对表进行修改。如果创建索引时指定关键字CONCURRENTLY以不阻塞DML的方式创建索引,即允许读取和更新表,以ShareUpdateExclusiveLock锁锁定表。


![在这里插入图片描述](https://img-blog.csdnimg.cn/5a0a45574aa84a7889becbb0f9b486f3.png)



lockmode = concurrent ? ShareUpdateExclusiveLock : ShareLock;
rel = heap\_open(relationId, lockmode);

如果没有指定索引名,ChooseIndexName根据规则生成索引名:



/\*

* Select name for index if caller didn’t specify.
*/
indexRelationName = stmt->idxname;
if (indexRelationName == NULL) {
indexRelationName = ChooseIndexName(RelationGetRelationName(rel),
namespaceId,
indexColNames,
stmt->excludeOpNames,
stmt->primary,
stmt->isconstraint);


为index\_create函数构造参数IndexInfo结构体:



/*

  • Prepare arguments for index_create, primarily an IndexInfo structure.
  • Note that ii_Predicate must be in implicit-AND format.
    */
    indexInfo = makeNode(IndexInfo);

Index\_create函数创建索引:  
 /\*  
 \* Make the catalog entries for the index, including constraints. Then, if  
 \* not skip\_build || concurrent, actually build the index.  
 \*/  
 indexRelationId = index\_create(rel,  
 ……  
 关闭表并返回索引表id:



heap\_close(rel, NoLock);
return indexRelationId;

### Index\_create函数


打开系统表pg\_class:



> 
> pg\_class = heap\_open(RelationRelationId, RowExclusiveLock);
> 
> 
> 


heap\_create创建relcache和索引物理文件:



/\*

* create the index relation’s relcache entry and physical disk file. (If
* we fail further down, it’s the smgr’s responsibility to remove the disk
* file again.)
*/
StorageType storage_type = RelationGetStorageType(heapRelation);
indexRelation = heap_create(indexRelationName, namespaceId, tableSpaceId, indexRelationId, relFileNode,
RELATION_CREATE_BUCKET(heapRelation) ? heapRelation->rd_bucketoid : InvalidOid, indexTupDesc, relKind,
relpersistence, isLocalPart, false, shared_relation, mapped_relation, allow_system_table_mods,
REL_CMPRS_NOT_SUPPORT, (Datum)reloptions, heapRelation->rd_rel->relowner, skip_create_storage,
isUstore ? TAM_USTORE : TAM_HEAP, /* XXX: Index tables are by default HEAP Table Type */
relindexsplit, storage_type, extra->crossBucket, accessMethodObjectId);


将索引表元信息存入系统表pg\_class:



/*
* store index’s pg_class entry
*/
InsertPgClassTuple(
pg_class, indexRelation, RelationGetRelid(indexRelation), (Datum)0, reloptions, relKind, NULL);

/* done with pg_class */
heap_close(pg_class, RowExclusiveLock);

将索引表元信息存入系统表pg\_index:



 UpdateIndexRelation(indexRelationId,
    heapRelationId,
    indexInfo,

……


Index\_build建立索引:



} else if (extra && (!extra->isPartitionedIndex || extra->isGlobalPartitionedIndex)) {
    /\* support regular index or GLOBAL partition index \*/
    index\_build(heapRelation, NULL, indexRelation, NULL, indexInfo, isprimary, false, PARTITION\_TYPE(extra));
}

### index\_build


index\_build调用index\_build\_storage,如果创建的是btree索引最终调用btbuild,如果是hash索引最终调用hashbuild,如果是psort则最终调用psortbuild,更多索引访问方法的信息可查看系统表pg\_am。



> 
> stats = index\_build\_storage(targetHeapRelation, targetIndexRelation, indexInfo);
> 
> 
> 



static IndexBuildResult* index_build_storage(Relation heapRelation, Relation indexRelation, IndexInfo* indexInfo)
{
RegProcedure procedure = indexRelation->rd_am->ambuild;
Assert(RegProcedureIsValid(procedure));

IndexBuildResult\* stats = (IndexBuildResult\*)DatumGetPointer(OidFunctionCall3(
    procedure, PointerGetDatum(heapRelation), PointerGetDatum(indexRelation), PointerGetDatum(indexInfo)));
Assert(PointerIsValid(stats));
if (RELPERSISTENCE_UNLOGGED == heapRelation->rd_rel->relpersistence) {

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

  • 12
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值