Re: pg_stat_statements: Avoid holding excessive lock

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Karina Litskevich <litskevichkarina(at)gmail(dot)com>
Cc: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: pg_stat_statements: Avoid holding excessive lock
Date: 2024-11-07 08:16:51
Message-ID: Zyx3c_dUmiGEN1Wh@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Nov 05, 2024 at 08:37:08PM +0300, Karina Litskevich wrote:
> I suggest eliminating holding the excessive lock. See the attached patch.
> This would also restore the consistency between the code and the comments
> about entry's mutex spinlock usage.

You are right. minmax_stats_since and stats_since are only set when
an entry is allocated or reset, so this is not going to matter.

> + /*
> + * There is no need to hold entry->mutex when reading stats_since and
> + * minmax_stats_since for (unlike counters) they are always written
> + * while holding pgss->lock exclusively. We are holding pgss->lock
> + * shared so there should be no race here.
> + */
> stats_since = entry->stats_since;
> minmax_stats_since = entry->minmax_stats_since;
> - SpinLockRelease(&entry->mutex);

The comment could be simpler, say a "The spinlock is not required when
reading these two as they are always updated when holding pgss->lock
exclusively.". Or something like that.
--
Michael

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrey M. Borodin 2024-11-07 08:34:14 Re: UUID v7
Previous Message Dave Page 2024-11-07 08:16:14 Re: doc: pgevent.dll location