Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kpuLB-0006XH-Af for pgsql-general@arkaria.postgresql.org; Thu, 17 Dec 2020 14:33:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kpuKd-000276-Ak for pgsql-general@arkaria.postgresql.org; Thu, 17 Dec 2020 14:32:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kpuKb-00026z-PW for pgsql-general@lists.postgresql.org; Thu, 17 Dec 2020 14:32:39 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kpuKX-0006kQ-LI for pgsql-general@postgresql.org; Thu, 17 Dec 2020 14:32:37 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 98CB8896 for ; Thu, 17 Dec 2020 09:32:30 -0500 (EST) Received: from imap2 ([10.202.2.52]) by compute5.internal (MEProxy); Thu, 17 Dec 2020 09:32:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= mime-version:message-id:date:from:to:subject:content-type; s= fm3; bh=RVfgO7kQ6mJy5OA6hRsPWr1OfaxbU1XAx1ANxARSL54=; b=IIQlnNCk oPrmMIyYfqr9OawLvm8gekO4hm0z+KjzxNEjOVQsXVKyE8/ToTJrHTbiztRqU5Hw QPqvt2KAplKTxZYRgZfdjCmQsY2sFkZXR8oF5JmwCfDywkvguePscak3K+r57GPR ta7g3Wu52U38jKWpZ7ISki8Xxp4tK0+kc4XSsG1JRXWQQYTRfzx7MMY64QXqDsiM VtvmqYEK8nNZcG0H+UGCr3eJU1BKrSX0eSFunbe+pBAb3EH5bJdUa1+XEN7j9dnL 5+Rgbk6DuosYTKOTb7iL2RPtjPYqn62jok84vay5UDZaMgmJyW6j94a3YjyZbBMM GoDh5sU38LwU9A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=RVfgO7kQ6mJy5OA6hRsPWr1OfaxbU 1XAx1ANxARSL54=; b=k2lrOyR1ip54a1uUiK+/PtObtZwZ5cagOw6kYFxtxGu57 aTgRTAdvzCl0U2g9EAeXCBUf8baGdIu5okP/N9+yZrnqeyJ9tawHqWh/xobxK4UF h+4N8Im8bjIR+8hFEGlE5/tqqDYM3WxenkiBWVIgUqBsBWbplywYbyxk8ROoUD4a RFu1GbVw7b6hRewjJYIti7mEknA/fz0jk3aTgXLE7XwTyvH6IJO09w0iYaufo2ki i5xht0p9k01tTVF/fBlJ6fEzZyCmCDXoa9IPP+E0rQtmGCQKlZLj4GwmRMNQ2VsY F+0KtZtqmLJWJHOy3zn8pISgJ8PQATI/nRpkWBzyw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudelgedgieefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsegrtderre erredtnecuhfhrohhmpedflfhovghlucflrggtohgsshhonhdfuceojhhovghlsegtohhm phhilhgvrhdrohhrgheqnecuggftrfgrthhtvghrnhepgefgvdfhgfefjedtleegteejje fhieegjedvhfehueetfeektdeiieefheffkeelnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomhepjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id A926CE0505; Thu, 17 Dec 2020 09:32:27 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.1-61-gb52c239-fm-20201210.001-gb52c2396 Mime-Version: 1.0 Message-Id: <7479ca12-af3c-44c3-9ddd-a2074eb82a12@www.fastmail.com> Date: Thu, 17 Dec 2020 15:32:09 +0100 From: "Joel Jacobson" To: pgsql-general@postgresql.org Subject: Raise exception without using plpgsql? Content-Type: multipart/alternative; boundary=7cf0a6d78f93461da35570f6bb041ed6 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --7cf0a6d78f93461da35570f6bb041ed6 Content-Type: text/plain Hi, Is there a way to raise an exception with a message, without having to add your own plpgsql helper-function? Currently this is what I have: CREATE OR REPLACE FUNCTION raise(message text, debug json, dummy_return_value anyelement) RETURNS anyelement LANGUAGE plpgsql AS $$ BEGIN RAISE '% %', message, debug; END; $$; The dummy value allows using the function in a context where a value of the given type is expected. Here is a mockup example on the coding pattern where I typically would use this raise() helper-function: SELECT CASE WHEN foo = 0 THEN f0(bar)::baz WHEN foo = 1 THEN f1(bar)::baz WHEN foo = 2 THEN f2(bar)::baz ELSE raise('Not implemented',json_build_object( 'foo',foo, 'bar',bar ),NULL::baz) END FROM ... The idea is to throw an exception when a case is not handled, instead of just letting the CASE produce a NULL value. The dummy_return_value anyelement with the same type as the other CASEs is necessary, otherwise, if trying to just return anyelement without the dummy_return_value you would get error: DETAIL: A result of type anyelement requires at least one input of type anyelement, anyarray, anynonarray, anyenum, or anyrange. Is there any idiomatic way of achieving the same, without having to rely on a plpgsql function in this way? Best regards, Joel --7cf0a6d78f93461da35570f6bb041ed6 Content-Type: text/html Content-Transfer-Encoding: quoted-printable
Hi,

Is there a way to raise an exception with a message,
without having to add your own plpgsql helper-function?
=

Currently this is what I have:
<= br>
CREATE OR REPLACE FUNCTION raise(message text, debug json,= dummy_return_value anyelement)
RETURNS anyelement
LANGUAGE plpgsql
AS $$
BEGIN
RAISE '% %', message, debug;
END;
$$;<= br>

The dummy value allows using the function i= n a context where a value of the given type is expected.
<= br>
Here is a mockup example on the coding pattern where I typ= ically would use this raise() helper-function:

=
SELECT
  CASE
    W= HEN foo =3D 0 THEN f0(bar)::baz
    WHEN fo= o =3D 1 THEN f1(bar)::baz
    WHEN foo =3D = 2 THEN f2(bar)::baz
    ELSE raise('Not imp= lemented',json_build_object(
    'foo',foo,=
    'bar',bar
  &n= bsp; ),NULL::baz)
  END
FROM ...

The idea is to throw an exception when a case is = not handled, instead of just letting the CASE produce a NULL value.
<= /div>

The dummy_return_value anyelement with the= same type as the other CASEs is necessary, otherwise, if trying to just= return anyelement without the dummy_return_value you would get error:

DETAIL:  A result of type anyelement re= quires at least one input of type anyelement, anyarray, anynonarray, any= enum, or anyrange.

Is there any idiomatic w= ay of achieving the same, without having to rely on a plpgsql function i= n this way?

Best regards,
Joel
--7cf0a6d78f93461da35570f6bb041ed6--