PostgreSQL数据库可插拔存储引擎——pg_am系统表_#define

pg_am系统表

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


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 */

oid

amname

amhandler

amtype

2

heap

heap_tableam_handler

t

403

btree

bthandler

i

405

hash

hashhandler

i

783

gist

gisthandler

i

2742

gin

ginhandler

i

4000

spgist

spghandler

i

3580

brin

brinhandler

i

以HeapAM为例,其在pg_am和pg_proc系统表中条目的数据如下所示,如下所示是查询的结果。

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


PostgreSQL数据库可插拔存储引擎——pg_am系统表_#define_04


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,

};

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

如上所示使用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。

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


如下图代码位于src/backend/parser/gram.y文件中,其中table_access_method_clause代表了using子句的处理。

PostgreSQL数据库可插拔存储引擎——pg_am系统表_#define_06


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