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:
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:
- 1EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
- 2SELECT count(*) FROM big
- Aggregate (actual rows=1.00 loops=1)
- Buffers: shared hit=2304 read=8512
- -> Seq Scan on big (actual rows=2000000.00 loops=1)
- Buffers: shared hit=2304 read=8512
this cluster starts with max_parallel_workers_per_gather = 0, like the browser. so: one process, one Seq Scan.
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.
-> 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.
- 1EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
- 2SELECT k, count(*) FROM big GROUP BY k ORDER BY k
- Finalize GroupAggregate (actual rows=1000.00 loops=1)
- Group Key: k
- Buffers: shared hit=2795 read=8042
- -> Gather Merge (actual rows=4000.00 loops=1)
- Workers Planned: 3
- Workers Launched: 3
- Buffers: shared hit=2795 read=8042
- -> Sort (actual rows=1000.00 loops=4)
- Sort Key: k
- Sort Method: quicksort Memory: 56kB
- Buffers: shared hit=2795 read=8042
- Worker 0: Sort Method: quicksort Memory: 56kB
- Worker 1: Sort Method: quicksort Memory: 56kB
- Worker 2: Sort Method: quicksort Memory: 56kB
- -> Partial HashAggregate (actual rows=1000.00 loops=4)
- Group Key: k
- Batches: 1 Memory Usage: 121kB
- Buffers: shared hit=2774 read=8042
- Worker 0: Batches: 1 Memory Usage: 121kB
- Worker 1: Batches: 1 Memory Usage: 121kB
- Worker 2: Batches: 1 Memory Usage: 121kB
- -> Parallel Seq Scan on big (actual rows=500000.00 loops=4)
- Buffers: shared hit=2774 read=8042
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:
SELECT current_setting('max_worker_processes') AS max_worker_processes,
current_setting('max_parallel_workers') AS max_parallel_workers;| max_worker_processestext | max_parallel_workerstext |
|---|---|
| 0 | 0 |
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:
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;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:
- Finalize Aggregate (actual rows=1.00 loops=1)
- Buffers: shared hit=335
- -> Gather (actual rows=5.00 loops=1)
- Workers Planned: 4
- Workers Launched: 4
- Buffers: shared hit=335
- -> Partial Aggregate (actual rows=1.00 loops=5)
- Buffers: shared hit=335
- -> Parallel Seq Scan on events (actual rows=5000.00 loops=5)
- Buffers: shared hit=335
- 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
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.
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.