Re: Maintaining state across function calls - Mailing list pgsql-general

From Craig Ringer
Subject Re: Maintaining state across function calls
Date
Msg-id 50AA341E.8090909@2ndQuadrant.com
Whole thread Raw
In response to Maintaining state across function calls  (matt@byrney.com)
Responses Re: Maintaining state across function calls  (matt@byrney.com)
List pgsql-general
On 11/19/2012 08:41 PM, matt@byrney.com wrote:
> I want to process all the records in a table through a C-language (well,
> C++) function (i.e. one function call per row of the table) in such a way
> that the function hangs onto its internal state across calls.  Something
> like
>
> SELECT my_function(a, b, c) FROM my_table ORDER BY d;
>
> The value returned in the last row of the table would be the result I'm
> looking for.  (This could be neatened up by using a custom aggregate and
> putting my calculation in the sfunc but that's a minor detail).
[snip]
> Alternatively, use this in a custom aggregate and make the ffunc do the
> garbage collection, which should prevents leakage altogether.
You don't generally need to do this cleanup yourself. Use appropriate
palloc memory contexts and it'll be done for you when the memory context
is destroyed.

I would want to implement this as an aggregate using the standard
aggregate / window function machinery. Have a look at how the existing
aggregates like string_agg are implemented in the Pg source code.

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



pgsql-general by date:

Previous
From: Yvon Thoraval
Date:
Subject: Re: remote connection refused
Next
From: matt@byrney.com
Date:
Subject: Re: Maintaining state across function calls