[HACKERS] Question about toasting code - Mailing list pgsql-hackers

From Mat Arye
Subject [HACKERS] Question about toasting code
Date
Msg-id CADsUR0B=jVvXtPpFr9kXS6EH8duHzXAwBzg7CLxJW=ycJciFqA@mail.gmail.com
Whole thread Raw
Responses Re: [HACKERS] Question about toasting code  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hi,

I am trying to create a custom aggregate and have run across some puzzling code while trying to figure out how to implement it.

This is in arrayfuncs.c:5022 (postgre 9.6.2) 

/*
* Ensure pass-by-ref stuff is copied into mcontext; and detoast it too if
* it's varlena.  (You might think that detoasting is not needed here
* because construct_md_array can detoast the array elements later.
* However, we must not let construct_md_array modify the ArrayBuildState
* because that would mean array_agg_finalfn damages its input, which is
* verboten.  Also, this way frequently saves one copying step.)
*/
if (!disnull && !astate->typbyval)
{
if (astate->typlen == -1)
dvalue = PointerGetDatum(PG_DETOAST_DATUM_COPY(dvalue));
else
dvalue = datumCopy(dvalue, astate->typbyval, astate->typlen);
}

I am a bit confused by the comment.

Does PG_DETOAST_DATUM_COPY(dvalue) modify dvalue? Shouldn't that  not modify the value (implied by _COPY)? If so then why does the detoasting have to happen ahead of time and cannot happen at a later stage (in the finalfunc or serializefunc etc.)? I understand that at those later stages you cannot modify the input, but why would you have to in order to DETOAST?

The thing I am struggling with is with the serialize/deserialize functions. Can I detoast inside the serialize function if I use _COPY? Or is there a reason I have to detoast during the sfunc?


Thanks,
Mat
TimescaleDB 
 





pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: [HACKERS] statement_timeout is not working as expected with postgres_fdw
Next
From: Daniele Varrazzo
Date:
Subject: [HACKERS] Detecting schema changes during logical replication