为 Postgres 编写存储引擎:内存表访问方法

diagoal 德哥国产数据库 HaloDB 要集成 DuckDB 了,还是用 TAM 方式实现的,前排坐等 Halo 16 GA。

alt

(图片来源:羲和(Halo)16 Release Preview 1)

恩,本文完。

等等,什么是 TAM?

从 PG 12 开始,在 PG 系统核心和表访问方法 (Table Access Methods) 之间增加了一层表访问方法接口,用于管理表的存储。

alt

每个表访问方法由 pg_am 系统目录中的一行描述。该 pg_am 条目指定表访问方法的名称和处理程序函数。

手头刚好有上周装的 IvorySQL 3.3,就拿这个环境做个实验。

ivorysql=# table pg_am;
 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
(7 rows)

创建 AM 语法 CREATE ACCESS METHOD

CREATE ACCESS METHOD name
    TYPE access_method_type <TABLE|INDEX>
    HANDLER handler_function

相关内容参见:

https://www.postgresql.org/docs/current/tableam.html

扩展 AM 示例

以 pgtam 为例,演示为 PG 增加一种内存表访问方法。

下载源码。

git clone https://github.com/eatonphil/pgtam.git

编译安装。

[root@ivorysql3 pgtam-main]# make
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -fPIC -fvisibility=hidden -I. -I./ -I/opt/ivorysql-3.3/include/postgresql/server -I/opt/ivorysql-3.3/include/postgresql/internal  -D_GNU_SOURCE   -c -o pgtam.o pgtam.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -fPIC -fvisibility=hidden pgtam.o -L/opt/ivorysql-3.3/lib   -Wl,--as-needed -Wl,-rpath,'/opt/ivorysql-3.3/lib',--enable-new-dtags -fvisibility=hidden -shared -o pgtam.so
[root@ivorysql3 pgtam-main]# make install
/usr/bin/mkdir -p '/opt/ivorysql-3.3/share/postgresql/extension'
/usr/bin/mkdir -p '/opt/ivorysql-3.3/share/postgresql/extension'
/usr/bin/mkdir -p '/opt/ivorysql-3.3/lib/postgresql'
/usr/bin/install -c -m 644 .//pgtam.control '/opt/ivorysql-3.3/share/postgresql/extension/'
/usr/bin/install -c -m 644 .//pgtam--0.0.1.sql  '/opt/ivorysql-3.3/share/postgresql/extension/'
/usr/bin/install -c -m 755  pgtam.so '/opt/ivorysql-3.3/lib/postgresql/'

连接数据库,创建扩展。

create extension pgtam;

创建 mem 引擎的表,并插入数据。

create table demo(a int) using mem;
INSERT INTO demo VALUES (20), (24);

查看 AM、表结构和数据。

ivorysql=# select * from pg_am where amtype = 't';
  oid  | amname |      amhandler       | amtype
-------+--------+----------------------+--------
     2 | heap   | heap_tableam_handler | t
 16398 | mem    | mem_tableam_handler  | t
(2 rows)

ivorysql=# \dt+ demo
                                   List of relations
 Schema | Name | Type  |  Owner   | Persistence | Access method |  Size   | Description
--------+------+-------+----------+-------------+---------------+---------+-------------
 public | demo | table | ivorysql | permanent   | mem           | 0 bytes |
(1 row)

ivorysql=# table demo;
 a
----
 20
 24
(2 rows)

参考资料

https://github.com/digoal/blog/blob/master/201903/20190331_03.md

🌻 往期精彩 ▼


-- / END / --

👉 这里可以找到我

如果这篇文章为你带来了灵感或启发,就请帮忙点『』or『在看』or『转发』吧,感谢!ღ( ´・ᴗ・` )~

本文由 mdnice 多平台发布

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值