Plan operator

HashAggregate

Plan node that groups rows by hashing the GROUP BY keys into an in-memory hash table.

A HashAggregate builds a hash table keyed by your GROUP BY columns and accumulates aggregate state in each bucket as rows stream past. Output is unsorted but the algorithm avoids the up-front cost of sorting the input.

The planner picks HashAggregate over GroupAggregate when the estimated number of distinct groups fits comfortably in work_mem. If the estimate is wrong and the hash table overflows, PostgreSQL 13+ spills partitions to disk; older versions could exceed memory limits.

For high-cardinality grouping (millions of distinct keys), a GroupAggregate driven by a sorted Index Scan is sometimes cheaper. Run EXPLAIN ANALYZE and compare planned vs actual rows to spot bad estimates.