Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uP6Yo-000PWy-BH for pgsql-general@arkaria.postgresql.org; Tue, 10 Jun 2025 21:31:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uP6Ym-00AAjC-Es for pgsql-general@arkaria.postgresql.org; Tue, 10 Jun 2025 21:31:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uP6Ym-00AAj4-48 for pgsql-general@lists.postgresql.org; Tue, 10 Jun 2025 21:31:08 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uP6Yl-001Iyz-0F for pgsql-general@lists.postgresql.org; Tue, 10 Jun 2025 21:31:08 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 55ALV4Oa805460; Tue, 10 Jun 2025 17:31:04 -0400 From: Tom Lane To: Logan Grosz cc: pgsql-general@lists.postgresql.org Subject: Re: Is it correct to raise an exception in a domain check In-reply-to: References: Comments: In-reply-to Logan Grosz message dated "Sun, 08 Jun 2025 22:01:47 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <805458.1749591064.1@sss.pgh.pa.us> Date: Tue, 10 Jun 2025 17:31:04 -0400 Message-ID: <805459.1749591064@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Logan Grosz 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