Skip to content

Instantly share code, notes, and snippets.

@fbeauchamp fbeauchamp/gist:9879820
Last active Aug 29, 2015

Embed
What would you like to do?
typo
-- Function: agent_notify()
-- DROP FUNCTION agent_notify();
CREATE OR REPLACE FUNCTION agent_notify()
RETURNS trigger AS
$BODY$
DECLARE
diff hstore;
t text;
BEGIN
IF TG_OP= 'INSERT'
THEN diff = NEW.Attributes ;
ELSE diff = NEW.Attributes - OLD.attributes;
END IF ;
IF diff = hstore('') THEN
-- All changed fields are ignored. Skip this update.
RETURN NULL;
END IF;
diff = diff || hstore('id',CAST(NEW.id as text));
PERFORM pg_notify('agent',
ARRAY_TO_STRING(ARRAY(
SELECT
(key || ':|:' || value)
FROM
EACH(diff)
), '*|*')) FROM agent;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION agent_notify()
OWNER TO florent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.