Goal Find the number of user-defined non-trigger routines based on their schema, language, and routine type. The routines can be used to implement virtual data layer. Thus the queriy gives some indications about the possible extent of the layer.
Notes The query only returns data about schemas that have at least one routine. The query excludes routines that are a part of an extension. ROLLUP ensures that the number of routines based on some combinations of the properties and the total number of routines is found.
Type Sofware measure (Numeric values (software measures) about the database)
License MIT License
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH routines AS (
SELECT 
routine_schema, 
coalesce(routine_type,'OTHER') AS routine_type,
external_language
FROM 
information_schema.routines
WHERE routine_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 (data_type<>'trigger' OR data_type IS NULL)
AND routine_name NOT IN ('f_assume_you_must_use_files', 'f_check_format_comma_separated_list', 'f_check_password', 'f_default_value_with_no_match')
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 = routines.specific_name))
SELECT routine_schema, routine_type, external_language, Count(*) AS cnt
FROM routines
GROUP BY cube (routine_schema,  routine_type, external_language)
ORDER BY routine_schema,  routine_type, external_language, Count(*) DESC;

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 .
Find quick numeric overview of the databaseQueries that return numeric values showing mostly the number of different types of database objects in the database
Categories

This query is classified under the following categories:

NameDescription
User-defined routinesQueries of this category provide information about the user-defined routines