0PricingLogin
PostgreSQL Performance & Query Optimization · Lesson

Parallel Aggregation and Hash Joins

Exploit partial aggregates and parallel-aware joins for heavy grouping workloads.

Why Parallelism for Aggregation

Heavy grouping queries such as GROUP BY over hundreds of millions of rows are usually CPU-bound: most time is spent hashing keys and combining values, not waiting on I/O.

A single backend process can only saturate one core. PostgreSQL's parallel query machinery lets the planner split the scan and the aggregation across several parallel workers, each running on its own core, then combine their results.

  • The launching backend is the leader.
  • Extra processes are parallel workers.
  • Work is divided at the table-scan level and merged at the top.

This lesson focuses on two cooperating pieces: parallel aggregation (partial aggregates) and parallel-aware hash joins.

Partial and Finalize Aggregate

Parallel aggregation works by splitting each aggregate into two phases:

  • Partial Aggregate — each worker aggregates its own slice of rows into a partial state (e.g. a running sum and count).
  • Finalize Aggregate — the leader combines those partial states into the final result.

This is possible because aggregates like count, sum, avg, min and max are combinable: a partial result from one worker can be merged with another via a combine function.

You see this in plans as a Partial Aggregate node under Gather and a Finalize Aggregate node above it.

All lessons in this course

  1. When the Planner Chooses Parallel Plans
  2. Tuning Worker Counts and Gather Costs
  3. Parallel Aggregation and Hash Joins
  4. Diagnosing Why Parallelism Was Disabled
← Back to PostgreSQL Performance & Query Optimization