pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nathan Bossart <nathandbossart@gmail.com>
To: Rahila Syed <rahilasyed90@gmail.com>
Cc: Sami Imseih <samimseih@gmail.com>
Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Andres Freund <andres@anarazel.de>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: Re: Improve LWLock tranche name visibility across backends
Date: Thu, 4 Sep 2025 08:51:21 -0500
Message-ID: <aLmZWcqRiPY7QRi-@nathan> (raw)
In-Reply-To: <CAH2L28uUiyZmFE1=AQ=yyhGsJdsV-NpZycGyuMg=_OUTxhz52g@mail.gmail.com>
References: <CAA5RZ0ve-fHuNYW-ruMwg1y1v7-aCqMm_MiNq1KOdg2Y2-pKDw@mail.gmail.com>
	<aLAFhwpzEwbjiWLG@ip-10-97-1-34.eu-west-3.compute.internal>
	<CAA5RZ0s4ip08Lsk=aYuXGCkBr78jhH=gOau939mhAp-rGuNC3Q@mail.gmail.com>
	<aLCEo0ZMV3vIGRWk@nathan>
	<CAA5RZ0vw-r1qjHw5hUNTzAyCiK3KQ-r+G5ECfWSn+qjdu-fpnw@mail.gmail.com>
	<aLHO-jh-spXWG1pt@nathan>
	<aLJnOnTFSweb2pud@nathan>
	<CAH2L28sz2NcLCS2v1MNkBNUiTiO=-CegFGdAvO24nE+hbjzgjQ@mail.gmail.com>
	<aLcPbyWLawp5_rdt@nathan>
	<CAH2L28uUiyZmFE1=AQ=yyhGsJdsV-NpZycGyuMg=_OUTxhz52g@mail.gmail.com>

On Thu, Sep 04, 2025 at 03:29:52PM +0530, Rahila Syed wrote:
> Since updates to LocalLWLockCounter and LWLockTrancheNames are performed
> while holding the lock, but reading LocalLWLockCounter and then
> LWLockTrancheNames in GetLWTrancheName can occur without acquiring the
> same lock in case trancheID < LocalLWLockCounter, There is a small window
> between updating LocalLWLockCounter and adding the name to
> LWLockTrancheNames. During this window, if GetLWTrancheNames is called,
> it might attempt to access a name in LWLockTrancheNames that hasn't been
> added yet.

We hold the lock when reading/writing the shared-memory LWLockCounter.
There's no need to take a lock when reading the backend-local
LocalLWLockCounter, as it can't be modified by any other process.
Additionally, when adding a new tranche name, we hold the lock the entire
time while we bump the shared-memory counter and copy the name, so there's
no chance another backend will see the counter update but not the name or
attempt to use the same LWLockTrancheNames slot.

-- 
nathan





view thread (107+ messages)  latest in thread

Message-ID: <aLmZWcqRiPY7QRi-@nathan>
Permalink:  ../aLmZWcqRiPY7QRi-@nathan/
Also on:    postgresql.org/message-id/aLmZWcqRiPY7QRi-@nathan

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: nathandbossart@gmail.com, rahilasyed90@gmail.com, samimseih@gmail.com, bertranddrouvot.pg@gmail.com, tgl@sss.pgh.pa.us, andres@anarazel.de
  Subject: Re: Improve LWLock tranche name visibility across backends
  In-Reply-To: <aLmZWcqRiPY7QRi-@nathan>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox