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.

2
  • 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. Commented Jun 28, 2011 at 15:16
  • Yes, I'm looking exactly for that. I was thinking of something like Apache 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. Commented Jun 28, 2011 at 20:26

2 Answers 2

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;

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

1 Comment

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.
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.

1 Comment

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?

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.