Re: Latches vs lwlock contention

From: Aleksander Alekseev <aleksander(at)timescale(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>, Peter Eisentraut <peter(at)eisentraut(dot)org>, vignesh C <vignesh21(at)gmail(dot)com>, Yura Sokolov <y(dot)sokolov(at)postgrespro(dot)ru>, Andres Freund <andres(at)anarazel(dot)de>
Subject: Re: Latches vs lwlock contention
Date: 2023-07-11 15:11:31
Message-ID: CAJ7c6TMxPGevTBLvZHCmwEpcMkVG=gUQq_7g6+77Nn9gJQQmjw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

> Maybe this is all ok, but it would be good to make the assumptions more
> explicit.

Here are my two cents.

```
static void
SetLatchV(Latch **latches, int nlatches)
{
/* Flush any other changes out to main memory just once. */
pg_memory_barrier();

/* Keep only latches that are not already set, and set them. */
for (int i = 0; i < nlatches; ++i)
{
Latch *latch = latches[i];

if (!latch->is_set)
latch->is_set = true;
else
latches[i] = NULL;
}

pg_memory_barrier();

[...]

void
SetLatches(LatchGroup *group)
{
if (group->size > 0)
{
SetLatchV(group->latches, group->size);

[...]
```

I suspect this API may be error-prone without some additional
comments. The caller (which may be an extension author too) may rely
on the implementation details of SetLatches() / SetLatchV() and use
the returned group->latches[] values e.g. to figure out whether he
attempted to change the state of the given latch. Even worse, one can
mistakenly assume that the result says exactly if the caller was the
one who changed the state of the latch. This being said I see why this
particular implementation was chosen.

I added corresponding comments to SetLatchV() and SetLatches(). Also
the patchset needed a rebase. PFA v4.

It passes `make installcheck-world` on 3 machines of mine: MacOS x64,
Linux x64 and Linux RISC-V.

--
Best regards,
Aleksander Alekseev

Attachment Content-Type Size
v4-0002-Provide-SetLatches-for-batched-deferred-latches.patch application/x-patch 9.6 KB
v4-0003-Use-SetLatches-for-synchronous-replication-wakeup.patch application/x-patch 3.4 KB
v4-0004-Use-SetLatches-for-SERIALIZABLE-DEFERRABLE-wakeup.patch application/x-patch 4.6 KB
v4-0005-Use-SetLatches-for-heavyweight-locks.patch application/x-patch 14.1 KB
v4-0001-Allow-palloc_extended-NO_OOM-in-critical-sections.patch application/x-patch 1.9 KB
v4-0006-Don-t-re-acquire-LockManager-partition-lock-after.patch application/x-patch 6.2 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2023-07-11 15:34:11 Re: Forgive trailing semicolons inside of config files
Previous Message Isaac Morland 2023-07-11 15:04:44 Re: Forgive trailing semicolons inside of config files