# psycopg2.OperationalError: 错误：内存不足
详细信息：请求大小为 123456 时失败。

- **ID:** `database/postgresql-too-many-pins`
- **领域:** database
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

PostgreSQL 服务器内存耗尽，通常是由于多个并发查询使用了过多的 work_mem，或单个查询需要非常大的内存（如排序、哈希操作）。

## 版本兼容性

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

## 解决方案

1. ```
   Reduce work_mem for the session or globally to limit per-operation memory usage:
SET work_mem = '32MB';
Or in postgresql.conf:
work_mem = 32MB
Then restart or reload.
   ```
2. ```
   Identify and optimize the problematic query using EXPLAIN ANALYZE, then rewrite it to use less memory (e.g., add indexes, reduce sort operations).
   ```

## 无效尝试

- **Increasing shared_buffers without adjusting work_mem** — shared_buffers is for caching, not for per-query memory; it does not prevent out-of-memory errors from work_mem. (85% 失败率)
- **Restarting PostgreSQL without changing memory settings** — The memory is freed temporarily but the same query pattern will exhaust it again. (90% 失败率)
