Skip to content

pgstat: Don't double count parallel worker activity in WAL statistics - #85

Open
lfittl wants to merge 1 commit into
masterfrom
fix-cumulative-wal-statistics-double-counting
Open

lfittl wants to merge 1 commit into
masterfrom
fix-cumulative-wal-statistics-double-counting

Conversation

@lfittl

@lfittl lfittl commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Each parallel worker tracks its own activity in its copy of the global instrumentation counters (pgWalUsage/pgBufferUsage), reports the delta through shared memory when it finishes, and the leader then accumulates that into its copy of the globals (InstrAccumParallelQuery).

Having the same activity in the globals of two processes was acceptable when the globals were only used for statement-level reporting, where the worker globals are effectively ignored. That stopped being true when commit 0146924 started diffing pgWalUsage to report WAL activity to the cumulative statistics system. This caused a bug with parallel workers double reporting their WAL in pg_stat_wal, once via the leader and once via the worker, leading to incorrect data for utility statements like parallel vacuum, and queries that cause pruning or update hint bits, e.g. in a parallel sequential scan. The same bug applies to the per-backend cumulative statistics accessible via pg_stat_get_backend_wal().

To fix, remember the cumulative total of WAL/buffer statistics that we have imported from the workers into the leader, and subtract it when reporting to the cumulative statistics system. That way each worker reports its activity itself, exactly once.

Each parallel worker tracks its own activity in its copy of the global
instrumentation counters (pgWalUsage/pgBufferUsage), reports the delta
through shared memory when it finishes, and the leader then accumulates
that into its copy of the globals (InstrAccumParallelQuery).

Having the same activity in the globals of two processes was acceptable
when the globals were only used for statement-level reporting, where the
worker globals are effectively ignored. That stopped being true when
commit 0146924 started diffing pgWalUsage to report WAL activity to
the cumulative statistics system. This caused a bug with parallel workers
double reporting their WAL in pg_stat_wal, once via the leader and once
via the worker, leading to incorrect data for utility statements like
parallel vacuum, and queries that cause pruning or update hint bits, e.g.
in a parallel sequential scan. The same bug applies to the per-backend
cumulative statistics accessible via pg_stat_get_backend_wal().

To fix, remember the cumulative total of WAL/buffer statistics that we
have imported from the workers into the leader, and subtract it when
reporting to the cumulative statistics system. That way each worker
reports its activity itself, exactly once.

Author: Lukas Fittl <lukas@fittl.com>
Reviewed-by:
Discussion:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant