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

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