skip to content

parallel query

Gather, workers and partial aggregates. why one query used four cores, and why it can't in this page.

address
everything.execution.parallel_query
part
part 9, how a query runs
chapter
chapter 13 of 13
reading time
9 min

postgres can run one query in several processes. a Gather node starts parallel workers, each runs a copy of the plan below it on its own share of the table (a Parallel Seq Scan hands out blocks to whoever asks), and the leader collects their rows. aggregates split into Partial (per process) and Finalize (once, at the top).

the postgres on this page is a single process with no room for workers. it can plan a parallel query, but never launch one, so the parallel plans here come from a real server.

the picture

you know how a count(*) on a big table pegs one core and leaves the others idle? since postgres 9.6 it doesn't have to. the planner can split the scan.

think of counting ballots from one big box with four people. each grabs a handful, counts it, grabs another, until the box is empty. then each writes a subtotal on a slip, and one of them, who was also counting, adds up the slips. the handfuls are blocks of the table, the slips are partial aggregates, and the person adding them up is the leader.

here's that, on a real postgres 18.6 server, counting a 85 MB table of 2,000,000 rows:

a real parallel count: the leader and 3 workers each scan part of the table and count it, and Gather brings the 4 partial counts up to one final sum. the row counts per process are EXPLAIN's average; which process got which blocks changes every run.

one query, four processes

the throwaway server starts with max_parallel_workers_per_gather = 0, the way this page's engine is set. allow up to 4 per Gather and the same count changes shape:

max_parallel_workers_per_gather =
query.sql
  1. 1EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
  2. 2SELECT count(*) FROM big
what postgres printed
  1. Aggregate (actual rows=1.00 loops=1)
  2. Buffers: shared hit=2304 read=8512
  3. -> Seq Scan on big (actual rows=2000000.00 loops=1)
  4. Buffers: shared hit=2304 read=8512
