Filter Queries
Found 16 queries.
- All the queries about database objects contain a subcondition to exclude from the result information about the system catalog.
- Although the statements use SQL constructs (common table expressions; NOT in subqueries) that could cause performance problems in case of large datasets, it shouldn't be a problem in case of the relatively small amount of data in the system catalog of a database.
#1. Derived tables that have a column with the xid type
INFORMATION_SCHEMA+system catalog base tablesFind the derived tables (views and materialized views) that have a column with the xid type, i.e., these use the data from the hidden xmin column of a base table. If one uses optimistic approach for dealing with the concurrent data modifications, then xmin values should be presented by views and used in routines that modify or delete rows.
#2. Derived tables with embedded row locking
INFORMATION_SCHEMA+system catalog base tablesThis query identifies derived tables (views and materialized views) whose defining subqueries utilize explicit row locking clauses (e.g., FOR UPDATE, FOR SHARE). Embedding locking semantics within a view definition is considered an architectural anti-pattern. It couples data projection with transaction control, causing simple read operations against the view to unexpectedly acquire locks. This behavior degrades concurrency by blocking other readers and violates the principle that reading a data element should not implicitly block simultaneous access.
#3. Excessive locking with FOR UPDATE in subqueries
INFORMATION_SCHEMA+system catalog base tablesThis query identifies performance and concurrency bottlenecks caused by excessive locking. It flags INSERT, UPDATE, or DELETE statements that utilize subqueries containing the FOR UPDATE clause. Using FOR UPDATE acquires an exclusive lock, which is semantically inappropriate if the rows in the subquery are merely being read for reference or validation rather than being modified. This practice degrades system concurrency by unnecessarily blocking other transactions.
#4. Explicit locking
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined routines that employ explicit locking mechanisms to supplement PostgreSQL's default Multi-Version Concurrency Control (MVCC). It detects the presence of table-level locking (LOCK TABLE) or explicit row-level locking clauses (e.g., SELECT ... FOR UPDATE, FOR SHARE). While MVCC generally provides sufficient isolation for concurrent transactions, explicit locking is necessary in specific race-condition scenarios. This inventory assists in auditing concurrency control strategies and detecting potential sources of deadlocks or serialization bottlenecks.
#5. Explicit locking is missing
INFORMATION_SCHEMA+system catalog base tablesThis query identifies concurrency risks in user-defined routines by flagging INSERT, UPDATE, or DELETE statements that utilize subqueries or Common Table Expressions (WITH clauses) without an explicit FOR SHARE or FOR UPDATE locking clause. Failure to acquire a lock on source rows allows concurrent transactions to modify or delete them between the subquery's execution and the outer data modification, potentially leading to race conditions and data inconsistencies. Routines utilizing the xmin system column or incrementing an explicit row-version column (e.g., version, rea_versioon) are excluded, as they implement Optimistic Concurrency Control (OCC). Additionally, routines that explicitly lock target rows in a preceding SELECT ... FOR UPDATE/SHARE statement immediately prior to a WITH clause are excluded.
#6. Explicit locking is missing (2) (ChatGPT version)
INFORMATION_SCHEMA+system catalog base tablesThis 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.
#7. Invalid explicit locking with aggregate functions
INFORMATION_SCHEMA+system catalog base tablesThis query identifies SQL statements that attempt to apply explicit row locking (e.g., FOR SHARE, FOR UPDATE) to the result of an aggregate function (e.g., COUNT(*)). This is a semantic error because locking clauses operate on specific physical rows, whereas aggregate functions return a derived scalar value that is decoupled from the underlying row versions. To correctly enforce a lock, the query must select the specific columns (typically the primary key) of the target rows, rather than a computed aggregate.
#8. Redundant and unconditional SELECT ... FOR UPDATE preceding UPDATE or DELETE on the same table
INFORMATION_SCHEMA+system catalog base tablesThis query identifies performance and concurrency bottlenecks in user-defined routines caused by executing a standalone SELECT ... FOR [NO KEY] UPDATE or PERFORM ... FOR [NO KEY] UPDATE statement on a table prior to modifying the exact same table with an UPDATE or DELETE statement.
In PostgreSQL, UPDATE and DELETE statements automatically acquire the appropriate row-level write lock (FOR NO KEY UPDATE for non-key updates, or FOR UPDATE for key-column updates and deletes) on target rows that satisfy the WHERE predicate. Executing a separate, preceding SELECT/PERFORM ... FOR [NO KEY] UPDATE on the same table (without an ORDER BY clause for deterministic multi-row deadlock prevention) is redundant—incurring an unnecessary extra scan and lock operation—and degrades concurrency when the initial lock predicate is less restrictive than the subsequent modification (for instance, locking a row solely by its primary key before verifying its status or optimistic lock version). If the subsequent UPDATE or DELETE affects zero rows due to a version mismatch or invalid state, the transaction still retains the write lock acquired by the preliminary query, unnecessarily blocking concurrent transactions.
#9. Routines acquiring multi-row locks (FOR [NO KEY] UPDATE or FOR [KEY] SHARE) without an ORDER BY clause
INFORMATION_SCHEMA+system catalog base tablesThis 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.
#10. Routines lacking explicit locking (MVCC risks)
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined routines that may be susceptible to concurrency anomalies due to a lack of explicit locking. PostgreSQL utilizes Multi-Version Concurrency Control (MVCC), where SELECT statements do not block data modification operations. Consequently, routines that read data to inform subsequent modifications without acquiring row-level locks (e.g., FOR UPDATE, FOR SHARE) or using isolation levels higher than READ COMMITTED are prone to race conditions. This query flags such routines for review to ensure transactional integrity is maintained.
#11. Routines with a redundant ORDER BY clause in single-row locking queries (FOR [NO KEY] UPDATE or FOR [KEY] SHARE)
INFORMATION_SCHEMA+system catalog base tablesThis 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:
- Single-row primary or unique key lookups: When the
WHEREclause constrains all columns of aPRIMARY KEYorUNIQUEindex using simple equality (=) withoutOR,IN (...),= ANY (...), range operators, orJOINs, the query is mathematically guaranteed to return at most one row (0 or 1 row). A single row has only one possible lock acquisition order and cannot cause an intra-query ordering deadlock; addingORDER BYmerely clutters the code and can prevent optimizer shortcuts or introduce unnecessary sort overhead if the sort column differs from the lookup index. EXISTS (SELECT ... ORDER BY ... FOR ...)subqueries withoutLIMIT/FETCH: AnEXISTSsubquery halts execution and locks only the very first matching row. Including anORDER BYclause inside anEXISTSsubquery without a row-limiting clause forces PostgreSQL to fetch and sort all candidate rows before locking and testing the first row.
#12. The use of xmin hidden column in views and routines
INFORMATION_SCHEMA+system catalog base tablesFind the number of views and materialized views that have a column with the xid type and the number of routines that contain a UPDATE or a DELETE statement that search condition refers to the xmin column. If one uses optimistic approach for dealing with the concurrent modifications of data, then xmin values should be presented by views and used in routines that modify or delete rows.
#13. Trying to lock a value instead of a row
INFORMATION_SCHEMA+system catalog base tablesThis query identifies SQL routines that utilize explicit row locking clauses (e.g., FOR UPDATE, FOR SHARE) in queries that do not target a specific base table or relation. For instance, a statement like SELECT 'text' AS v FOR UPDATE attempts to apply a lock to a scalar constant. Since row-level locks in PostgreSQL require a physical row version (tuple) within a table to be effective, such statements are semantically void. They indicate a fundamental misunderstanding of the concurrency control mechanism and should be corrected to target actual table rows.
#14. Unnecessary explicit locking in read-only routines
INFORMATION_SCHEMA+system catalog base tablesThis query identifies read-only routines that unnecessarily utilize explicit locking mechanisms. PostgreSQL's Multi-Version Concurrency Control (MVCC) ensures that readers do not block writers (and writers do not block readers) during standard query operations. Consequently, routines that perform no data modification (DML) and do not raise exceptions have no functional need to acquire table-level locks (LOCK TABLE) or row-level locks (FOR UPDATE / FOR SHARE). Acquiring locks in purely read-only contexts provides no consistency benefits while actively degrading database concurrency by causing transaction contention. The query explicitly excludes routines whose names indicate they were intentionally designed to handle locking.
#15. User-defined routines that use xmin hidden column
INFORMATION_SCHEMA+system catalog base tablesFind routines that contain a UPDATE or a DELETE statement that search condition refers to the xmin column. If one uses optimistic approach for dealing with the concurrent modifications of data, then xmin values should be presented by views and used in routines that modify or delete rows.
#16. Validation triggers reading reference tables without FOR SHARE locking
INFORMATION_SCHEMA+system catalog base tablesThis query identifies row-level BEFORE triggers that validate data by querying other tables (SELECT ... FROM ... WHERE) before raising an exception, but fail to acquire a shared lock (FOR SHARE) on the referenced rows. Under PostgreSQL's default READ COMMITTED isolation level, reading a reference or classifier row (such as checking whether a status, category, or type is active) without FOR SHARE allows concurrent transactions to deactivate or modify that reference row before the triggering transaction commits, leading to write skew and data integrity violations.
| # | Name(sorted ascending, activate to sort descending) | Goal(activate to sort ascending) | Type(activate to sort ascending) | Data source(activate to sort ascending) | Last update(activate to sort ascending) | License | Actions |
|---|---|---|---|---|---|---|---|
| 1 | Derived tables that have a column with the xid type | Find the derived tables (views and materialized views) that have a column with the xid type, i.e., these use the data from the hidden xmin column of a base table. If one uses optimistic approach for dealing with the concurrent data modifications, then xmin values should be presented by views and used in routines that modify or delete rows. | General | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 2 | Derived tables with embedded row locking | This query identifies derived tables (views and materialized views) whose defining subqueries utilize explicit row locking clauses (e.g., FOR UPDATE, FOR SHARE). Embedding locking semantics within a view definition is considered an architectural anti-pattern. It couples data projection with transaction control, causing simple read operations against the view to unexpectedly acquire locks. This behavior degrades concurrency by blocking other readers and violates the principle that reading a data element should not implicitly block simultaneous access. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 3 | Excessive locking with FOR UPDATE in subqueries | This query identifies performance and concurrency bottlenecks caused by excessive locking. It flags INSERT, UPDATE, or DELETE statements that utilize subqueries containing the FOR UPDATE clause. Using FOR UPDATE acquires an exclusive lock, which is semantically inappropriate if the rows in the subquery are merely being read for reference or validation rather than being modified. This practice degrades system concurrency by unnecessarily blocking other transactions. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 4 | Explicit locking | This query identifies user-defined routines that employ explicit locking mechanisms to supplement PostgreSQL's default Multi-Version Concurrency Control (MVCC). It detects the presence of table-level locking (LOCK TABLE) or explicit row-level locking clauses (e.g., SELECT ... FOR UPDATE, FOR SHARE). While MVCC generally provides sufficient isolation for concurrent transactions, explicit locking is necessary in specific race-condition scenarios. This inventory assists in auditing concurrency control strategies and detecting potential sources of deadlocks or serialization bottlenecks. | General | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 5 | Explicit locking is missing | This query identifies concurrency risks in user-defined routines by flagging INSERT, UPDATE, or DELETE statements that utilize subqueries or Common Table Expressions (WITH clauses) without an explicit FOR SHARE or FOR UPDATE locking clause. Failure to acquire a lock on source rows allows concurrent transactions to modify or delete them between the subquery's execution and the outer data modification, potentially leading to race conditions and data inconsistencies. Routines utilizing the xmin system column or incrementing an explicit row-version column (e.g., version, rea_versioon) are excluded, as they implement Optimistic Concurrency Control (OCC). Additionally, routines that explicitly lock target rows in a preceding SELECT ... FOR UPDATE/SHARE statement immediately prior to a WITH clause are excluded. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 6 | Explicit locking is missing (2) (ChatGPT version) | 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. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 7 | Invalid explicit locking with aggregate functions | This query identifies SQL statements that attempt to apply explicit row locking (e.g., FOR SHARE, FOR UPDATE) to the result of an aggregate function (e.g., COUNT(*)). This is a semantic error because locking clauses operate on specific physical rows, whereas aggregate functions return a derived scalar value that is decoupled from the underlying row versions. To correctly enforce a lock, the query must select the specific columns (typically the primary key) of the target rows, rather than a computed aggregate. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 8 | Redundant and unconditional SELECT ... FOR UPDATE preceding UPDATE or DELETE on the same table | This query identifies performance and concurrency bottlenecks in user-defined routines caused by executing a standalone SELECT ... FOR [NO KEY] UPDATE or PERFORM ... FOR [NO KEY] UPDATE statement on a table prior to modifying the exact same table with an UPDATE or DELETE statement.In PostgreSQL, UPDATE and DELETE statements automatically acquire the appropriate row-level write lock (FOR NO KEY UPDATE for non-key updates, or FOR UPDATE for key-column updates and deletes) on target rows that satisfy the WHERE predicate. Executing a separate, preceding SELECT/PERFORM ... FOR [NO KEY] UPDATE on the same table (without an ORDER BY clause for deterministic multi-row deadlock prevention) is redundant—incurring an unnecessary extra scan and lock operation—and degrades concurrency when the initial lock predicate is less restrictive than the subsequent modification (for instance, locking a row solely by its primary key before verifying its status or optimistic lock version). If the subsequent UPDATE or DELETE affects zero rows due to a version mismatch or invalid state, the transaction still retains the write lock acquired by the preliminary query, unnecessarily blocking concurrent transactions. |
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 9 | Routines acquiring multi-row locks (FOR [NO KEY] UPDATE or FOR [KEY] SHARE) without an ORDER BY clause | 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. |
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 10 | Routines lacking explicit locking (MVCC risks) | This query identifies user-defined routines that may be susceptible to concurrency anomalies due to a lack of explicit locking. PostgreSQL utilizes Multi-Version Concurrency Control (MVCC), where SELECT statements do not block data modification operations. Consequently, routines that read data to inform subsequent modifications without acquiring row-level locks (e.g., FOR UPDATE, FOR SHARE) or using isolation levels higher than READ COMMITTED are prone to race conditions. This query flags such routines for review to ensure transactional integrity is maintained. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 11 | Routines with a redundant ORDER BY clause in single-row locking queries (FOR [NO KEY] UPDATE or FOR [KEY] SHARE) | 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:
|
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 12 | The use of xmin hidden column in views and routines | Find the number of views and materialized views that have a column with the xid type and the number of routines that contain a UPDATE or a DELETE statement that search condition refers to the xmin column. If one uses optimistic approach for dealing with the concurrent modifications of data, then xmin values should be presented by views and used in routines that modify or delete rows. | Sofware measure | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 13 | Trying to lock a value instead of a row | This query identifies SQL routines that utilize explicit row locking clauses (e.g., FOR UPDATE, FOR SHARE) in queries that do not target a specific base table or relation. For instance, a statement like SELECT 'text' AS v FOR UPDATE attempts to apply a lock to a scalar constant. Since row-level locks in PostgreSQL require a physical row version (tuple) within a table to be effective, such statements are semantically void. They indicate a fundamental misunderstanding of the concurrency control mechanism and should be corrected to target actual table rows. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 14 | Unnecessary explicit locking in read-only routines | This query identifies read-only routines that unnecessarily utilize explicit locking mechanisms. PostgreSQL's Multi-Version Concurrency Control (MVCC) ensures that readers do not block writers (and writers do not block readers) during standard query operations. Consequently, routines that perform no data modification (DML) and do not raise exceptions have no functional need to acquire table-level locks (LOCK TABLE) or row-level locks (FOR UPDATE / FOR SHARE). Acquiring locks in purely read-only contexts provides no consistency benefits while actively degrading database concurrency by causing transaction contention. The query explicitly excludes routines whose names indicate they were intentionally designed to handle locking. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 15 | User-defined routines that use xmin hidden column | Find routines that contain a UPDATE or a DELETE statement that search condition refers to the xmin column. If one uses optimistic approach for dealing with the concurrent modifications of data, then xmin values should be presented by views and used in routines that modify or delete rows. | General | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 16 | Validation triggers reading reference tables without FOR SHARE locking | This query identifies row-level BEFORE triggers that validate data by querying other tables (SELECT ... FROM ... WHERE) before raising an exception, but fail to acquire a shared lock (FOR SHARE) on the referenced rows. Under PostgreSQL's default READ COMMITTED isolation level, reading a reference or classifier row (such as checking whether a status, category, or type is active) without FOR SHARE allows concurrent transactions to deactivate or modify that reference row before the triggering transaction commits, leading to write skew and data integrity violations. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View |