Re: Initializing Datums for use with SPI_execute_plan

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Jack Orenstein <jack(dot)orenstein(at)gmail(dot)com>
Cc: Andrew - Supernews <andrew(at)supernews(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: Initializing Datums for use with SPI_execute_plan
Date: 2006-09-20 15:11:56
Message-ID: 20060920151156.GF25362@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Sep 20, 2006 at 11:02:46AM -0400, Jack Orenstein wrote:
> >http://developer.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/mmgr/README?rev=1.9
>
> Thank you, that is a useful document. So let me make sure I understand:
>
> - My C function is invoked in a MessageContext (which you referred to
> in an earlier message as a short-lived context).
>
> - In such a context, I do not need to pfree anything, including memory
> that I palloc myself, and plans returned by SPI_prepare invocations,
> (assuming that I don't hold onto these things past the function
> invocation, e.g. in a static).

In general, any memory you allocate in a function will have been free'd
by the next time you're called. If you want memory to survive (like in
a static) you need to place it in a longer lived context.

The SPI reference has some details about how long various bits of
memory survive.

It's not a garbage collector, everything in a context goes when the
context is reset or deleted. If you still have a pointer there, it'll
be invalid. If you enable debugging stuff it'll clear freed memory so
the error becomes more obvious.

Hope this helps,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2006-09-20 15:12:19 Re: pg_dump output containing CREATE TYPE does not restore with psql
Previous Message Tony Caduto 2006-09-20 15:10:56 Re: postgresql rising