选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / 错误代码 / Class 53 资源不足

53300 too_many_connections

连接数过多

ERROR 已实测 详解 实测通过

类别
Class 53 资源不足
严重等级
ERROR
条件名
too_many_connections
宏名称
ERRCODE_TOO_MANY_CONNECTIONS
启用版本
7.4
状态
活跃

版本覆盖

速览

53300 是 PostgreSQL 类别 53 insufficient_resources 中的 too_many_connections 条件。新后端因为某个连接容量限制已经达到而无法接纳时,会产生这个代码。代表性案例把一个角色的连接数限制设为 1,再为该角色打开第二个会话。

这是连接启动阶段的失败。被拒绝的会话没有打开 SQL 事务。最终运行中,psycopg 返回了启动异常文本,但暴露的 sqlstateNone;PostgreSQL collector 记录了服务器实际发送的 FATAL SQLSTATE 53300。诊断连接池或认证网关时必须分开记录这两种观察。

案例 role_connection_limit 在 PostgreSQL 18.6 和隔离的 PostgreSQL 10.21 目标上均通过。run ID 和逐案例断言保存在公开证据 JSON中。

含义与触发路径

后端启动时,PostgreSQL 在认证角色后检查该角色的 rolconnlimit。对于连接限制为非负数且不是超级用户的角色,如果连接数超过限制,服务器会使用 ERRCODE_TOO_MANY_CONNECTIONS 报告 FATAL,主报文为 too many connections for role "%s"。源码注释说明并发启动可能竞争,因此角色计数的限制检查是近似的。

同一个 SQLSTATE 也可能表示其他容量路径,例如服务器范围的客户端限制。本页的角色限制案例不能说明每一次 53300 的根因;应结合主报文、collector 字段,以及角色、数据库和服务器配置来分类。

失败发生在 SQL 协议进入事务之前,因此被拒绝的会话没有 INERRORIDLE 这样的事务状态。仍保持打开的管理连接或池连接可以调整限制,再用新的会话证明恢复。

报文与诊断

下面的可执行摘录与 runner 使用相同的角色限制 SQL。隔离运行中 limited_user 会替换成临时角色。第二个连接是独立的客户端启动操作,因此在 SQL 语句之间说明,而不是伪造一条 SQL 来代表它。

ALTER ROLE limited_user CONNECTION LIMIT 1;
-- 保持一个 limited_user 会话处于打开状态。
-- 再以 limited_user 打开第二个会话:启动阶段返回 FATAL 53300。
ALTER ROLE limited_user CONNECTION LIMIT -1;
SELECT 1;

最新目标的 collector 记录形状为:

SQLSTATE: 53300                 # csvlog/jsonlog sql_state_code
severity: FATAL
message_primary: too many connections for role "<generated-role>"
source: miscinit.c / InitializeSessionUserId / line 880
driver startup sqlstate: null
transaction: none opened
repair: reset CONNECTION LIMIT; fresh role connection SELECT 1 -> 1

PostgreSQL 10.21 的 collector 产生相同的报文和 SQLSTATE,源码位置为 miscinit.c:575。两个目标都在恢复限制后成功建立已知角色的新连接。上面的角色名和端口是运行时生成值;collector 的 sql_state_code 才是服务器证据。具体驱动在启动错误上可能不提供 SQLSTATE 属性。

报文模板

源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。

主消息 sorry, too many clients already

来源:src/backend/tcop/backend_startup.c(lines 340-344) @ REL_18_6

适用范围:This template belongs to the server-wide startup gate and is distinct from the role-limit template.

主消息 too many connections for role "%s"

来源:src/backend/utils/init/miscinit.c(lines 868-880) @ REL_18_6

适用范围:The role name is dynamically inserted; database-wide and server-wide capacity branches may use another primary template.

诊断

记录不含密码的连接参数、角色和数据库、服务器版本、启动异常原文,以及按时间、用户和连接来源关联的 collector 记录。检查 pg_roles.rolconnlimit、数据库和服务器连接配置以及当前后端数量。连接池可能耗尽角色限制,而服务器仍有全局容量。

角色计数在突发并发启动下应当作为容量观察,而非精确的接纳证明;PostgreSQL 已在源码中说明这一检查是近似的。先根据主报文定位限制,再决定调整哪个配置,并保留管理通道用于恢复。

不要对被拒绝的会话执行事务清理命令,它从未进入可用的 SQL 事务。仍存活的所有者或管理连接可以恢复配置;有意义的修复断言是受影响角色建立新的连接并执行真实查询。

