// DuckDB error reference

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
Catégorie
Memory and resource errors
Message d'erreur
Out of Memory Error: failed to allocate data of size 32.0 MiB (48.1 MiB/61.0 MiB used)

Ce que cela signifie

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.

Causes courantes

  • 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';

Comment le corriger

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

    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:

    SET temp_directory = '/data/duckdb_tmp';
  3. 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;
  4. 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.

Reproduisez-le en toute sécurité

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.

Erreurs liées

Le code et le texte du message sont issus de DuckDB documentation. Parcourir la liste complète DuckDB error list ou Rechercher toutes les erreurs de base de données.

// moins d'erreurs, corrections plus rapides

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.

Texte et données de cette page : CC BY 4.0, en citant SQLly (sqlly.app). Code et extraits SQL : MIT. Détails de la licence Le texte des messages d’erreur cité appartient à l’éditeur de la base de données.