postgres.async icon indicating copy to clipboard operation
postgres.async copied to clipboard

LISTEN/NOTIFY Is it possible?

Open txbm opened this issue 9 years ago • 2 comments

Is it possible to execute! a LISTEN statement and have the callback be invoked asynchronously for new values? Or anyway of doing this?

Nice work by the way. Works well for everything else I'm doing.

txbm avatar Oct 04 '16 22:10 txbm

Hi,

short answer: not yet, long answer: yes and no.

The underlying Java library supports LISTEN, and you can access it directly as the object returned by open-db is a Listenable.

However, this hasn't added to the clojure driver (or documented) as the LISTEN/NOTIFY support is not yet reliable in the sense that if the connection is broken/closed due to e.g. a network glitch, the listening stops. The listener has no reliable way of detecting this and resubscribing so the events stop flowing.

The current horrible workaround would be to create a pool with a single connection only, checking out that connection, adding a listener and then running healthchecks on the connection.

alaisi avatar Oct 20 '16 06:10 alaisi

@alaisi Haha, I ended up discovering all that the hard way :) Ended up creating a dedicated LISTEN service that uses psycopg2 as it has more mature support for this feature and it replicates the PG mutations onto a Kafka topic which my Clojure service then consumes from. A bit of a wild work-around for the moment but my attempts to create a reliable LISTEN/NOTIFY setup using Java/Clojure proved fruitless. Anyway, appreciate you taking the time to confirm your experience and I look forward to future support. Thanks again.

txbm avatar Oct 31 '16 21:10 txbm