Goal This query identifies trigger functions that specify the TABLE (TABLE_NAME) or SCHEMA (SCHEMA_NAME) diagnostic option in a RAISE EXCEPTION ... USING clause using hardcoded string literals (e.g., TABLE = 'vastuvott', SCHEMA = 'public') rather than the built-in PL/pgSQL special trigger variables TG_TABLE_NAME and TG_TABLE_SCHEMA. Hardcoding table and schema names inside trigger exception blocks makes the database schema fragile and harder to maintain. If a table is later renamed (ALTER TABLE ... RENAME TO ...), moved to a different schema (ALTER TABLE ... SET SCHEMA ...), or if the trigger function is reused across multiple tables, hardcoded diagnostic literals silently become stale and report inaccurate metadata to client drivers, ORMs, and logs.
Notes Queries the PostgreSQL system catalogs to inspect the source code (prosrc) of user-defined trigger functions. Using case-insensitive regular expressions with negative lookaheads, the query flags any RAISE EXCEPTION statement where TABLE (or TABLE_NAME) is not assigned TG_TABLE_NAME (or TG_RELNAME), or where SCHEMA (or SCHEMA_NAME) is not assigned TG_TABLE_SCHEMA. In the returned routine definition, newline characters are replaced with HTML line break (<br>) tags for better readability in a web browser.
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)
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,
        EXISTS (
            SELECT 1 
            FROM pg_constraint AS c 
            WHERE c.oid = t.tgconstraint 
              AND c.contype = 't'
        ) AS is_constraint_trigger,
        t.tginitdeferred AS is_init_deferred
    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 
    Tr.trigger_schema, 
    Tr.trigger_table, 
    Tr.action_timing, 
    Tr.event_manipulation, 
    Tr.action_orientation, 
    Tr.action_condition, 
    string_agg(event_object_column::text, ',<br>' ORDER BY event_object_column) AS update_columns, 
    Tr.is_constraint_trigger,  
    Tr.is_init_deferred, 
    Tr.trigger_name, 
    Tr.routine_name, 
    regexp_replace(Tr.routine_definition, '[\r\n]', '<br>', 'g') AS routine_definition
FROM Tr 
LEFT JOIN information_schema.triggered_update_columns AS TUC 
    ON Tr.trigger_schema = TUC.trigger_schema 
   AND Tr.trigger_name = TUC.trigger_name 
   AND Tr.trigger_table = TUC.event_object_table
WHERE 
    Tr.routine_definition ~* '\yRAISE[[:space:]]+EXCEPTION\y[^;]*\yTABLE(_NAME)?[[:space:]]*=(?![[:space:]]*(TG_TABLE_NAME|TG_RELNAME)\y)'
    OR 
    Tr.routine_definition ~* '\yRAISE[[:space:]]+EXCEPTION\y[^;]*\ySCHEMA(_NAME)?[[:space:]]*=(?![[:space:]]*TG_TABLE_SCHEMA\y)'
GROUP BY 
    Tr.trigger_schema, 
    Tr.trigger_table, 
    Tr.action_timing, 
    Tr.event_manipulation, 
    Tr.action_orientation, 
    Tr.action_condition, 
    Tr.is_constraint_trigger,  
    Tr.is_init_deferred, 
    Tr.trigger_name, 
    Tr.routine_name, 
    regexp_replace(Tr.routine_definition, '[\r\n]', '<br>', 'g')
ORDER BY 
    Tr.trigger_schema, 
    Tr.trigger_table, 
    Tr.event_manipulation, 
    Tr.action_timing, 
    Tr.action_orientation, 
    Tr.trigger_name;

Fixing Suggestion

Replace hardcoded table and schema string literals in the RAISE EXCEPTION ... USING clause with the built-in PL/pgSQL trigger variables: TABLE = TG_TABLE_NAME and SCHEMA = TG_TABLE_SCHEMA.

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.