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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Logan Grosz <logan(dot)grosz(at)gmail(dot)com>
Cc: 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:31:04
Message-ID: 805459.1749591064@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Logan Grosz <logan(dot)grosz(at)gmail(dot)com> writes:
> 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?

It'd be better just to have the CHECK expression return false.
Admittedly, that's usually just going to end in an exception,
but pre-judging that inside the expression doesn't seem ideal.
An example of why not is that you'd break "soft" input error
handling in COPY.

As you say, there are some cases where it's hard to avoid
an exception, but I'm not sure that "better error message"
is a good justification for throwing one. Still, in the
end it's your own judgment to make.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Hayato Kuroda (Fujitsu) 2025-06-11 02:31:44 RE: Logical Replication Memory Allocation Error - "invalid memory alloc request size"
Previous Message Ron Johnson 2025-06-10 21:28:31 Re: Is it correct to raise an exception in a domain check