Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 938EF6347AB for ; Thu, 17 Sep 2009 23:40:12 -0300 (ADT) 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 94484-03 for ; Fri, 18 Sep 2009 02:40:04 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from QMTA01.emeryville.ca.mail.comcast.net (qmta01.emeryville.ca.mail.comcast.net [76.96.30.16]) by mail.postgresql.org (Postfix) with ESMTP id 7B3086329A6 for ; Thu, 17 Sep 2009 23:40:04 -0300 (ADT) Received: from OMTA03.emeryville.ca.mail.comcast.net ([76.96.30.27]) by QMTA01.emeryville.ca.mail.comcast.net with comcast id i2CD1c0040b6N64A12fVlH; Fri, 18 Sep 2009 02:39:29 +0000 Received: from tucker.local ([24.17.164.54]) by OMTA03.emeryville.ca.mail.comcast.net with comcast id i2g21c0041Ak4z48P2g2mA; Fri, 18 Sep 2009 02:40:03 +0000 From: Adrian Klaver To: pgsql-sql@postgresql.org Subject: Re: Rules, functions and RETURNING Date: Thu, 17 Sep 2009 19:40:05 -0700 User-Agent: KMail/1.9.10 References: <4AB25758.60702@geops.de> In-Reply-To: <4AB25758.60702@geops.de> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200909171940.06313.aklaver@comcast.net> 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: 200909/90 X-Sequence-Number: 33155 On Thursday 17 September 2009 8:35:52 am Nico Mandery wrote: > Hello list, > > I am trying to wirte a rule which calls a PLPgSQL-function upon an > Insert in a table. Here is a somewhat simplified example of what i got > so far: > > CREATE TABLE mytable ( > mytable_id serial PRIMARY KEY, > something text > ); > > > CREATE OR REPLACE FUNCTION _rule_insert_my(something text) > RETURNS integer AS > $BODY$ > BEGIN > -- do something > return mytable_id; > END; > $BODY$ > LANGUAGE 'plpgsql' VOLATILE > COST 100; > > > CREATE OR REPLACE RULE _insert AS > ON INSERT TO mytable DO INSTEAD SELECT > _rule_insert_my(new.something) AS mytable_id; > > > So far this works quite well. But I got a few situations where I need to > do a query which uses RETURNING to get the value of the newly generated > primary key. Like this one: > > INSERT INTO mytable (something) VALUES ('some text') RETURNING mytable_id; > > This breaks because I did not specify a RETURNING-Clause in the rule. > But how can specify RETURNING with SELECT? > > > Thank your in advance for your help. > > regards, > nico > > -- > Nico Mandery I am going to assume that '--do something' is more complicated then getting the mytable_id. If that is the case why not create an INSERT function/trigger that does the 'something' and then just do: INSERT INTO mytable (something) VALUES ('some text') RETURNING mytable_id; -- Adrian Klaver aklaver@comcast.net