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.
- Exception
Out of Memory Error- Categoría
- Memory and resource errors
Qué significa
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.
Causas comunes
- 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.
Find the cause
-- What is the budget and where does spilling go?
SELECT current_setting('memory_limit') AS memory_limit, current_setting('temp_directory') AS temp_directory;
-- Which operator classes hold memory now (tag, memory_usage_bytes, temporary_storage_bytes):
SELECT * FROM duckdb_memory();
-- Scratch files this database has spilled so far:
SELECT * FROM duckdb_temporary_files();
-- Size the inputs before the query (estimated_size reads 0 for a table created inside an uncommitted transaction):
SELECT estimated_size FROM duckdb_tables() WHERE table_name = 'big';
Cómo solucionarlo
-
Raise the limit to fit the data and the machine, checking what it is now:
SELECT current_setting('memory_limit'); SET memory_limit = '8GB'; -
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:
SET temp_directory = '/data/duckdb_tmp';
-
Reduce what the query materializes: aggregate in stages, prune columns early, and avoid DISTINCT over wide rows.
SELECT key, count(*) FROM big GROUP BY key;
-
Use out-of-core-friendly joins when possible, and let the optimizer choose by keeping statistics fresh; ANALYZE helps it size operators.
ANALYZE;
Other ways to handle it
Raising memory_limit fixes the symptom only if the machine really has the RAM; in a container it must also be raised there or the process will be killed by the OS instead, which looks like a crash rather than this error. For one-off huge jobs, process the data in ranged batches rather than lifting the cap.
Reprodúcelo de forma segura
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)
Preguntas
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.
Errores relacionados
IO ErrorA file could not be opened53300PostgreSQL: demasiadas conexionesConversion ErrorThe value is out of range for INT3253200PostgreSQL: memoria insuficienteSQLITE_FULLSQLite: la base de datos o el disco está lleno
El código y el texto del mensaje son de DuckDB documentation. Explora la lista completa de DuckDB error list o buscar cada error de base de datos.
Work on DuckDB in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for DuckDB and the other databases you use.
Texto y datos de esta página: CC BY 4.0, con atribución a SQLly (sqlly.app). Código y fragmentos SQL: MIT. Detalles de la licencia El texto citado de los mensajes de error pertenece a su proveedor de base de datos.