|The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide
by The PostgreSQL Global Development Group
Paperback (6"x9"), 478 pages
RRP £14.95 ($19.95)
Sales of this book support the PostgreSQL project! Get a printed copy>>>
5.15 Using C++ for Extensibility
It is possible to use a compiler in C++ mode to build PostgreSQL extensions by following these guidelines:
All functions accessed by the backend must present a C interface
to the backend; these C functions can then call C++ functions.
extern Clinkage is required for backend-accessed functions. This is also necessary for any functions that are passed as pointers between the backend and C++ code.
Free memory using the appropriate deallocation method. For example,
most backend memory is allocated using
palloc(), so use
pfree()to free it, i.e. using C++
delete()in such cases will fail.
Prevent exceptions from propagating into the C code (use a
catch-all block at the top level of all
extern Cfunctions). This is necessary even if the C++ code does not throw any exceptions because events like out-of-memory still throw exceptions. Any exceptions must be caught and appropriate errors passed back to the C interface. If possible, compile C++ with
-fno-exceptionsto eliminate exceptions entirely; in such cases, you must check for failures in your C++ code, e.g. check for NULL returned by
If calling backend functions from C++ code, be sure that the
C++ call stack contains only plain old data structure
(POD). This is necessary because backend errors
generate a distant
longjump()that does not properly unroll a C++ call stack with non-POD objects.
In summary, it is best to place C++ code behind a wall of
extern C functions that interface to the backend,
and avoid exception, memory, and call stack leakage.
|ISBN 9781906966065||The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide||See the print edition|