使用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM