Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 760A86345B2 for ; Thu, 25 Feb 2010 11:31:03 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 08223-02-7 for ; Thu, 25 Feb 2010 15:30:52 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id CC9F663408C for ; Thu, 25 Feb 2010 11:28:58 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o1PFSugl003703; Thu, 25 Feb 2010 10:28:56 -0500 (EST) To: Aron cc: pgsql-sql@postgresql.org Subject: Re: Using new.id in the WHERE condition of one UPDATE inside ON INSERT rule In-reply-to: <201002251619.32048.auryn@wanadoo.es> References: <201002251619.32048.auryn@wanadoo.es> Comments: In-reply-to Aron message dated "Thu, 25 Feb 2010 16:19:32 +0100" Date: Thu, 25 Feb 2010 10:28:56 -0500 Message-ID: <3702.1267111736@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/95 X-Sequence-Number: 33694 Aron writes: > I would like to avoid using "SELECT MAX(id) FROM my_table" (it doesn't seem a > good method), but if I use "new.id", I get new id values, not the "id" > inserted with the rule, and the condition is always false. "new.id" is a macro, which in this example will be expanded into a nextval() function call, which is why it doesn't work --- the nextval() in the WHERE condition will produce a different value from the one in the original INSERT. You would be far better off using a trigger here instead of a rule. regards, tom lane