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

时间:2016-11-05 01:03:20

标签: c postgresql makefile

是否可以从像pg_ctl或pg_dump这样的函数调用函数调用函数(函数是像hash_estimate_size(long,long)这样的Postgres代码?

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)

test_code.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 to hash_estimate_size'中 collect2:ld返回1退出状态 make:*** [test_code]错误1

解决此问题的任何帮助?

1 个答案:

答案 0 :(得分:0)

大多数后端代码无法从实用程序调用,因为它没有链接到实用程序。也不容易,因为前端代码没有ereport和内存上下文libpq,而后端代码往往很大程度上依赖于它。

只能在实用程序中使用libpgcommonlibpgportsrc/backend中的代码。 pg_xlogdump不能,除了一些例外,它们被重新编译为前端代码并链接到源树(例如libpgcommon使用xlogdesc代码)。

如果您要调用的内容足够通用并且在编译为前端代码时可以使用,请考虑提交修补程序以将其移至src/common({{1}})。