Re: Unexpected behaviour of a RAISE statement in an IMMUTABLE function - Mailing list pgsql-bugs

From Tom Lane
Subject Re: Unexpected behaviour of a RAISE statement in an IMMUTABLE function
Date
Msg-id 3712628.1669220426@sss.pgh.pa.us
Whole thread Raw
In response to Unexpected behaviour of a RAISE statement in an IMMUTABLE function  (Joel Mukuthu <jom@upright.co>)
Responses Re: Unexpected behaviour of a RAISE statement in an IMMUTABLE function  (Joel Mukuthu <jom@upright.co>)
List pgsql-bugs
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



pgsql-bugs by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Unexpected behaviour of a RAISE statement in an IMMUTABLE function
Next
From: Tom Lane
Date:
Subject: Re: BUG #17693: Slow performance: Much slower queries on pg_stat_all_tables since 13.4