Skip to content

Instantly share code, notes, and snippets.

@raphox
Created June 4, 2020 02:46
Show Gist options
  • Save raphox/bf2e9675e780d9611ce1127eb541ef8a to your computer and use it in GitHub Desktop.
Save raphox/bf2e9675e780d9611ce1127eb541ef8a 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