Re: Commit and Exception Block

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: soumik(dot)bhattacharjee(at)kpn(dot)com
Cc: pgsql-admin(at)lists(dot)postgresql(dot)org
Subject: Re: Commit and Exception Block
Date: 2021-10-08 15:03:16
Message-ID: 231422.1633705396@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

<soumik(dot)bhattacharjee(at)kpn(dot)com> writes:
> I have a procedure that may encounter an exception. I want to have an exception handling block where exception is to catch is any FK violations.
> Could you please suggest where it's getting missed as part of a transaction.

I think you're confused about the scope of the exception block.
The syntax is

BEGIN
some code here that might throw an exception
EXCEPTION
some WHEN clauses here to catch exceptions from the covered code
END

You wrote:

> begin
> RAISE SQLSTATE 'MYERR';
> EXCEPTION
> WHEN ...

So this exception block can *only* trap errors arising from that one
RAISE command, not anything earlier in the procedure. Seems unlikely
that's what you wanted.

regards, tom lane

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Tim 2021-10-08 17:22:07 Logical Replication Sync - Issue with Table Bloat
Previous Message soumik.bhattacharjee 2021-10-08 08:00:34 RE: Commit and Exception Block