Greenplum reports slowed down over a month of nightly loads, and a plan now broadcasts the biggest fact — diagnose
Each night a job appends about 200 million rows to fact_clicks (append-optimized, column-oriented; created and first loaded a month ago) and reloads the last seven days of the heap table sales_daily with DELETE ... WHERE day >= current_date - 7 followed by INSERT. No other maintenance runs. Reports joining the two now take 40 minutes instead of 4. Constraints: explain both problems from the output below and fix the job, not the reports.
-- report EXPLAIN, abridged
-> Hash Join
-> Seq Scan on sales_daily s
-> Hash
-> Broadcast Motion 48:48 (slice1; segments: 48) (rows=1150000)
-> Seq Scan on fact_clicks c (rows=1150000)
SELECT count(*) FROM fact_clicks; -- 6 100 000 000
SELECT relname, reltuples::bigint FROM pg_class
WHERE relname IN ('fact_clicks', 'sales_daily');
relname | reltuples
-------------+-----------
fact_clicks | 1150000
sales_daily | 210000000
SELECT bdirelname, bdirelpages, bdiexppages, bdidiag FROM gp_toolkit.gp_bloat_diag;
bdirelname | bdirelpages | bdiexppages | bdidiag
-------------+-------------+-------------+---------------------------------------
sales_daily | 9800000 | 1400000 | significant amount of bloat suspectedDiagnose the cause.
The loads go into tables that already have statistics, so auto-stats never fire again: the planner still sees 1.15M rows in fact_clicks and broadcasts it. Run ANALYZE after each load. The daily DELETE bloats heap sales_daily: VACUUM it, or swap partitions.
- Assuming automatic statistics keep up with loads into a table that already has statistics
- Reloading days with DELETE and INSERT into a heap table and never vacuuming it
- Expecting VACUUM to collect the column statistics ANALYZE gathers, or ANALYZE to reclaim dead rows
- How does VACUUM treat deleted rows in an append-optimized table differently from a heap table?
- Why does reloading a day by swapping in a freshly loaded partition avoid both bloat and the DELETE?
Problem 1 — statistics (Greenplum and its forks Arenadata DB and Greengage DB). Automatic statistics are governed by gp_autostats_mode. In on_no_stats mode it runs ANALYZE after a CREATE TABLE AS, INSERT or COPY by the table owner only when the table has no statistics yet. fact_clicks was analyzed at its first load a month ago: 1.15 million rows. Thirty nights of 200 million rows left those statistics untouched. The planner thinks the table is small and picks a Broadcast Motion: each of the 48 segments sends its share of 6.1 billion rows to all the others.
Problem 2 — bloat. A DELETE on a heap table frees nothing: the deleted row versions stay dead until VACUUM removes them. Every night seven days of sales_daily rows die, and without cleanup the table takes 9.8 million pages instead of the expected 1.4 million, and every scan reads the dead rows.
Fix in the load job:
-- after the load
ANALYZE fact_clicks; -- for a partitioned table: ANALYZE the new partition
ANALYZE sales_daily;
-- once: get the space back; VACUUM FULL locks the table, plan a window
VACUUM FULL sales_daily;
-- from now on: a plain VACUUM after the DELETE, or reload a day by swapping a partition
-- (load the day into its own table and swap it in for the day's partition), with no DELETE at all
VACUUM sales_daily;A plain VACUUM marks dead rows' space for reuse but does not shrink the file; VACUUM FULL rewrites the table and holds an exclusive lock. VACUUM refreshes at most the row and page counts in pg_class, not the column statistics ANALYZE collects, and ANALYZE does not remove dead rows — you need both. In an append-optimized table deleted rows are marked in a visibility map, and VACUUM compacts a segment file only when its share of hidden rows exceeds gp_appendonly_compaction_threshold.