pgsql-bugs(at)postgresql(dot)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