Re: Fixing memory leaks in postgres_fdw

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Matheus Alcantara <matheusssilv97(at)gmail(dot)com>
Cc: Etsuro Fujita <etsuro(dot)fujita(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Fixing memory leaks in postgres_fdw
Date: 2025-05-27 21:38:31
Message-ID: 1346940.1748381911@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Matheus Alcantara <matheusssilv97(at)gmail(dot)com> 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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-05-27 21:49:08 Re: Non-reproducible AIO failure
Previous Message Bruce Momjian 2025-05-27 21:35:11 Re: PG 18 release notes draft committed