postgresql "keepalives" parameter

zlatko picture zlatko · Oct 22, 2017 · Viewed 10.7k times · Source

According to https://www.postgresql.org/docs/current/static/libpq-connect.html there is parameter keepalives and further parameters keepalives_idle, keepalives_interval and keepalives_count which are relevant only if keepalives=1.

But it is not clear to me what does it mean if the parameter keepalives is set to zero?

What exactly happens if the keepalives parameter is set to zero (keepalives=0)?

Does it mean that a PostgreSQK session is never terminated? What happens if client application is closed? What happens if network connection is broken? Does it mean that the PostgreSQL server will never ever terminate idle session?

Answer

Laurenz Albe picture Laurenz Albe · Oct 23, 2017

keepalives is a client-side setting.

If you set it to 0, the TCP socket on the client machine will have the SO_KEEPALIVE socket option set to 0 (the default setting (on Linux) is 1, meaning that keepalive is enabled).

Then it won't send keepalive messages on idle connections to a database server to check if the other side is still alive.

The effect is that the client will not detect if the database server terminates unexpectedly, that is, without closing the TCP connection.

Usually it is not necessary to enable keepalive on the client side: The client will notice when the server has dies anyway when it next tries to talk to it.

The other use for keepalive messages would be to keep a firewall or proxy from closing an idle connection. But since the PostgreSQL server enables keepalive on the server side anyway, that should be taken care of.