Re: how to correctly react on exception in pfree function? - Mailing list pgsql-hackers

From Julien Rouhaud
Subject Re: how to correctly react on exception in pfree function?
Date
Msg-id Y0dw9eB4J0UIdmjr@jrouhaud
Whole thread Raw
In response to Re: how to correctly react on exception in pfree function?  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: how to correctly react on exception in pfree function?
List pgsql-hackers
On Wed, Oct 12, 2022 at 07:24:53PM -0400, Tom Lane wrote:
> Pavel Stehule <pavel.stehule@gmail.com> writes:
> > I had a talk with Julien about the correct handling of an exception raised
> > by pfree function.
> 
> > Currently, this exception (elog(ERROR, "could not find block containing
> > chunk %p", chunk);) is not specially handled ever.
> 
> There are hundreds, if not thousands, of "shouldn't ever happen" elogs
> in Postgres.  We don't make any attempt to trap any of them.  Why do
> you think this one should be different?

Because session variables are allocated in a persistent memory context, so
there's a code doing something like this to implement LET variable:

[...]
oldctxt = MemoryContextSwitchTo(SomePersistentContext);
newval = palloc(...);
MemoryContextSwitchTo(oldctxt);
/* No error should happen after that point or we leak memory */
pfree(var->val);
var->val = newval;
return;

Any error thrown in pfree would mean leaking memory forever in that backend.

Is it ok to leak memory in such should-not-happen case or should there be some
safeguard?



pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: Issue in GIN fast-insert: XLogBeginInsert + Read/LockBuffer ordering
Next
From: Tom Lane
Date:
Subject: Re: how to correctly react on exception in pfree function?