处理与修复

  • 限制连接池大小和并发连接创建,避免角色连接数反复耗尽。
  • 检查内存、工作负载和接纳策略后,再决定是否提高角色、数据库或服务器容量。
  • 为恢复保留管理容量和受控的管理连接;不要仅为绕过限制而授予超级用户权限。
  • 修改限制后,用新连接执行无害查询。清理池状态和陈旧会话,再重试应用工作。

代表性修复恢复了临时角色的限制,建立了新的角色连接,并观察到 SELECT 1 返回 1。这证明了角色限制路径的恢复,不能替代对其他数据库级或服务器级容量故障的验证。

可复现案例

在一次性实例上执行过的场景。其中 1 个附有可执行 SQL,正文相应小节里给出。

role_connection_limit PG 10 / 18 有 SQL

前置条件

  • A disposable login role
  • The role connection limit is set to one while one session is already connected

触发

Open a second connection for that role.

断言

  • The new connection receives FATAL SQLSTATE 53300
  • The existing owner connection can restore the limit and continue

处置

Pool and bound connections, reserve capacity for administration, and investigate the role/database/server limit that was reached.

清理

Restore the role limit, drop the role, and drop the case schema.

版本与边界

目录在 PostgreSQL 7.4 的锁定定义中已观察到 53300,并持续到 8.4.22 的 pre-9.0 定义;随后在列出的所有正式快照直到 PostgreSQL 18.6 以及 PostgreSQL 19 Beta 3 预览中存在。这是 definition_only 的存在边界,不是确切实现引入版本或运行时使用断言。9.0 头文件和 9.1 文本定义之间的类别标题变化已记录在目录中;它不是该条件实现发生变化的断言。

角色限制案例在 PostgreSQL 18.6 和 10.21 上通过。源码行号不同,驱动与 collector 的差异也是实测边界。数据库级、服务器级容量路径、连接池行为和操作系统资源耗尽需要独立证据。

来源

证据

断言

每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。

  • 53300 is the too_many_connections condition in Class 53 insufficient_resources.

    核实方式Read the Class 53 section and 53300 row in the frozen errcodes.txt snapshot.

    不覆盖Directory identity does not identify whether a role, database, or server-wide capacity limit caused an occurrence.

    来源src/backend/utils/errcodes.txt(lines 407-414)

  • During backend startup, a non-superuser role whose CountUserBackends result exceeds rolconnlimit receives FATAL SQLSTATE 53300 with too many connections for role "%s"; the source documents the count check as approximate under a race.

    核实方式Read the role connection-limit branch, explicit ERRCODE_TOO_MANY_CONNECTIONS, message construction, and concurrency comment.

    不覆盖This source path is role-specific; other capacity paths can use 53300 with different messages.

    来源src/backend/utils/init/miscinit.c(lines 868-880)

  • The server-wide startup capacity gate also reports SQLSTATE 53300 at FATAL with sorry, too many clients already.

    核实方式Read the CAC_TOOMANY backend-startup branch in the fixed source.

    不覆盖This is a source-confirmed server-wide startup gate; it is not covered by the role-limit runtime case and does not classify database or operating-system resource failures.

    来源src/backend/tcop/backend_startup.c(lines 340-344)

  • For the rejected startup connection, psycopg exposed startup text with sqlstate null while the PostgreSQL collector recorded FATAL SQLSTATE 53300 and the role-limit message on both targets.

    核实方式Compare the driver diagnostic, CSV collector state_code, message, severity, and source location in both final summaries and raw records.

    不覆盖Driver exposure can vary by client and startup failure; the collector record is the server-side SQLSTATE evidence here.

    来源doc/src/sgml/protocol.sgml(ErrorResponse fields)

  • After resetting the disposable role's connection limit, a fresh connection for that role executed SELECT 1 and returned 1 on PostgreSQL 18.6 and 10.21.

    核实方式Keep one holder session, trigger the second startup, restore the limit through an owner connection, and assert a new role session and query.

    不覆盖The repair proves the role-limit path only; it does not prove recovery of another global capacity limit.

    来源verify/cases/53300/cases.json(role_connection_limit) · verify/cases/53300/snippets.json(role_connection_limit ordered SQL)

  • The locked catalogue records 53300 in every listed formal snapshot from 9.0.23 through 18.6 and in 19beta3; pre-9.0 history is not scanned.

    核实方式Read the manifest snapshots and definition references for the code.

    不覆盖The first scanned release is a lower bound, not an asserted introduction version.

    来源sources/manifest.lock.json(snapshots and definition_blobs entries for the 53300 definition)

运行记录

目标服务器版本结果覆盖案例
latest 18.6 (Homebrew) passed role_connection_limit
pg10 10.21 (Debian 10.21-1.pgdg90+1) passed role_connection_limit

同类错误代码

Class 53 资源不足 下的其他成员。