[GENERAL] Freeing memory in native extension in case of error

Поиск
Список
Период
Сортировка
От Gabriel Furstenheim Milerud
Тема [GENERAL] Freeing memory in native extension in case of error
Дата
Msg-id CAJN3DWrGq11r4b76KJbvH+-zNjAiGoRa0n35QzaF7EP8tzrniA@mail.gmail.com
обсуждение исходный текст
Ответы Re: [GENERAL] Freeing memory in native extension in case of error
Список 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 по дате отправления:

Предыдущее
От: Igor Korot
Дата:
Сообщение: Re: [GENERAL] Connection options
Следующее
От: Tom Lane
Дата:
Сообщение: Re: [GENERAL] Freeing memory in native extension in case of error