-
-
Save AaronCowan/58e51e433fc4c231564f9247214b04f4 to your computer and use it in GitHub Desktop.
Get table change notification for update and insert actions
This file contains hidden or 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 table_update_notify() RETURNS trigger AS $$ | |
DECLARE | |
id bigint; | |
BEGIN | |
IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN | |
id = NEW.id; | |
ELSE | |
id = OLD.id; | |
END IF; | |
PERFORM pg_notify(concat('monitor:',TG_TABLE_NAME),concat('changed:',TG_TABLE_NAME)::text); | |
RETURN NEW; | |
END; | |
$$ LANGUAGE plpgsql; | |
DROP TRIGGER jobs_notify_update ON jobs; | |
CREATE TRIGGER jobs_notify_update AFTER UPDATE ON jobs FOR EACH ROW EXECUTE PROCEDURE table_update_notify(); | |
DROP TRIGGER jobs_notify_insert ON jobs; | |
CREATE TRIGGER jobs_notify_insert AFTER INSERT ON jobs FOR EACH ROW EXECUTE PROCEDURE table_update_notify(); | |
DROP TRIGGER jobs_notify_delete ON jobs; | |
CREATE TRIGGER jobs_notify_delete AFTER DELETE ON jobs FOR EACH ROW EXECUTE PROCEDURE table_update_notify(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment