使用golang 编写postgresql 扩展

postgresql 的扩展可以帮助我们做好多强大的事情,支持的开发语言有lua、perl、java、js、c
社区有人开发了一个可以基于golang开发pg 扩展的项目,使用起来很方便,同时为我们生成了
pg 扩展依赖的文件 control 、sql 文件,以及编译好的共享库

注意我使用的是centos7 操作系统

环境准备

  • golang 安装&&配置
这个比较简单,可以直接使用yum 安装,可能需要配置环境变量,以支持bin 工具的使用
  • 安装plgo
go get -u github.com/microo8/plgo/plgo
  • 安装对应版本的pg server dev 包

    注意安装版本的问题,当前master 分支支持的是pg11 需要checkout 最近几次修改的变动以支持pg10

pg10 支持版本
cd $GOPATH/src/github.com/microo8/plgo/
git checkout efae75298155d8f66a9c28a788e4def50916c
  • 安装pg server dev 包
pg10 
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
pg11
yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm

简单测试

为了简单,直接使用的官方的example 代码

  • 构建
cd $GOPATH/src/github.com/microo8/plgo/example
plgo ./
  • 效果
├── build
│ ├── example--0.1.sql
│ ├── example.control
│ ├── example.h
│ ├── example.so
│ └── Makefile
└── example_methods.go
  • 代码说明
    创建了一个函数以及一个触发器
    example--0.1.sql 内容
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION example" to load this file. \quit
CREATE OR REPLACE FUNCTION Meh()
RETURNS VOID AS
'$libdir/example', 'Meh'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION Meh() IS 'Meh prints out message to error elog
';

CREATE OR REPLACE FUNCTION ConcatAll(tableName text,colName text)
RETURNS text AS
'$libdir/example', 'ConcatAll'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION ConcatAll(text,text) IS 'ConcatAll concatenates all values of an column in a given table
';

CREATE OR REPLACE FUNCTION CreatedTimeTrigger()
RETURNS TRIGGER AS
'$libdir/example', 'CreatedTimeTrigger'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION CreatedTimeTrigger() IS 'CreatedTimeTrigger example trigger
';

CREATE OR REPLACE FUNCTION ConcatArray(strs text[])
RETURNS text AS
'$libdir/example', 'ConcatArray'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION ConcatArray(text[]) IS 'ConcatArray concatenates an array of strings
';

example.control 文件

# example extension
comment = 'example extension'
default_version = '0.1'

Makefile

EXTENSION = example
DATA = example--0.1.sql # script files to install
# REGRESS = example_test # our test script file (without extension)
MODULES = example # our c module file to build

# postgres build stuff
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
  • 安装
make install

效果

make install
/usr/bin/mkdir -p '/usr/pgsql-10/share/extension'
/usr/bin/mkdir -p '/usr/pgsql-10/share/extension'
/usr/bin/mkdir -p '/usr/pgsql-10/lib'
/usr/bin/install -c -m 644 .//example.control '/usr/pgsql-10/share/extension/'
/usr/bin/install -c -m 644 .//example--0.1.sql '/usr/pgsql-10/share/extension/'
/usr/bin/install -c -m 755 example.so '/usr/pgsql-10/lib/'

使用扩展

  • 创建扩展
CREATE EXTENSION example;
CREATE EXTENSION
  • 使用扩展
select concatarray(array['foo','bar']);
 concatarray
-------------
 foobar

说明

plgo 是基于cgo 进行的扩展开发,进行了包装,同时帮助我们生成了好多方便的代码,我们可以像编写普通golang
代码一样,编写pg 扩展,很方便,实际上我们可以基于rpm 包以及deb 包方便的分发我们的扩展。

参考资料

https://github.com/microo8/plgo
https://www.opsdash.com/blog/postgresql-triggers-golang.html
https://github.com/microo8/plgo/issues/28
https://www.postgresql.org/download/linux/redhat/
https://www.cnblogs.com/rongfengliang/p/10655310.html
https://www.cnblogs.com/rongfengliang/p/10654768.html
https://www.cnblogs.com/rongfengliang/p/10650888.html
https://gist.github.com/rongfengliang/53c11c85fb52185f23b24383c2d8faf0

posted on   荣锋亮  阅读(2170)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-04-08 elixir 使用mix umbrella 模块化项目
2014-04-08 web 分享代码片段
2014-04-08 Microsoft OWIN

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示