[GENERAL] Freeing memory in native extension in case of error - Mailing list pgsql-general

From Gabriel Furstenheim Milerud
Subject [GENERAL] Freeing memory in native extension in case of error
Date
Msg-id CAJN3DWrGq11r4b76KJbvH+-zNjAiGoRa0n35QzaF7EP8tzrniA@mail.gmail.com
Whole thread Raw
Responses Re: [GENERAL] Freeing memory in native extension in case of error  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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


pgsql-general by date:

Previous
From: Igor Korot
Date:
Subject: Re: [GENERAL] Connection options
Next
From: Tom Lane
Date:
Subject: Re: [GENERAL] Freeing memory in native extension in case of error