node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

PG bouncer with node-postgres

Open ranode opened this issue 11 years ago • 14 comments

Hi! We have a large number of databases present in a single instance of PG. Currently pooling is implemented in node-postgres, but max_connections is hitting soon. We are considering using pgbouncer. Has anyone implemented node-postgres with pgbouncer? Any pointers / best practices would help. Thanks!

ranode avatar Jul 23 '15 12:07 ranode

I'd recommend against using pg-bouncer. We've run into issues on this project in the past where pg-bouncer doesn't implement all of the protocol properly. That being said those issues were a long time ago. Might be worth trying.

On Thu, Jul 23, 2015 at 9:04 AM, linpy [email protected] wrote:

I am out of the office until 2015/07/24.

I would on leave from 2015/1/22~1/25. Please drop me mail if anything. Thanks.

Note: This is an automated response to your message "[node-postgres] PG bouncer with node-postgres (#817)" sent on 23/07/2015 20:30:50.

This is the only notification you will receive while this person is away.

— Reply to this email directly or view it on GitHub https://github.com/brianc/node-postgres/issues/817#issuecomment-124118271 .

brianc avatar Jul 23 '15 17:07 brianc

@ranode ,

In just about every case when someone tells about running out of connections, it boils down to not releasing the connections correctly in some cases. Try to use pg-promise, see if the connection issue is still there, and if it is gone, then you will know the problem was with your connection management.

vitaly-t avatar Jul 25 '15 15:07 vitaly-t

@brianc , thanks for your reply. Would you recommend some other light weight connection pooler in place of pgbouncer?

ranode avatar Jul 27 '15 12:07 ranode

@vitaly-t , thanks for your reply. Each tenant is a separate process, with its own pool (by node-postgres). Our problem perhaps is not connection leaking, but hitting the max connections because of the increasing number of tenants, thereby the number of connections in the pool. Would you have any specific recommendations to help this case?

ranode avatar Jul 27 '15 12:07 ranode

@ranode Have you tried to increase the default pool size of 10?

pg.defaults.poolSize = 100

vitaly-t avatar Jul 27 '15 12:07 vitaly-t

Each tenant has a separate db, and each one has a separate pool (with 10 connections). So a mere 30 tenants is causing the number connections to go beyond 300!

ranode avatar Jul 27 '15 12:07 ranode

If you are running 30+ databases at a time, then the solution to such problem should be architectural, not just through connection pool manipulation, although you can can increase it to anything you want, of course.

vitaly-t avatar Jul 27 '15 13:07 vitaly-t

@vitaly-t : thanks for your prompt responses. Would you have any pointers for an appropriate architecture?

ranode avatar Jul 27 '15 13:07 ranode

@ranode not without intimately knowing the system. This will have to be an internal research.

vitaly-t avatar Jul 27 '15 13:07 vitaly-t

Hey @ranode - I'm not sure of anything other than pgbouncer actually. The last time I had an issue reported related to it though was I think 2 years ago so chances are its improved a lot since then. Definitely interested in your experience if you try it out & it works for you. Off the top of my head I recall they had not implemented parameterized queries (which use a different protocol message) a few years ago.

brianc avatar Jul 27 '15 15:07 brianc

Thanks Brian for sharing. Will try pgbouncer and post my experiences.

On Mon, Jul 27, 2015 at 8:32 PM, Brian C [email protected] wrote:

Hey @ranode https://github.com/ranode - I'm not sure of anything other than pgbouncer actually. The last time I had an issue reported related to it though was I think 2 years ago so chances are its improved a lot since then. Definitely interested in your experience if you try it out & it works for you. Off the top of my head I recall they had not implemented parameterized queries (which use a different protocol message) a few years ago.

— Reply to this email directly or view it on GitHub https://github.com/brianc/node-postgres/issues/817#issuecomment-125238193 .

ranode avatar Jul 28 '15 03:07 ranode

pgbouncer is a standard de facto these days and a lot of huge projects use it. A lot of. I can provide a huge list of names, with numbers.

But you should be aware that pgbouncer has one important issue. It won't work correctly with Postgres prepared stmts, if you run it in "statement" and "transaction" pooling modes (see for example this: http://blog.luisico.net/2013/06/01/pgbouncer-and-prepared-statements/) – so if you use prepareds, you should stick to "session" pooling mode, which could be less productive in terms of performance

NikolayS avatar Aug 20 '15 16:08 NikolayS

I also use PGBouncer and never ran into any problems.

@brianc this can probably be closed.

joskuijpers avatar Jun 20 '16 14:06 joskuijpers

Thanks for the update @joskuijpers - as you can tell I'm pretty bad about closing issues, so anything I can close like this I appreciate! 😉 ❤️

brianc avatar Jun 20 '16 14:06 brianc