Re: Stange "duplicate key value violates unique constraint" after "delete" at ON UPDATE trigger - Mailing list pgsql-general

From bock@openit.de (Julian v. Bock)
Subject Re: Stange "duplicate key value violates unique constraint" after "delete" at ON UPDATE trigger
Date
Msg-id lp62fxl66s.fsf@warpcore.i.openit.de
Whole thread Raw
In response to Stange "duplicate key value violates unique constraint" after "delete" at ON UPDATE trigger  (Dmitry Koterov <dmitry@koterov.ru>)
Responses Re: Stange "duplicate key value violates unique constraint" after "delete" at ON UPDATE trigger  (Dmitry Koterov <dmitry@koterov.ru>)
List pgsql-general
Hi

>>>>> "DK" == Dmitry Koterov <dmitry@koterov.ru> writes:

DK> create table a(i integer);
DK> CREATE UNIQUE INDEX a_idx ON a USING btree (i);
DK> CREATE FUNCTION a_tr() RETURNS trigger AS
DK> $body$
DK> BEGIN
DK>     DELETE FROM a WHERE i = NEW.i;
DK>     RETURN NEW;
DK> END;
DK> $body$
DK> LANGUAGE 'plpgsql';
DK> CREATE TRIGGER a_tr BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE
DK> a_tr();

The DELETE doesn't see the row the other transaction inserted and
doesn't delete anything (and doesn't block). This happens later when the
row is inserted and the index is updated.

You can try the insert and catch the unique violation in a loop (see
http://www.postgresql.org/docs/9.1/static/plpgsql-control-structures.html)
although that won't work with a BEFORE trigger.

Regards,
Julian

--
Julian v. Bock               Projektleitung Software-Entwicklung
OpenIT GmbH                  Tel +49 211 239 577-0
In der Steele 33a-41         Fax +49 211 239 577-10
D-40599 Düsseldorf           http://www.openit.de
________________________________________________________________
HRB 38815 Amtsgericht Düsseldorf             USt-Id DE 812951861
Geschäftsführer: Oliver Haakert, Maurice Kemmann

pgsql-general by date:

Previous
From: Chris Travers
Date:
Subject: Re: Don't Thread On Me (PostgreSQL related)
Next
From: David Johnston
Date:
Subject: Fwd: [SQL] Query question