Skip to content

Instantly share code, notes, and snippets.

@jianhe-fun
Created April 2, 2023 13:14
Show Gist options
  • Save jianhe-fun/3c8f560d35e95903bdeb82c0b6325879 to your computer and use it in GitHub Desktop.
Save jianhe-fun/3c8f560d35e95903bdeb82c0b6325879 to your computer and use it in GitHub Desktop.
properly set search_path in cluster, database, role, session, function level.
--https://stackoverflow.com/questions/9067335/how-does-the-search-path-influence-identifier-resolution-and-the-current-schema/9067777#9067777
--properly set search path.
--Set it in cluster level.
ALTER SYSTEM SET search_path TO 'public';
SELECT pg_reload_conf();
--Set it as default for database test16.
ALTER DATABASE test16 SET search_path = '$user', 'public';
-- Set it as default for the role (bob) you connect with (effective cluster-wide):
ALTER ROLE bob SET search_path = '$user', 'public';
--Set as default for a role(bob) in a database (test16).
ALTER ROLE bob IN DATABASE test16 SET search_path = '$user', 'public';
--Set it in a session level.
SET search_path = '$user', 'public';
--set it for the function.
CREATE OR REPLACE FUNCTION test_search_path ()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = new, public, pg_temp
AS $fun$
BEGIN
--query part
END
$fun$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment