| Goal | This query identifies trigger functions that contain conditional statements (IF or CASE) without a preceding SELECT statement. In PostgreSQL, filtering conditions inside a trigger's CREATE TRIGGER ... WHEN (...) clause is significantly more efficient than checking them inside the function body, because PostgreSQL skips invoking the trigger function altogether when the WHEN condition is false—avoiding unnecessary PL/pgSQL execution overhead. However, PostgreSQL prohibits subqueries inside the WHEN clause; therefore, if a branch depends on querying other tables via SELECT, that conditional logic must remain inside the function body. By excluding routines with a prior SELECT, this query surfaces trigger functions whose conditions likely rely only on NEW, OLD, or simple expressions and could potentially be moved into the trigger's WHEN clause. |
|---|---|
| Notes | In the returned body of routine the query replaces each newline character with the line break (br) tag for the better readability in case the query result is displayed in a web browser. Trigger WHEN clause cannot contain a subquery. The query assumes that if a trigger function contains a SELECT statement, then its result is used in the condition, i.e., such function cannot be simplified. There could be multiple routines with the same name but with different parameters in the same schema (overloading). Thus, for the unique identification of the routine it is necessary to present also its parameters in addition to the schema name and routine name. The query excludes triggers that refer to special variables that name starts with "TG_" (e.g., TG_NAME). |
| Type | Problem detection Each row in the result could represent a flaw in the design |
| Reliability | Medium Medium number of false-positive results |
| License | MIT (opens in new tab) |
| Data Source | INFORMATION_SCHEMA+system catalog |
| SQL Query |
|
Fixing Suggestion
Move simple
IF or CASE conditions that depend solely on NEW or OLD values out of the trigger function body and into the trigger's WHEN clause (e.g., WHEN (OLD.status IS DISTINCT FROM NEW.status)) so PostgreSQL skips invoking the function when the condition is not met. 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:
Performance
Queries of this category provide information about indexes in a database.
Triggers and rules
Queries of this category provide information about triggers and rules in a database.
User-defined routines
Queries of this category provide information about the user-defined routines
| Name | Description |
|---|---|
| Performance | Queries of this category provide information about indexes in a database. |
| Triggers and rules | Queries of this category provide information about triggers and rules in a database. |
| User-defined routines | Queries of this category provide information about the user-defined routines |