In Greenplum, the table is evenly distributed, yet one segment does most of the join's work — diagnose it
A nightly query joins events (2 billion rows, DISTRIBUTED BY (event_id)) to partners on partner_id and aggregates per partner. It used to take 20 minutes; since a new traffic source went live it takes three hours, and monitoring shows seg17 at 100% CPU for hours while the other 47 segments finish early. The admin says the table "is not skewed". Constraints: base the cause on the output below, and fix it without redistributing events.
-- rows of events per segment
min_rows | max_rows
------------+------------
41 802 113 | 42 010 597
SELECT partner_id, count(*) FROM events GROUP BY 1 ORDER BY 2 DESC LIMIT 3;
partner_id | count
------------+---------------
0 | 1 390 000 000
4417 | 2 100 000
812 | 1 900 000
-- EXPLAIN ANALYZE, abridged
-> Hash Join (rows per segment: avg 41.9M, max 1406M on seg17)
Hash Cond: (e.partner_id = p.partner_id)
-> Redistribute Motion 48:48 (slice1; segments: 48)
Hash Key: e.partner_id
-> Seq Scan on events e
-> Hash
-> Seq Scan on partners pDiagnose the cause.
Not data skew: events is even. The join redistributes it on partner_id, and the 1.39 billion rows with partner_id = 0 all hash to seg17, which works alone. That is processing skew: handle the hot value apart, or aggregate before the motion.
- Checking only rows per segment of the stored table and concluding there is no skew
- Redistributing the table on the hot join key, which turns processing skew into permanent data skew
- Adding memory or hardware instead of removing the hot value from the redistributed stream
- What do
gp_toolkit.gp_skew_coefficientsandgp_toolkit.gp_skew_idle_fractionsmeasure, and why would neither have caught this? - How would pre-aggregating
eventsbypartner_idbefore the join change the rows each segment receives?
Two kinds of skew in Greenplum and its forks Arenadata DB and Greengage DB.
- Data skew — the table is stored unevenly: one segment holds more rows than the others. It is visible at rest:
gp_segment_id,gp_toolkit.gp_skew_coefficients. There is none here: minimum and maximum differ by half a percent. - Processing skew arises during a query: a Redistribute Motion rehashes rows on the key of a join, aggregation or window function, and if one key value is far more frequent than the others, all its rows go to one segment. The stored table does not show it; you find it through spill files and the per-segment maximum in
EXPLAIN ANALYZE.
Reading the output. 1.39 billion of the 2 billion events have partner_id = 0 — the new source writes "no partner". Redistributing on partner_id sends them all to seg17: 1,406M rows against an average of 41.9M. seg17 receives and joins those rows alone, long after the others have finished, and that segment sets the query time. Had the hot rows needed sorting or hashing — a window, a DISTINCT, a hash join built on that side — seg17 would also have spilled.
The fix — take the hot value out of the redistribution:
-- 1. The hot value as its own branch, with no join on the key
SELECT p.partner_id, p.name, s.events
FROM partners p
JOIN (SELECT partner_id, count(*) AS events
FROM events
WHERE partner_id <> 0
GROUP BY partner_id) s USING (partner_id)
UNION ALL
SELECT 0, 'no partner', count(*) FROM events WHERE partner_id = 0;
-- 2. Aggregate before the join: one row per partner per segment crosses the network
SELECT p.partner_id, p.name, sum(s.events)
FROM (SELECT partner_id, count(*) AS events FROM events GROUP BY partner_id) s
JOIN partners p USING (partner_id)
GROUP BY 1, 2;In the second version the planner can aggregate on every segment locally first and ship only partial totals. If partners is small, DISTRIBUTED REPLICATED for it also works: the join runs where the events rows already sit, without redistributing them. Redistributing events on partner_id is not a fix: processing skew would become permanent data skew, and seg17 would be overloaded in every query on the table. It is also worth finding out why the source writes 0: it may belong as NULL or as a real id.