improved concurrency detection
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 28 Apr 2008 12:15:25 +0000 (12:15 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 28 Apr 2008 12:15:25 +0000 (12:15 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@9476 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/sql/Pg/reporter-schema.sql
Open-ILS/src/sql/Pg/upgrade_simple_record.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;
index 8588929..3f87bb0 100644 (file)
@@ -33,13 +33,24 @@ SELECT  r.id,
   GROUP BY 1,2,3,4,5,6,8,9;
 
 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;