| Goal | This query identifies triggers whose underlying function specifies a CONSTRAINT (or CONSTRAINT_NAME) option in a RAISE EXCEPTION ... USING clause, but constructs the constraint identifier without including either the name of the table to which the trigger is attached (trigger_table) or the built-in PL/pgSQL variable TG_TABLE_NAME (or TG_RELNAME). When a trigger simulates a table constraint by setting the PG_DIAG_CONSTRAINT_NAME diagnostic field via USING CONSTRAINT = ..., following a consistent naming convention that incorporates the table name (e.g., CONSTRAINT = TG_TABLE_NAME || '_<rule>' or CONSTRAINT = '<table_name>_<rule>') ensures that the simulated constraint name is globally unique across the schema and immediately traceable to the affected relation. A missing table reference typically indicates one of three issues:
|
|---|---|
| Notes | Queries the PostgreSQL system catalogs (pg_trigger, pg_proc, pg_class, pg_namespace) and information_schema.triggers. For each trigger, the query dynamically constructs a case-insensitive regular expression with a tempered greedy token that scans the entire expression assigned to CONSTRAINT (or CONSTRAINT_NAME) from the = sign up to the next USING option keyword (MESSAGE, DETAIL, HINT, ERRCODE, COLUMN, CONSTRAINT, DATATYPE, TABLE, SCHEMA) or the statement-terminating semicolon (;). The trigger is flagged if the assigned expression contains neither TG_TABLE_NAME, TG_RELNAME, nor the trigger's trigger_table string in any position (supporting plain literals, || string concatenation, and format() calls). 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 |
|
Fixing Suggestion
Construct the
CONSTRAINT identifier dynamically using TG_TABLE_NAME (for example, CONSTRAINT = TG_TABLE_NAME || '_<rule_name>' or CONSTRAINT = 'chk_' || TG_TABLE_NAME || '_<rule_name>'), or update the hardcoded string literal so that it accurately includes the full table name (trigger_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 .
| Name | Description |
|---|---|
| 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 . |
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.
| Name | Description |
|---|---|
| Triggers and rules | Queries of this category provide information about triggers and rules in a database. |
Further reading and related materials: