improved concurrency detection
[Evergreen.git] / Open-ILS / src / sql / Pg / reporter-schema.sql
index 79e2eee..9fd9a9e 100644 (file)
@@ -157,13 +157,24 @@ ALTER TABLE reporter.materialized_simple_record ADD PRIMARY KEY (id);
 CREATE VIEW reporter.super_simple_record AS SELECT * FROM reporter.materialized_simple_record;
 
 CREATE OR REPLACE FUNCTION reporter.simple_rec_sync () RETURNS TRIGGER AS $$
+DECLARE
+    r_id        BIGINT;
+    new_data    RECORD;
 BEGIN
-    IF TG_OP IN ('UPDATE','DELETE') THEN
-        DELETE FROM reporter.materialized_simple_record WHERE id = OLD.record;
+    IF TG_OP IN ('DELETE') THEN
+        r_id := OLD.record;
+    ELSE
+        r_id := NEW.record;
     END IF;
 
-    IF TG_OP IN ('INSERT','UPDATE') AND NOT NEW.deleted THEN
-        INSERT INTO reporter.materialized_simple_record SELECT * FROM reporter.old_super_simple_record WHERE id = NEW.record;
+    SELECT * INTO new_data FROM reporter.materialized_simple_record WHERE id = r_id FOR UPDATE;
+    DELETE FROM reporter.materialized_simple_record WHERE id = r_id;
+
+    IF TG_OP IN ('DELETE') THEN
+        RETURN OLD;
+    ELSE
+        INSERT INTO reporter.materialized_simple_record SELECT DISTINCT ON (id) * FROM reporter.old_super_simple_record WHERE id = NEW.record;
+        RETURN NEW;
     END IF;
 
 END;