Re: C++ User-defined functions

Lists: pgsql-general
From: George Oakman <oakmang(at)hotmail(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: C++ User-defined functions
Date: 2009-03-09 16:38:37
Message-ID: COL115-W80BE89EBC14FB0CB8A9DDBAFA00@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


Dear all,

I am trying to write a user-defined function in C++. Most examples are give in plain C. I would be very grafeful for a sample program/code-snippet in C++.

Thank you very much,

George.

_________________________________________________________________
View your Twitter and Flickr updates from one place – Learn more!
http://clk.atdmt.com/UKM/go/137984870/direct/01/


From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: George Oakman <oakmang(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: C++ User-defined functions
Date: 2009-03-09 17:35:34
Message-ID: 49B55366.6010409@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

George Oakman wrote:

> I am trying to write a user-defined function in C++. Most examples are give in plain C. I would be very grafeful for a sample program/code-snippet in C++.

It's just like any other C/C++ code mixing. You must make sure that any
C-only headers are included within an `extern "C"' block, and declare
any functions that'll be accessed via dlopen() etc as 'extern "C"' too.
You may only use POD types, arrays of POD types, and structs of POD
types (with no methods) in calls to/from C code.

In other words, all PostgreSQL must see is plain C code, but your
'extern "C"' functions may call C++ methods and work with C++ objects
internally.

There's lots more information about this on the Internet. Just look for
generic resources on calling C from C++ and vice versa.

(I don't *think* there are any issues with libstdc++, though you'd
probably have to make sure that no other program linked into Pg brings
in a different version of libstdc++.)

--
Craig Ringer


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
Cc: George Oakman <oakmang(at)hotmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: C++ User-defined functions
Date: 2009-03-09 18:17:04
Message-ID: 9684.1236622624@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Craig Ringer <craig(at)postnewspapers(dot)com(dot)au> writes:
> There's lots more information about this on the Internet. Just look for
> generic resources on calling C from C++ and vice versa.

> (I don't *think* there are any issues with libstdc++, though you'd
> probably have to make sure that no other program linked into Pg brings
> in a different version of libstdc++.)

I believe this is fairly platform-dependent. On platforms where the
standard libc includes everything you need for C++, you'd likely be okay,
but we've seen problems on (possibly obsolete?) platforms where it
doesn't. One thing you've got to be really wary of is C++ exceptions,
which tend not to interoperate nicely with PG's longjmp-based error
handling.

My own advice is to think twice about how badly your function needs to
be C++ rather than plain C.

regards, tom lane


From: George Oakman <oakmang(at)hotmail(dot)com>
To: <craig(at)postnewspapers(dot)com(dot)au>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: C++ User-defined functions
Date: 2009-03-09 19:22:11
Message-ID: COL115-W3057AE644DE56E006B9332AFA00@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


Thanks Craig,

I'm trying to compile a very simple test with Visual Studio (2008), but I get the following errors:

c:\program files\postgresql\8.3\include\server\pg_config_os.h(188) : error C2011: 'timezone' : 'struct' type redefinition

c:\program files\postgresql\8.3\include\server\pg_config_os.h(188) : see declaration of 'timezone'
c:\program files\postgresql\8.3\include\server\pg_config_os.h(197) : error C2011: 'itimerval' : 'struct' type redefinition

c:\program files\postgresql\8.3\include\server\pg_config_os.h(197) : see declaration of 'itimerval'
c:\program files\postgresql\8.3\include\server\c.h(97) : fatal error C1083: Cannot open include file: 'libintl.h': No such file or directory

I must be doing something wrong - I don't even have a libintl.h on my drive...

This is my .cpp file:

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

#define _USE_32BIT_TIME_T

#include "postgres.h"
#include <string.h>
#include "fmgr.h"

/* by value */

PG_FUNCTION_INFO_V1(add_one);

Datum
add_one(PG_FUNCTION_ARGS)
{
int32 arg = PG_GETARG_INT32(0);

PG_RETURN_INT32(arg + 1);
}

If you can help, that would be wonderful.

Thanks a lot,

George.


> Date: Tue, 10 Mar 2009 02:35:34 +0900
> From: craig(at)postnewspapers(dot)com(dot)au
> To: oakmang(at)hotmail(dot)com
> CC: pgsql-general(at)postgresql(dot)org
> Subject: Re: [GENERAL] C++ User-defined functions
>
> George Oakman wrote:
>
> > I am trying to write a user-defined function in C++. Most examples are give in plain C. I would be very grafeful for a sample program/code-snippet in C++.
>
> It's just like any other C/C++ code mixing. You must make sure that any
> C-only headers are included within an `extern "C"' block, and declare
> any functions that'll be accessed via dlopen() etc as 'extern "C"' too.
> You may only use POD types, arrays of POD types, and structs of POD
> types (with no methods) in calls to/from C code.
>
> In other words, all PostgreSQL must see is plain C code, but your
> 'extern "C"' functions may call C++ methods and work with C++ objects
> internally.
>
> There's lots more information about this on the Internet. Just look for
> generic resources on calling C from C++ and vice versa.
>
> (I don't *think* there are any issues with libstdc++, though you'd
> probably have to make sure that no other program linked into Pg brings
> in a different version of libstdc++.)
>
> --
> Craig Ringer
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general

_________________________________________________________________
View your Twitter and Flickr updates from one place – Learn more!
http://clk.atdmt.com/UKM/go/137984870/direct/01/


From: Magnus Hagander <magnus(at)hagander(dot)net>
To: George Oakman <oakmang(at)hotmail(dot)com>
Cc: craig(at)postnewspapers(dot)com(dot)au, pgsql-general(at)postgresql(dot)org
Subject: Re: C++ User-defined functions
Date: 2009-03-09 19:30:46
Message-ID: 49B56E66.8090907@hagander.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

George Oakman wrote:
> Thanks Craig,
>
> I'm trying to compile a very simple test with Visual Studio (2008), but
> I get the following errors:

The PostgreSQL backend and header files are not compatible with Visual
Studio 2008. At least they're not tested wit hit. You need to try Visual
Studio 2005.

> c:\program files\postgresql\8.3\include\server\pg_config_os.h(188) :
> error C2011: 'timezone' : 'struct' type redefinition
> c:\program files\postgresql\8.3\include\server\pg_config_os.h(188) : see
> declaration of 'timezone'
> c:\program files\postgresql\8.3\include\server\pg_config_os.h(197) :
> error C2011: 'itimerval' : 'struct' type redefinition
> c:\program files\postgresql\8.3\include\server\pg_config_os.h(197) : see
> declaration of 'itimerval'
> c:\program files\postgresql\8.3\include\server\c.h(97) : fatal error
> C1083: Cannot open include file: 'libintl.h': No such file or directory
>
> I must be doing something wrong - I don't even have a libintl.h on my
> drive...

libintlh. is part of gettext, which is a dependency from the binary
version of pg. You can either get the whole sourcetree and reconfigure
it without gettext, or install gettext.

//Magnus


From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: George Oakman <oakmang(at)hotmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: C++ User-defined functions
Date: 2009-03-10 02:37:40
Message-ID: 49B5D274.1050201@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Tom Lane wrote:

> One thing you've got to be really wary of is C++ exceptions,
> which tend not to interoperate nicely with PG's longjmp-based error
> handling.

Hmm, that does sound problematic. You can always build with
-fno-exceptions (gcc; I think other compilers offer related options) and
adopt Pg's error handling scheme instead, though.

In general it's possible to use exceptions in C++ code that's being
called from C so long as you make sure you catch all possible exceptions
at the C/C++ interface point.

> My own advice is to think twice about how badly your function needs to
> be C++ rather than plain C.

Yep... unless you have to call C++ libraries or the like I'd be
reluctant to take all the possible bother on board. That's despite
personally having a strong preference for C++.

--
Craig Ringer


From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: Magnus Hagander <magnus(at)hagander(dot)net>
Cc: George Oakman <oakmang(at)hotmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: C++ User-defined functions
Date: 2009-03-10 02:42:39
Message-ID: 49B5D39F.8050002@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Magnus Hagander wrote:
> George Oakman wrote:
>> Thanks Craig,
>>
>> I'm trying to compile a very simple test with Visual Studio (2008), but
>> I get the following errors:
>
> The PostgreSQL backend and header files are not compatible with Visual
> Studio 2008. At least they're not tested wit hit. You need to try Visual
> Studio 2005.

You should build your extension with the same version of VC++ as the Pg
server was built with anyway, and at the moment that means 2005 if
you're using a stock binary distribution. Otherwise you may encounter
problems with mismatched C runtime libraries, resulting in exciting
problems with memory management, file handles, and other things. IIRC
Pg's use of palloc() should actually avoid the problems with memory
being allocated in one DLL then free()'d in another, but I'd still be wary.

>> c:\program files\postgresql\8.3\include\server\c.h(97) : fatal error
>> C1083: Cannot open include file: 'libintl.h': No such file or directory
>>
>> I must be doing something wrong - I don't even have a libintl.h on my
>> drive...

For some reason the Pg binary distribution doesn't include the headers
for gettext, despite the public backend headers requiring them. (IMO it
really should).

I actually landed up making a dummy libintl.h since I wasn't actually
linking to or using any of the gettext functions, but it'd be much
better to have the correct headers - as noted below:

> libintlh. is part of gettext, which is a dependency from the binary
> version of pg. You can either get the whole sourcetree and reconfigure
> it without gettext, or install gettext.

--
Craig Ringer


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
Cc: George Oakman <oakmang(at)hotmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: C++ User-defined functions
Date: 2009-03-10 03:19:58
Message-ID: 20662.1236655198@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Craig Ringer <craig(at)postnewspapers(dot)com(dot)au> writes:
> Tom Lane wrote:
>> One thing you've got to be really wary of is C++ exceptions,
>> which tend not to interoperate nicely with PG's longjmp-based error
>> handling.

> Hmm, that does sound problematic. You can always build with
> -fno-exceptions (gcc; I think other compilers offer related options) and
> adopt Pg's error handling scheme instead, though.

> In general it's possible to use exceptions in C++ code that's being
> called from C so long as you make sure you catch all possible exceptions
> at the C/C++ interface point.

The other half of the problem is that most user-written functions have
some reason to call functions in the core backend. If any of those
might throw an error then you have to do the reverse mapping too (catch
the longjmp, throw an exception, catch that back at your exit point...).
Otherwise your exception cleanup doesn't happen, which makes the whole
thing a bit useless.

regards, tom lane


From: George Oakman <oakmang(at)hotmail(dot)com>
To: <craig(at)postnewspapers(dot)com(dot)au>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: C++ User-defined functions
Date: 2009-03-11 13:05:34
Message-ID: COL115-W536FBE8AF4CA0A5C6EF819AF9E0@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


Hi,

Thank you Craig and Magnus for your answers.

I have tried compiling with Visual Studio 2005 and I'm still getting those errors:

c:\program files\postgresql\8.3\include\server\pg_config_os.h(188) : error C2011: 'timezone' : 'struct' type redefinition
c:\program files\postgresql\8.3\include\server\pg_config_os.h(188) : see declaration of 'timezone'

c:\program files\postgresql\8.3\include\server\pg_config_os.h(197) : error C2011: 'itimerval' : 'struct' type redefinition
c:\program files\postgresql\8.3\include\server\pg_config_os.h(197) : see declaration of 'itimerval'

Any advice most welcome.

Thanks,

George.


> Date: Tue, 10 Mar 2009 11:42:39 +0900
> From: craig(at)postnewspapers(dot)com(dot)au
> To: magnus(at)hagander(dot)net
> CC: oakmang(at)hotmail(dot)com; pgsql-general(at)postgresql(dot)org
> Subject: Re: [GENERAL] C++ User-defined functions
>
> Magnus Hagander wrote:
> > George Oakman wrote:
> >> Thanks Craig,
> >>
> >> I'm trying to compile a very simple test with Visual Studio (2008), but
> >> I get the following errors:
> >
> > The PostgreSQL backend and header files are not compatible with Visual
> > Studio 2008. At least they're not tested wit hit. You need to try Visual
> > Studio 2005.
>
> You should build your extension with the same version of VC++ as the Pg
> server was built with anyway, and at the moment that means 2005 if
> you're using a stock binary distribution. Otherwise you may encounter
> problems with mismatched C runtime libraries, resulting in exciting
> problems with memory management, file handles, and other things. IIRC
> Pg's use of palloc() should actually avoid the problems with memory
> being allocated in one DLL then free()'d in another, but I'd still be wary.
>
> >> c:\program files\postgresql\8.3\include\server\c.h(97) : fatal error
> >> C1083: Cannot open include file: 'libintl.h': No such file or directory
> >>
> >> I must be doing something wrong - I don't even have a libintl.h on my
> >> drive...
>
> For some reason the Pg binary distribution doesn't include the headers
> for gettext, despite the public backend headers requiring them. (IMO it
> really should).
>
> I actually landed up making a dummy libintl.h since I wasn't actually
> linking to or using any of the gettext functions, but it'd be much
> better to have the correct headers - as noted below:
>
> > libintlh. is part of gettext, which is a dependency from the binary
> > version of pg. You can either get the whole sourcetree and reconfigure
> > it without gettext, or install gettext.
>
> --
> Craig Ringer
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general

_________________________________________________________________
View your Twitter and Flickr updates from one place – Learn more!
http://clk.atdmt.com/UKM/go/137984870/direct/01/


From: 3dmashup <3dmashup(at)comcast(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: C++ User-defined functions
Date: 2010-08-11 15:07:56
Message-ID: 1281539276321-2472021.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


This information is correct the header file libintl.h is NOT included in
the binary distro.
For a workaround, you can create an empty libintl.h file in local header
directory and add that dir to the include path.
--
View this message in context: http://postgresql.1045698.n5.nabble.com/C-User-defined-functions-tp1911904p2472021.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


From: 3dmashup <3dmashup(at)comcast(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: C++ User-defined functions
Date: 2010-08-11 15:28:07
Message-ID: 1281540487156-2472055.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


This information is correct the header file libintl.h is NOT included in
the binary distro.
For a workaround, you can create an empty libintl.h file in local header
directory and add that dir to the include path.

When compilinga server side C function with VS2010 or VS2005

The struct redefiniition is due to itimerval being defined both in

include/port/win32.h

and

include/pg_config_os.h

IS this just a matter of getting the order of header includes right?
Or should both files be included?
Or should a struct be defined in both place?

Anyway commenting out the struct defintion in pg_config_os.h works as a
quick fix hack..

It would be good to know what the right approach is.

Cheers Tim
--
View this message in context: http://postgresql.1045698.n5.nabble.com/C-User-defined-functions-tp1911904p2472055.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.