From d6989f1e54104d09b8af6d22cf46ea4f6fc5f4dc Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl <trygvis@inamo.no>
Date: Wed, 10 Sep 2014 00:12:30 +0200
Subject: o Initial import of postgresql LISTEN/NOTIFY code.

---
 init.sql | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 init.sql

(limited to 'init.sql')

diff --git a/init.sql b/init.sql
new file mode 100644
index 0000000..0ec0477
--- /dev/null
+++ b/init.sql
@@ -0,0 +1,37 @@
+-- Create a table for incoming mail
+
+CREATE TABLE mail_raw (
+  id  SERIAL PRIMARY KEY,
+  raw TEXT
+);
+
+-- Insert a row into the table;
+
+INSERT INTO mail_raw (raw) VALUES ('my mail');
+NOTIFY mail_raw;
+
+--
+-- Alternate technique with implicit notifies through triggers
+--
+
+CREATE TABLE mail_raw_t (
+  id  SERIAL PRIMARY KEY,
+  raw TEXT
+);
+
+-- This trigger function does the notification
+CREATE FUNCTION notify_trigger()
+  RETURNS TRIGGER AS $$
+BEGIN
+  PERFORM pg_notify(TG_TABLE_NAME, NEW.id :: TEXT);
+  RETURN NULL;
+END;
+$$ LANGUAGE plpgsql;
+
+-- Create a trigger on the mail_raw_t table and make it run on each new row
+CREATE TRIGGER mail_raw_t_notify
+AFTER INSERT ON mail_raw_t
+FOR EACH ROW
+EXECUTE PROCEDURE notify_trigger();
+
+INSERT INTO mail_raw_t (raw) VALUES ('my mail');
-- 
cgit v1.2.3

