Skip to content

Instantly share code, notes, and snippets.

@AaronCowan
Forked from fritzy/1_triggers.sql
Last active June 16, 2019 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronCowan/58e51e433fc4c231564f9247214b04f4 to your computer and use it in GitHub Desktop.
Save AaronCowan/58e51e433fc4c231564f9247214b04f4 to your computer and use it in GitHub Desktop.
Get table change notification for update and insert actions
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