]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix low-probability leaks of PGresult objects in the backend.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 15 Jun 2017 19:03:39 +0000 (15:03 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 15 Jun 2017 19:03:39 +0000 (15:03 -0400)
commite6fee616d47f2ce661ccba4c663f54af41548698
treec7c19dfd67185d19d19a0e2581670d1c714bfdd4
parent1fe10463d9bd38d20b5c3c9356fc07ae9c3b6828
Fix low-probability leaks of PGresult objects in the backend.

We had three occurrences of essentially the same coding pattern
wherein we tried to retrieve a query result from a libpq connection
without blocking.  In the case where PQconsumeInput failed (typically
indicating a lost connection), all three loops simply gave up and
returned, forgetting to clear any previously-collected PGresult
object.  Since those are malloc'd not palloc'd, the oversight results
in a process-lifespan memory leak.

One instance, in libpqwalreceiver, is of little significance because
the walreceiver process would just quit anyway if its connection fails.
But we might as well fix it.

The other two instances, in postgres_fdw, are somewhat more worrisome
because at least in principle the scenario could be repeated, allowing
the amount of memory leaked to build up to something worth worrying
about.  Moreover, in these cases the loops contain CHECK_FOR_INTERRUPTS
calls, as well as other calls that could potentially elog(ERROR),
providing another way to exit without having cleared the PGresult.
Here we need to add PG_TRY logic similar to what exists in quite a
few other places in postgres_fdw.

Coverity noted the libpqwalreceiver bug; I found the other two cases
by checking all calls of PQconsumeInput.

Back-patch to all supported versions as appropriate (9.2 lacks
postgres_fdw, so this is really quite unexciting for that branch).

Discussion: https://postgr.es/m/22620.1497486981@sss.pgh.pa.us
src/backend/replication/libpqwalreceiver/libpqwalreceiver.c