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