-
-
Save akotlov/475506e6e27525d3d40401fb6ebce7de to your computer and use it in GitHub Desktop.
I used this trigger to notify table changes via NOTIFY (migrating off RethinkDB)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION notify_trigger() RETURNS trigger AS $$ | |
DECLARE | |
channel_name varchar DEFAULT (TG_TABLE_NAME || '_changes'); | |
BEGIN | |
IF TG_OP = 'INSERT' THEN | |
PERFORM pg_notify(channel_name, '{"id": "' || NEW.id || '"}'); | |
RETURN NEW; | |
END IF; | |
IF TG_OP = 'DELETE' THEN | |
PERFORM pg_notify(channel_name, '{"id": "' || OLD.id || '"}'); | |
RETURN OLD; | |
END IF; | |
IF TG_OP = 'UPDATE' THEN | |
PERFORM pg_notify(channel_name, '{"id": "' || NEW.id || '"}'); | |
RETURN NEW; | |
END IF; | |
END; | |
$$ LANGUAGE plpgsql; | |
DROP TRIGGER IF EXISTS users_changes_trigger on users; | |
CREATE TRIGGER users_changes_trigger AFTER INSERT OR UPDATE OR DELETE ON users FOR EACH ROW EXECUTE PROCEDURE notify_trigger(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment