PostgreSQL 9.6: measuring where parallel queries help
Explore parallel reads with a synthetic meter dataset, then inspect Gather, actual workers, and the impact on concurrent reports.
Archive retrospective · Topic year 2016
In this article
Where the reporting time goes
An energy report reads millions of meter records and returns eight group totals. Most of its work happens before the small result reaches the client. The parallel query capability introduced with PostgreSQL 9.6 in 2016 is a useful way to study this work. Running one query with several worker processes does not mean each process independently returns a complete copy of the same result. Work is divided and the outputs must be combined correctly. We will use synthetic meter data and measure more than the runtime of one request. Elapsed time, workers that actually launched, buffers, and delays for concurrent application readers answer different questions. A small reproducible experiment allows those results to be compared.
Build the fixture in a regular table
The fixture generates 2.4 million readings using two thousand meter IDs, a repeating hour pattern, and sample energy values between zero and 799. The modular pattern is chosen for reproducibility. It does not describe real energy behavior. Run it in an isolated test database. A regular table matters because workers cannot access a session's temporary table in the same way. Update relation statistics after loading. Repeat both warm and cold cache scenarios where practical. Reduce the row count on a limited lab machine if needed. The planner choosing a serial path for a smaller sample is itself a useful result. Write down the version and relevant settings before comparing plans across runs.
CREATE TABLE meter_hour (
meter integer NOT NULL,
observed_at timestamp NOT NULL,
watt_hours integer NOT NULL
);
INSERT INTO meter_hour
SELECT n % 2000,
timestamp '2016-10-01 00:00:00' + (n % 720) * interval '1 hour',
n % 800
FROM generate_series(1, 2400000) AS series(n);
ANALYZE meter_hour;
Read the Gather and aggregation nodes
Gather combines output from a parallel portion of the plan. This report has only eight result groups, making it a candidate for workers to send per-group partial aggregates instead of every source row. Depending on the chosen plan you may see Partial Aggregate and Finalize Aggregate. When an application returns a very large row set, the leader's duty of gathering results can become a bottleneck instead. Record Workers Planned together with Workers Launched. A plan can hold places for two workers while only one becomes available. The leader may participate, and the workers need not handle identical amounts of data. Use buffer statistics and the time measured by the application to give the node timings context.
Compare the same interval in serial and parallel
Set the maximum workers per Gather to two for the transaction in the example. Run a second transaction with zero to exclude parallel workers. Verify that grouping returns identical counts and energy totals. EXPLAIN ANALYZE executes the query, so account for its activity when testing alongside other traffic. Alternate the order of serial and parallel experiments to help disclose cache warming effects. Try a narrower interval as well as the broad one. Then start several reports while a small representative read runs between them. That smaller query's delay matters if the reports share a production server with interactive users. The setting applies only inside the transaction, helping subsequent trials begin under the conditions you intended.
BEGIN;
SET LOCAL max_parallel_workers_per_gather = 2;
EXPLAIN (ANALYZE, VERBOSE, BUFFERS)
SELECT meter % 8 AS group_id, count(*), sum(watt_hours)
FROM meter_hour
WHERE observed_at >= timestamp '2016-10-13 00:00:00'
AND observed_at < timestamp '2016-10-20 00:00:00'
GROUP BY 1;
ROLLBACK;
Understand what prevents a parallel plan
Parallel restrictions in 9.6 differ from those in later versions. Check any local functions, temporary data, and write effects before assuming a statement qualifies. A function that changes database state cannot safely be relabelled solely to improve planning. Group count matters too. If nearly every input row belongs to a different group, the final aggregator can receive too much intermediate data to benefit. A selective query using a suitable index can touch far fewer pages than a shared scan. Compare equivalent results when you evaluate an indexed path, and record the chosen date predicate with each plan. Recheck plans on the intended data distribution instead of copying another installation's settings wholesale.
Set acceptance criteria for a shared system
First check identical answers. Integer counts and totals make that comparison straightforward in this example. Then compare launched workers and buffers. Measure concurrent activity and keep the slowest observations as well as the median. A report that runs quickly three times in an idle lab can slow down during month-end traffic. Explain where elapsed time is measured, because a database execution timer and a user interface response timer include different costs. Fixing an overly broad filter or eliminating unused groups can sometimes be a better first step than increasing workers. The lasting lesson from this 2016 feature is to evaluate parallelism as an observable planning decision that changes how a real system uses resources.
Reader discussion
What would you change first for maintainability and testing in your own project?
No comments yet. Be the first to contribute.
Write a comment