Skip to content

Instantly share code, notes, and snippets.

@pianosnake
Last active March 1, 2018 22:39
Show Gist options
  • Save pianosnake/7783100 to your computer and use it in GitHub Desktop.
Save pianosnake/7783100 to your computer and use it in GitHub Desktop.
Function to send a Postgres Notification with a JSON object payload of the updated fields. Also the trigger to call the function.
CREATE OR REPLACE FUNCTION send_notification() RETURNS trigger AS $$
BEGIN
PERFORM pg_notify('usersupdate',
(SELECT row_to_json(r.*)::varchar FROM (
SELECT id, email, name from users where id = NEW.id)
r)
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER users_updated
AFTER UPDATE OF email,name
ON users
FOR EACH ROW
EXECUTE PROCEDURE send_notification();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment