Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a function is immutable?

Tags:

postgresql

I didn't find any information in the PostgreSQL documentation about the classification of translate() function. Is it classified as an immutable function?

like image 461
Rodrigo Avatar asked Jan 26 '23 19:01

Rodrigo


1 Answers

Query the system catalog pg_proc:

provolatile tells whether the function's result depends only on its input arguments, or is affected by outside factors. It is i for “immutable” functions, which always deliver the same result for the same inputs. It is s for “stable” functions, whose results (for fixed inputs) do not change within a scan. It is v for “volatile” functions, whose results might change at any time.

select proname, pronamespace::regnamespace, provolatile
from pg_proc
where proname = 'translate'

  proname  | pronamespace | provolatile 
-----------+--------------+-------------
 translate | pg_catalog   | i
(1 row) 

Alternatively, use the function pg_get_functiondef():

select pg_get_functiondef('pg_catalog.translate'::regproc)

                        pg_get_functiondef                         
-------------------------------------------------------------------
 CREATE OR REPLACE FUNCTION pg_catalog.translate(text, text, text)+
  RETURNS text                                                    +
  LANGUAGE internal                                               +
  IMMUTABLE STRICT                                                +
 AS $function$translate$function$                                 +

(1 row)
like image 82
klin Avatar answered Jan 30 '23 01:01

klin