Fantastic! That solved this problem.


However I still get a crash  if I change: 


is_even to bool num->is_even = ((base_num + i) % 2 == 0) ? true : false; 
retvals[1] = BoolGetDatum(list->pp_numbers[call_cntr]->is_even); CREATE OR 
REPLACE FUNCTION  pg_srf(OUT value integer, OUT is_even bit) RETURNS        
SETOF recordAS  'pg_srf.so', 'pg_srf'LANGUAGE  CSTRICTIMMUTABLE;


 
> Date: Sun, 15 Mar 2015 17:03:38 +0100
> From: and...@2ndquadrant.com
> To: it...@outlook.com
> CC: pgsql-hackers@postgresql.org
> Subject: Re: [HACKERS] Crash on SRF execution
> 
> 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
> 
> 
> -- 
> Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
                                          

Reply via email to