public inbox for [email protected]
help / color / mirror / Atom feedFrom: Chao Li <[email protected]>
To: Joel Jacobson <[email protected]>
Cc: Tom Lane <[email protected]>
Cc: Matheus Alcantara <[email protected]>
Cc: pgsql-hackers <[email protected]>
Subject: Re: Optimize LISTEN/NOTIFY
Date: Thu, 9 Oct 2025 16:39:43 +0800
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<CAK80=jhmE40KVqQ3ho37MArS7cAED1p9m7uikDxcnDmqdW7t8A@mail.gmail.com>
<[email protected]>
<[email protected]>
<CA+hUKGLrMGkWDB0cwTa0RqD+AF7O-Ywgck8aVYKwOQnZgYRRug@mail.gmail.com>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<CAFY6G8dap-bCnAnMG-2Gzew8yv2Vbi9gsx9+yszKMmd57ygfvA@mail.gmail.com>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
> On Oct 9, 2025, at 16:07, Joel Jacobson <[email protected]> wrote:
>
>>>> ```
>>>> /*
>>>> @@ -1865,6 +2087,7 @@ asyncQueueReadAllNotifications(void)
>>>> LWLockAcquire(NotifyQueueLock, LW_SHARED);
>>>> /* Assert checks that we have a valid state entry */
>>>> Assert(MyProcPid == QUEUE_BACKEND_PID(MyProcNumber));
>>>> + QUEUE_BACKEND_WAKEUP_PENDING(MyProcNumber) = false;
>>>> ```
>>>>
>>>> This piece of code originally only read the shared memory, so it can
>>>> use LW_SHARED lock mode, but now it writes to the shared memory, do we
>>>> need to change the lock mode to “exclusive”?
>>>
>>> No, LW_SHARED is sufficient here, since the backend only modifies its own state,
>>> and no other backend could do that, without holding an exclusive lock.
>>
>> Yes, the backend only modifies its own state to “false”, but other
>> backends may set its state to “true”, that is a race condition. So I
>> still think an exclusive lock is needed.
>
> No, other backends cannot alter our state without holding an exclusive lock,
> and they cannot obtain an exclusive lock on our backend until we've released
> the shared lock we're holding.
>
Ah… That’s true. This comment is resolved.
>>>>
>>
>> The hash function channel_hash_func() is defined by your own code, it
>> use strnlen() to get length of channel name, so that bytes after ‘\0’
>> won’t be used.
>
> No, the hash function is not used for comparison.
> We're using the default dshash_memcmp for comparison:
>
> ```
> /* parameters for the channel hash table */
> static const dshash_parameters channelDSHParams = {
> sizeof(ChannelHashKey),
> sizeof(ChannelEntry),
> dshash_memcmp,
> channelHashFunc,
> dshash_memcpy,
> LWTRANCHE_NOTIFY_CHANNEL_HASH
> };
> ```
>
> Looking at its implementation, we can see it's using memcmp under the hood:
>
> ```
> /*
> * A compare function that forwards to memcmp.
> */
> int
> dshash_memcmp(const void *a, const void *b, size_t size, void *arg)
> {
> return memcmp(a, b, size);
> }
> ```
>
> Here, the input parameter `size` comes from `sizeof(ChannelHashKey)`,
> so it will include all bytes in the comparison.
>
Okay, I think I misunderstood hash_function. So, this comment is also resolved.
I am thinking loudly. When a hash key is created, it has been memset to 0, meaning that in key->channel, all bytes after ‘\0’ are also 0, there should not be any random bytes in hash key, so that in channelHashFunc(), we don’t need to to use strnlen() anymore, which improves performance a little bit. Like this:
h = DatumGetUInt32(hash_uint32(k->dboid));
h ^= DatumGetUInt32(hash_any((const unsigned char *) k->channel,
sizeof(k->channel)));
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
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: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected]
Subject: Re: Optimize LISTEN/NOTIFY
In-Reply-To: <[email protected]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox