The list of all the queries

The number of user-defined routines based on their database usage

Query goal: Find the number of non-extension routines in a database that modify data. The routines can be used to implement virtual data layer. Thus the queriy gives some indications about the possible extent of the layer. The query distinguishes trigger and non-trigger routines, thus it also gives information about triggers that modify data.
Notes about the query: 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.
Query type: Sofware measure (Numeric values (software measures) about the database)
Query license: MIT License
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

WITH routines AS (SELECT 
 p.oid AS routine_oid,
  n.nspname AS routine_schema, 
  p.proname AS routine_name, 
  CASE WHEN t.typname='trigger' THEN 'TRIGGER ROUTINE' ELSE 'NON-TRIGGER ROUTINE' END AS routine_type,
  p.prosrc  AS routine_src
FROM 
pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON p.pronamespace = n.oid
JOIN pg_catalog.pg_language l ON p.prolang = l.oid
LEFT JOIN pg_catalog.pg_type t ON p.prorettype=t.oid
WHERE p.proname NOT IN ('f_assume_you_must_use_files', 'f_check_format_comma_separated_list', 'f_check_password', 'f_default_value_with_no_match') 
AND n.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=p.oid))
SELECT 
routine_schema, 
routine_type,
Count(*) AS number_of_routines,
Count(*) FILTER (WHERE routine_src~*'(insert|update|delete)[[:space:]]') AS number_of_routines_that_modify_data, 
Count(*) FILTER (WHERE routine_src!~*'(insert|update|delete)[[:space:]]' AND routine_src~*'select[[:space:]][^;]+from[[:space:]]') AS number_of_routines_that_only_read_data 
FROM routines
GROUP BY rollup (routine_schema,  routine_type)
ORDER BY routine_schema,  routine_type, Count(*) DESC;

Collections where the query belongs to

Collection nameCollection description
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 where the query belongs to

Category nameCategory description
Triggers and rulesQueries of this category provide information about triggers and rules in a database.
User-defined routinesQueries of this category provide information about the user-defined routines

The list of all the queries