Re: Fix cumulative WAL statistics double-counting from parallel workers

From: Lukas Fittl <lukas(at)fittl(dot)com>
To: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
Cc: shihao zhong <zhong950419(at)gmail(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, Andres Freund <andres(at)anarazel(dot)de>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Ahmed Gouda <gouda0x(at)gmail(dot)com>, Bernd Reiß <bd_reiss(at)gmx(dot)at>
Subject: Re: Fix cumulative WAL statistics double-counting from parallel workers
Date: 2026-11-10 22:47:56
Message-ID: CAP53Pkxo=TT3iOXxinGGrQEozt1Ai+NjdAy7uWrPkjCJK6LzRg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Mon, Nov 9, 2026 at 8:14 PM Bertrand Drouvot
<bertranddrouvot(dot)pg(at)gmail(dot)com> wrote:
> On Mon, Nov 09, 2026 at 09:51:13PM -0500, shihao zhong wrote:
> > Reading v1, I think the pgstat_backend.c part makes the leader drop
> > the WAL of its workers from pg_stat_get_backend_wal(). A worker drops
> > its own backend stats entry when it exits. So on master the leader
> > entry is the only place that counts that WAL, and it counts it once.
> >
> > I checked it with your parallel VACUUM case. On master I get
> >
> > actual_wal_bytes | pg_stat_wal_bytes | leader_backend_wal_bytes
> > 95926320 | 128524537 | 97581712
> >
> > With v1 I get
> >
> > actual_wal_bytes | pg_stat_wal_bytes | leader_backend_wal_bytes
> > 97900592 | 97581712 | 66638887
> >
> > So pg_stat_wal is right with v1, but the leader entry lost about
> > 31MB. Is leader only intended here, to match pg_stat_get_backend_io()?
>
> Yeah, I think so. The documentation says that pg_stat_get_backend_wal() reports
> statistics about the backend with the specified process ID, so worker's WAL should
> not be attributed to the leader.
>
> So I think the pgstat_backend.c changes should stay, but the commit message
> should probably clarify that this part fixes attribution rather than double
> counting.

Yeah - this is a bit of an odd case, since in practice you won't be
able to call pg_stat_get_backend_wal() on a worker, since the workers
are too short-lived.

I'm leaning towards Bertrand's point to leave this as is, so we match
what the documentation already says, and clarify in the commit
message.

In the future we could invent a mechanism to get aggregated parallel
worker activity for a given leader PID in a separate statistics kind.
An updated patch I'm drafting for stack-based instrumentation keeps
the worker imported statistics cleanly separated (in the per-query
Instrumentation struct), so it would be an easy extension on top of
that.

Thanks,
Lukas

--
Lukas Fittl

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message João Rito 2026-11-10 22:51:50 Re: pg_upgrade fails when FK constraint with same name exists on partitioned table and its partition
Previous Message Melanie Plageman 2026-11-10 22:25:21 Re: WAL_LOG CREATE DATABASE strategy broken for non-standard page layouts