Nickolay nitro at zhukcity.ru
Mon Jan 18 04:25:10 PST 2010
Hello all,

First of all, I suspect that slony recognizes any SQL operation and 
triggered operations as a single transaction, right? For example, I have 
some table with records, let's call it "messages" and some table with 
messages' categories, like "categories". And I have a row-level trigger 
that is fired AFTER insert, update or delete statements on "messages" 
and increases/decreases the corresponding counter row in table 
"categories", for example:

table messages
----------------

msg_id | category_id | text
1 | 15 | test tralala
2 | 15 | test trututu
3 | 16 | hello

table categories
-----------------
category_id | title | counter
15 | test category | 2
16 | welcomes | 1

CREATE OR REPLACE FUNCTION msgs_upd_counters()
  RETURNS SETOF trigger AS
BEGIN
  IF (TG_OP = 'DELETE') THEN
      UPDATE categories SET counter=counter-1 WHERE id = OLD.category_id;
      EXECUTE 'NOTIFY category'||OLD.category_id::text||'_changed';
    END IF;
    RETURN OLD;
  END IF;
  IF (TG_OP = 'UPDATE') THEN
    IF (NEW.category_id != OLD.category_id) THEN
      UPDATE categories SET counter=counter-1 WHERE id = OLD.category_id;
      EXECUTE 'NOTIFY category'||OLD.category_id::text||'_changed';
      UPDATE categories SET counter=counter+1 WHERE id = NEW.category_id;
      EXECUTE 'NOTIFY category'||NEW.category_id::text||'_changed';
    END IF;
  END IF;
  IF (TG_OP = 'INSERT') THEN
    UPDATE categories SET counter=counter+1 WHERE id = NEW.category_id;
    EXECUTE 'NOTIFY category'||NEW.category_id::text||'_changed';
  END IF;
  RETURN NEW;
END;

CREATE TRIGGER trg_upd_category
AFTER INSERT OR UPDATE OR DELETE
  ON messages
  FOR EACH ROW
  EXECUTE PROCEDURE msgs_upd_counters();

Everything works just fine, until I execute FAILOVER or DROP NODE (may 
be MOVE SET does it too, I don't know).
Sometimes after that the counters doesn't match the real messages count 
for some of the categories, i.e. for example, table "categories" shows 
that "counter" for category_id=15 is 2, but there is only one message in 
table "messages" with category_id=15.
I suspect two options:
1. Either Slony doesn't recognize INSERT INTO messages, UPDATE 
categories and NOTIFY as ONE SINGLE transaction
2. Or Slony allows my application to do INSERT/DELETEs while triggers 
are not activated by Slony (after FAILOVER) yet.

Does anyone has any thoughts on this subject?
I'm not happy with the idea that I would have to remove this trigger and 
add a bunch of SQL code to all the INSERT/DELETE/UPDATE operations in my 
code.

Best regards, Nick.




More information about the Slony1-general mailing list