Goal This query identifies user-defined routines with an SQL-standard body (BEGIN ATOMIC ... END) that execute an UPDATE statement on a base table containing a modification timestamp or date column, where neither the routine itself nor any row-level BEFORE UPDATE trigger on the target table updates that modification time column. Failing to maintain the modification timestamp during row updates compromises audit trails, data lineage tracking, and synchronization or caching mechanisms that depend on accurate last-modified timestamps.
Notes Queries information_schema.routine_column_usage (which records table and column dependencies for routines written using the SQL-standard BEGIN ATOMIC body introduced in PostgreSQL 14), information_schema.columns, information_schema.routines, and PostgreSQL system catalogs (pg_trigger, pg_proc, pg_class, pg_namespace).



  • Identifies base tables that have a column with a temporal data type (date or timestamp) whose name suggests a modification time (matching muutm, update, change, or modif).

  • Verifies that the routine executes an UPDATE <table_name> SET statement against that specific table, while not referencing the table's modification time column.

  • Excludes tables that have a row-level BEFORE UPDATE trigger whose trigger function assigns a value to the modification time column (NEW.<column_name> := ...), regardless of whether the trigger specifies a WHEN condition.

  • For better readability in a web browser, the query escapes < characters and replaces newline characters with HTML line break (<br>) tags. Routine parameters are included alongside the schema and routine name to uniquely identify overloaded routines.

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
WITH update_triggers AS (
    SELECT DISTINCT
        n.nspname AS trigger_schema,
        c.relname AS trigger_table
    FROM pg_catalog.pg_trigger t
    INNER JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid
    INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    INNER JOIN pg_catalog.pg_proc p ON p.oid = t.tgfoid
    WHERE 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\.[[:alnum:]_]*(muutm|update|change|modif)[[:alnum:]_]*[[:space:]]*:?='
),

routine_table AS (
    SELECT DISTINCT 
        t.table_schema, 
        t.table_name, 
        rtu.routine_schema, 
        rtu.routine_name, 
        rtu.specific_name,
        pg_get_function_identity_arguments(p.oid) AS parameters, 
        CASE 
            WHEN p.prokind <> 'a' THEN pg_get_functiondef(p.oid) 
            ELSE NULL 
        END AS routine_source, 
        r.routine_type
    FROM information_schema.tables AS t 
    INNER JOIN information_schema.routine_column_usage AS rtu 
        USING (table_schema, table_name)
    INNER JOIN information_schema.routines AS r 
        USING (specific_schema, specific_name)
    INNER JOIN pg_catalog.pg_proc AS p 
        ON p.oid = translate(substring(rtu.specific_name, '_[0-9]+$'), '_', '')::int::oid
    WHERE t.table_type = 'BASE TABLE' 
      AND p.prokind <> 'a'
      AND (t.table_schema, t.table_name) IN (
          SELECT table_schema, table_name
          FROM INFORMATION_SCHEMA.columns
          WHERE column_name ~* '(muutm|update|change|modif)'
            AND data_type ~* '(timestamp|date)'
      )
      AND t.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
      )
)

SELECT 
    table_schema, 
    table_name, 
    routine_schema, 
    routine_name, 
    parameters, 
    regexp_replace(
        regexp_replace(routine_source, '<', '&lt', 'g'),
        '[\r\n]', '<br>', 'g'
    ) AS routine_source, 
    routine_type
FROM routine_table
WHERE 
    routine_source ~* ('\yUPDATE[[:space:]]+(ONLY[[:space:]]+)?([[:alnum:]_."]+\.)?"?' || table_name || '"?[[:space:]]+((AS[[:space:]]+)?[[:alnum:]_"]+[[:space:]]+)?SET[[:space:]]')
    AND (routine_schema, specific_name) NOT IN (
        SELECT specific_schema, specific_name
        FROM INFORMATION_SCHEMA.routine_column_usage 
        INNER JOIN INFORMATION_SCHEMA.columns USING (table_schema, table_name, column_name)
        WHERE column_name ~* '(muutm|update|change|modif)'
          AND data_type ~* '(timestamp|date)'
    )
    AND (table_schema, table_name) NOT IN (
        SELECT trigger_schema, trigger_table
        FROM update_triggers
    )
ORDER BY table_schema, table_name, routine_schema, routine_name;

Fixing Suggestion

Update the modification time column explicitly in the routine's UPDATE ... SET clause (for example, SET viimase_muutm_aeg = CURRENT_TIMESTAMP(0) or statement_timestamp()), or create a row-level BEFORE UPDATE trigger on the target table that automatically updates the modification timestamp (NEW.<modification_time_column> := CURRENT_TIMESTAMP(0);) whenever a row is modified.

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:

Result quality depends on names

Queries of this category use names (for instance, column names) to try to guess the meaning of a database object. Thus, the goodness of names determines the number of false positive and false negative results.

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
Result quality depends on namesQueries of this category use names (for instance, column names) to try to guess the meaning of a database object. Thus, the goodness of names determines the number of false positive and false negative results.
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