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