SERIAL behaviour

From: Eugene <foo(at)bar(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: SERIAL behaviour
Date: 2002-07-14 01:59:15
Message-ID: agqlt8$2ndp$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi everyone,

I am new to PostgreSQL and I am using it in PHP application. For the most
part, I like it a lot. However, I am quite dismayed by the behavior of the
SERIAL type, or, more specifically, the PostgreSQL sequences.

I have three tables where table1 has its primary key of type SERIAL. Two
other tables use that same id (of type integer) as a foreign key (and also
part of the compound primary key). When inserting a new record, I have the
code that essentially looks like this:

BEGIN
id = nextval('sequence');
insert_in_table1(id, data1);
insert_in_table2(id, data2);
insert_in_table3(id, data3);

if( success ) {
COMMIT
} else {
ROLLBACK
}

The problem is that on a rollback, the value of the sequence does not go
back to its previous value! I know that I can use setval() to adjust the
value of the sequence, but that's not a solution I like. I expect ROLLBACK
to bring the database to the state it was before BEGIN, but that's not what
happens! What is the proper way to handle this? Also, how can I ensure
consistency when multiple clients are changing the same tables? (sequence
doesn't seem to be part of the transaction...)

thanks in advance,

Eugene

-------------------------------------------------------------------
eestrulyov at uwaterloo dot ca

Responses

Browse pgsql-general by date

  From Date Subject
Next Message fetchmail 2002-07-14 16:26:17 Re: Serious Crash last Friday
Previous Message Sean Reifschneider 2002-07-14 00:33:58 Weird (?) happenings with locks and limits?