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 1uK20M-00C0u0-BH for pgsql-hackers@arkaria.postgresql.org; Tue, 27 May 2025 21:38:38 +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 1uK20K-00BQJK-KC for pgsql-hackers@arkaria.postgresql.org; Tue, 27 May 2025 21:38:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uK20K-00BQJC-AY for pgsql-hackers@lists.postgresql.org; Tue, 27 May 2025 21:38:36 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uK20H-000R4f-1L for pgsql-hackers@lists.postgresql.org; Tue, 27 May 2025 21:38:35 +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 54RLcVeP1346941; Tue, 27 May 2025 17:38:31 -0400 From: Tom Lane To: Matheus Alcantara cc: Etsuro Fujita , pgsql-hackers@lists.postgresql.org Subject: Re: Fixing memory leaks in postgres_fdw In-reply-to: References: <2976982.1748049023@sss.pgh.pa.us> <216286.1748202797@sss.pgh.pa.us> <519144.1748217540@sss.pgh.pa.us> <918254.1748288174@sss.pgh.pa.us> Comments: In-reply-to Matheus Alcantara message dated "Tue, 27 May 2025 17:45:17 -0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1346939.1748381911.1@sss.pgh.pa.us> Date: Tue, 27 May 2025 17:38:31 -0400 Message-ID: <1346940.1748381911@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Matheus Alcantara writes: > I think that we can delay the allocation a bit more. The > dblink_security_check just use the rconn to pfree in case of a failure, > so I think that we can remove this parameter and move the rconn > allocation to the next if (connname) block. See attached as an example. Yeah, I thought of that too. But I think the point of the current setup is to ensure we have the rconn block before we create the PGconn object, because if we were to hit OOM after creating the connection, we'd leak the PGconn, which would be quite bad. Having said that, the idea that this sequence is OOM-safe is pretty silly anyway, considering that createNewConnection does a pstrdup, and creates a new hashtable entry which might require enlarging the hashtable, and for that matter might even create the hashtable. So maybe rather than continuing to adhere to a half-baked coding rule, we need to think of some other way to do that. Maybe it'd be reasonable to create a hashtable entry with NULL rconn, and then open the connection? This'd require rejiggering the lookup code to treat a hashtable entry with NULL rconn as not really being there. But there's not too many routines touching that hashtable, so maybe it's not hard. regards, tom lane