pgsql: Fix memory leak in _gin_parallel_merge

From: Tomas Vondra <tomas(dot)vondra(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix memory leak in _gin_parallel_merge
Date: 2025-05-02 21:06:40
Message-ID: E1uAxai-000g1c-2g@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix memory leak in _gin_parallel_merge

To insert the merged GIN entries in _gin_parallel_merge, the leader
calls ginEntryInsert(). This may allocate memory, e.g. for a new leaf
tuple. This was allocated in the PortalContext, and kept until the end
of the index build. For most GIN indexes the amount of leaked memory is
negligible, but for custom opclasses with large keys it may cause OOMs.

Fixed by calling ginEntryInsert() in a temporary memory context, reset
after each insert. Other ginEntryInsert() callers do this too, except
that the context is reset after batches of inserts. More frequent resets
don't seem to hurt performance, it may even help it a bit.

Report and fix by Vinod Sridharan.

Author: Vinod Sridharan <vsridh90(at)gmail(dot)com>
Reviewed-by: Tomas Vondra <tomas(at)vondra(dot)me>
Discussion: https://postgr.es/m/CAFMdLD4p0VBd8JG=Nbi=BKv6rzFAiGJ_sXSFrw-2tNmNZFO5Kg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/1681a70df3d68b6f9dc82645f97f8d4668edc42f

Modified Files
--------------
src/backend/access/gin/gininsert.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Geoghegan 2025-05-02 21:51:35 pgsql: Avoid treating nonrequired nbtree keys as required.
Previous Message Tom Lane 2025-05-02 20:16:32 pgsql: Don't use a tuplestore if we don't have to for SQL-language func