Last active
August 7, 2020 15:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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