Goal This query identifies user-defined routines containing SELECT queries that join multiple tables (FROM (table_a JOIN table_b ...)), but restrict row-level locking to only a single table alias via FOR SHARE OF <single_table> or FOR UPDATE OF <single_table>.


When a query joins multiple tables (for example, a supertype/subtype relationship such as isik and tootaja) and filters rows using attributes from one table (such as WHERE lower(i.e_post) = ...) while locking only the other table (FOR SHARE OF t), the unlocked table remains vulnerable to concurrent modifications. Under PostgreSQL's default READ COMMITTED isolation level, a concurrent transaction can modify the unlocked table's attributes (such as changing the email address or deactivating the person record) before the current transaction completes, leading to race conditions and data inconsistencies.

Notes Queries the PostgreSQL system catalogs (pg_proc and pg_namespace) to inspect the decompiled definition (pg_get_functiondef) of user-defined functions and procedures (prokind IN ('f', 'p')). The query uses a case-insensitive regular expression to detect SELECT queries that contain a JOIN in their FROM clause and a WHERE filter, followed by a single locking clause (FOR [KEY] SHARE OF <table_alias> or FOR [NO KEY] UPDATE OF <table_alias>) that specifies only one table identifier (without a comma-separated list of tables or a subsequent FOR ... OF clause) before the end of the subquery or statement. 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
WITH routines_src AS (
  SELECT 
    n.nspname AS routine_schema, 
    p.proname AS routine_name, 
    pg_get_function_identity_arguments(p.oid) AS parameters,
    CASE 
      WHEN p.prokind = 'f' THEN 'FUNCTION'
      WHEN p.prokind = 'p' THEN 'PROCEDURE'
    END AS routine_type,
    CASE 
      WHEN p.prokind <> 'a' THEN pg_get_functiondef(p.oid) 
      ELSE NULL 
    END AS raw_src
  FROM pg_catalog.pg_proc p
  INNER JOIN pg_catalog.pg_namespace n ON p.pronamespace = n.oid
  WHERE p.prokind IN ('f', 'p')
    AND n.nspname NOT IN (
      SELECT schema_name
      FROM INFORMATION_SCHEMA.schemata
      WHERE schema_name <> 'public' 
        AND schema_owner = 'postgres' 
        AND schema_name IS NOT NULL
    )
)
SELECT 
  routine_schema, 
  routine_name, 
  parameters,
  routine_type,
  regexp_replace(regexp_replace(raw_src, '<', '&lt', 'g'), '[\r\n]', '<br>', 'g') AS routine_src
FROM routines_src
WHERE   
  raw_src ~* '\ySELECT\y(?:(?!\ySELECT\y|\yFROM\y)[^;])+\yFROM\y(?:(?!\ySELECT\y|\yWHERE\y|\yJOIN\y)[^;])+\y([[:alnum:]_"]+)\y[[:space:]]+(?:(?:INNER|LEFT|RIGHT|FULL|CROSS|NATURAL)[[:space:]]+(?:OUTER[[:space:]]+)?)?\yJOIN\y[[:space:]]+(?:[[:alnum:]_."]+\.)?(?:[[:alnum:]_"]+[[:space:]]+(?:AS[[:space:]]+)?)?(?!(?:ON|USING|WHERE|JOIN|INNER|LEFT|RIGHT|FULL|CROSS|NATURAL|FOR)\y)([[:alnum:]_"]+)\y(?:(?!\ySELECT\y|\yWHERE\y)[^;])+\yWHERE\y[^;]+?(?<!\yOF[[:space:]]+[[:alnum:]_."]+[[:space:]]+)\yFOR[[:space:]]+(?:KEY[[:space:]]+|NO[[:space:]]+KEY[[:space:]]+)?(?:SHARE|UPDATE)[[:space:]]+OF[[:space:]]+(\1|\2)\y[[:space:]]*(?!,[[:space:]]*[[:alnum:]_."]|FOR[[:space:]])(?:\)|;|RETURNING\y)'
ORDER BY routine_schema, routine_name, parameters;

Fixing Suggestion

Ensure that all joined base tables whose attributes are read or validated in the query are locked against concurrent modifications. Either omit the OF <table_alias> qualification (writing simply FOR SHARE or FOR UPDATE, which automatically locks all non-outer-joined tables in the query's FROM clause) or explicitly include all joined table aliases in the locking clause (e.g., FOR SHARE OF t, i).

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:

User-defined routines

Queries of this category provide information about the user-defined routines

NameDescription
User-defined routinesQueries of this category provide information about the user-defined routines