Goal This query identifies trigger functions that compare the special trigger variables TG_TABLE_NAME or TG_RELNAME against hardcoded string literals (e.g., IF TG_TABLE_NAME = 'table_a' THEN ...). Branching on hardcoded table names inside a shared trigger function is a design and maintainability anti-pattern for several reasons:

  • Fragility during table renaming: When a table is renamed via ALTER TABLE ... RENAME TO ..., PostgreSQL automatically updates catalog references (such as pg_trigger), so the trigger continues to fire on the renamed table. However, hardcoded string literals inside the PL/pgSQL routine body are not updated, causing conditions like IF TG_TABLE_NAME = 'old_name' to silently evaluate to FALSE and execute the wrong ELSE branch or skip logic entirely.

  • Code duplication or poor cohesion: If multiple tables share the exact same validation logic and only differ in diagnostic metadata (such as constraint names), branching on TG_TABLE_NAME leads to unnecessary copy-pasted RAISE EXCEPTION blocks. Conversely, if the tables require genuinely different business logic, coupling them inside a single conditional trigger function violates the Single Responsibility Principle (SRP).

Notes Queries the PostgreSQL system catalogs (pg_proc, pg_namespace, pg_type) to inspect the source code (prosrc) of user-defined trigger functions (typname = 'trigger'). The query uses a case-insensitive regular expression to detect comparisons where TG_TABLE_NAME or the legacy alias TG_RELNAME is compared against a string literal using =, <>, !=, or IN (...). In the returned routine source code, 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
SELECT 
    n.nspname AS routine_schema,
    p.proname AS routine_name,
    regexp_replace(p.prosrc, '[\r\n]', '<br>', 'g') AS routine_src
FROM pg_catalog.pg_proc p
INNER JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
INNER JOIN pg_catalog.pg_type t ON t.oid = p.prorettype
WHERE t.typname = 'trigger'
  AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')  
  AND p.prosrc ~* '\yTG_(TABLE_NAME|RELNAME)\y[[:space:]]*(=|<>|!=|\yIN\y)[[:space:]]*\(?[[:space:]]*''[^'']+'''
ORDER BY routine_schema, routine_name;

Fixing Suggestion

Eliminate the hardcoded TG_TABLE_NAME comparison:



  • If the trigger logic is identical across tables and only diagnostic strings (such as the CONSTRAINT name) differ, construct the identifier dynamically (e.g., CONSTRAINT = TG_TABLE_NAME || '_rule_suffix') or pass table-specific values as trigger arguments in CREATE TRIGGER ... EXECUTE FUNCTION fn_name('arg') and read them via TG_ARGV[0].

  • If the tables require distinct business rules, split the shared routine into separate, dedicated trigger functions for each table.

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.