X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 3E48A53CC5 for ; Thu, 28 Apr 2005 06:27:48 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 50692-01 for ; Thu, 28 Apr 2005 09:27:39 +0000 (GMT) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by svr1.postgresql.org (Postfix) with SMTP id 6D29D53B8A for ; Thu, 28 Apr 2005 06:26:58 -0300 (ADT) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Thu Apr 28 11:26:41 2005 +0200 Received: from rodos.fzk.de (resy6.fzk.de [141.52.68.10]) by resy5.fzk.de (8.12.7/8.12.7/SuSE Linux 0.6) with ESMTP id j3S9QesG015720; Thu, 28 Apr 2005 11:26:40 +0200 Message-ID: <4270AC4F.6D62C9C8@rodos.fzk.de> Date: Thu, 28 Apr 2005 11:26:39 +0200 From: Christoph Haller X-Mailer: Mozilla 4.77 [en] (X11; U; HP-UX B.10.20 9000/780) X-Accept-Language: en MIME-Version: 1.0 To: weigelt@metux.de Cc: pgsql-sql Subject: Re: trigger/rule question References: <20050427165624.GA4253@nibiru.borg.metux.de> Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS - amavis-milter (http://www.amavis.org/) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.027 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/395 X-Sequence-Number: 21288 Enrico Weigelt wrote: > > Hi folks, > > for database synchronization I'm maintaining an mtime field in > each record and I'd like to get it updated automatically on > normal writes (insert seems trivial, but update not), but it > must remain untouched when data is coming in from another node > (to prevent sync loops). > > I first tried it with rules on update, but I didnt find any trick > to prevent infinite recoursion. If I'd replace update by delete > and reinsert, I'll probably run into trouble with constaints and > delete rules. > > Triggers dont seem to have this problem, but require an function > call per record, while a rule solution would only rewrite the > actual query. > > But still I've got the unsolved problem, how to decide when to > touch the mtime and when to pass it untouched. I didnt find any > trick to explicitly bypass specific triggers yet. > > Any ideas ? > > thx > -- I assume this still refers to [SQL] RULE for mtime recording from last Friday. I gave it another thought and I am now having something which seems to work. The trick is interpose a view to avoid the rule recursion: CREATE SEQUENCE inode_id_seq ; CREATE TABLE inode ( inode_id OID NOT NULL DEFAULT NEXTVAL('inode_id_seq'), mtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE joo ( bar TEXT ) INHERITS ( inode ); CREATE VIEW joo_view AS SELECT * FROM joo ; INSERT INTO joo(bar) VALUES ( 'A.R.M.D.' ); INSERT INTO joo(bar,mtime) VALUES ( 'A.L.A.F.' , '2004-04-28 09:43:22.204429' ); SELECT * FROM JOO ; inode_id | mtime | bar ----------+----------------------------+---------- 1 | 2005-04-28 11:20:33.012668 | A.R.M.D. 2 | 2004-04-28 09:43:22.204429 | A.L.A.F. (2 rows) CREATE OR REPLACE RULE joo_update_mtime_is_null AS ON UPDATE TO joo_view DO INSTEAD UPDATE joo SET bar = NEW.bar, mtime = CASE WHEN OLD.mtime = NEW.mtime THEN current_timestamp ELSE NEW.mtime END WHERE bar = OLD.bar ; UPDATE joo_view SET bar = ' H T H ' WHERE bar = 'A.R.M.D.' ; UPDATE joo_view SET bar = ' S T S ', mtime = '2003-04-28 09:43:22.204429' WHERE bar = 'A.L.A.F.' ; SELECT * FROM JOO ; inode_id | mtime | bar ----------+----------------------------+--------- 1 | 2005-04-28 11:23:23.04613 | H T H 2 | 2003-04-28 09:43:22.204429 | S T S (2 rows) Another rule to deal with INSERT, and that's it. At least I think. Does it help? Regards, Christoph