Is any C/C++ library available for postgres connection pooling? I have looked at pgpool which is more like a middleware. I am looking for a library which can be coded into my application.
2 Answers
Have you looked at libpqxx yet? It's not a connection pooler per se, however it provides a c++ API to abstract the connection handling from the app code. This allows an app to build and manage its own connection pool quite easily.
It's really pretty simple, here's an example (using boost for shared_ptr & pqxx) to illustrate a pool class, with factory method. You can imagine that the runQuery method would get a connection from the pool specified, and call the pqxx APIs to execute the query on the underlying connection. It can then return the connection to the pool.
class DbPool {
public:
static db_handle_t create(const string &conn,
uint32_t max = DEFAULT_CON_MAX,
uint32_t min = DEFAULT_CON_MIN);
static pqxx::result runQuery(db_handle_t pool,
const string& query);
private:
DbPool(const string& con, uint32_t max_cons,
uint32_t min_cons);
static boost::ptr_vector<DbPool> pool_; // Keep a static vector of pools,
pqxx::connection *createCon();
void releaseCon(pqxx::connection *c);
uint32_t initializeCons();
pqxx::connection *getCon();
boost::ptr_list<pqxx::connection> m_freeCons;
}
There isn't a good in-app connection pooling library. Nearly the entire community uses external proxies, notably pgbouncer due to its extra operational benefits. In the same breath, SOCI has a connection pool, but it isn't used nearly as widely as pgbouncer.
PGconn *. Perhaps you should be more specific about what it is that you need as connection pooling traditionally does refer to middleware.