Re: Crash on SRF execution

From: Itai <itaid(at)outlook(dot)com>
To: Andres Freund <andres(at)2ndquadrant(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Crash on SRF execution
Date: 2015-03-15 16:20:21
Message-ID: DUB127-W258A25F846E680772795B0AD050@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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: andres(at)2ndquadrant(dot)com
> To: itaid(at)outlook(dot)com
> CC: pgsql-hackers(at)postgresql(dot)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(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2015-03-15 16:39:34 Re: Patch: raise default for max_wal_segments to 1GB
Previous Message Andres Freund 2015-03-15 16:03:38 Re: Crash on SRF execution