Postgresql – Load Balancing PG Bouncer With HA Proxy

connection-poolinghaproxypgbouncerpostgresql

I have a server with two instances of PG Bouncer running on different ports. Both are using session mode to pool. I set up HA Proxy to load balance incoming connections between these two processes to distribute the load. I get "server closed the connection unexpectedly" intermittently when connecting through HA Proxy. If I connect directly to one of the PG Bouncer instances I do not receive these errors. Has anyone setup something similar to this?

Best Answer

I fail to see why you are loadbalancing between pgbouncers on the same server, I assume you have a single DB server instance, if they run on the same node you're not really balancing anything.

Than being said, this sounds like timeout issues, since pgsql connections tend to live longer than the standard haproxy timeouts I suggest your raise them: (for example in defaults section add : )

timeout queue           1m
timeout connect         1s
timeout client          3600s
timeout server          3600s
timeout check           2s

This will keep any connection that the client hasn't closed open for an hour. queries that take longer to run will be cut.

see https://github.com/gplv2/haproxy-postgresql/blob/master/haproxy-example.cfg for more inspiration from a working haproxy config.