Re: bug in RULE insert

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Александр <alexander_8901(at)mail(dot)ru>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: bug in RULE insert
Date: 2016-12-09 15:25:37
Message-ID: 16622.1481297137@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

=?UTF-8?B?0JDQu9C10LrRgdCw0L3QtNGA?= <alexander_8901(at)mail(dot)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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message garfield.lewis 2016-12-09 15:35:17 BUG #14458: PQfmod returns -1 for aggregate functions
Previous Message Александр 2016-12-09 12:18:40 bug in RULE insert