pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SERIAL type's sequence is double-called or ?
2+ messages / 2 participants
[nested] [flat]

* SERIAL type's sequence is double-called or ?
@ 2007-09-07 13:40  Aleksandr Vinokurov <aleksandr.vin@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Aleksandr Vinokurov @ 2007-09-07 13:40 UTC (permalink / raw)
  To: pgsql-sql



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.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: SERIAL type's sequence is double-called or ?
@ 2007-09-07 14:38  Richard Huxton <dev@archonet.com>
  parent: Aleksandr Vinokurov <aleksandr.vin@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Richard Huxton @ 2007-09-07 14:38 UTC (permalink / raw)
  To: Aleksandr Vinokurov <aleksandr.vin@gmail.com>; +Cc: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2007-09-07 14:38 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-09-07 13:40 SERIAL type's sequence is double-called or ? Aleksandr Vinokurov <aleksandr.vin@gmail.com>
2007-09-07 14:38 ` Richard Huxton <dev@archonet.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox