Re: How to manage shared library lifetime through C functions

From: Seref Arikan <serefarikan(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: How to manage shared library lifetime through C functions
Date: 2014-08-04 10:31:11
Message-ID: CA+4Thdp1yheVB6geLg8npF-=U-mwUjQs0QsW9M_mLKJ_NL-YGg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thanks a lot Heikki and Albe. Exactly what I was asking for.
Heikki: the libraries are written in languages that have their own runtime
and their documentation insists that both init and dispose calls are
performed when used from C. PG_init() and proc_exit sounds spot on.

Any ideas about keeping some data at session level between calls? Both
calls of the same function and different C functions. (though temp table is
always there as an option)

Best regards
Seref

On Mon, Aug 4, 2014 at 11:17 AM, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com
> wrote:

> On 08/04/2014 12:54 PM, Seref Arikan wrote:
>
>> Greetings,
>> I hope this is the right group to ask this question; apologies if this
>> should go the general or some other list.
>>
>> I have multiple shared libraries that can be called from C that I'd like
>> to
>> use from a C based postgresql function.
>>
>> These libraries perform some expensive initialization and they require the
>> C code to properly release resources when the library is no longer needed.
>>
>> This means that I need a mechanism to keep a session level pointer to a
>> library, initialize it when it is called first from a C based function and
>> dispose the library properly when the session ends (and terminated due to
>> a
>> problem) I would like to keep the libraries available as long as the
>> session is alive, so multiple calls are supposed to avoid
>> initialization/disposal costs every time.
>>
>> I could probably use a temp table as a container for the initalization and
>> even pointer values (sounds dirty) but I have no idea how to hook to
>> session end to clean up when session ends.
>>
>> What would be a good strategy here?
>>
>
> Define a function called _PG_init() in your C extension. PostgreSQL will
> call it once, when the library is loaded into the backend. (The time it's
> loaded will depend on whether the library is listed in
> shared_preload_libraries, local_preload_libraries, or neither.)
>
> Are you sure you need to do any cleanup? When the session ends, the
> backend process will terminate, which will close any open files and release
> memory that the library might be holding. If that's not enough, and the
> library really needs to do more complicated clean up, then you can register
> a callback with on_proc_exit().
>
> Look at the C extensions in the PostgreSQL source tree's contrib directory
> for examples of _PG_init() and on_proc_exit().
>
> - Heikki
>
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Shigeru Hanada 2014-08-04 10:36:16 Re: Optimization for updating foreign tables in Postgres FDW
Previous Message Heikki Linnakangas 2014-08-04 10:17:17 Re: How to manage shared library lifetime through C functions