Unix Technical Forum

SERIAL type's sequence is double-called or ?

This is a discussion on SERIAL type's sequence is double-called or ? within the pgsql Sql forums, part of the PostgreSQL category; --> Hello all, I have a question with the SERIAL type. I want to use it for default identification of ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 05:53 PM
Aleksandr Vinokurov
 
Posts: n/a
Default SERIAL type's sequence is double-called or ?



Hello all,

I have a question with the SERIAL type. I want to use it for default
identification of table entries:

create table chuwee (
num serial primary key,
mesg varchar(50) not null
);

And all the inserts to this table I'm gonna log to this table:

create table chuwee_log (
id serial primary key,
date timestamp default current_timestamp,
num integer not null,
mesg varchar(50) not null
);

Logging will be done with this rule:

create or replace rule chuwee_rule as on insert to chuwee
do insert into chuwee_log (num, mesg)
values (new.num, new.mesg);


And all seems to work, but a sequence is called twice for inserts, and
logged "num"-s get a +1 value:

sunline=# insert into chuwee (mesg) values ('Hello, world!');
INSERT 9596671 1
sunline=# insert into chuwee (mesg) values ('Aquarium full of tequilla');
INSERT 9596673 1
sunline=# select * from chuwee_log;
id | date | num | mesg
----+----------------------------+-----+---------------------------
1 | 2007-09-08 16:18:26.707384 | 2 | Hello, world!
2 | 2007-09-08 16:18:58.711599 | 4 | Aquarium full of tequilla
(2 rows)

sunline=# select * from chuwee;
num | mesg
-----+---------------------------
1 | Hello, world!
3 | Aquarium full of tequilla
(2 rows)


How it can be cured, if any?

Best dishes,
Aleksandr.

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 05:53 PM
Richard Huxton
 
Posts: n/a
Default Re: SERIAL type's sequence is double-called or ?

Aleksandr Vinokurov wrote:
>
> Logging will be done with this rule:
>
> create or replace rule chuwee_rule as on insert to chuwee
> do insert into chuwee_log (num, mesg)
> values (new.num, new.mesg);


Don't do logging with rules, do logging with triggers.

See mailing-list archives for details.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:12 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com