Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shut down idle connections to Postgres DB in Amazon RDS

We have a Postgres database set up in the RDS. I wrote some APIs to handle data ingestion to the db using Spring Boot. Recently I discovered that lots of connections kept their sessions alive after calling the APIs. Some of the sessions date back to 3 months ago.

I wonder if there is a way to automatically close these connections after it's been inactive for a while. From How to close idle connections in PostgreSQL automatically?, looks like I can set up a cron job to look for dead connections with a SQL query and terminate them with pg_trminate_backend. Is this the best option? Is there something that can be done in the web application layer? Or maybe some RDS parameters? Need some advice on this.

like image 958
ddd Avatar asked Jan 12 '18 02:01

ddd


People also ask

How do I turn off idle connections in PostgreSQL?

Kill an Idle Connection: We have to provide the process 'id' within the query in a terminate function. >> SELECT pg_terminate_backend(7408); The process has been magnificently killed.

What is Postgres idle connections?

idle: This indicates that the connection is idle and we need to track these connections based on the time that they have been idle. idle in transaction: This indicates the backend is in a transaction, but it is currently not doing anything and could be waiting for an input from the end user.

Which method closes of PostgreSQL connection?

pg_close() closes the non-persistent connection to a PostgreSQL database associated with the given connection instance.

What is idle database connection?

Last Updated January 31, 2022. The database connections made from depend on two database driver types - JDBC and ODBC regardless of which database engine is used. With both drivers, the product relies on these drivers connections pooling to create and release database connections.


1 Answers

from 9.6 on, you have:

https://www.postgresql.org/docs/current/static/runtime-config-client.html

idle_in_transaction_session_timeout (integer)

Terminate any session with an open transaction that has been idle for longer than the specified duration in milliseconds. This allows any locks held by that session to be released and the connection slot to be reused; it also allows tuples visible only to this transaction to be vacuumed. See Section 24.1 for more details about this.

The default value of 0 disables this feature.

(formatting mine)

before 9.6- yes the only native way - cron with SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE now()-state_change > '5 minute'::interval.

also if you use connection pooling with pgbouncer, then:

https://pgbouncer.github.io/config.html

server_idle_timeout If a server connection has been idle more than this many seconds it will be dropped. If 0 then timeout is disabled. [seconds]

Default: 600.0

(formatting mine)

like image 106
Vao Tsun Avatar answered Oct 04 '22 04:10

Vao Tsun