# psycopg2.OperationalError: 错误：无法向服务器发送数据：管道破裂

- **ID:** `database/postgresql-checkpoint-wait-timeout`
- **领域:** database
- **类别:** connection_error
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

PostgreSQL 服务器意外关闭连接，通常是由于超时（如 statement_timeout、idle_in_transaction_session_timeout）或服务器崩溃，导致客户端出现管道破裂。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| PostgreSQL 13 | active | — | — |
| PostgreSQL 14 | active | — | — |
| PostgreSQL 15 | active | — | — |

## 解决方案

1. ```
   Check PostgreSQL logs for the reason the connection was terminated (e.g., statement_timeout, idle_in_transaction_session_timeout). Adjust the relevant timeout parameters in postgresql.conf:
statement_timeout = 0  # disable or increase
idle_in_transaction_session_timeout = 0
   ```
2. ```
   Enable TCP keepalives on the client side to detect broken connections earlier and automatically reconnect:
import psycopg2
conn = psycopg2.connect("host=... keepalives=1 keepalives_idle=30 keepalives_interval=10 keepalives_count=5")
   ```

## 无效尝试

- **Increasing connection pool size in the application** — The error is not about pool exhaustion but about an existing connection being terminated; more connections don't fix broken pipes. (90% 失败率)
- **Restarting the application without checking server logs** — The underlying cause (e.g., server-side timeout) is not addressed; the error will recur. (85% 失败率)
