How to build a Postgres extension using cgo

huangapple go评论95阅读模式
英文:

How to build a Postgres extension using cgo

问题

这是我现在正在做的事情:

<!-- language: none -->

.
├── helloworld--1.0.sql
├── helloworld.control
├── helloworld.go
└── Makefile

helloworld.go:

package helloworld

/*
#cgo LDFLAGS: -rdynamic
#include &quot;postgres.h&quot;
#include &quot;fmgr.h&quot;
#include &quot;utils/builtins.h&quot;

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(helloworld);
PG_FUNCTION_INFO_V1(hello_text_arg);
PG_FUNCTION_INFO_V1(hello_ereport);

Datum
hello_world(PG_FUNCTION_ARGS)
{
	PG_RETURN_TEXT_P(cstring_to_text(&quot;Hello, World!&quot;));
}

Datum
hello_text_arg(PG_FUNCTION_ARGS)
{
	text *hello		= cstring_to_text(&quot;Hello, &quot;);
	int32 hello_sz	= VARSIZE(hello) - VARHDRSZ;

	text *name		= PG_GETARG_TEXT_P(0);
	int32 name_sz	= VARSIZE(name) - VARHDRSZ;

	text *tail		= cstring_to_text(&quot;!&quot;);
	int32 tail_sz	= VARSIZE(tail) - VARHDRSZ;

	int32 out_sz	= hello_sz + name_sz + tail_sz + VARHDRSZ;
	text *out		= (text *) palloc(out_sz);

	SET_VARSIZE(out, out_sz);

	memcpy(VARDATA(out), VARDATA(hello), hello_sz);
	memcpy(VARDATA(out) + hello_sz, VARDATA(name), name_sz);
	memcpy(VARDATA(out) + hello_sz + name_sz, VARDATA(tail), tail_sz);

	PG_RETURN_TEXT_P(out);
}

Datum
hello_ereport(PG_FUNCTION_ARGS)
{
	ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg(&quot;null value not allowed&quot;)));

	PG_RETURN_VOID();
}
*/
import &quot;C&quot;

Makefile:

MODULES = helloworld

EXTENSION = helloworld
DATA = helloworld--1.0.sql
PGFILEDESC = &quot;helloworld - example extension for postgresql&quot;

REGRESS = helloworld

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
INCLUDEDIR = $(shell $(PG_CONFIG) --includedir-server)
include $(PGXS)

helloworld.so:
	CGO_CFLAGS=&quot;-rdynamic -I$(INCLUDEDIR)&quot; CGO_LDFLAGS=&quot;-rdynamic $(LDFLAGS)&quot; go build -v -buildmode=c-shared -o helloworld.so .

在构建时产生了以下错误:

<!-- language: lang-none -->

/tmp/go-build019341122/github.com/amosbird/rpctest/helloworld/_obj/helloworld.cgo2.o: In function `hello_world&#39;:
helloworld.cgo2.c:(.text+0x48): undefined reference to `cstring_to_text&#39;
/tmp/go-build019341122/github.com/amosbird/rpctest/helloworld/_obj/helloworld.cgo2.o: In function `hello_text_arg&#39;:
helloworld.cgo2.c:(.text+0x63): undefined reference to `cstring_to_text&#39;
helloworld.cgo2.c:(.text+0x86): undefined reference to `pg_detoast_datum&#39;
helloworld.cgo2.c:(.text+0xa5): undefined reference to `cstring_to_text&#39;
helloworld.cgo2.c:(.text+0xd7): undefined reference to `palloc&#39;
/tmp/go-build019341122/github.com/amosbird/rpctest/helloworld/_obj/helloworld.cgo2.o: In function `hello_ereport&#39;:
helloworld.cgo2.c:(.text+0x1a8): undefined reference to `errstart&#39;
helloworld.cgo2.c:(.text+0x1bd): undefined reference to `errmsg&#39;
helloworld.cgo2.c:(.text+0x1c9): undefined reference to `errcode&#39;
helloworld.cgo2.c:(.text+0x1d7): undefined reference to `errfinish&#39;
collect2: ld returned 1 exit status
make: *** [helloworld.so] Error 2

我不知道这是否有效。这里发生了很多黑魔法。

所以主要问题是,有什么正确的 Makefile 可以用于在 cgo 中构建一个 Postgres 扩展?

对于这些错误的具体问题是,我该如何推迟 cgo 链接过程中的符号解析?

英文:

Here is what I'm doing right now,

<!-- language: none -->

.
├── helloworld--1.0.sql
├── helloworld.control
├── helloworld.go
└── Makefile

helloworld.go :

package helloworld

