Re: Optimize LISTEN/NOTIFY

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Joel Jacobson <joel(at)compiler(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Optimize LISTEN/NOTIFY
Date: 2025-10-16 02:54:12
Message-ID: 6F913129-ABEF-4004-AAF3-F22FC3429AE8@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Oct 15, 2025, at 23:36, Joel Jacobson <joel(at)compiler(dot)org> wrote:
>
>> I agree with Tom that GetPendingNotifyChannels() is too heavy and unnecessary.
>>
>> In PreCommit_Notify(), we can maintain a local hash table to record
>> pending nofications’ channel names. dahash also supports hash table in
>> local memory.
>
> I'm confused, I assume you mean "dynahash" since there is no "dahash"
> in the sources? I see dynahash has local-to-a-backend support,
> but I don't see why we would need a hash table for this,
> we just iterate over it once in SignalBackends,
> I think the local list is fine.
>
> The latest version gets rid of GetPendingNotifyChannels()
> and replaces it with the local list pendingNotifyChannels.

Sorry for the typo, Yes, I meant to dynahash” that you have already been using it.

In v18, I see you are building “pendingNotifyChannels” in PreCommit_Notify() with “List”:

```
+ /*
+ * Build list of unique channels for SignalBackends().
+ */
+ pendingNotifyChannels = NIL;
+ foreach_ptr(Notification, n, pendingNotifies->events)
+ {
+ char *channel = n->data;
+
+ /* Add if not already in list */
+ if (!list_member_ptr(pendingNotifyChannels, channel))
+ pendingNotifyChannels = lappend(pendingNotifyChannels, channel);
+ }
```

My suggestion of using dynahah was for the same purpose. Because list_member_ptr() iterates through all list nodes until find the target, so this code is still O(n^2).

Using a hash will make it faster. I used to work on project Concourse [1]. The system is heavily using the LISTEN/NOTIFY mechanism. There would be thousands of channels at runtime. In that case, hash search would be much faster than linear search.

[1] https://github.com/concourse/concourse

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message jian he 2025-10-16 02:56:28 Re: speedup COPY TO for partitioned table.
Previous Message Michael Paquier 2025-10-16 02:36:36 Re: [BUG] temporary file usage report with extended protocol and unnamed portals