title: What is the appropriate library for linking Postgres server-side C functions
body: <p>I am trying to compile some C extensions, on platform with Ubuntu 14.04, for Postgres 9.5.</p>
  
  <p>In my case, I want to write my C code and compile it first to a standalone executable (as you can see from my Makefile below). This is because I am also using the NumPy API and writing functions that convert Postgres <code>ArrayType</code> arrays into NumPy <code>PyArray</code> objects, and then use some NumPy array functions. It's very tricky to get the details right, deallocate <code>NpyIter</code> objects correctly, etc., so I definitely need to compile, run, observe errors, and test all <em>before</em> finalizing the details of how the library is built for the final part where I say <code>CREATE EXTENSION</code> in Postgres.</p>
  
  <p>When compiling, I get several <a href="https://stackoverflow.com/questions/15257120/undefined-reference-to-linker-error">undefined reference</a> issues, such as:</p>
  
  <pre><code>tmp.c:(.text+0x2d6): undefined reference to `get_typlenbyvalalign'
  tmp.c:(.text+0x346): undefined reference to `deconstruct_array'
  tmp.c:(.text+0x41f): undefined reference to `DatumGetFloat8'
  tmp.c:(.text+0x4ae): undefined reference to `pfree'
  tmp.c:(.text+0x4ba): undefined reference to `pfree'
  </code></pre>
  
  <p>These are server-side functions from the Postgres C API, but with lots of Googling and lots of strongarming of <code>pgxs</code> I cannot figure out how to obtain the name or path of the backend Postgres library that I'm failing to link.</p>
  
  <p>Almost all searches just mention <code>libpq</code>, but these functions are not defined in that client-side API library, so I'm looking for something else.</p>
  
  <p>For reference, here's the Makefile I'm currently using. Including the library directory from <code>pg_config --libdir</code> must also be incorrect, as it does not cause any changes in the undefined reference errors.</p>
  
  <pre><code>INCLUDEDIRS := -I.
  INCLUDEDIRS += -I/usr/include/python2.7
  INCLUDEDIRS += -I/home/username/anaconda/lib/python2.7/site-packages/numpy/core/include
  INCLUDEDIRS += -I$(shell pg_config --includedir-server)
  INCLUDEDIRS += -I$(shell pg_config --includedir)
  
  LIBS := -L$(shell pg_config --libdir)
  LIBS += -lpython2.7
  
  tmp: tmp.c Makefile
      gcc tmp.c -o tmp $(INCLUDEDIRS) $(LIBS)
  </code></pre>
  
  <p>Output of <code>pg_config --libdir</code> is:</p>
  
  <pre><code>user@computer:~/programming$ pg_config --libdir
  /usr/lib/x86_64-linux-gnu
  </code></pre>
  
  <p>In that library directory I also found <code>libpgcommon</code> and when I add it to the Makefile, some of the undefined references disappear, but not all. These still remain:</p>
  
  <pre><code>tmp.c:(.text+0x2d6): undefined reference to `get_typlenbyvalalign'
  tmp.c:(.text+0x346): undefined reference to `deconstruct_array'
  tmp.c:(.text+0x41f): undefined reference to `DatumGetFloat8'
  </code></pre>
  
  <p>So <code>pfree</code> was found by linking with <code>libpgcommon</code>, but nothing else.</p>
  
  <p>Digging even further, inside of <code>postgres.h</code> I can see where the <code>DatumGetFloat8</code> macro is defined (line 662):</p>
  
  <pre><code>#ifdef USE_FLOAT8_BYVAL
  extern float8 DatumGetFloat8(Datum X);
  #else
  #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
  #endif
  </code></pre>
  
  <p>So, it must be the case that Postgres was installed in some way that made use of the <code>USE_FLOAT8_BYVAL</code> flag. Is that standard? Would you expect that from a plain Postgres install using package repos for a popular OS like Ubuntu?</p>
  
  <p>Given this, what is the other source code or library from which <code>DatumGetFloat8</code> is <code>extern</code>'d from? For example, searching Google for "postgres DatumGetFloat8" sheds pretty much no light on this. The best I can find is a <a href="http://postgresql.nabble.com/Fwd-Re-Calling-it-a-day-td4360247.html" rel="nofollow noreferrer">message thread from 2011</a> stating (not sure if correctly):</p>
  
  <blockquote>
    <p>A missing reference to DatumGetFloat8 implies that the server was built<br>
    with float8 pass by value and pljava was built with float8 pass by<br>
    reference. </p>
  </blockquote>
  
  <p>(The <code>pljava</code> bit is irrelevant for me).</p>