# Out of Memory Error: The query hit DuckDB's memory limit

> A query needed more memory than DuckDB was allowed to use and could not spill it to disk, so the engine aborted it.

Source: https://sqlly.app/errors/duckdb/out-of-memory
Last updated: 2026-11-30
License: Text and data [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) (credit SQLly, sqlly.app); code and SQL snippets [MIT](https://opensource.org/license/mit). [Details](https://sqlly.app/license).
Note: Quoted error message text belongs to its database vendor.

- Exception: `Out of Memory Error`
- Category: Memory and resource errors

Error message:

```text
Out of Memory Error: failed to allocate data of size 32.0 MiB (48.1 MiB/61.0 MiB used)
```

## What it means

DuckDB caps buffer manager memory with the `memory_limit` setting and spills large sorts, hashes and joins to a temporary directory when it can. `Out of Memory Error` means a query pushed past that cap and the operator holding the data could not reduce its footprint. The parenthetical names the allocation that failed.

The limit is per database instance, not per query, so several concurrent queries together can trip it even when each alone would fit. Results that stream fine can still fail inside an operator that must materialize everything, such as a full outer join or a DISTINCT over a huge column.

## Common causes

- A query that materializes a very large intermediate: a big GROUP BY with many distinct groups, a DISTINCT, or a self join.
- A low memory_limit relative to the data, often left at a small value in a container.
- No usable spill target: temp_directory emptied or pointing somewhere unwritable, so DuckDB cannot offload and fails instead.
- Many concurrent queries sharing one instance's memory budget.

## How to fix it

1. Raise the limit to fit the data and the machine, checking what it is now:

   ```sql
   SELECT current_setting('memory_limit');
   SET memory_limit = '8GB';
   ```
2. Point temp_directory somewhere writable so DuckDB can spill instead of failing; a file-backed database already defaults to a folder named <database name>.tmp next to its database file, an in-memory one to .tmp in the working directory:

   ```sql
   SET temp_directory = '/data/duckdb_tmp';
   ```
3. Reduce what the query materializes: aggregate in stages, prune columns early, and avoid DISTINCT over wide rows.

   ```sql
   SELECT key, count(*) FROM big GROUP BY key;
   ```
4. Use out-of-core-friendly joins when possible, and let the optimizer choose by keeping statistics fresh; ANALYZE helps it size operators.

   ```sql
   ANALYZE;
   ```

## Reproduce it safely

```sql
SET memory_limit = '64MB';
SET temp_directory = '';   -- an in-memory database would otherwise spill to .tmp in the working directory
SELECT count(DISTINCT i) FROM range(50000000) t(i);   -- Out of Memory Error: could not allocate block of size 256.0 KiB (61.0 MiB/61.0 MiB used)
```

## Questions

### Why did my query fail with plenty of free RAM?

DuckDB respects memory_limit, which defaults to about 80 percent of physical RAM but is often set lower, especially in containers. Check current_setting('memory_limit') on the connection that failed.

### Can DuckDB spill to disk automatically?

Yes, for many large sorts, aggregations and joins, but at a small budget some still fail: a 4M-row sort at 64MB and a count(DISTINCT) over 50M rows both raised this error on a file-backed database. A file-backed database spills by default: temp_directory then points at a <database name>.tmp folder next to the database file, and an in-memory database uses .tmp in the working directory. Spilling is off only when temp_directory is emptied; then an over-budget query fails with this error. If temp_directory points somewhere unwritable, the query fails with an IO Error (Failed to create directory ... Permission denied) instead.

## Related errors

- [`IO Error`](https://sqlly.app/errors/duckdb/io-file-not-found): A file could not be opened
- [`53300`](https://sqlly.app/errors/postgresql/53300): PostgreSQL: too many connections
- [`Conversion Error`](https://sqlly.app/errors/duckdb/conversion-out-of-range): The value is out of range for INT32
- [`53200`](https://sqlly.app/errors/postgresql/53200): PostgreSQL: out of memory
- [`SQLITE_FULL`](https://sqlly.app/errors/sqlite/sqlite-full): SQLite: database or disk is full

The code and message text are from the [DuckDB documentation](https://duckdb.org/docs/).
