Freeing transient memory in aggregate functions

Поиск
Список
Период
Сортировка
От Matt Magoffin
Тема Freeing transient memory in aggregate functions
Дата
Msg-id C1897C9E-665C-4CDA-AE8C-8DA4119E6A12@msqr.us
обсуждение исходный текст
Ответы Re: Freeing transient memory in aggregate functions  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
I have a question about trying to keep memory from growing too much in a C aggregate function with pass-by-reference types. I am trying to keep track of a last-seen value in my aggregate state, so I have code roughly doing this:

Datum current;
MemoryContext aggContext;
AggCheckCallContext(fcinfo, &aggContext);
old = MemoryContextSwitchTo(aggContext);

if (!PG_ARGISNULL(0)) {
  current = PG_GETARG_DATUM(0);
  state->last = datumCopy(&current, typbyval, typlen);
}
MemoryContextSwitchTo(old);

I’m essentially doing a datumCopy() on every non-null input value. I was wondering if there is a way to free the previously copied datum, since I don’t really need it anymore? Something like

if (!PG_ARGISNULL(0)) {
  current = PG_GETARG_DATUM(0);
  if (state->last != NULL) {
    pfree(state->last);
  }
  state->last = datumCopy(&current, typbyval, typlen);
}

I wasn’t sure it was allowed to call pfree() like this. My actual function is dealing with array input values, and for large sets of inputs I didn’t want to grow memory use as large as the entire data set being aggregated.

Kind regards,
Matt

В списке pgsql-general по дате отправления:

Предыдущее
От: Pavel Stehule
Дата:
Сообщение: Re: Packages, inner subprograms, and parameterizable anonymous blocks for PL/pgSQL
Следующее
От: Peter Eisentraut
Дата:
Сообщение: Re: PGBouncer logs explanation required