From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joel Mukuthu <jom@upright.co>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: Unexpected behaviour of a RAISE statement in an IMMUTABLE function
Date: Wed, 23 Nov 2022 11:20:26 -0500
Message-ID: <3712628.1669220426@sss.pgh.pa.us> (raw)
In-Reply-To: <CAAT35tGXUYgjjViNZ5+9nFkrOcmgE4ce+VvekjgKDy_C38RT2g@mail.gmail.com>
References: <CAAT35tGXUYgjjViNZ5+9nFkrOcmgE4ce+VvekjgKDy_C38RT2g@mail.gmail.com>
Joel Mukuthu <jom@upright.co> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, jom@upright.co, pgsql-bugs@lists.postgresql.org
Subject: Re: Unexpected behaviour of a RAISE statement in an IMMUTABLE function
In-Reply-To: <3712628.1669220426@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox