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

Keepalive the Idle postgres connections

Open gopal-abbineni opened this issue 5 years ago • 13 comments

In our scenario there are router/switches that will reset the connection if the connection is idle for more than 5 min. This throws the ECONNRESET error and crashing up the server. Is there any way to keep the connection idle from pg cliient.

gopal-abbineni avatar Oct 06 '20 07:10 gopal-abbineni

You could use the undocumented keepAlive and keepAliveInitialDelayMillis connection parameters, to set up appropriate TCP keepalives.

boromisp avatar Oct 06 '20 11:10 boromisp

oof I need to document that!

brianc avatar Oct 06 '20 13:10 brianc

But with out passing those attributes I tried enabling the keepalive attributes( tcs_keepalive_idle, tcs_keepalive_interval, tcs_keepalive_count) on the DB side(postgresql.conf),

Screenshot 2020-10-06 at 3 58 48 PM

After that I am able to see the keepAlive packets are being passed in the tcp connection. Screenshot 2020-10-06 at 3 59 04 PM

Is this expected behaviour?

gopal-abbineni avatar Oct 06 '20 14:10 gopal-abbineni

@gopal-abbineni what you see is the keepalive packets sent by the server to the client. More about that here: https://www.postgresql.org/docs/9.6/runtime-config-connection.html. What @boromisp is referring to is the client generated keepalives which is equivalent to the native libpq: https://www.postgresql.org/docs/9.6/libpq-connect.html#LIBPQ-KEEPALIVES. I am not sure though if they are supported in the node implementation.

hammady avatar Feb 11 '21 22:02 hammady

Hi all, what's the purpose of keepAliveInitialDelayMillis ?

jodem avatar Feb 12 '21 14:02 jodem

I'm struggling to find clear documentation on the implications of setting keepAlive: true.

node-postgres seems to be using libpq. Libpq docs, nor node-postgres docs are clear on what happens when the connection to server is considered dead. Will it auto re-connect? Or does it require nodejs process restart.

rynop avatar Mar 04 '21 21:03 rynop

Hi all, what's the purpose of keepAliveInitialDelayMillis ?

@jodem pretty sure it is just used to set keepalives_idle. See code here, see docs on keepalives_idle here.

rynop avatar Mar 05 '21 17:03 rynop

I'm using Google Cloud functions and my connections are killed every 2min. Currently, this causes an exception. How can I make it reconnect automatically? KeepAlive=true doesn't seem to help.

lukas1994 avatar Apr 22 '21 10:04 lukas1994

Is there any reason keepAlive is not documented?

Is it a finished feature? 😅

PierBover avatar Mar 02 '22 05:03 PierBover

After some lengthy periods of inactivity in our stage environment (overnight, every night), the first attempt to write to pg consistently fails. The second and all other subsequent attempts succeed. It seems like the connections are being closed or are dying but are not removed from the connection pool. I'm left wondering if I should be using keepAlive, and what the implications are.

tasdflkjweio avatar May 17 '22 17:05 tasdflkjweio

@brianc Could you comment on whether keepAlive should be used? and if yes, why it is not enabled by default.

In particular, I am debugging why each query starts with tcp.connect. I wouldn't expect this to be necessary.

Screen Shot 2022-07-12 at 8 40 25 PM

gajus avatar Jul 13 '22 01:07 gajus

I believe this documents implications of keepAlive setting

https://stackoverflow.com/a/46884708/368691

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.

gajus avatar Jul 13 '22 01:07 gajus