HashAggregate loses worker spill stats on rescan

Started by Ayush Tiwariabout 1 month ago4 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Ayush Tiwari
ayushtiwari.slg01@gmail.com

Hi,

HashAgg seems to lose the worker's spill stats when Gather is rescanned.
In the example below, Worker 0 spills on the first scan, but EXPLAIN shows
one batch and no disk usage after the second:

CREATE TABLE hashagg_stats AS
SELECT value AS item, value % 10000 AS bucket
FROM generate_series(1, 1000000) AS value;
ALTER TABLE hashagg_stats SET (parallel_workers = 2);
ANALYZE hashagg_stats;

SET max_parallel_workers_per_gather = 2;
SET max_parallel_workers = 1;
SET parallel_leader_participation = off;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SET work_mem = '64kB';
SET hash_mem_multiplier = 2;
SET enable_sort = off;

EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT limits.maximum,
(SELECT count(*)
FROM (SELECT bucket, count(*) FROM hashagg_stats
WHERE item <= (SELECT limits.maximum)
GROUP BY bucket) AS grouped)
FROM (VALUES (1000000), (10)) AS limits(maximum);

AFAICS, ExecEndAgg() clobbers the shared slot on each worker exit, rather
than combining the stats from successive scans.

Patch attached to keep the memory/disk peaks and add the spill batches.
I think we want to count the initial batch only once, as in the
non-parallel case. Otherwise, wouldn't even a non-spilling rescan add
another batch?

Regards,
Ayush

Attachments:

