A nightly Greenplum ELT insert fails with «workfile per query size limit exceeded» while dashboards run — diagnose
At 02:00 the ELT role etl computes the gaps between each user's events for yesterday's 3 billion rows (events is evenly distributed by event_id): INSERT INTO event_gaps SELECT user_id, event_ts, event_ts - lag(event_ts) OVER (PARTITION BY user_id ORDER BY event_ts) FROM events WHERE event_date = current_date - 1. Since a mobile release that logs signed-out users as user_id = -1, it fails most nights. BI dashboards refresh at the same time. Someone proposes setting the workfile limit to unlimited. Constraints: explain the failure from the output below, say why the proposal is wrong, and give a fix for the query and one for the workload.
02:41:07 ERROR: workfile per query size limit exceeded (seg9 10.0.3.14:6001 pid=88211)
SELECT segid, round(sum(size) / 1024.0^3, 1) AS gb, sum(numfiles) AS files
FROM gp_toolkit.gp_workfile_usage_per_query
WHERE query LIKE 'INSERT INTO event_gaps%'
GROUP BY segid ORDER BY gb DESC LIMIT 4;
segid | gb | files
-------+-------+-------
9 | 380.2 | 9120
31 | 8.7 | 210
12 | 8.1 | 198
4 | 7.9 | 190
SELECT user_id, count(*) FROM events WHERE event_date = current_date - 1
GROUP BY 1 ORDER BY 2 DESC LIMIT 2;
user_id | count
---------+-------------
-1 | 910 000 000
88412 | 310 000
SELECT r.rolname, g.rsgname FROM pg_roles r JOIN pg_resgroup g ON g.oid = r.rolresgroup
WHERE r.rolname IN ('etl', 'bi_user');
rolname | rsgname
---------+---------------
etl | default_group
bi_user | default_groupDiagnose the cause.
The query spilled past its workfile limit, almost all on seg9: the window redistributes on user_id and sorts per user, so 910M rows of -1 land on one segment. Unlimited spill only fills seg9's disk. Handle -1 apart, and give etl its own resource group.
- Raising or removing the workfile limit, which moves the failure to a full disk on the hot segment
- Blaming the stored table's distribution when the skew comes from redistributing on a hot window partition key
- Running ELT and dashboards in one default resource group, where they compete for the same memory and concurrency slots
- How would you find which operator of the plan spilled, and on which segments, while the query is still running?
- Why can stale statistics on
eventsmake a hash join or aggregate spill even without a hot key?
What happened (Greenplum and its forks Arenadata DB and Greengage DB). When a sort, hash join or hash aggregate runs out of its memory, the operator spills data into temporary workfiles on the segment's disk. The limits gp_workfile_limit_per_query (size per segment) and gp_workfile_limit_files_per_query (file count) terminate a query that passes them — that is the error.
Reading the output.
- seg9 alone holds 380 GB of spill files, over 40 times any other segment (about 8 GB each). So the problem is not memory in general but one segment.
eventsis evenly distributed, but the windowPARTITION BY user_idredistributes rows onuser_idand sorts each user in full on one segment. The 910 million rows withuser_id = -1are a single "user", and all of them go to seg9: processing skew, not data skew.etlandbi_usersharedefault_group: the nightly ELT splits memory and concurrency slots with the dashboards and gets less than it needs.
Why not lift the limit. The limit is a fuse: without it seg9 writes spill files until its disk fills, and then every query on that host fails. And even with unlimited disk, one segment sorting 910 million rows still sets the time of the whole query.
Query fix — the hot value apart. Signed-out events have no user, so their gaps are computed per device, a key with millions of values:
INSERT INTO event_gaps
SELECT user_id, event_ts, event_ts - lag(event_ts) OVER (PARTITION BY user_id ORDER BY event_ts)
FROM events
WHERE event_date = current_date - 1 AND user_id <> -1;
INSERT INTO event_gaps_anon
SELECT device_id, event_ts, event_ts - lag(event_ts) OVER (PARTITION BY device_id ORDER BY event_ts)
FROM events
WHERE event_date = current_date - 1 AND user_id = -1;Workload fix — a resource group of its own for ELT, with lower concurrency and a larger memory share, separate from BI:
CREATE RESOURCE GROUP rg_etl WITH (CONCURRENCY = 4, CPU_RATE_LIMIT = 30, MEMORY_LIMIT = 40);
ALTER ROLE etl RESOURCE GROUP rg_etl;
-- Greenplum 6 syntax; in Greenplum 7: WITH (CONCURRENCY = 4, CPU_MAX_PERCENT = 30, MEMORY_QUOTA = <MB>)
-- in forks, check the attribute names in your build's docsFresh statistics matter too: the planner sizes hash tables and spill partitions from its row estimates. Where to look next: gp_toolkit.gp_workfile_usage_per_query while the query runs, and EXPLAIN ANALYZE, which shows which operator spilled and on which segment.