| Goal | This query identifies user-defined SQL and PL/pgSQL routines (functions, procedures, and trigger functions) that execute a SELECT or PERFORM query with a row-level locking clause (FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, or FOR KEY SHARE) and include an unnecessary ORDER BY clause when the query can lock at most a single row.While deterministic row ordering via ORDER BY is essential for preventing deadlocks when a query locks multiple rows, adding ORDER BY to a locking query is completely redundant in two cases:
|
|---|---|
| Notes | Refers to the column pg_proc.prokind and thus works starting from PostgreSQL 11. The query inspects the definitions (pg_get_functiondef) of user-defined functions, procedures, and trigger functions (prokind IN ('f', 'p')), strips SQL comments, and uses regexp_matches to extract every SELECT or PERFORM query that specifies both an ORDER BY clause and a row-level locking clause (FOR [NO KEY] UPDATE or FOR [KEY] SHARE).A locking query with ORDER BY is flagged as redundant if:
In the returned routine source code, < characters are escaped (<), newline characters are replaced with HTML line break (<br>) tags, and both ORDER BY and the locking keywords (FOR [NO KEY] UPDATE / FOR [KEY] SHARE) are highlighted in bold (<b>). |
| 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 the redundant
ORDER BY clause from the single-row locking query (or EXISTS subquery). Reserve ORDER BY in locking queries strictly for statements that can lock multiple rows (such as WHERE id IN (...), WHERE id = ANY (...), range filters, or non-unique foreign key lookups) or statements that use LIMIT / FETCH FIRST ... ROWS. 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:
Concurrency control
Queries of this category provide information about concurrency control.
Triggers and rules
Queries of this category provide information about triggers and rules in a database.
User-defined routines
Queries of this category provide information about the user-defined routines
| Name | Description |
|---|---|
| Concurrency control | Queries of this category provide information about concurrency control. |
| Triggers and rules | Queries of this category provide information about triggers and rules in a database. |
| User-defined routines | Queries of this category provide information about the user-defined routines |