Re: [PATCH] bufmgr: tighten LWLock:BufferMapping on InvalidateBuffer

From: Alexandre Felipe <o(dot)alexandre(dot)felipe(at)gmail(dot)com>
To: Yuhang Qiu <iamqyh(at)gmail(dot)com>
Cc: Andres Freund <andres(at)anarazel(dot)de>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] bufmgr: tighten LWLock:BufferMapping on InvalidateBuffer
Date: 2026-08-24 08:16:28
Message-ID: CAE8JnxMv=Di26-XYhVGTcpEUKRaygvcdQMbTx8noa=poYkUjFA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thank you Yuhang,

That is correct under the (very reasonable) assumption that a backend will
not hold more than one shared lock of the LWLock.

I also studied the current LWLock the implementation and there is room to
make the no-contention path faster. Is that something worth doing,
or is it pointless?

Regards,
Alexandre

On Mon, Aug 24, 2026 at 8:23 AM Yuhang Qiu <iamqyh(at)gmail(dot)com> wrote:

> > Is it undocumented feature, or something that should be fixed?
>
>
> ```C
> #define MAX_BACKENDS_BITS 18
> #define MAX_BACKENDS ((1U << MAX_BACKENDS_BITS)-1)
>
> /* assumes MAX_BACKENDS is a (power of 2) - 1, checked below */
> #define LW_VAL_EXCLUSIVE (MAX_BACKENDS + 1)
> #define LW_VAL_SHARED 1
>
> /* already (power of 2)-1, i.e. suitable for a mask */
> #define LW_SHARED_MASK MAX_BACKENDS
> #define LW_LOCK_MASK (MAX_BACKENDS | LW_VAL_EXCLUSIVE)
> ```
>
> It's documented, not very prominently - see the lwlock.c file header.
> LW_VAL_EXCLUSIVE is MAX_BACKENDS + 1, so overflowing needs MAX_BACKENDS + 1
> shared holders, and InitializeMaxBackends() won't let MaxBackends get that
> big.
>
> So there's nothing to fix here.
>
> Best Regards,
> Yuhang Qiu.
>
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Richard Guo 2026-08-24 08:18:32 Re: Fix CPU cost of right-semi and right-anti hash joins
Previous Message Nick Ivanov 2026-08-24 07:56:41 Re: Possible race condition in pg_basebackup