简体   繁体   English

如何在Amazon RDS中关闭与Postgres DB的空闲连接

[英]How to shut down idle connections to Postgres DB in Amazon RDS

We have a Postgres database set up in the RDS. 我们在RDS中设置了Postgres数据库。 I wrote some APIs to handle data ingestion to the db using Spring Boot. 我写了一些API来使用Spring Boot处理数据提取到数据库。 Recently I discovered that lots of connections kept their sessions alive after calling the APIs. 最近我发现很多连接在调用API后保持会话活动。 Some of the sessions date back to 3 months ago. 有些会议可追溯到3个月前。

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? 如何自动关闭PostgreSQL中的空闲连接? , 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 . ,看起来我可以设置一个cron作业来查找与SQL查询的死连接,并使用pg_trminate_backend终止它们。 Is this the best option? 这是最好的选择吗? Is there something that can be done in the web application layer? 是否可以在Web应用程序层中完成某些操作? Or maybe some RDS parameters? 或者可能是一些RDS参数? Need some advice on this. 需要一些建议。

from 9.6 on , you have: 从9.6开始 ,你有:

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

idle_in_transaction_session_timeout (integer) idle_in_transaction_session_timeout (整数)

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. 有关详细信息,请参见第24.1节。

The default value of 0 disables this feature. 默认值0禁用此功能。

(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 . 在9.6之前 - 是唯一的本地方式 - cron与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: 此外 ,如果你使用的连接与pgbouncer池,则:

https://pgbouncer.github.io/config.html 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. server_idle_timeout如果服务器连接空闲超过这么多秒,它将被丢弃。 If 0 then timeout is disabled. 如果为0则禁用超时。 [seconds] [秒]

Default: 600.0 默认值:600.0

(formatting mine) (格式化我的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM