1

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.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
Vasu
  • 2,406
  • 3
  • 20
  • 26
  • 1
    To clarify, you're looking for something which will maintain a list of connections to one or more databases? The most simplistic answer is to use an array of `PGconn *`. Perhaps you should be more specific about what it is that you need as connection pooling traditionally does refer to middleware. – unpythonic Jun 28 '11 at 15:16
  • Yes, I'm looking exactly for that. I was thinking of something like [Apache DBCP](http://commons.apache.org/dbcp/) for java. Once I configure the pool, I shouldn't be worrying about checking the health of the connection, closing broken connections etc. The pool should manage all this transparently and present a clean API. – Vasu Jun 28 '11 at 20:26

2 Answers2

3

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;

}
Dan B
  • 131
  • 1
  • 6
  • Great answer, but don't you have to manage the connections as well, like preventing them from interfering with each other's queries? I'm not to familiar with Postgres thus don't know if it already automatically manages this. –  Feb 09 '14 at 21:13
2

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.

Sean
  • 9,888
  • 4
  • 40
  • 43
  • I wanted to find a solution where I don't have to introduce any new services/processes in my servers. Looks like I'll have to write my own pooling library. BTW any suggestions on using [SQL Relay](http://sqlrelay.sourceforge.net/sqlrelay/)? – Vasu Jun 28 '11 at 20:31