Yes, it's this piece of code. Thank you for your patient explanation. What you said makes a lot of sense. Even after obtaining the available slots, we still need to traverse and check if there are any duplicate slots.


------------------ Original ------------------
From: Daniel Gustafsson <daniel@yesql.se>
Date: Tue,Jan 14,2025 6:57 PM
To: lxiaogang5 <lxiaogang5@gmail.com>
Cc: pgsql-bugs <pgsql-bugs@postgresql.org>
Subject: Re: There is a defect in the ReplicationSlotCreate() function whereit iterates throughReplicationSlotCtl->replication_slots[max_replication_slots] to find a slotbut does not break out of the loop when a slot is found.

> On 14 Jan 2025, at 05:24, lxiaogang5 <lxiaogang5@gmail.com> wrote:

> There is a logical defect in the function ReplicationSlotCreate (creating a new slot) when it internally traverses the ReplicationSlotCtl->replication_slots[max_replication_slots] array. When an available slot is found (slot = s), it should break the current for loop. This issue still exists in the latest code, resulting in wasted CPU resources.

We could exit early, but any system which max_replication_slots set high enough
that the savings are measurable in a non-hot codepath is likely having other
performance problems as well (max_replication_slots is by default 10).

If you feel strongly about I suggest creating a patch and submitting.  Also,
please don't send 6Mb images of text when simply copy/paste of the text would
have worked even better.

--
Daniel Gustafsson