| Goal | This query identifies data-modifying user-defined routines (functions, procedures, and trigger functions) where audit or actor columns and variables (such as looja, registreerija, viimane_muutja, kustutaja, arhiveerija, kinnitaja, sisestaja, created_by, updated_by, modified_by, deleted_by, approved_by, etc.) are assigned or compared against hardcoded numeric or string literals instead of dynamically determined user identifiers. Hardcoding actor identifiers inside database routines is a serious security, data integrity, and maintainability flaw:
|
|---|---|
| Notes | Refers to the column pg_proc.prokind and thus works starting from PostgreSQL 11. The query inspects user-defined functions, procedures, and trigger functions (prokind IN ('f', 'p')) from pg_catalog.pg_proc and pg_catalog.pg_namespace, excluding system schemas, postgres-owned non-public schemas, extension-owned routines, and helper routines. First, SQL line and block comments are stripped from pg_get_functiondef(p.oid) so that commented-out code does not cause false positives, and routines are filtered to those performing DML (INSERT INTO, UPDATE ... SET, DELETE FROM, MERGE INTO) or modifying NEW.<column> inside a trigger function. An identifier regex matches Estonian and English audit/actor names while explicitly excluding timestamps, statuses, roles, and type attributes (such as _aeg, _time, _date, _roll, _staatus, _seisund, _liik, _type). The query flags a routine if any actor identifier receives a hardcoded numeric or string literal (with optional parentheses and type casts) via:
For better readability in a web browser, < characters are escaped (<) and newline characters are replaced 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 |
|
Fixing Suggestion
Remove all hardcoded numeric and string literals for actor/audit attributes and resolve the acting user dynamically:
- Pass the caller's identifier (such as an email address, username, or user ID) as an explicit, mandatory input parameter to the routine without a hardcoded
DEFAULTvalue, and look up the corresponding actor ID from the user/employee table (e.g., usingSELECT ... FOR SHARE). - Alternatively, if database sessions or connection pools establish the caller's identity at the database level, derive the actor using PostgreSQL session functions (
CURRENT_USER,SESSION_USER) or custom transaction/session settings (e.g.,current_setting('app.current_user_id', true)).
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:
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.
User-defined routines
Queries of this category provide information about the user-defined routines
| Name | Description |
|---|---|
| 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. |
| User-defined routines | Queries of this category provide information about the user-defined routines |