pgsql: Fix memory leakage in nodeSubplan.c.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix memory leakage in nodeSubplan.c.
Date: 2025-09-10 20:15:13
Message-ID: E1uwRDl-0004MN-0o@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix memory leakage in nodeSubplan.c.

If the hash functions used for hashing tuples leaked any memory,
we failed to clean that up, resulting in query-lifespan memory
leakage in queries using hashed subplans. One way that could
happen is if the values being hashed require de-toasting, since
most of our hash functions don't trouble to clean up de-toasted
inputs.

Prior to commit bf6c614a2, this leakage was largely masked
because TupleHashTableMatch would reset hashtable->tempcxt
(via execTuplesMatch). But it doesn't do that anymore, and
that's not really the right place for this anyway: doing it
there could reset the tempcxt many times per hash lookup,
or not at all. Instead put reset calls into ExecHashSubPlan
and buildSubPlanHash. Along the way to that, rearrange
ExecHashSubPlan so that there's just one place to call
MemoryContextReset instead of several.

This amounts to accepting the de-facto API spec that the caller
of the TupleHashTable routines is responsible for resetting the
tempcxt adequately often. Although the other callers seem to
get this right, it was not documented anywhere, so add a comment
about it.

Bug: #19040
Reported-by: Haiyang Li <mohen(dot)lhy(at)alibaba-inc(dot)com>
Author: Haiyang Li <mohen(dot)lhy(at)alibaba-inc(dot)com>
Reviewed-by: Fei Changhong <feichanghong(at)qq(dot)com>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/19040-c9b6073ef814f48c@postgresql.org
Backpatch-through: 13

Branch
------
REL_13_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/8b6c29afd1254481a63e08e89bcefcde1c8d5418

Modified Files
--------------
src/backend/executor/execGrouping.c | 6 ++++
src/backend/executor/nodeSubplan.c | 70 ++++++++++++++-----------------------
2 files changed, 33 insertions(+), 43 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2025-09-10 21:51:31 pgsql: Avoid faulty alignment of Datums in build_sorted_items().
Previous Message Nathan Bossart 2025-09-10 16:22:33 pgsql: meson: Build numeric.c with -ftree-vectorize.