pg_am系统表
pg_am系统表定义在src/include/catalog/pg_am.h文件中,其包含的字段amname为access method name,amhandler为该am句柄函数,amtype为am的类型(i是索引、t是表)。
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系统表中条目的数据如下所示,如下所示是查询的结果。
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。
USING关键字
如上所示使用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子句的处理。