Goal Find routines with the same parameters (same name and type) regardless of the order of parameters. Make sure that there is no accidental duplication. The query helps users to group together routines that probably have related tasks.
Notes The query does not consider the routines that are a part of an extension. In case of the string_agg function, the line break (br) tag is used as a part of the separator for the better readability in case the query result is displayed in a web browser.
Type General (Overview of some aspect of the database.)
License MIT License
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH parameters_grouped AS (SELECT p.specific_schema AS routine_schema,
regexp_replace(p.specific_name,'_[0-9]*$','') AS routine_name,
(SELECT r.routine_type FROM information_schema.routines AS r WHERE r.specific_schema=p.specific_schema AND r.specific_name=p.specific_name) AS routine_type, 
pg_get_function_identity_arguments(translate(substring(p.specific_name,'_[0-9]+$'),'_','')::int::oid) AS parameters,
string_agg(p.parameter_name || ' ' || p.data_type || ' ' || p.parameter_mode, ', ' ORDER BY parameter_name, parameter_mode) AS parameters_normalized
FROM 
information_schema.parameters AS p
WHERE specific_schema 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 inner join pg_catalog.pg_proc pc ON d.objid=pc.oid
WHERE EXISTS (SELECT 1 FROM pg_catalog.pg_extension e WHERE d.refobjid=e.oid) AND
pc.proname || '_' || pc.oid = p.specific_name)
GROUP BY p.specific_schema, p.specific_name, parameters, routine_type)
SELECT parameters_normalized, Count(*) AS number_of_occurrences, 
string_agg(routine_type || ' ' || routine_schema || '.' || routine_name || '(' || parameters || ')', ';
' ORDER BY routine_schema, routine_name, parameters) AS routines FROM parameters_grouped GROUP BY parameters_normalized HAVING Count(*)>1 ORDER BY Count(*) DESC, parameters_normalized;
Collections

This query belongs to the following collections:

NameDescription
Find problems by overviewQueries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .
Categories

This query is classified under the following categories:

NameDescription
Duplication of implementation elementsQueries of this catergory provide information about the duplication of the database objects.
User-defined routinesQueries of this category provide information about the user-defined routines