Postgresql – How to check and change function volatility in Postgres

ddlfunctionspostgresql

I have a function that I think is defined as volatile, but I now know I want it stable.

How can I check the function volatility (volatile, stable, immutable) of a certain function?

When a function is defined as volatile, can I change the volatility to stable by using a create or replace function query? Or does the function needs to be dropped and created again but this time as stable?

create or replace function my_function()
returns int as $$
    select a
    from my_table
    order by 1
    limit 1
$$ language sql stable;

I checked the documentation for this, but it doesn't mention changing volatility.

Best Answer

I checked the documentation again, and the answer to my second question is, yes you can alter it using the alter function query.

alter function my_function() stable;

Also, I see that checking for function volatility has been asked and answered before.

https://stackoverflow.com/questions/54452415/how-to-check-whether-a-function-is-immutable