Re: Possible to prevent transaction abort?

Поиск
Список
Период
Сортировка
От Adam B
Тема Re: Possible to prevent transaction abort?
Дата
Msg-id 49FB7C8D.7050605@videx.com
обсуждение исходный текст
Ответ на Re: Possible to prevent transaction abort?  (Johan Nel <johan555.nel555@xsinet555.co.za>)
Список pgsql-general
I'm intrigued by this solution, Johan.=C2=A0 It might be just the ticket!=
=C2=A0
I'll do some benchmarks when I have time in a week or so.

Johan Nel wrote:
Adam
B wrote:

  Hello all,


Is it possible to prevent Postgre from aborting the transaction upon a
constraint violation?


=46rom the help files maybe the following could get you on the right
track:


This example uses exception handling to perform either UPDATE or
INSERT, as appropriate:

CREATE TABLE db (a INT PRIMARY KEY, b TEXT);

CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS

$$

BEGIN

=C2=A0=C2=A0=C2=A0 LOOP

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -- first try to update the key

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 UPDATE db SET b =3D data WHERE a=
 =3D key;

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 IF found THEN

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 RETURN;

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 END IF;

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -- not there, so try to insert t=
he key

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -- if someone else inserts the s=
ame key concurrently,

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -- we could get a unique-key fai=
lure

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 BEGIN

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 INSERT I=
NTO db(a,b) VALUES (key, data);

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 RETURN;

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 EXCEPTION WHEN unique_violation =
THEN

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -- do no=
thing, and loop to try the UPDATE again

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 END;

=C2=A0=C2=A0=C2=A0 END LOOP;

END;

$$

LANGUAGE plpgsql;


HTH,


Johan Nel

Pretoria, South Africa.







Videx Inc. 1105 N. E. Circle Blvd. Corva=
llis OR 97330 (541) 758-0521
CONFIDENTIAL COMMUNICATION: The email message and&=
nbsp;any attachments are intended only for th=
e addressee.  They may be privileged, co=
nfidential, and protected from disclosure. If =
;you are not the intended recipient, any =
;dissemination, distribution, or copying is expres=
sly prohibited.  If you received this em=
ail message in error, please notify the =
sender immediately by replying to this e-mail=
 message or by telephone

В списке pgsql-general по дате отправления:

Предыдущее
От: Adam B
Дата:
Сообщение: Re: Possible to prevent transaction abort?
Следующее
От: Tomas Vondra
Дата:
Сообщение: Re: 08P01: unexpected EOF on client connection