Re: Catching errors inside a function

From: Francesco Casadei <f_casadei(at)libero(dot)it>
To: Alvaro Herrera <alvherre(at)atentus(dot)com>
Cc: Francesco Casadei <f_casadei(at)libero(dot)it>, pgsql-general(at)postgresql(dot)org
Subject: Re: Catching errors inside a function
Date: 2001-09-04 18:03:08
Message-ID: 20010904200308.B3650@goku.kasby
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Sep 03, 2001 at 03:47:35PM -0400, Alvaro Herrera wrote:
> On Mon, 3 Sep 2001, Francesco Casadei wrote:
>
> > 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.
>
> Wouldn't it be easier if you just used a sequence to generate the
> primary key, if you just want uniqueness?
>
> > 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);
> > }
>
> Any error will abort the transaction. You can't avoid this. Perhaps you
> can try inserting the value outside the transaction, and then open it to
> do whatever you want with the value inserted.
>
> > Is there a way to disable this behaviour?
>
> No.
>
> --
> Alvaro Herrera (<alvherre[(at)]atentus(dot)com>)
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl
>
> end of the original message

Mmmmm... I don't remember why I chose not to use a sequence! Actually, it seems
to me a good idea. I will think about it. Thank you for your suggestion.

Francesco Casadei

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-09-04 18:31:34 Re: Index usage question
Previous Message Francesco Casadei 2001-09-04 17:57:50 Re: Catching errors inside a function