Build PostgreSQL C Functions On Windows
Build PostgreSQL C Functions On Windows
1.0 Introduction
This document describes how to build “Server-side” C functions with PostgreSQL 8.4 and 9.0 on
Windows using Visual Studio 2010. It deals with setting up the build environment including setting up VS
2010, creating a project, setting the correct include directories, setting the compiler and linker command
line options.
This document was developed when creating C functions on a 32 bit Windows XP, 64 Bit Vista and 64 bit
Windows 7 platforms, using both a 64 bit and 32 bit PostgreSQL server. To develop on 32 bit systems all
references in this doc to Program Files (x86) should be changed to Program Files.
Server side C Functions can be used to create new data types, define processing functions and indexing
utility functions.
You can create either a DLL Project or a make file project to for building functions as DLL’s project. For
a DLL project, set it up as a empty project and no precompiled headers
Use the Wizard Radio Buttons to Select a DLL Application Type, and an Empty Project. We don’t use
precompiled headers.
The Windows SDK is downloaded from URL is used for the following files;
#include <windows.h>
#include "postgres.h"
#include "fmgr.h"
#if defined(_WIN32)
#else
#define DLLEXPORT
#endif
DLLEXPORT Datum
add_one_float8(PG_FUNCTION_ARGS)
PG_RETURN_FLOAT8(arg + 1.0);
#define WIN32_LEAN_AND_MEAN
#define NOCOMM
WIN32_LEAN_AND_MEAN Uses a smaller and faster (compile) subset of Windows header files.
NOapi Excludes the specific api from the Windows header file see <windows.h>
for details.
/showIncludes Shows a list of the include files. It’s useful for debugging include directories issues.
The function object files need to be linked with postgreslib.lib to create a DLL. Postgres Lib is found in
the directory;
Any Windows library files requires can be obtain from including the directory
Windows uses back slashes for the delimiter in paths between folders. If you choose to use Windows
style paths the SQL required to load a C function a DLL on Windows, needs the path name to be an
Escaped SQL string. Where each single backslash (\) replaced with a double (\\) one.
e.g.
Setting up the $libdir environment variable, allows PostgreSQL to find the DLL's to load
E.g.
AS '$libdir//mydir/testfunc1.dll', 'add_one'
LANGUAGE C STRICT;
The C functions can be tested with pgAdmin III SQL Query Tool
1.) Copy the DLL and PDB (symbols) Files from the build directory to the PostgreSQL
Directory
2.) Define the function to PostgreSQL using Create Function SQL command with the pgAdmin III
Query tool.
4.) Get the pgAdmin III Query Tools Proces ID “select pg_backend_pid();”
5.) Attach the VC++ debugger to the PostgreSQL process running this function.
To find out the specific PID (Process ID) to attach to run the query
select pg_backend_pid();
Then attach the VC++ debug to that process and re-execute the query. When debugging on 64 bit
Windows, or Windows Vista or Windows 7, due to an enhanced security mode, it will be necessary
to restart Visual Studio with elevated privileges.
When going through the edit, build, test cycle. It’s necessary to stop and restart the PostgreSQL server as
Windows locks the DLL once it’s loaded by the server. The most effective sequence is
It’s possible to build C Functions and C UDT’s on Windows (x64 bit) platforms. PostgreSQL 9 has a 64 bit
Windows build. However, for various reasons better to develop and debug the functionality as a 32 bit
(x86) application. Then port the working functionality to 64 bits.
There are noticeable differences with Windows are the Program Files directories on 6 4 bit systems.
There are two directories.
It’s possible to install and concurrently run both the 32 bit and 64 bit versions of the PostgreSQL
servers on a Windows (x64) system. To build a 64 bit DLL it’s necessary to
2. Set the Include file directories to Compile with the appropriate header files
Sample Code
Books
PostgreSQL 8 for Windows at Amazon books. A good overview and introduction to PostgreSQL
on Windows. It's not useful for server side functions.
Online Documentation
12.1 Tools
A useful tool for debugging what is in a DLL is Anywhere PE Viewer from UCWare.
By dropping a DLL on to its Window you can see the DLL exports (it doesn’t work for 64 bit DLL’s).
12.2 Wishlist
Here is a list of things in no particular priority that would help the development of PostgreSQL C
Functions on Windows.
A quick way to unload DLL’s so they can be copied without re-starting the server.
A Visual Studio PostgreSQL syntax checker. Intelli-sense provides great value but wants to syntax
check SQL files against SQL Server syntax.
A Visual Studio Add-in to execute SQL queries directly from the VS Tool.