| From: | Prashant Chinnam <crprashant(at)hotmail(dot)com> |
|---|---|
| To: | Tatsuya Kawata <kawatatatsuya0913(at)gmail(dot)com> |
| Cc: | Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: Hash aggregation shows only the last execution's stats for parallel workers |
| Date: | 2026-11-24 00:09:58 |
| Message-ID: | SN7P223MB0600831EBACB74CD241B251FDD442@SN7P223MB0600.NAMP223.PROD.OUTLOOK.COM |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Tatsuya,
Thanks for the patch. I reproduced the problem and verified the
fix on current master (207b5293b8d); v1 applies cleanly and the
diffstat is exactly the advertised +10 -3 in nodeAgg.c. My
verdict is Ready for Committer. Details below.
Your analysis is right: when a Gather or Gather Merge node is
rescanned it starts a new set of workers, and each worker's
ExecEndAgg() overwrites the Agg statistics in the shared memory
slots, so EXPLAIN ANALYZE reports only the last execution's
per-worker Batches / Memory Usage / Disk Usage. Accumulating in
ExecEndAgg(), where the worker hands its stats back, is the
right layer, and the shape matches the precedent you cite: the
Memoize fix already accumulates its counters in exactly this
spot, with Max() for the peak value.
What I ran, using your reproducer's settings (work_mem = 64kB
to force spilling, parallel_leader_participation = off,
max_parallel_workers = 1 so Workers Planned: 2 but only
Worker 0 launches and runs every execution; 2,000,000-row
table, GROUP BY over 2,000 groups):
case master patched
your repro, rescanned twice 5 9
same, rescanned three times 5 13
rescanned twice, no spilling 1 1
single execution, spilling 5 5
rescanned twice, two workers 5 / 5 9 / 9
So the patched Worker 0 Batches is 9 for two executions
(5 + 5, with the in-memory batch counted once) and 13 for
three, while Memory Usage (169kB) and Disk Usage (7344kB)
are unchanged from master in these identical-execution
cases, since they keep the maximum. The two controls I cared
about most both hold: a single execution reports exactly the
same numbers as master (no double counting, because the
shared slot starts at zero), and two executions that never
spill still show Batches: 1 with no Disk Usage line, which a
naive sum would have broken (explain.c prints Disk Usage
only when Batches > 1, so summing would have produced a
phantom "Disk Usage: 0kB").
Scope checks: ExecEndAgg() is the only place that writes the
shared AggregateInstrumentation slots, and explain.c is the
only reader, so the fix covers the whole affected path. The
leader cannot have this bug (its stats are read from its own
AggState, and indeed a leader-executed HashAggregate that is
rescanned twice already shows Batches: 9 on unpatched master;
the patch makes the worker path consistent with it), and
GroupAggregate has no hash statistics to accumulate. All of
my non-parallel and leader-executed control cases produced
byte-identical Batches values before and after the patch.
Tests: I ran the full meson test suite (TAP enabled) on the
patched build: 404 ok, 0 failures, 36 skipped; core
regression 243/243 (including aggregates, select_parallel,
explain, groupingsets and partition_aggregate) and isolation
139/139. I also ran installcheck-world on a separate
autoconf build of the same patched tree (TAP enabled,
against the installed server): all 55 pg_regress suites
passed, 668 tests, zero regression.diffs, core 243/243 and
isolation 139/139 included. The TAP suites run under it
passed as well, except pg_upgrade's on the first pass:
008_extension_control_path.pl needs the test modules' .so
files, which a plain make does not build. With them built,
that suite passes standalone (9 files, 161 tests).
The patch adds no regression test, and I do not think its
absence should hold the fix up — no existing test prints
per-worker Batches, and the analogous Memoize fix shipped
without one. That said, the Batches number turns out to be
stable enough to test (it reproduced exactly in both our
environments, unlike Memory/Disk: your 161kB / 11048kB vs
my 169kB / 7344kB), so I wrote one and attached it, for you
or a committer to take or leave. It adds a case to
select_parallel that rescans a Gather over a spilling
HashAggregate with one launched worker and asserts only the
extracted Batches line: 9 with the fix, 5 without, plus a
no-spill pass that must stay at 1. With the test added, the
core regression suite passes on the patched tree; on
unpatched master it fails with exactly that one line
(Batches: 5 for the expected 9).
The patch is marked for backpatching to stable; how far back
is a committer's call, but since this only changes EXPLAIN
ANALYZE statistics reporting and not query results, it looks
safe to backpatch wherever the Memoize fix went.
I used AI assistance for the builds and test runs.
Regards,
Prashant
| Attachment | Content-Type | Size |
|---|---|---|
| regression-test.patch | application/octet-stream | 6.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Michael Paquier | 2026-11-24 00:15:26 | Re: Bug in logical decoding with DDL and subtransactions |
| Previous Message | Bharath Rupireddy | 2026-11-23 23:57:41 | Re: pg_walinspect: add functions to locate and list WAL by time and LSN |