Re: serialization errors when inserting new records

From: Tino Wildenhain <tino(at)wildenhain(dot)de>
To: Ralph van Etten <ralph(at)et10(dot)org>
Cc: Postgresql-General <pgsql-general(at)postgresql(dot)org>
Subject: Re: serialization errors when inserting new records
Date: 2005-01-22 11:46:39
Message-ID: 1106394399.4014.112.camel@Andrea.peacock.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Am Samstag, den 22.01.2005, 11:14 +0100 schrieb Ralph van Etten:
> Hoi,
>
> I searched the archives but couldn't find an answer to this:
>
> I have a table (simplyfied)
>
> CREATE TABLE test (
> id INT PRIMARY KEY,
> name VARCHAR(250)
> );
>
> I insert records with
>
> INSERT INTO test (id, name)
> SELECT COALESCE(MAX(id)+1, 1), 'name' FROM test
>
> Ofcourse this gives problems when two clients are inserting a record at
> the same time. (duplicate primary keys) But, i can't use a sequence in my
> application (the pk consists of more than just a sequence)
>
> one solution would be to do a 'LOCK TABLE test IN SHARE MODE' before
> inserting. This solves my problem but i'm not sure if its the
> best way to deal with this kind of concurrency problems ? Is there a
> better way ?

Of course. The solution to this problem is inherent to a good database
and has therefore been done long long ago ;)

See: http://borg.postgresql.org/docs/7.4/static/datatype.html#DATATYPE-SERIAL

Regards
Tino

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gary Doades 2005-01-22 11:56:59 Re: serialization errors when inserting new records
Previous Message Ralph van Etten 2005-01-22 10:14:20 serialization errors when inserting new records