Building on top of theory's answer, here's how you permanent prepend a schema to another user's search_path. Useful for setting up read-only users and splitting setup of various schemas in different .sql files.
create or replace function prepend_search_path(
role_name text, schema_name text
) returns void as $$
declare
current_search_path text;
begin
-- First, we get the current search_path for that user
select replace(sc.configval,'search_path=','')
from pg_db_role_setting rs
left
join pg_roles r
on r.oid = rs.setrole,
lateral unnest(rs.setconfig) as sc(configval)
where sc.configval like 'search_path=%'
and r.rolname = role_name
into current_search_path;
-- It is possible that a new user is not in pg_roles. To fix this,
-- we find the default search_path values.
if not found then
select boot_val
from pg_settings
where name='search_path'
into current_search_path;
end if;
-- Prepend the schema_name to search_path
if current_search_path !~ ('(^|, )' || schema_name || '(,|$)') then
current_search_path := schema_name || ', ' || current_search_path;
end if;
-- Make the changes
execute format('alter role %I set search_path = %s', role_name, current_search_path);
end
$$ language plpgsql;
Why a prepend? Depends on your use case. IMO, it's useful to have schemas for each stored procedure. That means if you change a stored procedure, you can simply define it in a separate schema and overwrite the search_path of the user that uses it.