# psycopg2.OperationalError: ERROR: could not create temporary file: No space left on device HINT: You might need to increase the max_files_per_process limit.

- **ID:** `database/temp-file-limit-exceeded`
- **Domain:** database
- **Category:** resource_error
- **Error Code:** `53100`
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

PostgreSQL has exhausted either disk space in the temporary file directory or the kernel's file descriptor limit, preventing the creation of temporary files for query execution.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| PostgreSQL 12 | active | — | — |
| PostgreSQL 13 | active | — | — |
| PostgreSQL 14 | active | — | — |
| PostgreSQL 15 | active | — | — |
| PostgreSQL 16 | active | — | — |

## Workarounds

1. **Check disk usage with `df -h` and `df -i` on the partition containing pg_stat_tmp and base directory. Free space by removing old WAL files or archiving logs: `pg_archivecleanup /path/to/archive $(ls -t /path/to/archive | tail -1)`** (80% success)
   ```
   Check disk usage with `df -h` and `df -i` on the partition containing pg_stat_tmp and base directory. Free space by removing old WAL files or archiving logs: `pg_archivecleanup /path/to/archive $(ls -t /path/to/archive | tail -1)`
   ```
2. **Add more disk space or mount a larger volume to the temporary directory. Then, restart PostgreSQL or set temp_tablespaces to a tablespace on a volume with sufficient space: `ALTER SYSTEM SET temp_tablespaces = 'fast_disk'; SELECT pg_reload_conf();`** (85% success)
   ```
   Add more disk space or mount a larger volume to the temporary directory. Then, restart PostgreSQL or set temp_tablespaces to a tablespace on a volume with sufficient space: `ALTER SYSTEM SET temp_tablespaces = 'fast_disk'; SELECT pg_reload_conf();`
   ```
3. **If the issue is file descriptor limits, increase max_files_per_process in postgresql.conf and restart: `ALTER SYSTEM SET max_files_per_process = 2000;` then restart the server** (70% success)
   ```
   If the issue is file descriptor limits, increase max_files_per_process in postgresql.conf and restart: `ALTER SYSTEM SET max_files_per_process = 2000;` then restart the server
   ```

## Dead Ends

- **Increase max_files_per_process in postgresql.conf without checking disk space** — The error is usually caused by full disk, not file descriptor limit; raising max_files_per_process won't free disk space (60% fail)
- **Delete random files from pg_tblspc thinking they are temporary** — pg_tblspc contains symbolic links to tablespaces; deleting them can corrupt the database (80% fail)
- **Restart PostgreSQL without clearing temp files** — Restarting doesn't reclaim disk space; the underlying issue of full disk or exhausted inodes remains (70% fail)
