False unique constraint violation (exception block)

From: whiplash <whiplash(at)bss(dot)org(dot)ua>
To: pgsql-general(at)postgresql(dot)org
Subject: False unique constraint violation (exception block)
Date: 2013-04-12 15:44:56
Message-ID: 51682BF8.4060802@bss.org.ua
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello.

This is simple example for describe my problem.

I created a table:

CREATE TABLE table0
(
id serial NOT NULL,
field0 integer,
field1 text
);

I created an unique index for this table:

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?

Thanks for replay.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Yang Zhang 2013-04-12 17:15:42 Re: Why is PostgreSQL 9.1 not using index for simple equality select
Previous Message Tom Lane 2013-04-12 14:11:57 Re: Why is PostgreSQL 9.1 not using index for simple equality select