Plan nodes · Grouping and sorting
Aggregate
Computes aggregates: count(), sum()… over all rows or per group.
With no GROUP BY it is a plain aggregate and returns one row. With GROUP BY it uses one of two strategies: Sorted (GroupAggregate) reads input sorted on the group key and finishes each group as the key changes; Hashed (HashAggregate) keeps a hash table of groups, which needs no sorted input but needs memory for every group. Mixed handles grouping sets that need both.
What to look for
A HashAggregate with "batches" above 1 spilled to disk.
Example
SELECT genre, count(*) FROM tracks GROUP BY genre
2 nodes
- Aggregaterows 6 (estimated 6) cost 0.29..2344.35 buffers: 1 hit, 86 readstrategy
Sortedgroup bygenre- Index Only Scan using tracks_genre on tracksrows 100,000 (estimated 100,000) cost 0.29..1844.29 buffers: 1 hit, 86 read1 index searches
Run with EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF) on the sample database, PostgreSQL 19.0. Open it in the EXPLAIN visualizer →
Related
Sort · Incremental Sort · Group · Unique · WindowAgg · Limit