From: | Jeff Janes <jeff(dot)janes(at)gmail(dot)com> |
---|---|
To: | whiplash <whiplash(at)bss(dot)org(dot)ua> |
Cc: | "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: False unique constraint violation (exception block) |
Date: | 2013-04-12 22:31:40 |
Message-ID: | CAMkU=1wrN11AEgYR-xC8gZ2p2osEWYSSci2aU=0rCMnM+wkVBA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Fri, Apr 12, 2013 at 8:44 AM, whiplash <whiplash(at)bss(dot)org(dot)ua> wrote:
>
> CREATE UNIQUE INDEX idx_table0_unique
> ON table0 ( field0, coalesce ( field1, 'INDEX_COLUMN_NULL' ) );
>
> I created function for insert only unique record (part of code):
>
> BEGIN
> INSERT INTO table0 ( field0, field1 ) VALUES ( p_field0, p_field1 )
> RETURNING id INTO v_table0_id;
> EXCEPTION WHEN unique_violation THEN
> SELECT id FROM table0 WHERE field0 = p_field0 AND field1 = p_field1;
> END;
>
> I use this function for add data to table and sometimes I getting false
> unique violation (in block above). Why?
>
if p_field1 is NULL, then the select cannot return any rows, but can still
violate the constraint.
Also, you may have a race, where the row exists when the insert was
attempted, but was gone by the time it tried to do the select.
Cheers,
Jeff
From | Date | Subject | |
---|---|---|---|
Next Message | whiplash | 2013-04-12 23:46:53 | Re: False unique constraint violation (exception block) |
Previous Message | Kevin Grittner | 2013-04-12 21:52:50 | Re: False unique constraint violation (exception block) |