Re: Crash on SRF execution

Поиск
Список
Период
Сортировка
От Andres Freund
Тема Re: Crash on SRF execution
Дата
Msg-id 20150315160338.GC9324@alap3.anarazel.de
обсуждение исходный текст
Ответ на Re: Crash on SRF execution  (Itai <itaid@outlook.com>)
Ответы Re: Crash on SRF execution  (Itai <itaid@outlook.com>)
Список pgsql-hackers
Hi,

On 2015-03-15 17:59:39 +0200, Itai wrote:
> Thanks for the quick response!! :)
> But I don't get it... isn't:
> if (SRF_IS_FIRSTCALL()){
> }
> the iterator one time
> "initialization block" where I setup the data to be iterated
> upon? 
> 
> Can you please elaborate on how should I fix this? I'm probably missing something basic...

> > >  if (SRF_IS_FIRSTCALL())
> > >  {
> > >   length = 4000;
> > >   base_num = 1203000000;
> > >   list = (NumberList *)palloc(sizeof(NumberList));
> > >   list->pp_numbers = (Number **)palloc(sizeof(Number*) * length);

You allocate memory in the per call context here.

> > >   list->length = length;
> > >   i = 0;
> > >   for (; i < length; i++)
> > >   {
> > >    num = (Number *)palloc(sizeof(Number));
> > >    num->value = base_num + i;
> > >    num->is_even = ((base_num + i) % 2 == 0) ? 1 : 0;
> > >    list->pp_numbers[i] = num;
> > >   }
> > >   ereport(INFO, (errmsg("----------- data source -----------")));
> > >   i = 0;
> > >   for (; i < length; i++)
> > >   {
> > >    ereport(INFO, (errmsg("value: %d", list->pp_numbers[i]->value)));
> > >    ereport(INFO, (errmsg("is_even: %d", list->pp_numbers[i]->is_even)));   
> > >   }
> > >   
> > >   funcctx = SRF_FIRSTCALL_INIT();
> > >   oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

Because you only switch the memory context here. Move this up, to the
beginning of the SRF_IS_FIRSTCALL block. Before the palloc()s.

Greetings,

Andres Freund

-- Andres Freund                       http://www.2ndQuadrant.com/PostgreSQL Development, 24x7 Support, Training &
Services



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

Предыдущее
От: Itai
Дата:
Сообщение: Re: Crash on SRF execution
Следующее
От: Itai
Дата:
Сообщение: Re: Crash on SRF execution