Re: DETOASTing in custom memory context

From: strk <strk(at)keybit(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: DETOASTing in custom memory context
Date: 2003-10-27 20:29:41
Message-ID: 20031027212941.E51468@freek.keybit.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

tgl wrote:
> strk <strk(at)keybit(dot)net> writes:
> >> From whitin an aggregate sfunc I did:
> > oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
> > geom = (GEOMETRY *)PG_DETOAST_DATUM(datum);
> > MemoryContextSwitchTo(oldcontext);
>
> > And later in aggregate's finalfunc:
> > pfree(geom);
>
> > Result:
> > segfault!
>
> > What's wrong with it ?
>
> Perhaps you wanted PG_DETOAST_DATUM_COPY(). Or possibly use
> PG_FREE_IF_COPY() rather than an unconditional pfree, though
> that would depend on just what your usage pattern is.

Sure, how did I miss that !
PG_FREE_IF_COPY is unapplicable here since pfree() call is in a
different function that the one DETOASTING it (finalfunc and sfunc
respectively of an aggregate), but PG_DETOAST_DATUM_COPY() did at
least force a copy and thus the context-switch...

thanks,
--strk;

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Jan Wieck 2003-10-27 20:31:39 Re: Vacuum thoughts
Previous Message Jan Wieck 2003-10-27 19:57:31 Re: Foreign Key bug -- 7.4b4