> ISTM that your proposal will actually use more memory because > pstate->p_sourcetext does not get free'd, but we must now allocate > space for a new "cleaned" query.
I'm not sure that I understand. Sure we allocate a new temporary buffer for the cleaned up query string but this will be freed soon. The query string stored in the prepared statement will stay in memory as long as the prepare statement exist so this any cleanup can actually save a lot of memory.
My point is pstate->p_sourcetext doesn't get freed just because we're not referencing it from CachedPlanSource in prepared_queries. Instead, with multi-statement strings, prepared_queries now use a newly allocated string, new_query, which will be around until DEALLOCATE.
So this patch always increases memory usage for multi-statement strings; we have both the original multi-statement string and the cleaned up copy around.