# sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.

- **ID:** `python/sqlalchemy-operational-error-server-closed-connection`
- **Domain:** python
- **Category:** network_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

The database server closed the connection due to timeout, crash, or network issue, often when a long-running query or idle connection exceeds server limits.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **Enable connection pool pre-ping** (85% success)
   ```
   Configure SQLAlchemy engine with pool_pre_ping=True to check connection health before use.
   ```
2. **Set pool_recycle parameter** (90% success)
   ```
   Set pool_recycle to a value less than the server's idle timeout (e.g., 3600 seconds) to recycle connections before they are dropped.
   ```

## Dead Ends

- **Retrying the query without pool reset** —  (60% fail)
- **Increasing client-side timeout only** —  (70% fail)
