c - 是否可以从 util 调用 postgres 内部方法?

标签 c postgresql makefile

是否可以从 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,以及后端代码往往严重依赖的其他东西。

只有 libpqlibpgcommonlibpgport 中的代码可以从实用程序中使用。 src/backend 不能,除了一些被重新编译为前端代码并链接到源代码树中的异常(exception)情况(如 pg_xlogdump 对 xlogdesc 代码的使用)。

如果您要调用的内容足够通用并且在编译为前端代码时可以正常工作,请考虑提交补丁以将其移动到 libpgcommon (src/common)。

关于c - 是否可以从 util 调用 postgres 内部方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433784/

相关文章:

java - H2 和 Postgres 阵列兼容性

python - 大于缓冲页 1/3 的值无法编入索引 Django 错误

创建 makefile 来编译位于不同目录中的多个源文件

ruby - 在 OS X Mountain Lion 上通过 RVM 安装 Ruby 时运行 'make' 时出错

使用 C 控制键盘 LED 灯

c - 我不知道如何更改字符串并在末尾添加更多字符

c - 寻求澄清/理解 gcc 的标签作为值(value)特征

c++ - gdb中是否存在自动 "print locals"命令?

php - 比较 PostgreSQL 中的不同数据类型。

c++ - cmake 生成的 makefile 不创建静态库