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.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability High (Few or no false-positive results)
License MIT (opens in new tab)
Fixing Suggestion Associate the routine with a proper trigger or change the routine.
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
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

This query belongs to the following collections:

Find problems automatically

Queries, 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 .

NameDescription
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

This query is classified under the following categories:

Triggers and rules

Queries of this category provide information about triggers and rules in a database.

NameDescription
Triggers and rulesQueries of this category provide information about triggers and rules in a database.