I’m trying to create a system in Postgres where each client can create its own subscriptions via listen + notify + triggers. I.e. the client will listen to a channel, and then create a trigger which runs notify on that channel when its conditions have been met. The issue is that I want Postgres to clean up properly in case of improper client termination (e.g. the remote client process dies). To be more specific, I want that trigger which is calling the notify to be removed as there is no longer a listener anyways. How can I accomplish this?
I’ve thought about having a table to map triggers to client ids and then using that to remove triggers where the client is gone, but it seems like a not so great solution.
2
Answers
I found an answer to this in another question: Drop trigger/function at end of session in PostgreSQL?
That’s not how it works.
CREATE TRIGGER
requires that you either own the table or have theTRIGGER
privilege on it (which nobody in their right mind will give you, because it enables you to run arbitrary code in their name). Moreover,CREATE TRIGGER
requires anSHARE ROW EXCLUSIVE
lock on the table andDROP TRIGGER
requires anACCESS EXCLUSIVE
lock, which can be disruptive.Create a single trigger and keep that around.