Mark Harrison wrote:
> I'm looking for feedback from anybody who has used pg in a
> multi-threaded program, particularly one in which several
> threads each open a database connection.
>
> It's documented to work in that scenario, but I'm interested
> in anybody who can share some real-world with that.
We've wrapped the libpq commands inside some code that makes use of a glib
thread pool. On startup, we have a bunch of threads make persistant
connections to the database. The rest of our app simply has to push
querries onto this pool, and the first available thread will make use of
its persistant connection to return the results as fast as possible. If
all threads are busy working on queries, our wrapper code takes care of
the queuing for us.
It's a scaleable solution that we can make as complex as we want. (For
instance, if the database crashes, each thread should be able to
re-establish its connection.) It works pretty well. The only drawback is
that the application can't make use of the threadpool for multi-query
transactions, but we've wrapped all of those in stored procs anyway.