Re: Using a C++ library in PostgreSQL - Mailing list pgsql-hackers

From Sam Mason
Subject Re: Using a C++ library in PostgreSQL
Date
Msg-id 20090719165724.GH5407@samason.me.uk
Whole thread Raw
In response to Using a C++ library in PostgreSQL  (Xin Wang <andywx@gmail.com>)
List pgsql-hackers
On Sun, Jul 19, 2009 at 10:13:35PM +0800, Xin Wang wrote:
>     My research project is based on PostgreSQL. I want to get the
> Post-Schema-Validation-Infoset (PSVI) of an XML document being
> stored. However, the current libxml2 library does not support PSVI at
> all. So I have to use the Xerces C++ library in PostgreSQL.

I've never had to do anything like this before, so these will just be
general recommendations that I believe I've seen made before on this
list.

> But I wonder if it is possible to call a C++ API in a C code base.

In most common cases, yes this is perfectly possible.  Just make sure
that you catch all exceptions and have all functions that have to be
callable from normal C code declared as 'extern "C"'.

I have a feeling that PG does things that interact badly with the way
C++ handles exceptions.  As far as I can tell[1], this only applies when
you're calling back into Postgres' C code from C++.  There are various
other annoyances (i.e. the sort of thing you always get when trying
to get code talking together that wasn't expecting to be) to do with
Postgres, like things in the headers using names that are reserved in
C++ and not in C.

> Could anyone here give me some guidelines to do it? Can the recently
> discussed "Mostly Harmless C++" patch be helpful to what I want to do?

Isolate all your C++ code away in one part of your code and have some C
code in the middle to do the translations between the two worlds.  As
far as PG should be concerned it will just be calling C code.

If you can't get that to work and performance doesn't matter too much;
put your C++ code into a separate program and have the C code run said
program when needed.

--  Sam  http://samason.me.uk/
[1] http://wiki.postgresql.org/wiki/C++_Compatibility


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Using results from INSERT ... RETURNING
Next
From: Tom Lane
Date:
Subject: Re: GEQO vs join order restrictions