Memoize shows only the last execution's stats for parallel workers

Started by Tatsuya Kawataabout 1 month ago4 messageshackers
Jump to latest
#1Tatsuya Kawata
kawatatatsuya0913@gmail.com

Hi,

I noticed this while reviewing the patch in [1]/messages/by-id/CAApHDvoUWCms-b27MpDKd5_RUFtAnVrxPgN9u3QoQbBihk+S6g@mail.gmail.com.

When a Gather (or Gather Merge) node is rescanned, a new set of workers
is started, and they write their Memoize stats into the same shared
memory slots as the previous ones. ExecEndMemoize() does this with
memcpy(), so EXPLAIN ANALYZE shows the per-worker Hits / Misses etc.
only for the last execution. Since the "actual rows" and "loops" of the
same worker are accumulated across all executions, the two don't match.

=== Example

CREATE TABLE mz_a (x int);
CREATE TABLE mz_b (y int);
INSERT INTO mz_a SELECT g % 10 FROM generate_series(1, 1000) g;
INSERT INTO mz_b SELECT g FROM generate_series(1, 1000) g;
CREATE INDEX ON mz_b (y);
ANALYZE mz_a, mz_b;
ALTER TABLE mz_a SET (parallel_workers = 2);

SET enable_hashjoin = off;
SET enable_mergejoin = off;
SET enable_material = off;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SET max_parallel_workers_per_gather = 2;
SET parallel_leader_participation = off;
SET max_parallel_workers = 1; -- so that Worker 0 runs both executions

EXPLAIN (ANALYZE, VERBOSE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT o.z, (SELECT count(*) + o.z FROM mz_a a JOIN mz_b b ON b.y = a.x)
FROM (VALUES (1), (2)) o(z);

=== Output on master

Hits + Misses (1000) doesn't match the loops (2000), and
Misses (10) doesn't match the loops of the inner Index Only Scan (20):

-> Memoize (actual rows=0.90 loops=2000)
...
Worker 0: actual rows=0.90 loops=2000
Hits: 990 Misses: 10 Evictions: 0 Overflows: 0 Memory Usage:
1kB
-> Index Only Scan using mz_b_y_idx on public.mz_b b (actual
rows=0.90 loops=20)
...
Worker 0: actual rows=0.90 loops=20

=== With the patch

Hits: 1980 Misses: 20 Evictions: 0 Overflows: 0 Memory Usage: 1kB

The attached patch accumulates the stats instead of using memcpy(), as
is already done for Parallel Bitmap Heap Scan (5a1e6df3b84). For Memory
Usage, it keeps the maximum.

Regards,
Tatsuya Kawata

[1]: /messages/by-id/CAApHDvoUWCms-b27MpDKd5_RUFtAnVrxPgN9u3QoQbBihk+S6g@mail.gmail.com
/messages/by-id/CAApHDvoUWCms-b27MpDKd5_RUFtAnVrxPgN9u3QoQbBihk+S6g@mail.gmail.com

Attachments:

t254151_1
v1-0001-Accumulate-Memoize-parallel-worker-stats-across-r.patchapplication/octet-stream; name=v1-0001-Accumulate-Memoize-parallel-worker-stats-across-r.patchDownload+13-2
#2David Rowley
dgrowleyml@gmail.com
In reply to: Tatsuya Kawata (#1)
Re: Memoize shows only the last execution's stats for parallel workers

On Mon, 12 Oct 2026 at 19:36, Tatsuya Kawata
<kawatatatsuya0913@gmail.com> wrote:

=== Output on master
Hits: 990 Misses: 10 Evictions: 0 Overflows: 0 Memory Usage: 1kB
=== With the patch
Hits: 1980 Misses: 20 Evictions: 0 Overflows: 0 Memory Usage: 1kB

The attached patch accumulates the stats instead of using memcpy(), as
is already done for Parallel Bitmap Heap Scan (5a1e6df3b84). For Memory
Usage, it keeps the maximum.

Yeah, I also noticed this when working on the patch you mentioned.
I'd say this one should be backpatched since it's misreporting.

I'll look into doing that once the tags for v19rc1 are applied.

David

#3David Rowley
dgrowleyml@gmail.com
In reply to: David Rowley (#2)
Re: Memoize shows only the last execution's stats for parallel workers

On Mon, 12 Oct 2026 at 20:16, David Rowley <dgrowleyml@gmail.com> wrote:

On Mon, 12 Oct 2026 at 19:36, Tatsuya Kawata

The attached patch accumulates the stats instead of using memcpy(), as
is already done for Parallel Bitmap Heap Scan (5a1e6df3b84). For Memory
Usage, it keeps the maximum.

I'll look into doing that once the tags for v19rc1 are applied.

RC1 tags are now applied, so I've pushed this and backpatched to v14.

David

#4Tatsuya Kawata
kawatatatsuya0913@gmail.com
In reply to: David Rowley (#3)
Re: Memoize shows only the last execution's stats for parallel workers

Hi David,

RC1 tags are now applied, so I've pushed this and backpatched to v14.

Thank you for pushing and backpatching!

Regards,
Tatsuya Kawata