2016-07-26 34 views
4

我想编译并运行go代码作为Postgresql存储过程。 我的动机是因为PostgreSQL可以用C语言编写golang excensions可以被编译为c-共享Golang过程语言Postgresql

所以我要文件,pl.go:

package main 

/* 
#cgo CFLAGS: -Wall -Wpointer-arith -Wno-declaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fpic -I. -I./ -I/usr/include/postgresql/server -I/usr/include/postgresql/internal -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2 
#cgo LDFLAGS: -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fpic -L/usr/lib -Wl,-O1,--sort-common,--as-needed,-z,relro -Wl,--as-needed -Wl,-rpath,'/usr/lib',--enable-new-dtags -shared 

#include "postgres.h" 
#include "fmgr.h" 
#include "utils/builtins.h" 

#ifdef PG_MODULE_MAGIC 
PG_MODULE_MAGIC; 
#endif 

//the return value must be allocated trough palloc 
void* ret(void *val, uint64 *size) { 
    void *retDatum = palloc(*size); 
    memcpy(retDatum, val, *size); 
    return retDatum; 
} 

PG_FUNCTION_INFO_V1(plgo_func); 
*/ 
import "C" 
import "unsafe" 

func main() {} 

//PGVal returns the Postgresql C type from Golang type (currently implements just stringtotext) 
func PGVal(val interface{}) (ret interface{}) { 
    var size uintptr 
    switch v := val.(type) { 
    case string: 
     ret = C.cstring_to_text(C.CString(v)) 
     size = unsafe.Sizeof(ret) 
    default: 
     ret = val 
     size = unsafe.Sizeof(ret) 
    } 
    return C.ret(ret, (*C.uint64)(unsafe.Pointer(size))) 
} 

CFLAGSLDFLAGS i'we了从pg_config

,并在那里我创建函数来调用该文件,plgo.go:

package main 

/* 
#include "postgres.h" 
#include "fmgr.h" 
#include "utils/builtins.h" 
*/ 
import "C" 

//export plgo_func 
func plgo_func(fcinfo *C.FunctionCallInfoData) interface{} { 
    return PGVal("meh") 
} 

共享库与创建:go build -buildmode=c-shared -o plgo.so plgo.go pl.go && sudo cp plgo.so /usr/lib/postgresql

PostgreSQL中的功能与创建:

CREATE OR REPLACE FUNCTION public.plgo_func(integer) 
    RETURNS text AS 
'$libdir/plgo', 'plgo_func' 
    LANGUAGE c IMMUTABLE STRICT 
    COST 1; 

但是当我运行:psql -U root -d meh -c "select plgo_func(0)"

服务器崩溃有:

server closed the connection unexpectedly 
    This probably means the server terminated abnormally 
    before or while processing the request. 
connection to server was lost 

编辑:我已经成功创建了golang“库”,用于在golang中创建存储过程和触发器plgo :)

+1

C没有任何Go接口{}的概念。您需要从导出的函数中返回一个C类型。 (如果这不起作用,您需要从服务器获得更多调试信息,以了解它为什么崩溃) – JimB

+0

这没有奏效,我该如何在该代码中添加一些调试/日志? '导入“日志”'而不是打印到文件不起作用... – microo8

+1

我想你是'ret'函数中的segfaulting,因为'cstring_to_text'返回一个'* text',而你只有palloc该指针的大小,然后将'text'结构复制到该位置。我会首先在C语言中做一个概念验证,以确保在扩展到Go/cgo之前可以完成这个工作。 – JimB

回答

3

诀窍是使用0版本的调用约定,因为那些允许调用简单的C函数而不使用它们添加到版本1调用中的所有花哨的宏。

您还需要一个C文件来调用PG_MODULE_MAGIC宏。

我有一个可以完成所有工作的工作示例项目,如果您只是将其复制为起点,那么可能是最简单的。

不确定它会让你做到你想要的东西,但它的确适用于某些用例。

https://github.com/dbudworth/gopgfuncs

也将概述回购告诉你同样的东西...

步骤1

创建你函数。去文件和CGO包括

package main 

//#include "postgres.h" 
//#include "fmgr.h" 
//#include <string.h> 
import "C" 
import (
    "log" 
    "sync/atomic" 
) 

// Functions are scoped to the db session 
var counter int64 

//Inc atomically increments a session local counter by a given delta 
//export Inc 
func Inc(delta C.int64) C.int64 { 
    log.Printf("Inc(%v) called", delta) 
    return C.int64(atomic.AddInt64(&counter, int64(delta))) 
} 

//AddOne adds one to the given arg and retuns it 
//export AddOne 
func AddOne(i C.int) C.int { 
    log.Printf("AddOne(%v) called", i) 
    return i + 1 
} 

func main() { 
} 

步骤2

在同一个d中创建一个.c文件从postgres.h调用PG_MODULE_MAGIC宏的irectory,这是所有扩展库的要求。 Go会在编译时自动包含C文件,不需要特别指示。

#include "postgres.h" 
#include "fmgr.h" 

PG_MODULE_MAGIC; 

步骤3

建立您因此文件,诱骗这里是使用-buildmode = C-共享

go build -buildmode=c-shared -o libMyMod.so 

步骤4

执行SQL注册你的功能。由于注册扩展需要绝对路径,所以我宁愿在命令行上传递该模块以避免使“install.sql”脚本在其中包含硬编码路径。

PGMOD=`pwd`/libMyMod.so 
psql $(DBNAME) --set=MOD=\'$(PGMOD)\' -f install.sql 

安装脚本包含您导出的每个函数中的这些语句之一。

由于系统无法验证输入/输出类型,所以输入/输出类型无疑是绝对关键的,您可能最终会踩踏内存,存储等等。您可以看到pg类型转换为c类型here

create or replace function add_one(integer) 
    returns integer as :MOD,'AddOne' 
    LANGUAGE C STRICT; 
+0

看起来不错,我工作过版本1,现在在我的github上有一个复杂的混乱:https://github.com/microo8/plgo 有没有所有的CFLAGS和LDFLAGS选项?只是建立共享? – microo8

+1

是的,这就是为什么我坚持版本0.我想存储过程中去。不要写一堆C代码来将它们连接在一起。当然,你不需要所有的标志都采取我的方法,也许这是V1宏所必需的 –