Search code examples
cpostgresqllibpq

PostgreSQL says 'symbol not found' when I try to use PQnfelds


I wrote a C function to be executed from within PostgreSQL (with a CREATE FUNCTION ... LANGUAGE C; command) but when I do so, it responds 'PQnfields: symbol not found'. I tried to include the path to where the libpq library is when I compile, like this:

 gcc -lpq -fpic -c crossdb.c -I`pg_config --includedir-server` -I /usr/include/ -std=c99

But it didn't work. I think that it is simply not possible to use libpq to query the database from a function... So if you know how to do it or what is causing this error message, I'd be happy to ear it from you.


Solution

  • You'll have to put the directory containing libpq.so.* on PostgreSQL's shared library path, either by setting LD_LIBRARY_PATH in the server's environment or by adding -Wl,-rpath,/path/to/so to the compile line.

    Are you using the PGXS framework to build the shared library? That gives you most of the proper flags automatically.

    Look at postgres_fdw's Makefile for inspiration, it links with libpq too.

    Taking a step back: are you sure that you need a function called crossdb that calls the client? I would investigate if a foreign table or dblink can do what you are trying to achieve.