/*
#cgo LDFLAGS: -rdynamic
#include &quot;postgres.h&quot;
#include &quot;fmgr.h&quot;
#include &quot;utils/builtins.h&quot;

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(helloworld);
PG_FUNCTION_INFO_V1(hello_text_arg);
PG_FUNCTION_INFO_V1(hello_ereport);

Datum
hello_world(PG_FUNCTION_ARGS)
{
	PG_RETURN_TEXT_P(cstring_to_text(&quot;Hello, World!&quot;));
}

Datum
hello_text_arg(PG_FUNCTION_ARGS)
{
	text *hello		= cstring_to_text(&quot;Hello, &quot;);
	int32 hello_sz	= VARSIZE(hello) - VARHDRSZ;

	text *name		= PG_GETARG_TEXT_P(0);
	int32 name_sz	= VARSIZE(name) - VARHDRSZ;

	text *tail		= cstring_to_text(&quot;!&quot;);
	int32 tail_sz	= VARSIZE(tail) - VARHDRSZ;

	int32 out_sz	= hello_sz + name_sz + tail_sz + VARHDRSZ;
	text *out		= (text *) palloc(out_sz);

	SET_VARSIZE(out, out_sz);

	memcpy(VARDATA(out), VARDATA(hello), hello_sz);
	memcpy(VARDATA(out) + hello_sz, VARDATA(name), name_sz);
	memcpy(VARDATA(out) + hello_sz + name_sz, VARDATA(tail), tail_sz);

	PG_RETURN_TEXT_P(out);
}

Datum
hello_ereport(PG_FUNCTION_ARGS)
{
	ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg(&quot;null value not allowed&quot;)));

	PG_RETURN_VOID();
}
*/
import &quot;C&quot;

Makefile :

MODULES = helloworld

EXTENSION = helloworld
DATA = helloworld--1.0.sql
PGFILEDESC = &quot;helloworld - example extension for postgresql&quot;

REGRESS = helloworld

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
INCLUDEDIR = $(shell $(PG_CONFIG) --includedir-server)
include $(PGXS)

helloworld.so:
	CGO_CFLAGS=&quot;-rdynamic -I$(INCLUDEDIR)&quot; CGO_LDFLAGS=&quot;-rdynamic $(LDFLAGS)&quot; go build -v -buildmode=c-shared -o helloworld.so .

It produced these errors when making :

<!-- language: lang-none -->

/tmp/go-build019341122/github.com/amosbird/rpctest/helloworld/_obj/helloworld.cgo2.o: In function `hello_world&#39;:
helloworld.cgo2.c:(.text+0x48): undefined reference to `cstring_to_text&#39;
/tmp/go-build019341122/github.com/amosbird/rpctest/helloworld/_obj/helloworld.cgo2.o: In function `hello_text_arg&#39;:
helloworld.cgo2.c:(.text+0x63): undefined reference to `cstring_to_text&#39;
helloworld.cgo2.c:(.text+0x86): undefined reference to `pg_detoast_datum&#39;
helloworld.cgo2.c:(.text+0xa5): undefined reference to `cstring_to_text&#39;
helloworld.cgo2.c:(.text+0xd7): undefined reference to `palloc&#39;
/tmp/go-build019341122/github.com/amosbird/rpctest/helloworld/_obj/helloworld.cgo2.o: In function `hello_ereport&#39;:
helloworld.cgo2.c:(.text+0x1a8): undefined reference to `errstart&#39;
helloworld.cgo2.c:(.text+0x1bd): undefined reference to `errmsg&#39;
helloworld.cgo2.c:(.text+0x1c9): undefined reference to `errcode&#39;
helloworld.cgo2.c:(.text+0x1d7): undefined reference to `errfinish&#39;
collect2: ld returned 1 exit status
make: *** [helloworld.so] Error 2

I have no idea if this would work. There are so many black magic happened here.

So the main question is, what is the correct Makefile that can be used to build a Postgres extension in cgo?

A specific question to these errors is, what can I do to defer those symbol resolution in cgo's linking process?

答案1

得分: 4

好的,以下是翻译好的内容:

好的,我花了一整天的时间找到了一个可行的解决方案。

我们需要确保扩展的主要源文件以以下方式开始:

package main  // 确保使用 main 包

/*
#cgo CFLAGS: -I/path/to/postgres/include/server
#cgo LDFLAGS: -Wl,-unresolved-symbols=ignore-all

使用 go build -o myext.so -buildmode=c-shared myext.go 生成 myext.so

如果需要从 C 侧使用一些 Go 方法,我们应该在方法声明之前添加 //export methodname。这将生成没有包名前缀的符号。然后我们可以在 C 侧使用 extern 来引用这些符号。确保导出的 Go 方法位于除 main 之外的包中。

package test :

//export Merge
func Merge(cint C.int) C.int ...

package main :

extern int Merge(int);

import "./test"
var _ = test.Somevar  // 用于占位,以模拟使用 test 包。
英文:

Ok, I spent a whole day and found a viable solution.

We need to have the extension's main source file start with:

package main  // make sure to use main package

/*
#cgo CFLAGS: -I/path/to/postgres/include/server
#cgo LDFLAGS: -Wl,-unresolved-symbols=ignore-all

Use go build -o myext.so -buildmode=c-shared myext.go to generate myext.so.

If some Go method is needed from the C side, we should add //export methodname above the method declaration. This will generate symbols without package name prefix. Then we can extern these symbols on the C side. Make sure the exported Go methods reside in packages other than main.

package test :

//export Merge
func Merge(cint C.int) C.int ...

package main :

extern int Merge(int);

import &quot;./test&quot;
var _ = test.Somevar  // dumb placeholder to fake use package test.

huangapple
  • 本文由 发表于 2017年1月4日 13:27:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/41456777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定