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 1tXhgo-00D1mo-OV for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Jan 2025 14:14:43 +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 1tXhgm-0010Cz-KY for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Jan 2025 14:14:41 +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 1tXhgm-0010Cq-CA for pgsql-bugs@lists.postgresql.org; Tue, 14 Jan 2025 14:14:40 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tXhgj-000MiJ-1E for pgsql-bugs@postgresql.org; Tue, 14 Jan 2025 14:14:37 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 50EEEY1s678866; Tue, 14 Jan 2025 09:14:34 -0500 From: Tom Lane To: Daniel Gustafsson cc: lxiaogang5 , pgsql-bugs Subject: Re: There is a defect in the ReplicationSlotCreate() function where it iterates through ReplicationSlotCtl->replication_slots[max_replication_slots] to find a slot but does not break out of the loop when a slot is found. In-reply-to: References: Comments: In-reply-to Daniel Gustafsson message dated "Tue, 14 Jan 2025 11:57:34 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <678864.1736864074.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 14 Jan 2025 09:14:34 -0500 Message-ID: <678865.1736864074@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Daniel Gustafsson writes: > On 14 Jan 2025, at 05:24, lxiaogang5 wrote: >> There is a logical defect in the function ReplicationSlotCreate (creati= ng a new slot) when it internally traverses the ReplicationSlotCtl->replic= ation_slots[max_replication_slots] array. When an available slot is found = (slot =3D s), it should break the current for loop. This issue still exist= s 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 o= ther > performance problems as well (max_replication_slots is by default 10). Are we reading the same code? for (i =3D 0; i < max_replication_slots; i++) { ReplicationSlot *s =3D &ReplicationSlotCtl->replication_slots[i]; if (s->in_use && strcmp(name, NameStr(s->data.name)) =3D=3D 0) ereport(ERROR, (errcode(ERRCODE_DUPLICATE_OBJECT), errmsg("replication slot \"%s\" already exists", name= ))); if (!s->in_use && slot =3D=3D NULL) slot =3D s; } In the first place, breaking early would be wrong: we would fail to scan all slots to check for duplicate name. In the second place, the code does choose the first not-in-use slot, because of the check for "slot =3D=3D NULL" before changing the value of "slot". If we intended to support hundreds or thousands of replication slots, it'd perhaps be worthwhile to replace this with a hashtable lookup. But that doesn't seem like a very credible use-case. For typical numbers of slots this way is likely faster than messing with a hashtable. regards, tom lane