![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
On Tue, Nov 15, 2005 at 10:51:10PM -0700, Michael Fuhr wrote: On Wed, Nov 16, 2005 at 06:29:40AM +0100, Tomas Zerolo wrote: AFAIK, serials are not guaranteed to produce sequential values; tehy will produce unique values. That means that they can (and sometimes will) jump. In this particular case, however, the behavior is due to the rule on test1: CREATE RULE test1_on_insert AS ON INSERT TO test1 DO INSERT INTO test_log1 (qid) VALUES (new.id); [...] Oops, I didn't see that. Your eyes are sharper than mine ;-) thanks -- tomas |
#2
| |||
| |||
|
|
On Wed, Nov 16, 2005 at 10:31:10AM +0200, Sarunas Krisciukaitis wrote: I understand that RULES are like macros. Strangest thing here is that INSERT to test1 will touch only one sequence: test1_id_seq. And it increments test1_id_seq twice during insert with RULE. Yes, that's a well-known effect of rewriting a query that includes a call to nextval(). NEW.id in the rule doesn't refer to the value that's inserted, but rather to the expression that's evaluated to get that value. Since you didn't provide a value for id it gets the default: nextval('test1_id_seq'). That expression is used in both inserts, so the sequence gets incremented twice. See the archives for numerous past discussions of this behavior. |
#3
| |||
| |||
|
Ok Then I found the solution in this partical case:CREATE RULE test1_on_insert AS ON INSERT TO test1 DO INSERT INTO test_log1 (qid) VALUES ( (SELECT lastval()) ); With this rule all inserts are working as expected ![]() Thank you for you advise ![]() Sarunas Michael Fuhr wrote: On Wed, Nov 16, 2005 at 10:31:10AM +0200, Sarunas Krisciukaitis wrote: I understand that RULES are like macros. Strangest thing here is that INSERT to test1 will touch only one sequence: test1_id_seq. And it increments test1_id_seq twice during insert with RULE. Yes, that's a well-known effect of rewriting a query that includes a call to nextval(). NEW.id in the rule doesn't refer to the value that's inserted, but rather to the expression that's evaluated to get that value. Since you didn't provide a value for id it gets the default: nextval('test1_id_seq'). That expression is used in both inserts, so the sequence gets incremented twice. See the archives for numerous past discussions of this behavior. ---------------------------(end of broadcast)--------------------------- TIP 6: explain analyze is your friend |
![]() |
| Thread Tools | |
| Display Modes | |
| |