# 57P03 — cannot_connect_now

> PostgreSQL 57P03 的来源与诊断参考。
---

# 57P03

## 速览 {#at-a-glance}
`57P03` 在连接启动阶段、数据库尚未达到可接受连接状态时发出。固定 guard 区分 startup、shutdown、recovery、禁用 hot standby，以及两种不同的 recovery 尚未达到一致状态。

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| 字段 | 值 |
| --- | --- |
| SQLSTATE | `57P03` |
| 条件名 | `cannot_connect_now` |
| 状态 | `有效` |
| 已知存在于 | `7.4` |
| 锁定快照 | `9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3` |
| 宏 | `ERRCODE_CANNOT_CONNECT_NOW` |
| 别名 | `—` |

<!-- source facts: data/errcodes/57P03.json -->
<!-- END SQLSTATE FACTS -->

## 含义 {#meaning}
startup packet 在正常认证完成前被拒绝。所有固定分支都使用 FATAL，因此尝试连接直接结束，不会进入客户端事务。hot-standby 分支不能只看 primary：detail 会区分功能禁用、recovery snapshot 尚未 ready，以及 recovery 尚未达到一致状态。

## 消息 {#messages}

- startup gate：primary `the database system is starting up`，FATAL。
- hot standby 禁用：primary `the database system is not accepting connections`，FATAL；detail 为 `Hot standby mode is disabled.`。
- recovery snapshot 未 ready：primary `the database system is not yet accepting connections`，FATAL；detail 为 `Recovery snapshot is not yet ready for hot standby.`；hint 为 `To enable hot standby, close write transactions with more than %d subtransactions on the primary server.`。
- recovery 尚未一致：primary `the database system is not yet accepting connections`，FATAL；detail 为 `Consistent recovery state has not been yet reached.`。
- shutdown gate：primary `the database system is shutting down`，FATAL。
- recovery gate：primary `the database system is in recovery mode`，FATAL。

## 诊断 {#diagnosis}
记录 primary/detail/hint、startup 阶段、hot-standby 配置、recovery 一致性/snapshot 状态、服务器 ready 日志及目标服务器角色。反复出现带 snapshot detail 的 `not yet accepting` 指向 hint 所说的 primary 端子事务条件；一致性 detail 则表示 recovery 尚未过早阶段。不要把它当成 `INERROR` 事务：连接在 session 事务建立前就被拒绝。

## 处理 {#response}
按界限退避，等待相关 startup、shutdown 或 recovery 状态转换。若 hot standby 是有意禁用的，要注意 `hot_standby` 是 `PGC_POSTMASTER` 设置：修改服务器所属的配置来源后必须重启服务器，SIGHUP/reload 不能激活该设置。若 hint 指向 primary 上长时间写事务，则处理该运维原因。ready 后建立新连接。被拒绝的 startup 路径没有事务可回滚；若此前客户端操作在状态转换中断开，重放前先核对业务结果。

## 版本 {#versions}
锁定目录从 7.4 记录此条件；该目录边界不等于精确实现引入版本。固定源码覆盖 PostgreSQL 18.6。

## 相关 {#related}
[`57P01`](../57p01/)、[`57P04`](../57p04/)、[`08001`](../08001/)

## 来源 {#sources}
[`src/backend/tcop/backend_startup.c#L298-338`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/backend_startup.c#L298)

[`src/backend/utils/misc/guc_tables.c#L1895-1903`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/misc/guc_tables.c#L1895)

结构化[证据记录](../../data/evidence/57p03.json)保存全部固定 startup gate 以及源码/运行范围。
