Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Forked from raphox/mmanager_trigger.sql
Created June 4, 2020 12:10
Show Gist options
  • Save jgcmarins/63e03ce8be039a7264ec73426d7cf917 to your computer and use it in GitHub Desktop.
Save jgcmarins/63e03ce8be039a7264ec73426d7cf917 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION notify_rows_changes()
RETURNS trigger AS $$
DECLARE
record JSON;
BEGIN
record := row_to_json(CASE TG_OP
WHEN 'INSERT' THEN NEW
WHEN 'UPDATE' THEN NEW
ELSE OLD
END);
PERFORM pg_notify(
'row_changed',
json_build_object(
'operation', TG_OP,
'record', record
)::text
);
RETURN COALESCE(NEW, OLD);
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER rooms_changed
AFTER INSERT OR UPDATE OR DELETE
ON rooms
FOR EACH ROW
EXECUTE PROCEDURE notify_rows_changes();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment