Re: Stange "duplicate key value violates unique constraint" after "delete" at ON UPDATE trigger

Поиск
Список
Период
Сортировка
От bock@openit.de (Julian v. Bock)
Тема Re: Stange "duplicate key value violates unique constraint" after "delete" at ON UPDATE trigger
Дата
Msg-id lp62fxl66s.fsf@warpcore.i.openit.de
обсуждение исходный текст
Ответ на Stange "duplicate key value violates unique constraint" after "delete" at ON UPDATE trigger  (Dmitry Koterov <dmitry@koterov.ru>)
Ответы Re: Stange "duplicate key value violates unique constraint" after "delete" at ON UPDATE trigger  (Dmitry Koterov <dmitry@koterov.ru>)
Список 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 по дате отправления:

Предыдущее
От: Chris Travers
Дата:
Сообщение: Re: Don't Thread On Me (PostgreSQL related)
Следующее
От: David Johnston
Дата:
Сообщение: Fwd: [SQL] Query question