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 1oxsUB-0007Lq-2m for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Nov 2022 16:20:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oxsU9-0000XO-P6 for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Nov 2022 16:20:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oxsU9-0000X5-Gl for pgsql-bugs@lists.postgresql.org; Wed, 23 Nov 2022 16:20:29 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oxsU7-0006X7-0I for pgsql-bugs@lists.postgresql.org; Wed, 23 Nov 2022 16:20:28 +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 2ANGKQSa3712629; Wed, 23 Nov 2022 11:20:26 -0500 From: Tom Lane To: Joel Mukuthu cc: pgsql-bugs@lists.postgresql.org Subject: Re: Unexpected behaviour of a RAISE statement in an IMMUTABLE function In-reply-to: References: Comments: In-reply-to Joel Mukuthu message dated "Wed, 23 Nov 2022 15:12:43 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3712627.1669220426.1@sss.pgh.pa.us> Date: Wed, 23 Nov 2022 11:20:26 -0500 Message-ID: <3712628.1669220426@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Joel Mukuthu writes: > CREATE FUNCTION raise_exception_immutable(IN err_message text) > RETURNS void > LANGUAGE 'plpgsql' > IMMUTABLE > AS $BODY$ > BEGIN > RAISE EXCEPTION > USING MESSAGE = err_message; > END; > $BODY$; A function with side-effects (like raising an error) isn't really immutable [1]. We do fudge that a bit, since hardly anything could be marked immutable if there were a strict rule about it --- but when the primary point of the function is to cause that side-effect, you can't fudge it. > 4. This raises an exception that was surprising to me: > SELECT raise_exception_immutable('foo') WHERE false; > -- ERROR: foo > -- CONTEXT: PL/pgSQL function raise_exception_immutable(text) line 3 at > RAISE The allegedly-immutable function is evaluated during constant folding. > 5. This does not raises an exception, that was also surprising to me: > SELECT raise_exception_immutable(format('foo')) WHERE false; format() isn't immutable, only stable; so constant-folding can't reach the error. regards, tom lane [1] https://www.postgresql.org/docs/current/xfunc-volatility.html