Re: memory leak in trigger handling (since PG12)

From: Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: memory leak in trigger handling (since PG12)
Date: 2023-05-24 19:49:13
Message-ID: c3ff31ce-622c-81a9-9dcc-dbb27c5eaf9e@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 5/24/23 20:55, Andres Freund wrote:
> Hi,
>
> On 2023-05-24 15:38:41 +0200, Tomas Vondra wrote:
>> I looked at this again, and I think GetPerTupleMemoryContext(estate)
>> might do the trick, see the 0002 part.
>
> Yea, that seems like the right thing here.
>
>
>> Unfortunately it's not much
>> smaller/simpler than just freeing the chunks, because we end up doing
>>
>> oldcxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
>> updatedCols = ExecGetAllUpdatedCols(relinfo, estate);
>> MemoryContextSwitchTo(oldcxt);
>
> We could add a variant of ExecGetAllUpdatedCols that switches the context.
>

Yeah, we could do that. I was thinking about backpatching, and modifying
ExecGetAllUpdatedCols signature would be ABI break, but adding a
variant should be fine.

>
>> and then have to pass updatedCols elsewhere. It's tricky to just switch
>> to the context (e.g. in ExecASUpdateTriggers/ExecARUpdateTriggers), as
>> AfterTriggerSaveEvent allocates other bits of memory too (in a longer
>> lived context).
>
> Hm - on a quick look the allocations in trigger.c itself are done with
> MemoryContextAlloc().
>
> I did find a problematic path, namely that ExecGetChildToRootMap() ends up
> building resultRelInfo->ri_ChildToRootMap in CurrentMemoryContext.
>
> That seems like a flat out bug to me - we can't just store data in a
> ResultRelInfo without ensuring the memory is lives long enough. Nearby places
> like ExecGetRootToChildMap() do make sure to switch to es_query_cxt.
>
>
> Did you see other bits of memory getting allocated in CurrentMemoryContext?
>

No, I simply tried to do the context switch and then gave up when it
crashed on the ExecGetRootToChildMap info. I haven't looked much
further, but you may be right it's the only bit.

It didn't occur to me it might be a bug - I think the code simply
assumes it gets called with suitable memory context, just like we do in
various other places. Maybe it should document the assumption.

>
>> So we'd have to do another switch again. Not sure how
>> backpatch-friendly would that be.
>
> Yea, that's a valid concern. I think it might be reasonable to use something
> like ExecGetAllUpdatedColsCtx() in the backbranches, and switch to a
> short-lived context for the trigger invocations in >= 16.
>

WFM

regards

--
Tomas Vondra
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2023-05-24 19:54:36 Re: memory leak in trigger handling (since PG12)
Previous Message Andres Freund 2023-05-24 18:55:59 Re: memory leak in trigger handling (since PG12)