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

From matt@byrney.com
Subject Re: Maintaining state across function calls
Date
Msg-id 5493c927d901b76d5973db3d6ad89db3.squirrel@localhost
Whole thread Raw
In response to Re: Maintaining state across function calls  (Craig Ringer <craig@2ndQuadrant.com>)
Responses Re: Maintaining state across function calls
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.

Thanks for your reply.  A follow-up question: to use the palloc/pfree
functions with a C++ STL container, do I simply give the container an
allocator which uses palloc and pfree instead of the default allocator,
which uses new and delete?

Matt


pgsql-general by date:

Previous
From: Craig Ringer
Date:
Subject: Re: Maintaining state across function calls
Next
From: "Welty, Richard"
Date:
Subject: Re: Experiences with pl/Java