X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 1E998D1B432 for ; Sun, 4 Jan 2004 23:41:23 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 47683-07 for ; Sun, 4 Jan 2004 19:40:35 -0400 (AST) Received: from sraigw.sra.co.jp (sraigw.sra.co.jp [202.32.10.2]) by svr1.postgresql.org (Postfix) with ESMTP id 5F626D1B45E for ; Sun, 4 Jan 2004 19:40:32 -0400 (AST) Received: from srascb.sra.co.jp (srascb [133.137.8.65]) by sraigw.sra.co.jp (Postfix) with ESMTP id 8835B625C5; Mon, 5 Jan 2004 08:40:33 +0900 (JST) Received: from sranhm.sra.co.jp (IDENT:root@localhost [127.0.0.1]) by srascb.sra.co.jp (8.9.3p2/3.7W-sra) with ESMTP id IAA03936; Mon, 5 Jan 2004 08:40:33 +0900 Received: from localhost (sraihb-hub.sra.co.jp [133.137.8.6]) by sranhm.sra.co.jp (8.9.3+3.2W/3.7W-srambox) with ESMTP id IAA25875; Mon, 5 Jan 2004 08:40:32 +0900 Date: Mon, 05 Jan 2004 08:39:51 +0900 (JST) Message-Id: <20040105.083951.78727440.t-ishii@sra.co.jp> To: tgl@sss.pgh.pa.us Cc: pgsql-sql@postgresql.org, JanWieck@Yahoo.com Subject: Re: DO INSTEAD in rule From: Tatsuo Ishii In-Reply-To: <21119.1073238506@sss.pgh.pa.us> References: <20040104.225115.71101762.t-ishii@sra.co.jp> <21119.1073238506@sss.pgh.pa.us> X-Mailer: Mew version 2.3 on Emacs 20.7 / Mule 4.1 =?iso-2022-jp?B?KBskQjAqGyhCKQ==?= Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/24 X-Sequence-Number: 16417 > Tatsuo Ishii writes: > > In the last SELECT I exepcted j = 0, rather than j = 1 since I use DO > > INSTEAD in the rule and the default value for j is 0. Am I missing > > something? > > > CREATE rule t1_ins AS ON INSERT TO t1 > > WHERE (EXISTS (SELECT 1 FROM t1 > > WHERE i = new.i)) > > DO INSTEAD UPDATE t1 SET j = j + 1 > > WHERE i = new.i; > > Hm. The problem is that the rule query runs after the INSERT and so it > sees the inserted row as something to update. The logic is essentially > > if (not (EXISTS ...)) then do the INSERT; > if (EXISTS ...) then do the UPDATE; > > and the second command sees the inserted row as existing, so it updates > it. Oh I see. I think I can live with it. However I guess documentations should be clearner about this... > Consider using a trigger instead of a rule to do this. Or, accept > that the UPDATE will happen unconditionally, and start J off one less > than it should be. > > Note that either solution will have race conditions if multiple > processes try to insert the same row at the same time. There are > discussions in the archives about how to avoid that, but I'm not > sure anyone found a really satisfactory answer that didn't involve > an unpleasant amount of locking. Now I remember the discussion. Probably I should not use rules like my examples for real world applications. -- Tatsuo Ishii