Chapter 13. Linking Dynamically-Loaded Functions

Table of Contents
Linux
DEC OSF/1
SunOS 4.x, Solaris 2.x and HP-UX

After you have created and registered a user-defined function, your work is essentially done. Postgres, however, must load the object code (e.g., a .o file, or a shared library) that implements your function. As previously mentioned, Postgres loads your code at runtime, as required. In order to allow your code to be dynamically loaded, you may have to compile and link-edit it in a special way. This section briefly describes how to perform the compilation and link-editing required before you can load your user-defined functions into a running Postgres server.

You should expect to read (and reread, and re-reread) the manual pages for the C compiler, cc(1), and the link editor, ld(1), if you have specific questions. In addition, the contrib area (PGROOT/contrib) and the regression test suites in the directory PGROOT/src/test/regress contain several working examples of this process. If you copy an example then you should not have any problems.

The following terminology will be used below:

The following general restrictions and notes also apply to the discussion below:

Linux

Under Linux ELF, object files can be generated by specifying the compiler flag -fpic.

For example,

# simple Linux example
% cc -fpic -c foo.c
    
produces an object file called foo.o that can then be dynamically loaded into Postgres. No additional loading or link-editing must be performed.