Re: Possible to prevent transaction abort?

From: Adam B <adamb(at)videx(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Possible to prevent transaction abort?
Date: 2009-05-01 22:49:49
Message-ID: 49FB7C8D.7050605@videx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="" text="#000000">
I'm intrigued by this solution, Johan.  It might be just the ticket! 
I'll do some benchmarks when I have time in a week or so.<br>
<br>
Johan Nel wrote:
<blockquote cite="mid:gtfhei$gs9$1(at)news(dot)motzarella(dot)org" type="cite">Adam
B wrote:
<br>
<blockquote type="cite">Hello all,
<br>
<br>
Is it possible to prevent Postgre from aborting the transaction upon a
constraint violation?
<br>
</blockquote>
>From the help files maybe the following could get you on the right
track:
<br>
<br>
This example uses exception handling to perform either UPDATE or
INSERT, as appropriate:
<br>
CREATE TABLE db (a INT PRIMARY KEY, b TEXT);
<br>
CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
<br>
$$
<br>
BEGIN
<br>
    LOOP
<br>
        -- first try to update the key
<br>
        UPDATE db SET b = data WHERE a = key;
<br>
        IF found THEN
<br>
            RETURN;
<br>
        END IF;
<br>
        -- not there, so try to insert the key
<br>
        -- if someone else inserts the same key concurrently,
<br>
        -- we could get a unique-key failure
<br>
        BEGIN
<br>
            INSERT INTO db(a,b) VALUES (key, data);
<br>
            RETURN;
<br>
        EXCEPTION WHEN unique_violation THEN
<br>
            -- do nothing, and loop to try the UPDATE again
<br>
        END;
<br>
    END LOOP;
<br>
END;
<br>
$$
<br>
LANGUAGE plpgsql;
<br>
<br>
HTH,
<br>
<br>
Johan Nel
<br>
Pretoria, South Africa.
<br>
<br>
</blockquote>
<br>
<BR />
<BR />
<HR />
Videx&nbsp;Inc.&nbsp;1105&nbsp;N.&nbsp;E.&nbsp;Circle&nbsp;Blvd.&nbsp;Corvallis&nbsp;OR&nbsp;97330&nbsp;(541)&nbsp;758-0521<BR />
CONFIDENTIAL&nbsp;COMMUNICATION:&nbsp;The&nbsp;email&nbsp;message&nbsp;and&nbsp;any&nbsp;attachments&nbsp;are&nbsp;intended&nbsp;only&nbsp;for&nbsp;the&nbsp;addressee.&nbsp;&nbsp;They&nbsp;may&nbsp;be&nbsp;privileged,&nbsp;confidential,&nbsp;and&nbsp;protected&nbsp;from&nbsp;disclosure.&nbsp;If&nbsp;you&nbsp;are&nbsp;not&nbsp;the&nbsp;intended&nbsp;recipient,&nbsp;any&nbsp;dissemination,&nbsp;distribution,&nbsp;or&nbsp;copying&nbsp;is&nbsp;expressly&nbsp;prohibited.&nbsp;&nbsp;If&nbsp;you&nbsp;received&nbsp;this&nbsp;email&nbsp;message&nbsp;in&nbsp;error,&nbsp;please&nbsp;notify&nbsp;the&nbsp;sender&nbsp;immediately&nbsp;by&nbsp;replying&nbsp;to&nbsp;this&nbsp;e-mail&nbsp;message&nbsp;or&nbsp;by&nbsp;telephone<BR />
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 2.7 KB

In response to

Browse pgsql-general by date

  From Date Subject
Next Message rwade 2009-05-02 01:54:59 function in pgAdmin
Previous Message Adam B 2009-05-01 22:48:41 Re: Possible to prevent transaction abort?