Skip to content

Instantly share code, notes, and snippets.

@fbeauchamp
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbeauchamp/9879820 to your computer and use it in GitHub Desktop.
Save fbeauchamp/9879820 to your computer and use it in GitHub Desktop.
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