t254264_1
v1-0001-Accumulate-HashAggregate-parallel-worker-stats-across-rescans.patchapplication/octet-stream; name=v1-0001-Accumulate-HashAggregate-parallel-worker-stats-across-rescans.patchDownload+8-4
#2Osama Abdul Qader
osamaabdulqaderpostgres@gmail.com
In reply to: Ayush Tiwari (#1)
Re: HashAggregate loses worker spill stats on rescan

On Sun, 18 Oct 2026 at 22:55, Ayush Tiwari <ayushtiwari.slg01@gmail.com>
wrote:

Hi,

HashAgg seems to lose the worker's spill stats when Gather is rescanned.
In the example below, Worker 0 spills on the first scan, but EXPLAIN shows
one batch and no disk usage after the second:

CREATE TABLE hashagg_stats AS
SELECT value AS item, value % 10000 AS bucket
FROM generate_series(1, 1000000) AS value;
ALTER TABLE hashagg_stats SET (parallel_workers = 2);
ANALYZE hashagg_stats;

SET max_parallel_workers_per_gather = 2;
SET max_parallel_workers = 1;
SET parallel_leader_participation = off;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SET work_mem = '64kB';
SET hash_mem_multiplier = 2;
SET enable_sort = off;

EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT limits.maximum,
(SELECT count(*)
FROM (SELECT bucket, count(*) FROM hashagg_stats
WHERE item <= (SELECT limits.maximum)
GROUP BY bucket) AS grouped)
FROM (VALUES (1000000), (10)) AS limits(maximum);

AFAICS, ExecEndAgg() clobbers the shared slot on each worker exit, rather
than combining the stats from successive scans.

Patch attached to keep the memory/disk peaks and add the spill batches.
I think we want to count the initial batch only once, as in the
non-parallel case. Otherwise, wouldn't even a non-spilling rescan add
another batch?

Regards,
Ayush

Hi Ayush,

Thanks for the report and patch.

I'm going to reproduce the issue and review the changes. I had a couple of
questions about the batch-counting logic:

-

Is the assumption that hash_batches_used is always at least 1 when
ExecEndAgg() records the worker's statistics guaranteed by the
surrounding code?
-

Could the si->hash_batches_used == 0 check be affected by any rescan or
worker-reuse scenario that would cause the initial batch to be counted more
than once?

I'll also check how the existing tests cover parallel HashAggregate rescans
and whether a regression test would be appropriate.
Regards,
Osama Abdul Qader

#3Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Osama Abdul Qader (#2)
Re: HashAggregate loses worker spill stats on rescan

Hi,

On Tue, 20 Oct 2026 at 01:49, Osama Abdul Qader
<osamaabdulqaderpostgres@gmail.com> wrote:

On Sun, 18 Oct 2026 at 22:55, Ayush Tiwari <ayushtiwari.slg01@gmail.com> wrote:

Hi,

HashAgg seems to lose the worker's spill stats when Gather is rescanned.
In the example below, Worker 0 spills on the first scan, but EXPLAIN shows
one batch and no disk usage after the second:

CREATE TABLE hashagg_stats AS
SELECT value AS item, value % 10000 AS bucket
FROM generate_series(1, 1000000) AS value;
ALTER TABLE hashagg_stats SET (parallel_workers = 2);
ANALYZE hashagg_stats;

SET max_parallel_workers_per_gather = 2;
SET max_parallel_workers = 1;
SET parallel_leader_participation = off;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SET work_mem = '64kB';
SET hash_mem_multiplier = 2;
SET enable_sort = off;

EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT limits.maximum,
(SELECT count(*)
FROM (SELECT bucket, count(*) FROM hashagg_stats
WHERE item <= (SELECT limits.maximum)
GROUP BY bucket) AS grouped)
FROM (VALUES (1000000), (10)) AS limits(maximum);

AFAICS, ExecEndAgg() clobbers the shared slot on each worker exit, rather
than combining the stats from successive scans.

Patch attached to keep the memory/disk peaks and add the spill batches.
I think we want to count the initial batch only once, as in the
non-parallel case. Otherwise, wouldn't even a non-spilling rescan add
another batch?

Regards,
Ayush

Is the assumption that hash_batches_used is always at least 1 when
ExecEndAgg() records the worker's statistics guaranteed by the
surrounding code?

AFAICS yes for hashed/mixed aggs. ExecInitAgg() starts it at 1,
hashagg_spill_finish() increments it, and ExecReScanAgg() leaves it alone.

Could the si->hash_batches_used == 0 check be affected by any rescan or
worker-reuse scenario that would cause the initial batch to be counted
more than once?

I don't see one so far. The slot is zeroed by ExecAggInitializeDSM(),
but not on a Gather rescan, so the next worker should just add to the
existing count.

Regards,
Ayush

#4Tatsuya Kawata
kawatatatsuya0913@gmail.com
In reply to: Ayush Tiwari (#3)
Re: HashAggregate loses worker spill stats on rescan

Hi Ayush,

HashAgg seems to lose the worker's spill stats when Gather is rescanned.

I looked at v1, and it LGTM.
I had noticed the same problem and posted a patch in another thread [1]/messages/by-id/CAHza6qfgOQ0Psd5kbYkHn2RCAOfcBMvRohUX3SUptd1kZj6euA@mail.gmail.com,
but as David pointed out, it was a duplicate, so I've withdrawn it. The
two differ only in how the following part is written, and they gave the
same results.

Yours:
+ if (si->hash_batches_used == 0)
+ si->hash_batches_used = node->hash_batches_used;
+ else
+ si->hash_batches_used += node->hash_batches_used - 1;
Mine:
+ si->hash_batches_used = Max(si->hash_batches_used, 1) +
+ node->hash_batches_used - 1;

I wrote it this way because I couldn't find a similar if...else in other
nodes, and it can be written more simply without one. But I think it's
just a matter of taste.

Regards,
Tatsuya Kawata

[1]: /messages/by-id/CAHza6qfgOQ0Psd5kbYkHn2RCAOfcBMvRohUX3SUptd1kZj6euA@mail.gmail.com
/messages/by-id/CAHza6qfgOQ0Psd5kbYkHn2RCAOfcBMvRohUX3SUptd1kZj6euA@mail.gmail.com