Re: serialization errors when inserting new records

Поиск
Список
Период
Сортировка
От Ralph van Etten
Тема Re: serialization errors when inserting new records
Дата
Msg-id Pine.LNX.4.44.0501230848180.2281-100000@exp-toy.et10.loc
обсуждение исходный текст
Ответ на Re: serialization errors when inserting new records  (Tino Wildenhain <tino@wildenhain.de>)
Ответы Re: serialization errors when inserting new records  (Bruno Wolff III <bruno@wolff.to>)
Re: serialization errors when inserting new records  (William Yu <wyu@talisys.com>)
Список pgsql-general
On Sat, 22 Jan 2005, Tino Wildenhain wrote:

> > 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 ;)

I agree that a serial would be better.

But I think there are situations where a serial isn't convenient
Like when you want an primary key which consists of the current
year and an sequence number. Like ('05', 1), ('05', 2), ('05', 3) etc.
With a sequence you must write extra code to reset the sequence every year
and you get into trouble if someone inserts data from the previous year...
A 'MAX(id)+1' is much simpler and cleaner then.

Ralph.





В списке pgsql-general по дате отправления:

Предыдущее
От: Chris
Дата:
Сообщение: Re: SCHEMA compatibility with Oracle/DB2/Firebird
Следующее
От: Bruno Wolff III
Дата:
Сообщение: Re: serialization errors when inserting new records