| 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:
|
|---|---|
| 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 |
|
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 .
| 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. |