pgsql: Use single LWLock for lock statistics in pgstats

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Use single LWLock for lock statistics in pgstats
Date: 2026-04-06 05:03:25
Message-ID: E1w9c7Q-003BZI-1q@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Use single LWLock for lock statistics in pgstats

Previously, one LWLock was used for each lock type, adding complexity
without an observable performance benefit as data is gathered only for
paths involving lock waits, at least currently. This commit replaces
the per-type set of LWLocks with a single LWLock protecting the stats
data of all the lock types, like the stats kinds for SLRU or WAL. A
good chunk of the callbacks get simpler thanks to this change.

The previous approach also had one bug in the flush callback when nowait
was called with "true": a backend iterating over all entries could
successfully flush some entries while skipping others due to contention,
then unconditionally reset the pending data. This would cause some
stats data loss.

Oversight in 4019f725f5d4.

Reported-by: Tomas Vondra <tomas(at)vondra(dot)me>
Author: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
Discussion: https://postgr.es/m/1af63e6d-16d5-4d5b-9b03-11472ef1adf9@vondra.me

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/404a17c155ac6e80d990fb0625569a3854f7090d

Modified Files
--------------
src/backend/utils/activity/pgstat_lock.c | 86 +++++++++++---------------------
src/include/utils/pgstat_internal.h | 7 +--
2 files changed, 31 insertions(+), 62 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2026-04-06 05:41:04 Re: pgsql: Convert all remaining subsystems to use the new shmem allocation
Previous Message Michael Paquier 2026-04-06 04:23:39 pgsql: Improve more stability of worker_spi termination test