Thread: Stateful C-language function with state managed by third-party library
Stateful C-language function with state managed by third-party library
From
Denys Rtveliashvili
Date:
Hello,
My function neeeds to call a third-party library which would create a state and then that state should be kept for the duration of the current query. The library can deallocate that state in a correct way.
I understand that fn_extra is normally used for this and usually the state is created in a memory context which is deallocated at the end of the query. So normally it is not an issue. However, I cannot make that library use PostgreSQL utilities for memory management.
I am afraid that for long-running sessions it may cause serious memory leaks if they do not deallocate state correctly and in a timely manner.
Is there a mechanism for adding a finalizer hook which would be called and passed that pointer after the query is complete? Or perhaps there is another mechanism? I looked in the documentation and in the source but I do not see it mentioned.
Thank you.
With kind regards,
Denys Rtveliashvili
Denys Rtveliashvili <rtvd@icloud.com> writes: > My function neeeds to call a third-party library which would create a state and then that state should be kept for theduration of the current query. The library can deallocate that state in a correct way. > I understand that fn_extra is normally used for this and usually the state is created in a memory context which is deallocatedat the end of the query. So normally it is not an issue. However, I cannot make that library use PostgreSQL utilitiesfor memory management. > I am afraid that for long-running sessions it may cause serious memory leaks if they do not deallocate state correctlyand in a timely manner. > Is there a mechanism for adding a finalizer hook which would be called and passed that pointer after the query is complete?Or perhaps there is another mechanism? I looked in the documentation and in the source but I do not see it mentioned. In HEAD, you could use a memory context reset callback for this purpose. I don't believe there's any fully satisfactory solution in the released branches; the closest you could get is an ExprContext callback, which has the fatal-for-this-purpose defect that it's only called on successful query completion, not if an error occurs. regards, tom lane
On Tue, Mar 10, 2015 at 11:54:23AM -0400, Tom Lane wrote: > Denys Rtveliashvili <rtvd@icloud.com> writes: > > My function neeeds to call a third-party library which would create a state and then that state should be kept for theduration of the current query. The library can deallocate that state in a correct way. > > > I understand that fn_extra is normally used for this and usually the state is created in a memory context which is deallocatedat the end of the query. So normally it is not an issue. However, I cannot make that library use PostgreSQL utilitiesfor memory management. > > > I am afraid that for long-running sessions it may cause serious memory leaks if they do not deallocate state correctlyand in a timely manner. > > > Is there a mechanism for adding a finalizer hook which would be called and passed that pointer after the query is complete?Or perhaps there is another mechanism? I looked in the documentation and in the source but I do not see it mentioned. > > In HEAD, you could use a memory context reset callback for this purpose. > > I don't believe there's any fully satisfactory solution in the released > branches; the closest you could get is an ExprContext callback, which > has the fatal-for-this-purpose defect that it's only called on successful > query completion, not if an error occurs. RegisterSubXactCallback() is an established tool for addressing this scenario in released branches. See how plpgsql and postgres_fdw use these xact callbacks to explicitly release special resources.