Re: Stateful C-language function with state managed by third-party library - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Stateful C-language function with state managed by third-party library
Date
Msg-id 16590.1426002863@sss.pgh.pa.us
Whole thread Raw
In response to Stateful C-language function with state managed by third-party library  (Denys Rtveliashvili <rtvd@icloud.com>)
Responses Re: Stateful C-language function with state managed by third-party library  (Noah Misch <noah@leadboat.com>)
List pgsql-hackers
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



pgsql-hackers by date:

Previous
From: Jim Nasby
Date:
Subject: Re: proposal: searching in array function - array_position
Next
From: Pavel Stehule
Date:
Subject: Re: proposal: disallow operator "=>" and use it for named parameters