当前位置:   article > 正文

PostgreSQL数据库可插拔存储引擎——pg_am系统表_postgresql am

postgresql am

在这里插入图片描述

pg_am系统表

在这里插入图片描述
pg_am系统表定义在src/include/catalog/pg_am.h文件中,其包含的字段amname为access method name,amhandler为该am句柄函数,amtype为am的类型(i是索引、t是表)。

CATALOG(pg_am,2601,AccessMethodRelationId){
	Oid			oid;			/* oid */	
	NameData	amname;			/* access method name */	
	regproc		amhandler BKI_LOOKUP(pg_proc); /* handler function */	
	char		amtype;			/* see AMTYPE_xxx constants below */
} FormData_pg_am;
/* Allowed values for amtype */
#define AMTYPE_INDEX					'i' /* index access method */
#define AMTYPE_TABLE					't' /* table access method */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
oidamnameamhandleramtype
2heapheap_tableam_handlert
403btreebthandleri
405hashhashhandleri
783gistgisthandleri
2742ginginhandleri
4000spgistspghandleri
3580brinbrinhandleri

以HeapAM为例,其在pg_am和pg_proc系统表中条目的数据如下所示,如下所示是查询的结果。
在这里插入图片描述
在这里插入图片描述
heap_tableam_handler函数定义在src/backend/access/heap/heapam_handler.c文件中,该函数直接使用PG_RETURN_POINTER宏封装并返回了heapam_methods结构体指针。TableAmRoutine是表访问方法的API结构体,作为服务端声明周期有效的数据结构,需要将其作为静态常量结构体定义,通过FormData_pg_am.amhandler获取。如果需要实现其他TableAM,只需要实现自己的TableAmRoutine结构,实现并定义TableAmRoutine里面所有的API。

Datum heap_tableam_handler(PG_FUNCTION_ARGS){ PG_RETURN_POINTER(&heapam_methods); }
const TableAmRoutine *GetHeapamTableAmRoutine(void){ return &heapam_methods; }
static const TableAmRoutine heapam_methods = {
	.type = T_TableAmRoutine,
	.slot_callbacks = heapam_slot_callbacks,};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

USING关键字

CREATE EXTENSION magic_storage;
CREATE TABLE something () USING magic_storage;
SET default_table_access_method = ‘magic_storage’;
CREATE TABLE else (); -- still uses magic_storage
  • 1
  • 2
  • 3
  • 4

如上所示使用USING关键字定义了表使用的可插拔存储引擎。default_table_access_method作为guc参数,用于Sets the default table access method for new tables,该参数也和using关键字的处理息息相关。src/backend/commands/tablecmds.c/DefineRelation函数中如下代码表示了accessMethod的处理。首先,如果使用using关键字指明了table am,则直接使用;否则,针对RELKIND_RELATION、RELKIND_TOASTVALUE、RELKIND_MATVIEW使用default_table_access_method参数代表的table am。
在这里插入图片描述
如下图代码位于src/backend/parser/gram.y文件中,其中table_access_method_clause代表了using子句的处理。
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/595452
推荐阅读
相关标签
  

闽ICP备14008679号