| Goal | This query identifies trigger routines that contain an unreachable RETURN statement (such as RETURN NULL;) immediately following a RAISE EXCEPTION statement within the same execution block. In PL/pgSQL, RAISE EXCEPTION immediately aborts execution of the routine and rolls back the current transaction or subtransaction. Consequently, any RETURN statement placed directly after RAISE EXCEPTION in the same scope can never be reached. Such dead code adds unnecessary clutter, confuses the distinction between aborting a transaction (RAISE EXCEPTION) and silently skipping a row operation (RETURN NULL), and creates a maintenance hazard if the exception level is later downgraded to RAISE WARNING or RAISE NOTICE (which would cause the dormant RETURN NULL; statement to execute and silently discard data modifications). |
|---|---|
| Notes | Queries the system catalogs (pg_trigger, pg_proc, pg_class, pg_namespace) and information_schema views to inspect the source code (prosrc) of user-defined trigger routines. The regular expression searches for a RAISE EXCEPTION statement followed immediately (allowing for intervening whitespace and SQL comments) by a RETURN statement. 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
Remove the unreachable RETURN statement (e.g., RETURN NULL;) that immediately follows the RAISE EXCEPTION statement. PostgreSQL's PL/pgSQL compiler recognizes that RAISE EXCEPTION terminates control flow and does not require a subsequent RETURN statement.
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. |