sql - AWS RDS PostgreSQL 错误 "remaining connection slots are reserved for non-replication superuser connections"

标签 sql postgresql amazon-web-services database-connection amazon-rds

在仪表板中,我看到当前有 22 个打开的数据库实例连接,阻止新连接并出现错误:

remaining connection slots are reserved for non-replication superuser connections.

我正在从 EC2 实例上运行的 Web 服务 API 访问数据库,并始终保持最佳实践:

Connection connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
Class.forName(DB_CLASS);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL_Query_String);
...
resultSet.close();
statement.close();
connection.close();
  1. 我可以在代码中做其他事情吗?

  2. 我应该在数据库管理中做些其他事情吗?

  3. 有没有办法定期关闭连接?

最佳答案

亚马逊必须根据每个模型对一定内存和连接数的要求来设置连接数

MODEL      max_connections innodb_buffer_pool_size
---------  --------------- -----------------------
t1.micro   34                326107136 (  311M)
m1-small   125              1179648000 ( 1125M,  1.097G)
m1-large   623              5882511360 ( 5610M,  5.479G)
m1-xlarge  1263            11922309120 (11370M, 11.103G)
m2-xlarge  1441            13605273600 (12975M, 12.671G)
m2-2xlarge 2900            27367833600 (26100M, 25.488G)
m2-4xlarge 5816            54892953600 (52350M, 51.123G)

但如果你愿意,你可以通过

将最大连接大小更改为自定义值

从 RDS 控制台 > 参数组 > 编辑参数,

您可以将 max_connections 参数的值更改为自定义值。

要定期关闭连接,您可以像这样设置一个 cron 作业。

select pg_terminate_backend(procpid)
from pg_stat_activity
where usename = 'yourusername'
 and current_query = '<IDLE>'
 and query_start < current_timestamp - interval '5 minutes';

关于sql - AWS RDS PostgreSQL 错误 "remaining connection slots are reserved for non-replication superuser connections",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38555679/

相关文章:

sql - 使用 SQL 查找数据组中的缺失值

sql - 修复 oracle 更新语句的任何建议

python - PostgreSQL 物化 View 未从 Python 刷新

ruby-on-rails - 在 AWS elastic beanstalk 上部署 Rails - 静态 Assets 路由不起作用

amazon-web-services - redis 错误内存 > 'maxmemory' 但我使用的内存不到最大值的一半

sql - 多次调用 oracle 内联函数

sql - 如何在 Postgres 中获取时间间隔的平均值

sql - 选择最大项目数,获取排名和百分位数

postgresql - TypeORM 条件可为空?

python - 在 AWS EC2 上部署 Flask 应用程序