Goal This query identifies trigger functions that raise an exception with a CONSTRAINT (or CONSTRAINT_NAME) option in the USING clause, but omit the corresponding TABLE (TABLE_NAME) or SCHEMA (SCHEMA_NAME) diagnostic options.


When a native PostgreSQL table constraint (CHECK, UNIQUE, PRIMARY KEY, or FOREIGN KEY) is violated, the database server automatically populates three distinct diagnostic fields in the error response packet: PG_DIAG_SCHEMA_NAME, PG_DIAG_TABLE_NAME, and PG_DIAG_CONSTRAINT_NAME (accessible in PL/pgSQL via GET STACKED DIAGNOSTICS and exposed by client drivers such as JDBC, Npgsql, and psycopg). However, when a trigger simulates a constraint violation using RAISE EXCEPTION, PostgreSQL does not populate the schema and table diagnostic fields automatically. Specifying CONSTRAINT without TABLE = TG_TABLE_NAME and SCHEMA = TG_TABLE_SCHEMA results in an incomplete error payload, breaking client-side error handlers, ORMs, and logging frameworks that rely on the (schema_name, table_name, constraint_name) tuple to uniquely identify and map the violated rule.

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 with positive and negative lookaheads within the scope of a single statement ([^;]+) to find RAISE EXCEPTION statements that include CONSTRAINT (or CONSTRAINT_NAME), but lack either TABLE (TABLE_NAME) or SCHEMA (SCHEMA_NAME). 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 ~* '\yRAISE[[:space:]]+EXCEPTION\y(?=[^;]+\yCONSTRAINT[[:space:]]*=)(?:(?![^;]+\yTABLE[[:space:]]*=)|(?![^;]+\ySCHEMA[[:space:]]*=))'
ORDER BY routine_schema, routine_name;

Fixing Suggestion

Include TABLE = TG_TABLE_NAME and SCHEMA = TG_TABLE_SCHEMA in the USING clause of every RAISE EXCEPTION statement that specifies a CONSTRAINT name in a trigger function (for example: USING ERRCODE = 'check_violation', CONSTRAINT = '...', TABLE = TG_TABLE_NAME, 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.