Skip to content

Instantly share code, notes, and snippets.

View jjzabkar's full-sized avatar

JJ Zabkar jjzabkar

  • Seattle
  • 11:49 (UTC -07:00)
View GitHub Profile
@jjzabkar
jjzabkar / 1_triggers.sql
Created March 29, 2018 04:22 — forked from fritzy/1_triggers.sql
Get table change notifications from Postgres as JSON
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('table_update', json_build_object('table', TG_TABLE_NAME, 'id', id, 'type', TG_OP)::text);