Re: Bug #485: strange behavior when creting rules with serial id - Mailing list pgsql-bugs

From Tom Lane
Subject Re: Bug #485: strange behavior when creting rules with serial id
Date
Msg-id 26815.1003329616@sss.pgh.pa.us
Whole thread Raw
In response to Bug #485: strange behavior when creting rules with serial id  (pgsql-bugs@postgresql.org)
List pgsql-bugs
pgsql-bugs@postgresql.org writes:
> CREATE RULE insert_test2 AS ON INSERT TO test1 DO INSERT INTO test2
> (sum1, name2, id, id1) VALUES (new.sum, new.name, new.id,
> nextval('"test2_id1_seq"'::text));

"NEW" is a macro, not a variable.  This rule will be expanded to
something like

INSERT INTO test2
(sum1, name2, id, id1) VALUES ('1', '2', NEXTVAL('"test1_id_seq"'::text),
nextval('"test2_id1_seq"'::text));

which means that nextval('test1_id_seq') is evaluated twice, once during
the rule and once during the actual insert into test1.

You can't do what you want with a rule; you'll need to use a
trigger instead.

            regards, tom lane

pgsql-bugs by date:

Previous
From: Tony Tomov
Date:
Subject: strange behavior when creting rules
Next
From: Tom Lane
Date:
Subject: Re: Bug #484: TIMESTAMP arithmetic insconsistencies