Goal This query identifies user-defined routines (functions and procedures) that explicitly assign a value to a table's modification timestamp column in an UPDATE ... SET statement when the target table already has a row-level BEFORE UPDATE trigger that automatically updates that same column. Maintaining the modification timestamp in both the data-modifying routine and a BEFORE UPDATE trigger violates the Don't Repeat Yourself (DRY) principle and creates architectural ambiguity:

  • Redundant and overwritten assignments: Because a BEFORE UPDATE trigger executes after the SET expressions of the UPDATE statement are evaluated, an unconditional assignment in the trigger (e.g., NEW.updated_at := CURRENT_TIMESTAMP(0);) silently overwrites whatever timestamp the routine explicitly computed and passed in the SET clause.

  • Inconsistent timestamp semantics: If the routine uses one timestamp function (e.g., statement_timestamp()) while the trigger uses another (e.g., CURRENT_TIMESTAMP or clock_timestamp()), developers reading the routine code are misled about the actual value persisted in the table.

Notes Queries information_schema.columns, information_schema.tables, and PostgreSQL system catalogs (pg_trigger, pg_proc, pg_class, pg_namespace). First, the query identifies base tables that have a temporal column matching modification-time naming patterns (muutm, update, change, modif) and a row-level BEFORE UPDATE trigger whose function assigns a value to NEW.<mod_column_name>. Next, it inspects the definitions (pg_get_functiondef) of all non-trigger routines and flags those that execute an UPDATE <table_name> SET statement assigning a value to <mod_column_name> within the SET clause (prior to any FROM, WHERE, or RETURNING clause). For better readability in a web browser, < characters are escaped and newline characters are replaced with HTML line break (<br>) tags.
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
WITH mod_time_triggers AS (    
    SELECT 
        c.table_schema,
        c.table_name,
        c.column_name AS mod_column_name,
        string_agg(DISTINCT t.tgname || ' (' || p.proname || ')', ', ' ORDER BY t.tgname || ' (' || p.proname || ')') AS trigger_info
    FROM information_schema.columns c
    INNER JOIN information_schema.tables tbl 
        ON c.table_schema = tbl.table_schema 
       AND c.table_name = tbl.table_name
    INNER JOIN pg_catalog.pg_namespace n 
        ON n.nspname = c.table_schema
    INNER JOIN pg_catalog.pg_class cl 
        ON cl.relnamespace = n.oid 
       AND cl.relname = c.table_name
    INNER JOIN pg_catalog.pg_trigger t 
        ON t.tgrelid = cl.oid
    INNER JOIN pg_catalog.pg_proc p 
        ON p.oid = t.tgfoid
    WHERE tbl.table_type = 'BASE TABLE'
      AND c.column_name ~* '(muutm|update|change|modif)'
      AND c.data_type ~* '(timestamp|date)'
      AND NOT t.tgisinternal

      AND (t.tgtype::integer & 1) <> 0
      AND (t.tgtype::integer & 2) <> 0
      AND (t.tgtype::integer & 16) <> 0
      
      AND p.prosrc ~* ('\yNEW\."?' || c.column_name || '"?[[:space:]]*:?=')
      AND c.table_schema NOT IN (
          SELECT schema_name
          FROM information_schema.schemata
          WHERE schema_name <> 'public' 
            AND schema_owner = 'postgres' 
            AND schema_name IS NOT NULL
      )
    GROUP BY c.table_schema, c.table_name, c.column_name
),

routines_src AS (
    SELECT 
        ns.nspname AS routine_schema,
        pr.proname AS routine_name,
        pg_get_function_identity_arguments(pr.oid) AS parameters,
        CASE 
            WHEN pr.prokind = 'f' THEN 'FUNCTION'
            WHEN pr.prokind = 'p' THEN 'PROCEDURE'
        END AS routine_type,
        CASE 
            WHEN pr.prokind <> 'a' THEN pg_get_functiondef(pr.oid) 
            ELSE NULL 
        END AS raw_src
    FROM pg_catalog.pg_proc pr
    INNER JOIN pg_catalog.pg_namespace ns ON ns.oid = pr.pronamespace
    LEFT JOIN pg_catalog.pg_type pt ON pt.oid = pr.prorettype
    WHERE pr.prokind IN ('f', 'p')
      AND COALESCE(pt.typname, '') <> 'trigger'
      AND ns.nspname NOT IN (
          SELECT schema_name
          FROM information_schema.schemata
          WHERE schema_name <> 'public' 
            AND schema_owner = 'postgres' 
            AND schema_name IS NOT NULL
      )
      AND NOT EXISTS (
          SELECT 1
          FROM pg_catalog.pg_depend d 
          INNER JOIN pg_catalog.pg_extension e ON d.refobjid = e.oid
          WHERE d.objid = pr.oid
      )
)

SELECT 
    mtt.table_schema,
    mtt.table_name,
    mtt.mod_column_name,
    mtt.trigger_info AS updating_triggers,
    r.routine_schema,
    r.routine_name,
    r.parameters,
    r.routine_type,
    regexp_replace(
        regexp_replace(r.raw_src, '<', '&lt', 'g'),
        '[\r\n]', '<br>', 'g'
    ) AS routine_source
FROM mod_time_triggers mtt
INNER JOIN routines_src r
    
    ON r.raw_src ~* (
        '\yUPDATE[[:space:]]+(ONLY[[:space:]]+)?([[:alnum:]_."]+\.)?"?' || mtt.table_name || '"?[[:space:]]+((AS[[:space:]]+)?[[:alnum:]_"]+[[:space:]]+)?SET\y(?:(?!\yWHERE\y|\yFROM\y|\yRETURNING\y)[^;])*?\y"?' || mtt.mod_column_name || '"?[[:space:]]*='
    )
ORDER BY mtt.table_schema, mtt.table_name, r.routine_schema, r.routine_name, r.parameters;

Fixing Suggestion

Standardize on a single mechanism for maintaining the modification timestamp across the table. Either remove the explicit modification-time column assignment from the routine's UPDATE ... SET clause and rely on the BEFORE UPDATE trigger, or drop the trigger and manage the timestamp exclusively within the data-modifying routines.

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:

Triggers and rules

Queries of this category provide information about triggers and rules in a database.

NameDescription
Triggers and rulesQueries of this category provide information about triggers and rules in a database.