Goal This query identifies concurrency risks in user-defined routines by flagging INSERT, UPDATE, or DELETE statements that utilize subqueries without a FOR SHARE locking clause. Failure to acquire a shared lock on source rows allows concurrent transactions to modify or delete them between the subquery's execution and the outer operation, potentially leading to data inconsistencies. Routines utilizing the xmin system column are excluded, operating on the assumption that they implement Optimistic Concurrency Control (version checking) and therefore do not require pessimistic locking.
Notes Refers to the column pg_proc.prokind and thus works starting from PostgreSQL 11. In the returned body of routine the query replaces each newline character with the line break (br) tag for the better readability in case the query result is displayed in a web browser. There could be multiple routines with the same name but with different parameters in the same schema (overloading). Thus, for the unique identification of the routine it is necessary to present also its parameters in addition to the schema name and routine name. The query assumes that explicit locking is needed if the routine reads data and as the result raises an exception or decides as to whether to modify data or not.
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)
Fixing Suggestion Use SELECT … FOR SHARE in the subquery to block concurrent modification of data based on that the current modification is made.
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
SELECT 
  pg_namespace.nspname AS routine_schema, 
  pg_proc.proname AS routine_name, 
  pg_get_function_identity_arguments(pg_proc.oid) AS parameters,
  CASE WHEN pg_proc.prokind='f' THEN 'FUNCTION'
  WHEN pg_proc.prokind='p' THEN 'PROCEDURE'
  WHEN pg_proc.prokind='w' THEN 'WINDOW FUNCTION' END AS routine_type,
   regexp_replace(regexp_replace(regexp_replace( regexp_replace(pg_get_functiondef(pg_proc.oid),'<','&lt','g'),'[\r\n]','<br>','g'),'SELECT','<b>SELECT</b>','gi'),'FOR[[:space:]]+SHARE','<b>FOR SHARE</b>','gi')  AS routine_src
FROM 
  pg_catalog.pg_proc, 
  pg_catalog.pg_namespace
WHERE 
  pg_proc.pronamespace = pg_namespace.oid
  AND pg_proc.prokind<>'a'
 AND pg_proc.proname NOT IN ('f_assume_you_must_use_files', 'f_check_format_comma_separated_list', 'f_check_password', 'f_default_value_with_no_match') 
 AND pg_namespace.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 (
    -- Regex 1: INSERT/UPDATE/DELETE sees olevad SELECT laused
    pg_get_functiondef(pg_proc.oid)~*'(INSERT|UPDATE|DELETE)[^;]+(?<!=[[:space:]]*[(][[:space:]]*)SELECT(?=[^;]+FROM[[:space:]]+[^;]+WHERE)(?![^;]+FOR[[:space:]]+SHARE)'
    OR 
    -- Regex 2: WITH CTE sees olevad SELECT laused
    pg_get_functiondef(pg_proc.oid)~*'WITH[^;]+(?<!=[[:space:]]*[(][[:space:]]*)SELECT(?=[^;]+FROM[[:space:]]+[^;]+WHERE)(?![^;]+FOR[[:space:]]+SHARE)[^;]+(INSERT|UPDATE|DELETE)[[:space:]]'
) AND pg_get_functiondef(pg_proc.oid)!~*'\bxmin\b'
ORDER BY routine_schema, routine_name, parameters;

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:

Concurrency control

Queries of this category provide information about concurrency control.

Does not work in some earlier PostgreSQL version

Queries of this category provide information that was not available in some earlier PostgreSQL version

Transactions

Queries of this catergory provide information about the use of transactions.

User-defined routines

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

NameDescription
Concurrency controlQueries of this category provide information about concurrency control.
Does not work in some earlier PostgreSQL versionQueries of this category provide information that was not available in some earlier PostgreSQL version
TransactionsQueries of this catergory provide information about the use of transactions.
User-defined routinesQueries of this category provide information about the user-defined routines