Freeing memory in native extension in case of error

From: Gabriel Furstenheim Milerud <furstenheim(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Freeing memory in native extension in case of error
Date: 2017-06-15 20:00:11
Message-ID: CAJN3DWrGq11r4b76KJbvH+-zNjAiGoRa0n35QzaF7EP8tzrniA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,
I've written an extension in C to sum jsonb. For that I use the
jsonbiterator defined in
https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/jsonb_util.c#L743

In the comments of JsonbIteratorNext it states: 'Callers in such a
scenario, that are particularly sensitive to leaking memory in a
long-lived context may walk the ancestral tree from the final iterator
we left them with to its oldest ancestor, pfree()ing as they go'

In the extension that I've written, I sometimes stop in the middle of
an iteration, but only to raise an error (if the json contains a not
numeric value), with 'ereport(ERROR, ...'

Do I have to free the memory in that case? According to this guide to
write extensions
(http://big-elephants.com/2015-10/writing-postgres-extensions-part-i/)
it wouldn't be necessary because 'Memory allocated by palloc will be
freed automatically at the end of each transaction'. So if I
understand it correctly I wouldn't have to.

Thanks

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2017-06-15 20:07:46 Re: Freeing memory in native extension in case of error
Previous Message Igor Korot 2017-06-15 19:02:07 Re: Connection options