Postgresql init and cleanup module functions

Started by Yan Pasover 8 years ago3 messagesgeneral
Jump to latest
#1Yan Pas
yanp.bugz@gmail.com

High!

I'm writing C postgresql module with some psql-functions and global state.
Do module API provide any init and cleanup functions? If yes then it would
be fine to see them in "35.9. C-Language Functions" help page."

Regards, Yan

#2Andres Freund
andres@anarazel.de
In reply to: Yan Pas (#1)
Re: Postgresql init and cleanup module functions

On 2017-09-05 20:15:57 +0300, Yan Pas wrote:

I'm writing C postgresql module with some psql-functions and global state.
Do module API provide any init and cleanup functions? If yes then it would
be fine to see them in "35.9. C-Language Functions" help page."

The relevant doc page is at
https://www.postgresql.org/docs/current/static/xfunc-c.html

what you're looking for is _PG_init(). There effectively is no cleanup
logic, as modules cannot be unloaded anymore.

- Andres

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

#3Yan Pas
yanp.bugz@gmail.com
In reply to: Andres Freund (#2)
Re: Postgresql init and cleanup module functions

Sorry, replied only to direct sender, duplicating to both sender and
mailing list
*---*
*MSG1:*
Thanks you, that is what I was looking for!

I was testing sample module and found it's behavior odd:
1. functions have few permissions, they even were not able to create file
in "/tmp" directory. At least I'm able to use pglog (ereport(LOG,
(errmsg("init")));)
2. _PG_init() is called separately for each connection right before any of
the module's function is called by the client, so there are many instances
of the module
3. For cleaning up I've use trick with standard 'atexit' function, called
inside _PG_init() and it works properly
4. Global object's constructor and destructor are called on module unload.

I thought it works like "dlopen on postgres start, dlclose on postgres
shutdown". I think I should stick to background worker process which will
provide some C-Postgres functions
*---*
*MSG2:*
After reading bg workers' manual it seems to me that they cannot provide
postgres functions. So the only way to achieve ONE global state and
postgres functions using this state is to use some IPC between background
process and dynamicaly loadable module (e.g. Unix/TCP sockets)

Regards, Yan