Received: from sss.pgh.pa.us ([192.204.191.242]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5ILgja03600; Mon, 18 Jun 2001 17:42:45 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.4/8.11.4) with ESMTP id f5ILgG308899; Mon, 18 Jun 2001 17:42:16 -0400 (EDT) To: Christopher Smith cc: pgsql-sql@postgresql.org, pgsql-jdbc@postgresql.org Subject: Re: [SQL] Problems ensuring uniqueness? In-reply-to: <992898628.6594.1.camel@rivest.xdrive.com> References: <992898628.6594.1.camel@rivest.xdrive.com> Comments: In-reply-to Christopher Smith message dated "18 Jun 2001 14:10:27 -0700" Date: Mon, 18 Jun 2001 17:42:15 -0400 Message-ID: <8896.992900535@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200106/51 X-Sequence-Number: 792 Christopher Smith 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