Skip to content

Instantly share code, notes, and snippets.

@thospfuller
Last active August 7, 2020 15:17
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 thospfuller/6484f9a682a5f3ad84a190206b4937f8 to your computer and use it in GitHub Desktop.
Save thospfuller/6484f9a682a5f3ad84a190206b4937f8 to your computer and use it in GitHub Desktop.
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