Re: double insert on inherited table with where constraint based on sequence - Mailing list pgsql-bugs

From Tom Lane
Subject Re: double insert on inherited table with where constraint based on sequence
Date
Msg-id 15218.1153336921@sss.pgh.pa.us
Whole thread Raw
In response to double insert on inherited table with where constraint based on sequence  ("Andrew Hammond" <andrew.george.hammond@gmail.com>)
List pgsql-bugs
"Andrew Hammond" <andrew.george.hammond@gmail.com> writes:
> Rules:
>     t2_part AS
>     ON INSERT TO t2
>    WHERE new.id > 10 DO INSTEAD  INSERT INTO t2_child (id, name)
>   VALUES (new.id, new.name)

"new.id" is replaced by "nextval('t2_id_seq'::regclass)" each time it
appears ... which will be four separate times, viz the two evaluations
of the WHERE clause and the two VALUES clauses.

Basically, volatile functions and rules do not mix.  Consider using
a trigger on t2 instead to redirect the insert somewhere else.

            regards, tom lane

pgsql-bugs by date:

Previous
From: "Marius Žalinauskas"
Date:
Subject: BUG #2535: Unicode on Windows: aogonek is not distinguished from agrave
Next
From: Thiago Silva
Date:
Subject: XML2 module: odd query results