postgresql 的擴展能夠幫助咱們作好多強大的事情,支持的開發語言有lua、perl、java、js、c
社區有人開發了一個能夠基於golang開發pg 擴展的項目,使用起來很方便,同時爲咱們生成了
pg 擴展依賴的文件 control 、sql 文件,以及編譯好的共享庫html
注意我使用的是centos7 操做系統java
這個比較簡單,能夠直接使用yum 安裝,可能須要配置環境變量,以支持bin 工具的使用
go get -u github.com/microo8/plgo/plgo
注意安裝版本的問題,當前master 分支支持的是pg11 須要checkout 最近幾回修改的變更以支持pg10linux
pg10 支持版本 cd $GOPATH/src/github.com/microo8/plgo/ git checkout efae75298155d8f66a9c28a788e4def50916c
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 代碼git
cd $GOPATH/src/github.com/microo8/plgo/example plgo ./
├── build │ ├── example--0.1.sql │ ├── example.control │ ├── example.h │ ├── example.so │ └── Makefile └── example_methods.go
-- 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 文件github
# example extension comment = 'example extension' default_version = '0.1'
Makefilegolang
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
效果sql
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 包方便的分發咱們的擴展。shell
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/
http://www.javashuo.com/article/p-zaltechz-bv.html
http://www.javashuo.com/article/p-qzvgcxbm-bt.html
http://www.javashuo.com/article/p-qglvcjjg-v.html
https://gist.github.com/rongfengliang/53c11c85fb52185f23b24383c2d8faf0centos