Re: [PATCH] Fix memory leak in pgstat_progress_parallel_incr_param()

From: "Tristan Partin" <tristan(at)partin(dot)io>
To: "Baji Shaik" <baji(dot)pgdev(at)gmail(dot)com>
Cc: <bertranddrouvot(dot)pg(at)gmail(dot)com>, <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Fix memory leak in pgstat_progress_parallel_incr_param()
Date: 2026-06-05 21:29:46
Message-ID: DJ1FOVN3MWNT.1ZQ7NHQAVJPXY@partin.io
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri Jun 5, 2026 at 5:44 PM UTC, Baji Shaik wrote:
> Hi,
>
> While running parallel vacuum with track_cost_delay_timing=on, I
> noticed memory in the parallel worker process keeps growing
> proportionally to vacuum runtime, and is never reclaimed until the
> worker exits.
>
> I think pgstat_progress_parallel_incr_param() (backend_progress.c)
> leaks memory on every call from a parallel worker.
>
> The suspected block:
>
> static StringInfoData progress_message;
> initStringInfo(&progress_message); /* palloc -> A */
> pq_beginmessage(&progress_message, PqMsg_Progress);
> /* pq_beginmessage internally calls initStringInfo again ->
> palloc -> B, A is orphaned */
> pq_sendint32(&progress_message, index);
> pq_sendint64(&progress_message, incr);
> pq_endmessage(&progress_message); /* pfree(B), A leaked
> */
>
> So one palloc(~1 kB) leaks per call, into the per-worker context.
>
> This is an oversight of f1889729dd3 ("Add new parallel message type
> to progress reporting"); track_cost_delay_timing just makes it more
> visible. With that GUC enabled, a long-running parallel vacuum leaks
> megabytes per worker (~232 MB observed in a 43-min vacuum at default
> settings on a 15M-row, 30-index workload).
>
> The proposed fix is in the attached patch which does a one-time init of the
> static
> buffer, then pq_beginmessage_reuse() / pq_endmessage_reuse() so the
> buffer is allocated once and reused.

Hey Baji,

This looks pretty reasonable to me. Nice find. Did you think about
keeping the code path as is and just removing the first initStringInfo()
call? Removing the allocation per progress message seems like a good
idea to me. Maybe you could separate this change into two patches. One
to fix the memory leak and another to remove the allocation per message.
A committer could then decide for themselves if the second patch is
worth committing.

--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeff Davis 2026-06-05 21:34:45 Re: dict_synonym.c: fix truncation of multibyte sequence
Previous Message Jacob Champion 2026-06-05 21:16:20 Re: Fix OAuth validator docs for error_detail on internal errors