X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 10285D1BB36 for ; Thu, 22 Apr 2004 06:49:05 -0300 (ADT) 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 72270-07 for ; Thu, 22 Apr 2004 06:49:03 -0300 (ADT) Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89]) by svr1.postgresql.org (Postfix) with ESMTP id 55DE1D1BACD for ; Thu, 22 Apr 2004 06:49:01 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-31.mail.demon.net with esmtp (Exim 3.35 #1) id 1BGapL-0003US-0V; Thu, 22 Apr 2004 10:49:03 +0100 Received: from client17.archonet.com (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 99B50167AC; Thu, 22 Apr 2004 10:49:01 +0100 (BST) From: Richard Huxton To: Kemin Zhou , pgsql-sql@postgresql.org Subject: Re: rule's behavior with join interesting Date: Thu, 22 Apr 2004 10:49:01 +0100 User-Agent: KMail/1.5 References: <4086D465.1060907@ferring.com> In-Reply-To: <4086D465.1060907@ferring.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200404221049.01201.dev@archonet.com> X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200404/223 X-Sequence-Number: 17598 On Wednesday 21 April 2004 21:07, Kemin Zhou wrote: > Here I have a very simple case > > table1 > table1_removed > > anotherTable > > create or replace RULE rec_remove as ON DELETE TO table1 > do insert into table1_remove > select old.*, a.acc from old g join anotherTable a on g.acc=a.other_acc; > === > the parser complained ERROR: relation "*OLD*" does not exist > So I used > select old.*, a.acc from table1 g join anotherTable a on g.acc=a.other_acc; > > This worked find. > > When I run delete on table1, 213 rows. > > tmp table received 213X213 = 45369 rows. each row is duplicated 213 times. The issue here is that although you can refer to values such as OLD.acc, OLD is not a table but more like single row. So, you probably want ...DO INSERT INSTO table1_remove SELECT old.*, a.acc FROM anotherTable a WHERE a.other_acc = OLD.acc; Your second example just ignored the OLD.acc altogether in the join, so of course you got an unconstraind join of 213 x 213. -- Richard Huxton Archonet Ltd