搜索

PostgreSQL常见错误:sorry, too many clients already

发表于 2025-11-04 14:56:50 来源:益强智未来

当应用程序连接 PostgreSQL 数据库遇到“FATAL: sorry,常见错误 too many clients already”错误时,表示数据库连接数已经到达服务器允许的常见错误最大值,无法建立新的常见错误连接。

原因分析

PostgreSQL 允许的常见错误最大客户端连接数由配置参数 max_connections ,默认值通常为 100。常见错误

复制SHOW max_connections; max_connections| ---------------+ 100 |1.2.3.4.5.

那是常见错误不是意味着客户端一定可以创建 100 个并发连接呢?

并不是,因为 PostgreSQL 还有另外两个相关参数:

复制SHOW superuser_reserved_connections; superuser_reserved_connections| ------------------------------+ 3 |1.2.3.4.5.

superuser_reserved_connections 参数代表了 PostgreSQL 数据库为超级用户保留的常见错误连接数,默认值为 3。常见错误

也就是常见错误说,当客户端连接数到达 max_connections - superuser_reserved_connections 时,常见错误只有超级用户才能继续创建新的常见错误连接。

复制SHOW reserved_connections; reserved_connections| --------------------+ 0 |1.2.3.4.5.

reserved_connections 参数代表了 PostgreSQL 数据库为拥有 pg_use_reserved_connections 角色的服务器托管常见错误用户保留的连接数,默认值为 0。常见错误这个参数是常见错误 PostgreSQL 16 新增参数。

当可用连接数大于 superuser_reserved_connections 并且小于等于 superuser_reserved_connections + reserved_connections 时,常见错误只有超级用户或者拥有 pg_use_reserved_connections 角色的用户才能继续创建新的连接。

总结一下,假设 max_connections 参数设置为 100,superuser_reserved_connections 参数设置为 3,reserved_connections 参数设置为 10。此时,客户端最多可以同时创建 100 个连接;当连接数到达 87 并且小于 97 时,只有超级用户和 pg_use_reserved_connections 角色用户可以继续创建连接;当连接数到达 97 时,只有超级用户可以继续创建连接。

解决方法

我们可以利用数据库为超级用户保留的连接登录数据库,然后查看当前服务器进程情况:

复制SELECT * FROM pg_stat_activity; datid|datname |pid |leader_pid|usesysid|usename |application_name |client_addr|client_hostname|client_port|backend_start |xact_start |query_start |state_change |wait_event_type|wait_event |state |backend_xid|backend_xmin|query_id|query |backend_type | -----+--------+-----+----------+--------+--------+-----------------------------------------+-----------+---------------+-----------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+---------------+-------------------+------+-----------+------------+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+ 5|postgres|19256| | 10|postgres|DBeaver 24.1.5 - Main <postgres> |127.0.0.1 | | 55986|2024-08-28 21:20:25.682 +0800| |2024-08-28 21:20:25.795 +0800|2024-08-28 21:20:25.795 +0800|Client |ClientRead |idle | | | |SHOW search_path |client backend | 5|postgres|22216| | 10|postgres|DBeaver 24.1.5 - Metadata <postgres> |127.0.0.1 | | 55987|2024-08-28 21:20:25.826 +0800| |2024-08-28 22:03:37.376 +0800|2024-08-28 22:03:37.376 +0800|Client |ClientRead |idle | | | |SELECT c.oid,c.*,d.description,pg_catalog.pg_get_expr(c.relpartbound, c.oid) as partition_expr, pg_catalog.pg_get_partkeydef(c.oid) as partition_key ¶FROM pg_catalog.pg_class c¶LEFT OUTER JOIN pg_catalog.pg_description d ON d.objoid=c.oid AND d.objsubid=|client backend | 5|postgres|10736| | 10|postgres|DBeaver 24.1.5 - SQLEditor <Script-2.sql>|127.0.0.1 | | 55988|2024-08-28 21:20:26.003 +0800|2024-08-28 22:03:41.802 +0800|2024-08-28 22:03:41.803 +0800|2024-08-28 22:03:41.803 +0800| | |active| |1032 | |select * from pg_stat_activity |client backend | | |20852| | | | | | | |2024-08-24 20:56:59.100 +0800| | | | | | | | | | |autovacuum launcher | | | 9236| | 10|postgres| | | | |2024-08-28 21:13:57.480 +0800| | | |Activity |LogicalLauncherMain| | | | | |logical replication launcher| | |19468| | | | | | | |2024-08-24 20:56:59.082 +0800| | | |Activity |WalWriterMain | | | | | |walwriter | | | 3524| | | | | | | |2024-08-24 20:56:58.608 +0800| | | |Activity |CheckpointerMain | | | | | |checkpointer | | | 8896| | | | | | | |2024-08-24 20:56:58.620 +0800| | | |Activity |BgwriterHibernate | | | | | |background writer |1.2.3.4.5.6.7.8.9.10.11.12.

系统视图 pg_stat_activity 显示了所有后端进程的源码库信息,其中 backend_type 字段取值为 client backend 的进程对应客户端连接。通过这个视图可以了解客户端的连接情况。

如果应用程序的确需要更多的数据库连接,可以修改上面介绍的 PostgreSQL 配置参数,这些参数的修改都需要重启服务。

如果应用程序并不需要这么多连接,而是由于代码问题导致连接泄露,例如创建了数据库连接后没有正确地释放,或者数据库连接池配置不当导致打开了过多连接。这种情况就需要调整应用端代码,确保正确管理了数据库连接。

企商汇
随机为您推荐
版权声明:本站资源均来自互联网,如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

Copyright © 2016 Powered by PostgreSQL常见错误:sorry, too many clients already,益强智未来  滇ICP备2023006006号-17sitemap

回顶部