1

I have a compiled library on the same PC with PostgreSQL that I use with #include <seal/seal.h> when writing some C++ code. I must use this library to work on some table values in my PostgresSQL database. Running a separate Client on the same PC with Postgres for the communication with the database is unfortunately not an option.

Is it possible to include and use the functions provided with #include <seal/seal.h> in a PostgreSQL function or a procedure? I read something about using:

external "C" {

#include <seal/seal.h>
// do something with the seal functions here

}

But there are not so many examples out there and I didn't understand it. The section about C++ in the PostgreSQL documentation didn't make it clearer either.

1
  • To realize such you'll need to have a compiled module added to your PostgreSQL distribution IIRC. Commented Jul 30, 2018 at 18:37

1 Answer 1

2

As the documentation recommends, the functions that are called from PostgreSQL must be declared as extern C so that C code can link with them.

In the places where you have to call he functions from your C++ library, you use an exception handler that uses catch (...) to catch all possible exceptions and converts them into a PostgreSQL error message by calling ereport() (outside the catch clause, so that there is nothing C++ on your call stack).

If you have to call a PostgreSQL C function, make sure that the call stack does not contain any values that are not plain old data structures:

A PDS type in C++, or Plain Old C++ Object, is defined as either a scalar type or a PDS class. A PDS class has no user-defined copy assignment operator, no user-defined destructor, and no non-static data members that are not themselves PDS. Moreover, a PDS class must be an aggregate, meaning it has no user-declared constructors, no private nor protected non-static data, no virtual base classes and no virtual functions.

In short, write your code as C as possible, except where you call the library functions.

Sign up to request clarification or add additional context in comments.

5 Comments

So lets see if I got you right. I start declaring a function or a procedure for my table in PostgerSQL and I write my C/C++ in extern "C" { here }. If so far was correct, then can the SQL variables outside extern "C" { } be used by variables inside extern "C" { }?
I don't follow... what is an "SQL variable"?
Poor choice of words. I meant any declared variable outside the extern "C" { } section but inside the PosgresSQL procedure or function.
Yes, certainly. As far as I know, extern C only influences how the linker names the symbol for the decorated function.
Thanks for the help. I'm going to be working on it this Weekend, hopefully it works as planned :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.