How to completely hide a function from all other users and roles?

You can revoke and grant access privileges from/to specific Postgres roles but the name and source of the function will be still visible.

To keep the function in top secret create it in the session in which it is needed, in the schema pg_temp. It is a schema associated with the session for keeping temporary resources. It is automatically created when needed and automatically dropped at the end of the session.

create or replace function pg_temp.top_secret_function()
returns text language sql as $$
    select 'Top secret!'::text;
$$;

select pg_temp.top_secret_function();

CREATE FUNCTION
 top_secret_function 
---------------------
 Top secret!
(1 row)

There is no need to drop the function, it will be removed when the session terminates. Nobody (even you as the same user in another concurrent session) can see the function.


As of Now there is no inbuilt functionality in the PostgreSQL to to give a particular user/role only execute access . The function source code is always visible. klin pointed it out right "source of the function will be still visible.", but somehow its misleading as he added some scripts that does not exactly satisfy the question condition.

I really hope this feature comes soon in postgres.