Re: [BUGS] bug in RULE insert - Mailing list pgsql-bugs

From Tom Lane
Subject Re: [BUGS] bug in RULE insert
Date
Msg-id 16622.1481297137@sss.pgh.pa.us
Whole thread Raw
In response to [BUGS] bug in RULE insert  (Александр <alexander_8901@mail.ru>)
List pgsql-bugs
Александр <alexander_8901@mail.ru> writes:
> Strange the rule works for an insertion

This is expected behavior, because the rule works like a macro, and
you have a volatile argument (that is, the nextval() call for the
serial column's default) being passed to it and thereby being executed
twice.  IOW, what you wrote is equivalent to

insert into test(id, name)
values 
(nextval('test_id_seq'), '1'),
(nextval('test_id_seq'), '2'),
(nextval('test_id_seq'), '3');

and that executes, then the rule causes this to also be executed:

insert into v_test (v_id, v_name)
values 
(nextval('test_id_seq'), '1'),
(nextval('test_id_seq'), '2'),
(nextval('test_id_seq'), '3');

What you seem to want would be much more reliably done with a trigger.
        regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

pgsql-bugs by date:

Previous
From: Александр
Date:
Subject: [BUGS] bug in RULE insert
Next
From: garfield.lewis@lzlabs.com
Date:
Subject: [BUGS] BUG #14458: PQfmod returns -1 for aggregate functions