Given the following two functions, where the only difference between them is that one is VOLATILE while the other is IMMUTABLE:
CREATE FUNCTION raise_exception_volatile(IN err_message text)
RETURNS void
LANGUAGE 'plpgsql'
VOLATILE
AS $BODY$
BEGIN
RAISE EXCEPTION
USING MESSAGE = err_message;
END;
$BODY$;
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$;
1. This raises an exception, as expected:
SELECT raise_exception_volatile('foo') WHERE true;
-- ERROR: foo
-- CONTEXT: PL/pgSQL function raise_exception_volatile(text) line 3 at RAISE
2. This does not raises an exception, as expected:
SELECT raise_exception_volatile('foo') WHERE false;
3. This raises an exception, as expected:
SELECT raise_exception_immutable('foo') WHERE true;
-- ERROR: foo
-- CONTEXT: PL/pgSQL function raise_exception_immutable(text) line 3 at RAISE
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
5. This does not raises an exception, that was also surprising to me:
SELECT raise_exception_immutable(format('foo')) WHERE false;
Tested on a postgres:12.8-alpine docker container.
Is this behaviour expected?
Best regards,