Re: Optimize LISTEN/NOTIFY

Chao Li <li.evan.chao@gmail.com>

From: Chao Li <li.evan.chao@gmail.com>
To: Joel Jacobson <joel@compiler.org>
Cc: Tom Lane <tgl@sss.pgh.pa.us>, Matheus Alcantara <matheusssilv97@gmail.com>, pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2025-10-08T04:36:35Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Optimize LISTEN/NOTIFY via shared channel map and direct advancement.

  2. Fix incorrect logic for caching ResultRelInfos for triggers


> On Oct 8, 2025, at 11:43, Chao Li <li.evan.chao@gmail.com> wrote:
> 
> 6
> ```
> +static inline void
> +ChannelHashPrepareKey(ChannelHashKey *key, Oid dboid, const char *channel)
> +{
> +	memset(key, 0, sizeof(ChannelHashKey));
> +	key->dboid = dboid;
> +	strlcpy(key->channel, channel, NAMEDATALEN);
> +}
> ```
> 
> Do we really need the memset()? If “channel” is of length NAMEDATALEN, then it still results in a non-0 terminated key->channel; if channel is shorter than NAMEDATALEN, strlcpy will auto add a tailing ‘\0’. I think previous code should have ensured length of channel should be less than NAMEDATALEN.


For comment 6, the result is the same that I don’t think memset() is needed. However, my previous explanation of strlcpy() was wrong, which should apply to strncpy(). For strlcpy(), it already makes a termination ‘\0’.

And one more nit comment:

9
```
+	int			allocated_listeners;	/* Allocated size of array */
```

For “size” here, I guess you meant “length”, though “size” also works, but usually “size” means bytes occupied by an array and “length” means number of elements of an array. So, “length” would be clearer here.

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