[HACKERS] How to manage shared library lifetime through C functions

2014-08-04 Thread Seref Arikan
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?

Regards
Seref


Re: [HACKERS] How to manage shared library lifetime through C functions

2014-08-04 Thread Seref Arikan
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 hlinnakan...@vmware.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




Re: [HACKERS] How to manage shared library lifetime through C functions

2014-08-04 Thread Seref Arikan
Hi,


On Mon, Aug 4, 2014 at 11:58 AM, Heikki Linnakangas hlinnakan...@vmware.com
 wrote:

 On 08/04/2014 01:31 PM, Seref Arikan wrote:

 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.


 You might also consider creating your own PL handler for those languages.
 Or google around if one exists already.

That was what I checked first, but unfortunately none exists at the moment.
I really want to write one,which would probably be a much cleaner way of
doing what I'm going to do, but time pressure does not let me.
At the moment the lowest hanging fruit is to pass values through C
functions to these libraries and get the results back; hence, my questions.



  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)


 You can use a global variable in the C extension. If you need to allocate
 memory that survives across function calls, use 
 MemoryContextSwitchTo(TopMemoryContext);
 ... = palloc(...). I'm sure you'll find examples of that in the existing
 extensions too.


Thanks, I'll check this out as well.


 - Heikki




[HACKERS] Detailed documentation for external calls (threading, shared resources etc)

2011-06-12 Thread Seref Arikan
Greetings,
This is actually a request for documentation guidance. I intend to
develop an extension to postgresql. Basically I'd like to place calls
to network using ZeroMQ, and I need to have detailed information about
a lot of things, especially  threading issues. I need to have some
global resources which will be presumably used by  multiple threads.
I can see that there is a lot of documentation, but I'd really
appreciate pointers towards the books, or key documents that'd help me
move forward faster (docs/books about inner workings of key
functionality) I'll be using C (most likely the best option) to
develop code, so which books/documents would you recommend?

Cheers
Seref

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers