On 11/19/2012 08:41 PM, matt@xxxxxxxxxx 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 -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general