]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Avoid resource leaks when a dblink connection fails.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 May 2025 14:39:55 +0000 (10:39 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 May 2025 14:39:55 +0000 (10:39 -0400)
commit8eef55db13fe5b14202729e893eac86ec66ef00d
treefe573a07975c37d8409b614f081b2133426eb9ce
parent7e8b44f4e0e630971d912628f07a38a7d3a6fae8
Avoid resource leaks when a dblink connection fails.

If we hit out-of-memory between creating the PGconn and inserting
it into dblink's hashtable, we'd lose track of the PGconn, which
is quite bad since it represents a live connection to a remote DB.
Fix by rearranging things so that we create the hashtable entry
first.

Also reduce the number of states we have to deal with by getting rid
of the separately-allocated remoteConn object, instead allocating it
in-line in the hashtable entries.  (That incidentally removes a
session-lifespan memory leak observed in the regression tests.)

There is an apparently-irreducible remaining OOM hazard, which
is that if the connection fails at the libpq level (ie it's
CONNECTION_BAD) then we have to pstrdup the PGconn's error message
before we can release it, and theoretically that could fail.  However,
in such cases we're only leaking memory not a live remote connection,
so I'm not convinced that it's worth sweating over.

This is a pretty low-probability failure mode of course, but losing
a live connection seems bad enough to justify back-patching.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Discussion: https://postgr.es/m/1346940.1748381911@sss.pgh.pa.us
Backpatch-through: 13
contrib/dblink/dblink.c