Skip site navigation (1) Skip section navigation (2)

Re: [SQL] Problems ensuring uniqueness?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Christopher Smith <x(at)xman(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org, pgsql-jdbc(at)postgresql(dot)org
Subject: Re: [SQL] Problems ensuring uniqueness?
Date: 2001-06-18 21:42:15
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-jdbcpgsql-sql
Christopher Smith <x(at)xman(dot)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

In response to


pgsql-sql by date

Next:From: Tony ReinaDate: 2001-06-18 21:54:24
Subject: Are there array functions in Postgres?
Previous:From: Ingo OellersDate: 2001-06-18 21:13:08
Subject: Re: Moving between databases

pgsql-jdbc by date

Next:From: Christopher SmithDate: 2001-06-18 21:57:45
Subject: Re: [SQL] Problems ensuring uniqueness?
Previous:From: Tum KhawcharoenpornDate: 2001-06-18 21:21:18
Subject: JDBC Ant Problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group