| 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>.
|
|---|---|
| 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 |
|
Fixing Suggestion
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 .
| 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:
User-defined routines
Queries of this category provide information about the user-defined routines
| Name | Description |
|---|---|
| User-defined routines | Queries of this category provide information about the user-defined routines |