Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v6LvB-003FQA-70 for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Oct 2025 04:37:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1v6Lv6-0077CB-7K for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Oct 2025 04:36:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v6Lv5-0077Bk-PO for pgsql-hackers@lists.postgresql.org; Wed, 08 Oct 2025 04:36:56 +0000 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v6Lv3-000buz-0B for pgsql-hackers@postgresql.org; Wed, 08 Oct 2025 04:36:55 +0000 Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-b632a6b9effso518508a12.1 for ; Tue, 07 Oct 2025 21:36:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759898212; x=1760503012; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=sp9rJ/krAe8MqZaLtP8nM9YwJnrYIHTlmrUm6Y35FCk=; b=PLxWuReYE4+HhEWuvbxMeh9f134VuOfK5vX0CvbkhgTOSgelKEv/Nj1tzyhYkt1H7N 3XGgwuaTr3QEqNcygwWO1Nmv/is+igL+5T34C8WgUuBBLyclVgHPa0JRlCez9UMa7uk8 62EZ1DI0FN/K4D5ETaQGX5Dc4Qr3/w6iSmz4bah/d4U+WYvAxuJeyRh0yYEtOZG7/GH8 lz4+7BN3nVPKvyezyag/RoVFNEQKNhhLAA8JMYd7lMRMe9tnkhcBEtCshfQV0IsVVz71 IoScXo33x2ToFem+1SxjEtfu6Oj4lY/XsPrjk3trEhQaFvmLolKafEmVFBcI5ZY0kZvw dRqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759898212; x=1760503012; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sp9rJ/krAe8MqZaLtP8nM9YwJnrYIHTlmrUm6Y35FCk=; b=HXtejku3it7Ype0Ymi9/CRK3SdveyI3Hf6j66Lf45g2pWeiZbhxced32FAVTXEjWI4 qTNVRXi8Y50DmkxDGRc6hKfL5K53nIGypqs6JXgs1fQV7/CIA2RaO2conD/n3NzFpytF HDG6r4OlFpr2lberlOKxTsPjbHVF+QUZoVt/htOPMcD8VhF3g/YAjMj5v1P3Nmmadpua NprnJYHR/dL/EBr0dezRK686LLcNcUmvJ6SVIKfj3Fa5YkaaZrk5HEerSVoYA1GU3A8N FM77++gDbs8zeZRhE2iZajSiq0t45je2heL/VAwUOvoXab8DWYwmIXA/uD7LvW2Hcvym nzkw== X-Forwarded-Encrypted: i=1; AJvYcCWHS1Xg6rCjLKs4zw4cspV9I5qB++0GvAJzS7/puYk8lzPS+27ZVp7PLcS4N0VkHBmFMIl7DHHaG4wi0xn8@postgresql.org X-Gm-Message-State: AOJu0Yx/uU0r2KP/Nsu+sB+fbNKkaXhHnpW3h4uO6nVEwAn3ZCXk8xtR 995SkgBCinvnzRd5tkLf9LtS37zTEf6hif3y9Vf2e2J1oOysckBgKq3d X-Gm-Gg: ASbGncvwNgD0YH3mvLxdg+6rSGe60QxDwS+wOSVRCS8zZoPeJYnBkoRFB/U0OL3wvfy jCElSYpGQg8xooO9YBAqxixdNeu+fB6ATclMkYpg8dJ+wRRM9XD90u1ob2zGKi7ECxwPArtUT9R XsKHbtY7jdEGMH0pa2JedqouW7qXH8agCSF1Fz2U+kCVuY/vA80EleI9AaqOXCD+YzQ7JpUtSM+ IzrJ4bU/Nwa3RSlcQGSAqnH1gTFN1rtf1PPiQK5SeuZwRnHjAGkoQa4xRmyDwFhR8DmE4jPizE7 FEAXIrmOXvdn9unLD6f9Jmr1hU3Dsn0WuvXalU/TFiJvIDN7+5oVYaPzSRdi1ef5l2AoK/mNFYI +DO/OurUFVUByIiGsI0oApWAVlwVy8PFQT6FYDFEymo9/xhc36TrMhJtp8g== X-Google-Smtp-Source: AGHT+IF7I+FAQVE4S/5wQPx2d4KvOvZCKeVS8G2OMBXGesVpUxx7INsML65QoRNjV4j5TmaiepC1Mw== X-Received: by 2002:a17:902:e94e:b0:270:4aa8:2dcc with SMTP id d9443c01a7336-2902737c5e9mr26669785ad.19.1759898212383; Tue, 07 Oct 2025 21:36:52 -0700 (PDT) Received: from smtpclient.apple ([170.178.170.211]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-28e8d11191bsm183412895ad.11.2025.10.07.21.36.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 Oct 2025 21:36:51 -0700 (PDT) From: Chao Li Message-Id: <6055D998-7333-4094-9BEA-F52EFC5338A9@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_6FC999D7-1FAF-4D66-A579-11F552C6A1DD" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: Optimize LISTEN/NOTIFY Date: Wed, 8 Oct 2025 12:36:35 +0800 In-Reply-To: Cc: Tom Lane , Matheus Alcantara , pgsql-hackers To: Joel Jacobson References: <6899c044-4a82-49be-8117-e6f669765f7e@app.fastmail.com> <165530.1752362320@sss.pgh.pa.us> <02a7cd37-e2fc-4212-8b19-f8c239c95fb8@app.fastmail.com> <96f00bf1-cc9d-4520-9d02-9e14e7767c88@app.fastmail.com> <30c2aa7d-dd6c-4b68-a2e4-f217a1a34acf@app.fastmail.com> <0b4d402a-9ac2-4aa8-acf8-8231dbe579ea@app.fastmail.com> <3095599.1758644879@sss.pgh.pa.us> <0dc6a2cc-5216-4dc1-9dd2-430cafc6095b@app.fastmail.com> <52CC167F-763B-4ECA-B0B4-DAB381816828@gmail.com> <9186C6D0-F7A9-482A-9183-89E530B57E36@gmail.com> <1073593.1759423179@sss.pgh.pa.us> <4bd5e6c4-6fa7-44bb-869d-59a32a331fa8@app.fastmail.com> <85828f29-e72e-4400-94f3-9a69bc8dc239@app.fastmail.com> <2495353.1759860890@sss.pgh.pa.us> <8aeae418-92a6-4bbd-9c06-9574c79e59f7@app.fastmail.com> X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_6FC999D7-1FAF-4D66-A579-11F552C6A1DD Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Oct 8, 2025, at 11:43, Chao Li wrote: >=20 > 6 > ``` > +static inline void > +ChannelHashPrepareKey(ChannelHashKey *key, Oid dboid, const char = *channel) > +{ > + memset(key, 0, sizeof(ChannelHashKey)); > + key->dboid =3D dboid; > + strlcpy(key->channel, channel, NAMEDATALEN); > +} > ``` >=20 > Do we really need the memset()? If =E2=80=9Cchannel=E2=80=9D 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 =E2=80=98\0=E2=80=99. 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=E2=80=99t think = memset() is needed. However, my previous explanation of strlcpy() was = wrong, which should apply to strncpy(). For strlcpy(), it already makes = a termination =E2=80=98\0=E2=80=99. And one more nit comment: 9 ``` + int allocated_listeners; /* Allocated = size of array */ ``` For =E2=80=9Csize=E2=80=9D here, I guess you meant =E2=80=9Clength=E2=80=9D= , though =E2=80=9Csize=E2=80=9D also works, but usually =E2=80=9Csize=E2=80= =9D means bytes occupied by an array and =E2=80=9Clength=E2=80=9D means = number of elements of an array. So, =E2=80=9Clength=E2=80=9D would be = clearer here. -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_6FC999D7-1FAF-4D66-A579-11F552C6A1DD Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

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 =3D dboid;
+ strlcpy(key->channel, channel, = NAMEDATALEN);
+}
```

Do we really need the memset()? If =E2=80=9Cchanne= l=E2=80=9D 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 =E2=80=98\0=E2=80=99. 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=E2=80=99t think memset() is = needed. However, my previous explanation of strlcpy() was wrong, which = should apply to strncpy(). For strlcpy(), it already makes a termination = =E2=80=98\0=E2=80=99.

And one more nit = comment:

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

For =E2=80=9Csize=E2=80=9D = here, I guess you meant =E2=80=9Clength=E2=80=9D, though =E2=80=9Csize=E2=80= =9D also works, but usually =E2=80=9Csize=E2=80=9D means bytes occupied = by an array and =E2=80=9Clength=E2=80=9D means number of elements of an = array. So, =E2=80=9Clength=E2=80=9D would be clearer = here.

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




= --Apple-Mail=_6FC999D7-1FAF-4D66-A579-11F552C6A1DD--