Re: Problem returning strings with pgsql 8.3.x

From: "Josh Tolley" <eggyknap(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Martijn van Oosterhout" <kleptog(at)svana(dot)org>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Problem returning strings with pgsql 8.3.x
Date: 2008-05-14 03:25:41
Message-ID: e7e0a2570805132025m7ef05212udaaf6762ca187a00@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Tue, May 13, 2008 at 8:19 AM, Josh Tolley <eggyknap(at)gmail(dot)com> wrote:
> On Tue, May 13, 2008 at 8:01 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > Martijn van Oosterhout <kleptog(at)svana(dot)org> writes:
> > > On Mon, May 12, 2008 at 11:23:17PM -0600, Josh Tolley wrote:
> > >> SPI_push();
> > >> retval =
> > >> InputFunctionCall(&flinfo, lolVarGetString(returnVal, true),
> > >> resultTypeIOParam, -1);
> > >> SPI_pop();
> >
> > > Won't this cause the return value to be allocated inside a new memory
> > > block which gets freeds at the SPI_pop?
> >
> > The SPI_pop in itself is harmless ... the problem is the SPI_finish
> > further down, which will release all simple palloc's done within the
> > SPI function context. What he needs is something comparable to this bit
> > in plpgsql:
> >
> > /*
> > * If the function's return type isn't by value, copy the value
> > * into upper executor memory context.
> > */
> > if (!fcinfo->isnull && !func->fn_retbyval)
> > {
> > Size len;
> > void *tmp;
> >
> > len = datumGetSize(estate.retval, false, func->fn_rettyplen);
> > tmp = SPI_palloc(len);
> > memcpy(tmp, DatumGetPointer(estate.retval), len);
> > estate.retval = PointerGetDatum(tmp);
> > }
> >
> > ie, push the data into something allocated with SPI_palloc().
>
> I'll give this a shot as soon as I can... many thanks
>
>
> > I would bet large amounts of money that the problem is not "new in
> > 8.3.0", either. Perhaps Josh was not testing in an --enable-cassert
> > (CLOBBER_FREED_MEMORY) build before.
>
> I'll check... that's definitely not unlikely. Again, thanks.
>
> - Josh
>

Proper (I hope) use of SPI_palloc() took care of this. And yes, the
8.2.x version I was using without problem was compiled without
enable-cassert. Once again, thanks.

- Josh / eggyknap

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vyacheslav Kalinin 2008-05-14 04:00:19 Re: Couple of question on functions
Previous Message Chris 2008-05-14 03:04:20 Re: Strange behaviour - performance decreases after each TRUNCATE

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2008-05-14 03:52:36 Re: libpq object hooks
Previous Message Andrew Dunstan 2008-05-14 03:13:28 Re: libpq object hooks