pgsql-general
❮
libpq usage from C++
- Jump to comment-1Igor Korot<ikorot01@gmail.com>Mar 14, 2026, 4:41 AM UTCHi, ALL,
Does anybody use libpq from C++?
C++ introduces STL.
C++ introduces smart-pointers.
Is there a simple way of using those writing libpq communication?
Or should I just forget about them and use good old "dump pointers"?
Thank you.- Jump to comment-1Ron Johnson<ronljohnsonjr@gmail.com>Mar 14, 2026, 4:47 AM UTCOn Sat, Mar 14, 2026 at 12:41 AM Igor Korot <ikorot01@gmail.com> wrote:
Hi, ALL,
Maybe use an existing libpq wrapper for c++?
Does anybody use libpq from C++?
C++ introduces STL.
C++ introduces smart-pointers.
Is there a simple way of using those writing libpq communication?
Or should I just forget about them and use good old "dump pointers"?
--
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster! - Jump to comment-1Adrian Klaver<adrian.klaver@aklaver.com>Mar 14, 2026, 4:51 AM UTCOn 3/13/26 9:40 PM, Igor Korot wrote:
Hi, ALL,
Does anybody use libpq from C++?
Search engine of choice:
libpq c++
yields:
https://pqxx.org/development/libpqxx/C++ introduces STL.
C++ introduces smart-pointers.
Is there a simple way of using those writing libpq communication?
adrian.klaver@aklaver.com
Or should I just forget about them and use good old "dump pointers"?
Thank you.
-- Adrian Klaver - Jump to comment-1Dominique Devienne<ddevienne@gmail.com>Mar 16, 2026, 10:25 AM UTCOn Sat, Mar 14, 2026 at 5:41 AM Igor Korot <ikorot01@gmail.com> wrote:
Does anybody use libpq from C++?
YesC++ introduces STL.
Using an existing wrapper is simplest, of course.
C++ introduces smart-pointers.
Is there a simple way of using those writing libpq communication?
Or should I just forget about them and use good old "dump pointers"?
We wrote our own. To each its own. E.g.
PS: Avoid the fwd-decl by using the actual libpq headers instead.
extern "C" {
struct pg_conn;
struct pg_result;
typedef struct pg_conn PGconn;
typedef struct pg_result PGresult;
typedef struct _PQconninfoOption PQconninfoOption;
typedef struct pgNotify PGnotify;
void PQfreemem(void *ptr);
void PQclear(PGresult* res);
void PQfinish(PGconn* conn);
void PQconninfoFree(PQconninfoOption* conn);
} // extern "C"
namespace acme::postgresql {
struct PGDeleterFunctor {
};void operator()(PGresult* r) { if (r != nullptr) { PQclear(r); } } void operator()(PGconn* c) { if (c != nullptr) { PQfinish(c); } } void operator()(PQconninfoOption* c) { if (c != nullptr) { PQconninfoFree(c); } } void operator()(PGnotify* n) { if (n != nullptr) { PQfreemem(n); } }
using PGresultuptr = std::uniqueptr<PGresult, PGDeleterFunctor>;
using PGconnuptr = std::uniqueptr<PGconn, PGDeleterFunctor>;
using PGconninfouptr = std::uniqueptr<PQconninfoOption, PGDeleterFunctor>;
using PGnotifyuptr = std::uniqueptr<PGnotify, PGDeleterFunctor>;
...
}
Then we have higher-level wrappers that compose those RAII low-level ones. --DD