The list of all the queries

Trigger routines with TG_OP variable that are not associated with a suitable trigger

Query goal: Automatically defined TG_OP variable in a trigger function has data type text. Its value is a string of INSERT, UPDATE, DELETE, or TRUNCATE telling for which operation the trigger was fired. Find the routines that according to the TG_OP value must react to a certain operation but the routine is not associated with any triggers that are fired by the operation. For instance, the routine specifies reaction to DELETE operation but the routine is not associated with any DELETE trigger.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: High (Few or no false-positive results)
Query license: MIT License
Fixing suggestion: Associate the routine with a proper trigger or change the routine.
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

WITH trigger_routines_with_tg_op AS (SELECT
    p.oid,
    np.nspname AS routine_schema,
    p.proname AS routine_name,
     p.prosrc AS routine_definition,
     regexp_replace(p.prosrc,'[\r\n]','<br>','g')  AS routine_definition_format
   FROM pg_proc p,
    pg_namespace np,
    pg_type
  WHERE p.pronamespace=np.oid
  AND p.prorettype=pg_type.oid
  AND pg_type.typname='trigger'
  AND np.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 p.prosrc~*'TG_OP')
SELECT routine_schema, routine_name, routine_definition_format, 'Is not associated with an INSERT trigger' AS explanation
FROM trigger_routines_with_tg_op AS tp
WHERE routine_definition~*'''INSERT'''
AND NOT EXISTS (SELECT *
FROM pg_trigger
WHERE pg_trigger.tgfoid=tp.oid
AND (tgtype::integer & 4)<>0)
UNION SELECT routine_schema, routine_name, routine_definition_format, 'Is not associated with an UPDATE trigger' AS explanation
FROM trigger_routines_with_tg_op AS tp
WHERE routine_definition~*'''UPDATE'''
AND NOT EXISTS (SELECT *
FROM pg_trigger
WHERE pg_trigger.tgfoid=tp.oid
AND (tgtype::integer & 16)<>0)
UNION SELECT routine_schema, routine_name, routine_definition_format, 'Is not associated with a DELETE trigger' AS explanation
FROM trigger_routines_with_tg_op AS tp
WHERE routine_definition~*'''DELETE'''
AND NOT EXISTS (SELECT *
FROM pg_trigger
WHERE pg_trigger.tgfoid=tp.oid
AND (tgtype::integer & 8)<>0)
UNION SELECT routine_schema, routine_name, routine_definition_format, 'Is not associated with a TRUNCATE trigger' AS explanation
FROM trigger_routines_with_tg_op AS tp
WHERE routine_definition~*'''TRUNCATE'''
AND NOT EXISTS (SELECT *
FROM pg_trigger
WHERE pg_trigger.tgfoid=tp.oid
AND (tgtype::integer & 32)<>0)
ORDER BY routine_schema, routine_name;

Collections where the query belongs to

Collection nameCollection description
Find problems automaticallyQueries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .

Categories where the query belongs to

Category nameCategory description
Triggers and rulesQueries of this category provide information about triggers and rules in a database.

Reference materials for further reading

Reference
https://www.postgresql.org/docs/current/plpgsql-trigger.html

The list of all the queries