Revision 2 was not so quick, but also not so dirty. Here's my first draft.
I have modified:
- Version number of Pgtcl changed to 1.4 (to reflect the additional command)
- added a command pg_on_connection_loss
Syntax: pg_on_connection_loss conn ?callback?
Use no callback parameter to query the actuall callback script and
with parameter to set/change callback. The callback is evaluated
whenever the connection to the backend gets lost (eof condition ?).
I do this by checking for a false return value from PQconsumeInput.
Today I did some quick testing, no segfaults, no errors. Broken backends
are detected both in idle connections and when you're in a COPY command
(these are the two possibilities, where PQconsumeInput is called).
I'm using the same readable fileevent on the database connection as
pg_listen does internally. The structure Pg_ConnectionId was extended by
two fields : char *conn_loss to store the callback and Tcl_Interp
*interp to store the interpreter, in which context the callback will be
evaluated.
Can someone with more experience on Tcl event programming take a look at
this, I mananged to write some Tcl extensions, but am new to modifying
the Tcl event-loop. (what I did, was copying most of the pg_listen stuff).
Basically PgStartNotifyEventSource() is called, when a callback proc is
supplied to pg_on_connection_loss, which (unmodified) does install a
readable fileevent on the db connection, then I check for false return
value of PQconsumeInput and add a ConnLossEvent type to the event queue.
(pg_)listen callbacks are deleted immediately from the the event queue
when the db connection is broken, just like in the unpatch version. What
This a point worth to discuss: Which commands will dump core when used
in the disconnect handler ? pg_disconnect works, but the best thing
might be to invoke pg_disconnect as the first command within the
disconnect handler per default (in C code, not in Tcl) so possible
subsequent operations on the just closed connection would at least
trigger a "... is not a valid postgresql connection" error, but not run
into any segfaults.
I will do some more testing tomorrow, but since I only can post via
google at work, I'd like to get some feedback more quickly.
Gerhard
PS: I need my patch also for an older version of Postgresql (7.0.2), can
I copy the whole libpgtcl directory into the 7.0.2 source path without
any risk ?
--
Gerhard Hintermayer
http://www.inode.at/g.hintermayer