Symptom
Queries fail with one of these messages, often only at month end or after a dataset grows:
Query exceeded per-node memory limit of 30GB(error nameEXCEEDED_LOCAL_MEMORY_LIMIT)Query exceeded distributed user memory limit of 100GB(EXCEEDED_GLOBAL_MEMORY_LIMIT)The cluster is out of memory and ... query was killed(CLUSTER_OUT_OF_MEMORY), for a query that looked small- Workers restart with
java.lang.OutOfMemoryErrorand every query on them fails
These are different problems. The first two mean one query hit its own cap. The third means the low-memory killer chose this query to save the cluster. The last means the limits are configured larger than the JVM can actually hold.
How Trino accounts for memory
Property (config.properties) | Scope | Notes |
|---|---|---|
query.max-memory-per-node | One query on one worker | Default is a fraction of the JVM max heap (30% in documented releases) |
query.max-memory | One query, user memory, across the cluster | Hard cap per query |
query.max-total-memory | One query, user plus system memory, across the cluster | Must be at least query.max-memory |
memory.heap-headroom-per-node | Heap kept back for untracked allocations | Default is also a fraction of max heap |
Two rules decide whether the limits are safe:
query.max-memory-per-node+memory.heap-headroom-per-nodemust be less than the worker's-Xmxinjvm.config. If not, a single query can exhaust the heap and the worker dies with a JVM OOM instead of a clean Trino error.- Per-node usage is what fails first in practice. A query with a 100 GB cluster limit can still fail at 30 GB on one worker if its data isn't spread evenly.
Session properties let you lower the limits for a query (query_max_memory, query_max_memory_per_node, query_max_total_memory). Trino applies the smaller of the session value and the server config, so you can't raise a limit above the server config from a session.
Checks
1. Which limit and which stage?
Get the query ID from the error, then pull its stats from the coordinator:
TRINO=https://trino.example.com
QID=20261116_083012_00042_abcde
curl -s -u "$USER" "$TRINO/v1/query/$QID" \
| jq '{state, error: .failureInfo.message, peakUser: .queryStats.peakUserMemoryReservation, peakTotal: .queryStats.peakTotalMemoryReservation}'
Then look at the plan with real numbers. Run it on a smaller date range if the full query fails:
EXPLAIN ANALYZE
SELECT c.region, sum(o.amount)
FROM lake.sales.orders o
JOIN lake.sales.customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= DATE '2026-10-01'
GROUP BY c.region;
For each fragment, EXPLAIN ANALYZE shows peak memory, input rows and the distribution of rows across tasks. Look for the stage with the highest peak memory and check whether a join is REPLICATED (broadcast) or PARTITIONED.
2. Is it skew?
If one task in a stage processes far more rows than the others, or the per-node error fires while cluster-wide usage is well under query.max-memory, you have skew. Find the heavy keys directly:
SELECT customer_id, count(*) AS n
FROM lake.sales.orders
WHERE order_date >= DATE '2026-10-01'
GROUP BY customer_id
ORDER BY n DESC
LIMIT 20;
Typical culprits are NULL, 0, 'unknown' or a single test customer. A hash-partitioned join or aggregation sends every row for that key to one worker.
3. Is a broadcast join too big?
A REPLICATED join copies the build side to every worker. If table statistics are missing or stale, the cost-based optimizer may broadcast a table that is far larger than it thinks.
SHOW STATS FOR lake.sales.customers;
Empty or NULL row counts mean the optimizer is guessing. Collect statistics where your connector supports it (ANALYZE lake.sales.customers; for Hive and Iceberg connectors).
4. Is the cluster simply full?
If the error is CLUSTER_OUT_OF_MEMORY, the query may be innocent. Check what else was running:
SELECT query_id, state, "user", created, substr(query, 1, 80) AS query
FROM system.runtime.queries
WHERE state IN ('RUNNING', 'QUEUED')
ORDER BY created;
The low-memory killer policy (query.low-memory-killer.policy, default total-reservation-on-blocked-nodes) kills the query holding the most memory on the nodes that ran out. That policy only applies when task-level retries are off.
Fixes
| Cause | Fix |
|---|---|
| Skewed join or group-by key | Filter or handle the hot key separately (WHERE customer_id IS NOT NULL plus a separate branch for nulls), or pre-aggregate before the join |
| Broadcast of a large table | SET SESSION join_distribution_type = 'PARTITIONED'; or lower join_max_broadcast_table_size; collect stats |
| Wrong join order | Collect stats so the CBO can reorder; as a last resort, rewrite so the smaller side is the build side |
Huge DISTINCT / GROUP BY on high-cardinality columns | Use approx_distinct() where exact counts aren't required; aggregate in stages |
| Limits larger than heap | Make query.max-memory-per-node + memory.heap-headroom-per-node fit under -Xmx |
| Many large queries together | Resource groups with softMemoryLimit and hardConcurrencyLimit per group |
| Legitimately big batch query | Fault-tolerant execution with task retries (below) |
Session-level fix for a single job:
SET SESSION join_distribution_type = 'PARTITIONED';
SET SESSION query_max_memory_per_node = '20GB'; -- lower, to fail early instead of starving others
Warning
Raising
query.max-memory-per-nodewithout raising the JVM heap moves the failure from a clean query error to a worker crash, which kills every query on that worker. Change both together, and restart workers one at a time.
Fault-tolerant execution for big batch queries
For ETL-style queries that legitimately need more memory than one pass allows, task-level retries (retry-policy=TASK) let Trino split work into smaller tasks, spool intermediate data to an exchange manager and retry failed tasks. Configure it on a dedicated batch cluster, not on the interactive one, because it adds latency to every query.
config.properties:
retry-policy=TASK
exchange-manager.properties:
exchange-manager.name=filesystem
exchange.base-directories=s3://trino-exchange-spool/batch-cluster
Azure Blob/ADLS and GCS are also supported as exchange storage; see the fault-tolerant execution docs for your release for the exact properties and supported connectors. Put a lifecycle rule on the spool location, because abandoned exchange data accumulates.
The Trino documentation describes legacy spill-to-disk (spill-enabled) as a legacy feature and recommends fault-tolerant execution instead. If you still use spill, keep the spill path on fast local disk and expect queries to slow down a lot when it engages.
Tip
With
retry-policy=TASK, the low-memory killer works per task instead of killing the whole query. A memory spike now becomes a task retry with more memory, rather than a failed query.
Prevent it next time
- Collect table statistics as part of your ingestion jobs, so join choices don't depend on missing stats.
- Run interactive and batch workloads on separate clusters, or at least separate resource groups with memory limits.
- Alert on worker JVM OOMs and restarts, not only on failed queries. A worker crash is a sizing bug.
- Keep a short list of known hot keys per large table, and add data quality checks that flag new ones.
- Review
jvm.configand memory properties together whenever you change worker instance size.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get