Goal This query identifies row-level BEFORE triggers that invoke a RAISE WARNING or RAISE EXCEPTION statement without any conditional logic—neither in the trigger's WHEN clause nor within the function body. This means that the trigger unconditionally raises the message for every affected row. This anti-pattern either entirely blocks the triggering DML operation (in the case of an error) or floods the database logs (in the case of a warning), which typically indicates left-over debugging code or an incomplete implementation.
Type Problem detection Each row in the result could represent a flaw in the design
Reliability Medium Medium number of false-positive results
License MIT (opens in new tab)
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH Tr AS (SELECT
    n.nspname AS trigger_schema,
    t.tgname AS trigger_name,
    em.text AS event_manipulation,        
    c.relname AS trigger_table, 
    np.nspname AS routine_schema,
    p.proname AS routine_name,
     p.prosrc AS routine_definition,
     (SELECT DISTINCT action_condition FROM INFORMATION_SCHEMA.triggers AS it WHERE it.trigger_schema=n.nspname AND it.event_object_table=c.relname AND it.trigger_name=t.tgname) AS action_condition,    
        CASE t.tgtype::integer & 1
            WHEN 1 THEN 'ROW'
            ELSE 'STATEMENT'
        END AS action_orientation,
        CASE t.tgtype::integer & 66
            WHEN 2 THEN 'BEFORE'
            WHEN 64 THEN 'INSTEAD OF'
            ELSE 'AFTER'
        END AS action_timing	
   FROM pg_namespace n,
    pg_class c,
    pg_trigger t,
    pg_proc p,
    pg_namespace np,
    ( VALUES (4,'INSERT'), (8,'DELETE'), (16,'UPDATE'), (32,'TRUNCATE')) em(num, text)
  WHERE n.oid = c.relnamespace 
  AND c.oid = t.tgrelid 
  AND t.tgfoid=p.oid
  AND p.pronamespace=np.oid
  AND (t.tgtype::integer & em.num) <> 0 
  AND NOT t.tgisinternal 
  AND NOT pg_is_other_temp_schema(n.oid))
SELECT trigger_schema, trigger_table, action_timing, event_manipulation, action_orientation, action_condition, routine_name, regexp_replace(routine_definition,'[\r\n]','<br>','g')  AS routine_definition
FROM Tr
WHERE action_orientation='ROW' 
AND action_timing='BEFORE'
AND routine_definition~* '[[:space:]]+\y(WARNING|EXCEPTION)\y[[:space:]]+'
AND routine_definition!~* '\yIF\y[[:space:]][^;]*[[:space:]]+\y(WARNING|EXCEPTION)\y[[:space:]]+'
AND action_condition IS NULL
ORDER BY trigger_schema, trigger_table, routine_name;

Fixing Suggestion

To resolve this issue, review the trigger's intended business logic. If the trigger is meant to enforce a specific rule on individual records, wrap the RAISE statement within an IF condition or add a WHEN clause. However, if the actual goal is to completely forbid a specific DML operation (e.g., preventing all UPDATEs on an append-only table), replace the row-level trigger with a statement-level trigger (FOR EACH STATEMENT), as it is significantly more efficient and semantically correct. If the unconditional error was merely leftover debug code, drop the trigger entirely.

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.