processes scanning big (the scan's loops)
1
4

this cluster starts with max_parallel_workers_per_gather = 0, like the browser. so: one process, one Seq Scan.

real output, a postgres 18.6 server · parallel
the same count, without and with workers. with them, the scan's loops=4 is four processes, and its rows are per process. the bar counts the processes that scanned the table.

read it bottom-up, like any plan. the Parallel Seq Scan is a seq scan that shares its position: every process asks it for the next block, so the table is split on the fly and nobody needs to know the split in advance. above it, each process runs its own Partial Aggregate and returns one row. Gather receives those 4 rows (1 from each process) and the Finalize Aggregate adds them up. everything under the Gather runs in every process; everything above it, only in the leader.

predict the output
Workers Planned: 3, Workers Launched: 3. how many processes scanned the table?
text
->  Gather (actual rows=4.00 loops=1)
      Workers Planned: 3
      Workers Launched: 3
      ->  Partial Aggregate (actual rows=1.00 loops=4)

why 3 workers and not 4? the planner sizes the team to the table: one worker for a table of min_parallel_table_scan_size (8MB), then one more each time the table is three times bigger: 24MB, 72MB, 216MB. big is 85 MB, so 3, under the cap of 4. max_parallel_workers (8 here) is the cap for the whole server, and when it's used up by other queries, a query gets fewer workers than it planned, or none, and the leader does the rest.

parallel GROUP BY

grouping works the same way, with one more step: each process's partial groups have to be combined by key.

query.sql
  1. 1EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
  2. 2SELECT k, count(*) FROM big GROUP BY k ORDER BY k
what postgres printed
  1. Finalize GroupAggregate (actual rows=1000.00 loops=1)
  2. Group Key: k
  3. Buffers: shared hit=2795 read=8042
  4. -> Gather Merge (actual rows=4000.00 loops=1)
  5. Workers Planned: 3
  6. Workers Launched: 3
  7. Buffers: shared hit=2795 read=8042
  8. -> Sort (actual rows=1000.00 loops=4)
  9. Sort Key: k
  10. Sort Method: quicksort Memory: 56kB
  11. Buffers: shared hit=2795 read=8042
  12. Worker 0: Sort Method: quicksort Memory: 56kB
  13. Worker 1: Sort Method: quicksort Memory: 56kB
  14. Worker 2: Sort Method: quicksort Memory: 56kB
  15. -> Partial HashAggregate (actual rows=1000.00 loops=4)
  16. Group Key: k
  17. Batches: 1 Memory Usage: 121kB
  18. Buffers: shared hit=2774 read=8042
  19. Worker 0: Batches: 1 Memory Usage: 121kB
  20. Worker 1: Batches: 1 Memory Usage: 121kB
  21. Worker 2: Batches: 1 Memory Usage: 121kB
  22. -> Parallel Seq Scan on big (actual rows=500000.00 loops=4)
  23. Buffers: shared hit=2774 read=8042
real output, a postgres 18.6 server · parallel
a parallel GROUP BY on the server. each process builds its own partial groups and sorts them; Gather Merge merges the sorted streams, and the Finalize GroupAggregate combines the partial counts per k.

each of the 4 processes hashes its rows into 1,000 partial groups and sorts them. Gather Merge merges the 4 sorted streams into one sorted stream of 4,000 rows (a plain Gather would lose the order), and the Finalize GroupAggregate adds up the 4 partial counts of each k as they go by. Worker 0: lines are the workers' own numbers; the line without a prefix is the leader's. the split into Partial and Finalize is the same one partitions use, and the same rule applies: count(DISTINCT ...) can't be split.

and in this page

the browser's postgres is one process with max_worker_processes = 0:

sqldb=events
SELECT current_setting('max_worker_processes') AS max_worker_processes,
       current_setting('max_parallel_workers') AS max_parallel_workers;
output, from postgres 18.6
SELECT 1
max_worker_processestextmax_parallel_workerstext
00
(1 row)

so nothing can be launched. but the planner doesn't know that; it only plans. give events the settings that make even a small table worth splitting, and it plans 4 workers anyway:

sqldb=events
SET max_parallel_workers_per_gather = 4;
SET min_parallel_table_scan_size = 0;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
 
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT count(*) FROM events;
output, from postgres 18.6
#1 · SET
#2 · SET
#3 · SET
#4 · SET
#5 · EXPLAIN
Finalize Aggregate (actual rows=1.00 loops=1) Buffers: shared read=335 -> Gather (actual rows=1.00 loops=1) Workers Planned: 4 Workers Launched: 0 Buffers: shared read=335 -> Partial Aggregate (actual rows=1.00 loops=1) Buffers: shared read=335 -> Parallel Seq Scan on events (actual rows=25000.00 loops=1) Buffers: shared read=335 Planning: Buffers: shared hit=28 read=5

Workers Planned: 4, Workers Launched: 0, and the leader scans all 25,000 rows in one loop. here's the same query and settings on the real server:

a postgres 18.6 server
  1. Finalize Aggregate (actual rows=1.00 loops=1)
  2. Buffers: shared hit=335
  3. -> Gather (actual rows=5.00 loops=1)
  4. Workers Planned: 4
  5. Workers Launched: 4
  6. Buffers: shared hit=335
  7. -> Partial Aggregate (actual rows=1.00 loops=5)
  8. Buffers: shared hit=335
  9. -> Parallel Seq Scan on events (actual rows=5000.00 loops=5)
  10. Buffers: shared hit=335
real output, a postgres 18.6 server · parallel
this page (PGlite)
  1. Finalize Aggregate (actual rows=1.00 loops=1)
  2. Buffers: shared read=335
  3. -> Gather (actual rows=1.00 loops=1)
  4. Workers Planned: 4
  5. Workers Launched: 0
  6. Buffers: shared read=335
  7. -> Partial Aggregate (actual rows=1.00 loops=1)
  8. Buffers: shared read=335
  9. -> Parallel Seq Scan on events (actual rows=25000.00 loops=1)
  10. Buffers: shared read=335
real output, postgres 18.6 (PGlite 0.5.12) · parallel-browser
the same query with the same settings, on a real postgres 18.6 server and in this page. same plan. on the server 4 workers start and the scan's 25,000 rows are split five ways (5,000 per process on average); here none can start, so loops=1 and the leader does it all.

a plan with a Gather and no workers is correct, just not faster: the leader runs the whole thing. it's what you'd see on a server where every worker slot is busy.

quick check
which of these can't use a parallel plan?

keylines

  • Gather starts workers; everything below it runs in every process, everything above only in the leader.
  • a Parallel Seq Scan hands out blocks on demand, so the work splits itself.
  • aggregates split into Partial (each process) and Finalize (once).
  • the leader works too: 3 workers means 4 processes, loops=4.
  • the browser's postgres plans parallel queries but has no workers: Launched: 0.