Rules, functions and RETURNING

From: Nico Mandery <nico(dot)mandery(at)geops(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Subject: Rules, functions and RETURNING
Date: 2009-09-17 15:35:52
Message-ID: 4AB25758.60702@geops.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gavin McCullagh 2009-09-17 16:44:39 extracting from epoch values in pgsql
Previous Message Leo Mannhart 2009-09-17 15:07:21 Re: Hibernate, web application and only one sequence for all primary keys