Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of the table_change trigger and notify_change function required to run the PGNotificationListenerInPostGreSQLDatabaseExample.groovy script.
CREATE OR REPLACE FUNCTION notify_change() RETURNS TRIGGER AS $$
BEGIN
--
-- WARNING: Case is VERY IMPORTANT here! If we use 'exampleChannel' PG converts this to
-- examplechannel and no events will be received!!
--
-- UPDATE: [to be confirmed] Case can be handled in PostgreSQL by using double quotes.
--
-- In theory, if you had the following line as the listener, it would work in camelCase.
--
-- statement.execute('LISTEN "exampleChannel"');
--
-- The same applies to any identifier in Postgres.
--
PERFORM pg_notify('examplechannel', NEW.phrase);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER table_change
AFTER INSERT OR UPDATE OR DELETE ON example
FOR EACH ROW EXECUTE PROCEDURE notify_change();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment