PostgreSQL JDBC Connection issue

8/19/2019

We have PostgreSQL 9.6 instance at a ubuntu 18.04 machine. When we restart java services deployed in a Kubernetes cluster then already existing idle connection didn't get remove and service create new connections on each restart. Due to this, we have reached a connection limit so many times and we have to terminate connection manually every time. Same service versions are deployed on other instances but we are not getting this scenario on other servers.

I have some questions regarding this

  1. Can it be a PostgreSQL configuration issue? However, i didn't find any timeout-related setting difference in 2 instances (1 is working fine and another isnt)

  2. If this is a java service issue then what should I check?

  3. If its neither a PostgreSQL issue not a java issue then what should i look into?

-- Yogi
connection-pooling
java
kubernetes
postgresql

1 Answer

8/20/2019

If the client process dies without closing the database connection properly, it takes a while (2 hours by default) for the server to notice that the connection is dead.

The mechanism for that is provided by TCP and is called keepalive: after a certain idle time, the operating system starts sending keepalive packets. After a certain number of such packets without response, the TCP connection is closed, and the database backend process will die.

To make PostgreSQL detect dead connections faster, set the tcp_keepalives_idle parameter in postgresql.conf to less than 7200 seconds.

-- Laurenz Albe
Source: StackOverflow