# 57P02 — crash_shutdown

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

# 57P02

## 速览 {#at-a-glance}
`57P02` 是 crash-shutdown 路径。crash-and-restart 周期开始时，backend 向客户端发送 client-only WARNING，说明 postmaster 命令其回滚并退出；随后由于共享内存可能已损坏，进程不运行正常清理回调而退出。

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

| 字段 | 值 |
| --- | --- |
| SQLSTATE | `57P02` |
| 条件名 | `crash_shutdown` |
| 状态 | `有效` |
| 已知存在于 | `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_CRASH_SHUTDOWN` |
| 别名 | `—` |

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

## 含义 {#meaning}
固定 primary 是 `terminating connection because of crash of another server process`。detail 说明 postmaster 因另一个服务器进程异常退出、可能破坏共享内存，而命令当前 server process 回滚并退出；hint 建议稍后重连。源码使用 `WARNING_CLIENT_ONLY` 而不是 FATAL，使信号处理器能先把消息发给客户端再强制进程退出。连接仍会关闭，之后不能在原 session 发送普通恢复命令。

## 消息 {#messages}

- primary：`terminating connection because of crash of another server process`。
- detail：`The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.`
- hint：`In a moment you should be able to reconnect to the database and repeat your command.`
- severity 来源：`WARNING_CLIENT_ONLY`，这是发给客户端但不写服务器日志的 warning；报告后进程退出。因此不能把它理解成可在同一 session 继续的语句级 WARNING。

## 诊断 {#diagnosis}
把 SQLSTATE 和完整诊断与 postmaster 崩溃/恢复日志、backend PID 及执行中的命令关联。源码明确要求回滚当前事务，但连接在 crash 周期中断开，重放非幂等请求前仍必须核对持久化业务状态。要和有序管理员 shutdown（`57P01`）及启动拒绝（`57P03`）区分。

## 处理 {#response}
不要在已断开的连接上发送 `ROLLBACK`，也不要人为制造崩溃测试此路径。等待 postmaster 恢复并 ready，建立新连接，检查持久化业务状态和受影响对象；只有在操作幂等或已确认先前效果后再重试。保留 primary/detail/hint 及服务器日志用于事故诊断。

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

## 相关 {#related}
[`57P01`](../57p01/)、[`57P03`](../57p03/)、[`08006`](../08006/)

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

[`src/include/utils/elog.h#L46-55`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/include/utils/elog.h#L46)

结构化[证据记录](../../data/evidence/57p02.json)保存固定消息以及源码/运行范围。
