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
SELECT 
  pg_namespace.nspname AS func_schema, 
  pg_proc.proname AS func_name, 
  pg_get_function_identity_arguments(pg_proc.oid) AS parameters,
  regexp_replace(pg_proc.prosrc, '[\r\n]', '<br>', 'g') AS func_src
FROM 
  pg_catalog.pg_proc, 
  pg_catalog.pg_namespace,
  pg_catalog.pg_type
WHERE pg_proc.pronamespace = pg_namespace.oid 
  AND pg_type.oid = pg_proc.prorettype
  AND pg_type.typname = 'trigger'   
  AND prosrc ~* '\yBEGIN\y(?:(?!(?:\ySELECT\y|\yPERFORM\y|\yIF\y|\yCASE\y))[\s\S])*?\y(IF|CASE)\y(?:(?!(?:\ySELECT\y|\yTHEN\y|\yTG_(NAME|WHEN|LEVEL|OP|RELID|RELNAME|TABLE_NAME|TABLE_SCHEMA|NARGS|ARGV)\y))[\s\S])+?\yTHEN\y'
  AND pg_namespace.nspname NOT IN (
    SELECT schema_name
    FROM INFORMATION_SCHEMA.schemata
    WHERE schema_name <> 'public' 
      AND schema_owner = 'postgres' 
      AND schema_name IS NOT NULL
  )
ORDER BY func_schema, func_name, parameters;

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 .

NameDescription
Find problems automaticallyQueries, 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

NameDescription
PerformanceQueries of this category provide information about indexes in a database.
Triggers and rulesQueries of this category provide information about triggers and rules in a database.
User-defined routinesQueries of this category provide information about the user-defined routines