On Sun, Mar 08, 2020 at 04:30:44PM -0400, Tom Lane wrote: > BTW, another thing I noticed while looking around is that some of > the functions using SRF_RETURN_DONE() think they should clean up > memory beforehand. This is a waste of code/cycles, as long as the > memory was properly allocated in funcctx->multi_call_memory_ctx, > because funcapi.c takes care of deleting that context. > > We should probably document that *any* manual cleanup before > SRF_RETURN_DONE() is an antipattern. If you have to have cleanup, > it needs to be done via RegisterExprContextCallback instead.
This part appears to be already in place since e4186762ffaa4188e16702e8f4f299ea70988b96: |The memory context that is current when the SRF is called is a transient |context that will be cleared between calls. This means that you do not need to |call pfree on everything you allocated using palloc; it will go away anyway. |However, if you want to allocate any data structures to live across calls, you |need to put them somewhere else. The memory context referenced by |multi_call_memory_ctx is a suitable location for any data that needs to survive |until the SRF is finished running. In most cases, this means that you should |switch into multi_call_memory_ctx while doing the first-call setup. -- Justin