Goal This query identifies SECURITY DEFINER routines defined with a SQL-standard body (introduced in PostgreSQL 14) that explicitly specify a search_path. While setting the search_path is critical for securing older routine types, it is technically redundant for SQL-standard bodies because they securely resolve objects at parse time. Nevertheless, developers might intentionally configure the path to maintain code consistency and improve readability across the database schema.
Type General Overview of some aspect of the database.
License MIT (opens in new tab)
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
SELECT 
  pg_namespace.nspname AS routine_schema, 
  pg_proc.proname AS routine_name,
pg_get_function_identity_arguments(pg_proc.oid) AS parameters,
CASE WHEN pg_proc.prokind='f' THEN 'FUNCTION'
  WHEN pg_proc.prokind='p' THEN 'PROCEDURE'  
  WHEN pg_proc.prokind='w' THEN 'WINDOW FUNCTION' END AS routine_type,
  array_to_string(proconfig, ',') AS search_path,
  regexp_replace(pg_get_functiondef(pg_proc.oid),'[\r\n]','<br>','g')  AS routine_src
FROM 
  pg_catalog.pg_proc, 
  pg_catalog.pg_namespace
WHERE 
  pg_proc.pronamespace = pg_namespace.oid
 AND prosecdef=TRUE
 AND pg_proc.prokind<>'a'
 AND (array_to_string(proconfig, ';') IS NULL OR
 array_to_string(proconfig, ';') !~ 'search_path=[^=]*pg_temp(;|$)')
 AND pg_get_functiondef(pg_proc.oid)~'\yBEGIN\y[[:space:]]+\yATOMIC\y.*\yEND\y'
AND pg_namespace.nspname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND NOT EXISTS (SELECT 1
FROM pg_catalog.pg_depend d 
WHERE EXISTS (SELECT 1 FROM pg_catalog.pg_extension e WHERE d.refobjid=e.oid) AND
d.objid=pg_proc.oid)
ORDER BY routine_schema, routine_name, parameters;

Fixing Suggestion

Since this configuration does not pose a security risk or functional error, no immediate corrective action is strictly required. However, you should align these routines with your project's coding standards. You can either remove the redundant search_path configurations to keep the SQL-standard definitions minimal, or consciously retain them to maintain uniform documentation and visual consistency across all routines.

Categories

This query is classified under the following categories:

Security

Queries of this category provide information about the security measures.

User-defined routines

Queries of this category provide information about the user-defined routines

NameDescription
SecurityQueries of this category provide information about the security measures.
User-defined routinesQueries of this category provide information about the user-defined routines