Re: Is it correct to raise an exception in a domain check

From: Ron Johnson <ronljohnsonjr(at)gmail(dot)com>
To: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Is it correct to raise an exception in a domain check
Date: 2025-06-10 21:28:31
Message-ID: CANzqJaBYUDRua2LYQHp6rZVAR2OUVe_65Hw-NFd_gA1pxksFzQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jun 10, 2025 at 4:53 PM Logan Grosz <logan(dot)grosz(at)gmail(dot)com> wrote:

> Hi,
>
> I have a `DOMAIN` on `JSONB`. I need to verify the shape of the JSON, so I
> have a check expression written in PL/pgSQL. The docs say
>
> > Each constraint must be an expression producing a Boolean result
>
> Would it be correct to raise an exception to indicate a failed check? I
> perform type-casts (that throw already) and other checks that would benefit
> from attaching a description to the client error.
>

Since PG will throw an error when any CHECK constraint fails, are you
hoping to provide more details by throwing your own exception?

--
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2025-06-10 21:31:04 Re: Is it correct to raise an exception in a domain check
Previous Message Adrian Klaver 2025-06-10 20:17:05 Re: pg_upgradecluster version 10 to 16 question