网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
#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) {
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!