Re: [SQL] Problems ensuring uniqueness?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: [SQL] Problems ensuring uniqueness?
Дата
Msg-id 8896.992900535@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Problems ensuring uniqueness?  (Christopher Smith <x@xman.org>)
Список pgsql-jdbc
Christopher Smith <x@xman.org> writes:
> begin
> lock table foo in access exclusive mode
> select count(*) from foo where key1 = bar, key2 = baz

> if count > 1
>     rollback
> else
>     insert into foo (key1, key2, other) values (bar, baz, stuff)
>     commit

> Now, table foo has a primary key made up of key1 & key2. Without
> concurrent access, the code works great. However, provided I use enough
> threads, I inevitably get back errors from the database indicating that
> I've tried to insert a record which violates the uniquness of the
> primary key.

Um ... surely that should be "if count > 0" ?  Or was that just a
transcription error?

This approach certainly ought to work as desired given the exclusive
lock, so a silly typo seems like a plausible explanation...

            regards, tom lane

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

Предыдущее
От: Christopher Smith
Дата:
Сообщение: Re: [SQL] Problems ensuring uniqueness?
Следующее
От: Tom Lane
Дата:
Сообщение: Re: [SQL] Problems ensuring uniqueness?