| 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) capable of locking multiple rows (or using LIMIT / FETCH FIRST ... ROWS), but omit an ORDER BY clause.When a query locks multiple rows without a deterministic ORDER BY clause, PostgreSQL acquires row locks in the order tuples are visited by the execution plan (such as physical heap order or index scan order). Because tuple positions and access paths can vary across concurrent transactions, two transactions locking overlapping sets of rows can acquire locks in opposite orders, resulting in a deadlock. Although FOR SHARE locks are mutually compatible with other FOR SHARE locks, they conflict with write locks (UPDATE, DELETE, FOR [NO KEY] UPDATE); therefore, a multi-row FOR SHARE query can deadlock with a concurrent transaction that modifies the same rows unless both transactions acquire locks in a consistent order (e.g., ascending primary key order). Furthermore, using LIMIT or FETCH FIRST/NEXT with a locking clause without ORDER BY locks an arbitrary subset of rows. Queries that lock at most a single row (such as equality lookups on a PRIMARY KEY or UNIQUE index, or EXISTS (...) subqueries) cannot deadlock due to intra-query row ordering and are excluded. |
|---|---|
| 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 statement-level or cursor-loop (FOR ... IN SELECT ... LOOP) SELECT or PERFORM query that specifies FOR [NO KEY] UPDATE or FOR [KEY] SHARE without an outer ORDER BY clause. Parenthesized subqueries such as EXISTS (SELECT ... FOR SHARE) and scalar subqueries are excluded because they stop after or permit at most one row.A locking query without ORDER BY is flagged if it can lock multiple rows or operates non-deterministically under any of three conditions:
In the returned routine source code, < characters are escaped (<), newline characters are replaced with HTML line break (<br>) tags, 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
Add an explicit
ORDER BY clause on the primary key or unique identifier of the locked table immediately before the locking clause (for example, SELECT ... FROM table_name WHERE ... ORDER BY id FOR NO KEY UPDATE; or ORDER BY id FOR SHARE;) so that all concurrent transactions acquire row locks in a consistent, deterministic order. 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 |