Catching errors inside a function

From: Francesco Casadei <f_casadei(at)libero(dot)it>
To: pgsql-general(at)postgresql(dot)org
Subject: Catching errors inside a function
Date: 2001-09-03 18:07:28
Message-ID: 20010903200727.A5843@goku.kasby
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I want to execute an INSERT query that may fail due to a primary key constraint
check. The primary key is on a field whose value is generated randomly by
another C function.
Is it possible to wrap the insert into a function that checks for failures and
retry the insert until success? Something like this:

function blah
{
do {
cod = generate code;
execute insert with cod as primary key;
} while (! errors);
}

I tried with SQL, PL/pgSQL and C functions but the backend aborts the
transaction and stops execution of the function as soon as the error is thrown.
Is there a way to disable this behaviour?

Thanks in advance for your help.

Francesco Casadei

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-09-03 18:42:20 Re: Question about plan
Previous Message Gilles DAROLD 2001-09-03 18:03:02 PG log analyzer v1.1