是否可以从 pg_ctl 或 pg_dump 等实用程序调用函数(函数在 Postgres 代码中,如 hash_estimate_size(long, long)?
hash_estimate_size (long, long) 在文件 src/backend/utils/hash/dynahash.c 中定义,并在 src/include/utils/hsearch.h 中声明。
我用 Makefile 和 test_code.c 创建了一个新的 util 文件夹:src/bin/test_code。
Makefile 的内容
PGFILEDESC = "test_code"
PGAPPICON=win32
subdir = src/bin/test_code
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
OBJS= test_code.o $(WIN32RES)
all: test_code
test_code: $(OBJS) | submake-libpgport
$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
install: all installdirs
$(INSTALL_PROGRAM) test_code$(X) '$(DESTDIR)$(bindir)/test_code$(X)'
installdirs:
$(MKDIR_P) '$(DESTDIR)$(bindir)'
uninstall:
rm -f '$(DESTDIR)$(bindir)/test_code$(X)'
clean distclean maintainer-clean:
rm -f test_code$(X) $(OBJS)
测试代码.c
#include "postgres.h"
#include "replication/walreceiver.h"
int main(int argc, char *argv[])
{
printf("Has estimate value is = %zu\n", hash_estimate_size(10, 10));
return 0;
}
当我运行“make”时,它会出错
test_code.o:在函数 main' 中:
test_code.c:(.text+0x17a): undefined reference
hash_estimate_size'
collect2: ld 返回 1 退出状态
make: *** [test_code] 错误 1
对解决这个问题有什么帮助吗?
最佳答案
大多数后端代码无法从实用程序调用,因为它没有链接到实用程序。这也不容易,因为前端代码没有 palloc
和内存上下文、ereport
,以及后端代码往往严重依赖的其他东西。
只有 libpq
、libpgcommon
和 libpgport
中的代码可以从实用程序中使用。 src/backend
不能,除了一些被重新编译为前端代码并链接到源代码树中的异常(exception)情况(如 pg_xlogdump
对 xlogdesc 代码的使用)。
如果您要调用的内容足够通用并且在编译为前端代码时可以正常工作,请考虑提交补丁以将其移动到 libpgcommon
(src/common
)。
关于c - 是否可以从 util 调用 postgres 内部方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433784/