Q: Executing functions at connect/disconnect? - Mailing list pgsql-general

From Mattias Kregert
Subject Q: Executing functions at connect/disconnect?
Date
Msg-id 002a01c34177$ccf926e0$09000a0a@kregert.se
Whole thread Raw
Responses Re: Q: Executing functions at connect/disconnect?  (Darko Prenosil <darko.prenosil@finteh.hr>)
List pgsql-general
Is there a way to automagically execute a function at connect/disconnect?
I was thinking about the possibility to have some kind of automatic cleanup when the client disconnects.

I would like to:
- Register a session id at connect time. (INSERT into sessions(sessId, user, host, ...) ...)
- Use the session id to mark some objects as "in use" (UPDATE tbl SET usedBy=<sessionid> WHERE...).
- Automatically update all rows with this session-id as "unused" when the client disconnects from the backend.

I can not trust the client to exit cleanly all the time, so the cleanup function must be executed by the backend when
theconnection is lost. The client could exit nicely or crash or have a power failure or whatever. I don't expect bad
thingsto happen very often, but if/when they do i want to be sure that no rows are "in use". 

Any ideas?

/Mattias


pgsql-general by date:

Previous
From: "scott.marlowe"
Date:
Subject: Re: ERROR: Relation "pg_user" does not exist
Next
From: "Bruno BAGUETTE"
Date:
Subject: [REPOST] Problem for dumping a 6.5.2 database