pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Create infrastructure to reliably prevent leakage of PGresults.
Date: Fri, 25 Jul 2025 20:42:52 +0000
Message-ID: <E1ufPFk-000j9E-0C@gemulon.postgresql.org> (raw)

Create infrastructure to reliably prevent leakage of PGresults.

Commit 232d8caea fixed a case where postgres_fdw could lose track
of a PGresult object, resulting in a process-lifespan memory leak.
But I have little faith that there aren't other potential PGresult
leakages, now or in future, in the backend modules that use libpq.
Therefore, this patch proposes infrastructure that makes all
PGresults returned from libpq act as though they are palloc'd
in the CurrentMemoryContext (with the option to relocate them to
another context later).  This should greatly reduce the risk of
careless leaks, and it also permits removal of a bunch of code
that attempted to prevent such leaks via PG_TRY blocks.

This patch adds infrastructure that wraps each PGresult in a
"libpqsrv_PGresult" that provides a memory context reset callback
to PQclear the PGresult.  Code using this abstraction is inherently
memory-safe to the same extent as we are accustomed to in most backend
code.  Furthermore, we add some macros that automatically redirect
calls of the libpq functions concerned with PGresults to use this
infrastructure, so that almost no source-code changes are needed to
wheel this infrastructure into place in all the backend code that
uses libpq.

Perhaps in future we could create similar infrastructure for
PGconn objects, but there seems less need for that.

This patch just creates the infrastructure and makes relevant code
use it, including reverting 232d8caea in favor of this mechanism.
A good deal of follow-on simplification is possible now that we don't
have to be so cautious about freeing PGresults, but I'll put that in
a separate patch.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Discussion: https://postgr.es/m/2976982.1748049023@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7d8f5957792421ec3bb9d1b9b6ca25d689d974b7

Modified Files
--------------
contrib/postgres_fdw/postgres_fdw.c     |  36 ++---
contrib/postgres_fdw/postgres_fdw.h     |   2 +-
src/backend/utils/mmgr/mcxt.c           |  39 ++++-
src/include/libpq/libpq-be-fe-helpers.h |  27 ++--
src/include/libpq/libpq-be-fe.h         | 259 ++++++++++++++++++++++++++++++++
src/include/utils/palloc.h              |   2 +
src/tools/pgindent/typedefs.list        |   1 +
7 files changed, 322 insertions(+), 44 deletions(-)



Message-ID: <E1ufPFk-000j9E-0C@gemulon.postgresql.org>
Permalink:  ../E1ufPFk-000j9E-0C@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1ufPFk-000j9E-0C@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Create infrastructure to reliably prevent leakage of PGresults.
  In-Reply-To: <E1ufPFk-000j9E-0C@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox