postgres/contrib/dblink
Tom Lane 470273da0f 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
2025-05-29 10:39:55 -04:00
..
expected oauth: Disallow OAuth connections via postgres_fdw/dblink 2025-04-29 13:08:24 -07:00
sql oauth: Disallow OAuth connections via postgres_fdw/dblink 2025-04-29 13:08:24 -07:00
t Fix whitespace 2025-05-07 07:01:03 +02:00
.gitignore Support "make check" in contrib 2011-04-25 22:27:11 +03:00
dblink--1.1--1.2.sql Fix parallel-safety markings for contrib/dblink. 2016-06-17 23:08:21 -04:00
dblink--1.2.sql Fix parallel-safety markings for contrib/dblink. 2016-06-17 23:08:21 -04:00
dblink--1.0--1.1.sql Fix typo in update scripts for some contrib modules. 2013-07-19 04:13:01 +09:00
dblink.c Avoid resource leaks when a dblink connection fails. 2025-05-29 10:39:55 -04:00
dblink.control Update dblink extension for parallel query. 2016-06-17 15:18:44 -04:00
Makefile dblink: SCRAM authentication pass-through 2025-03-26 10:49:23 +01:00
meson.build dblink: SCRAM authentication pass-through 2025-03-26 10:49:23 +01:00
pg_service.conf Diagnose incompatible OpenLDAP versions during build and test. 2014-07-22 11:01:03 -04:00