Re: INSERT WHERE NOT EXISTS

From: DeJuan Jackson <djackson(at)speedfc(dot)com>
To: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: INSERT WHERE NOT EXISTS
Date: 2003-06-27 23:11:04
Message-ID: 3EFCCF08.7070702@speedfc.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Karsten Hilbert wrote:

>>// check if entry already exists
>>SELECT COUNT(*) FROM tablename WHERE [cond]
>>
>>
>You may want to try this instead:
>
> select exists(select * from tablename where [cond])
>
>which should be faster than count(*). You'll get back a
>boolean value, though, not an integer.
>
>Karsten
>
>
Why not do:
SELECT 1 FROM tablename WHERE [cond];

And use row_count > 0 to determine a hit.

And I can't think of a way to prevent the race-condition thing besides
using SERIALIZABLE ISOLATION.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Karsten Hilbert 2003-06-27 23:36:30 Re: INSERT WHERE NOT EXISTS
Previous Message Ron Johnson 2003-06-27 22:58:08 Re: Redhat's "enhancements" to PG