Tom, thanks again for the quick answer and
sorry for the lame question about memor allocation.

I hope this is acceptable:
Is there a way to make PG_DETOAST_DATUM and friends allocate
memory in a custom memory context ?

Right now I'm DETOASTing, memcopying in a custom context
and pfreeing the DETOASTed datum, I'd like to avoid one
copy.

TIA.
--strk;

Mon, 27 Oct 2003 18:23:12 +0100
strk <strk@keybit.net>
DETOASTing in custom memory context
 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 ?

NOTE that if I MemoryContextAllocate in fcinfo->flinfo->fn_mcxt and
memcopy DETOASTED geom, everything works (ar at least it seems to)

--strk;

strk wrote:
> Tom, thanks again for the quick answer and
> sorry for the lame question about memor allocation.
> 
> I hope this is acceptable:
> Is there a way to make PG_DETOAST_DATUM and friends allocate
> memory in a custom memory context ?
> 
> Right now I'm DETOASTing, memcopying in a custom context
> and pfreeing the DETOASTed datum, I'd like to avoid one
> copy.
> 
> TIA.
> --strk;
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if your
>       joining column's datatypes do not match

Mon, 27 Oct 2003 19:57:04 +0100
strk <strk@keybit.net>
Re: DETOASTing in custom memory context
strk <strk@keybit.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.

			regards, tom lane

Mon, 27 Oct 2003 14:13:11 -0500
Tom Lane <tgl@sss.pgh.pa.us>
Re: DETOASTing in custom memory context 
tgl wrote:
> strk <strk@keybit.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;


Mon, 27 Oct 2003 21:29:41 +0100
strk <strk@keybit.net>
Re: DETOASTing in custom memory context