| 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.
|
|---|---|
| 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 |
|
Fixing Suggestion
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 .
| 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. |