| Goal | 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. |
|---|---|
| Notes | In the returned subquery of view/materialized view 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. |
| Type | Problem detection (Each row in the result could represent a flaw in the design) |
| Reliability | High (Few or no false-positive results) |
| License | MIT (opens in new tab) |
| Fixing Suggestion | Drop the view and recreate it without using explicit row locking in the subquery. |
| Data Source | INFORMATION_SCHEMA+system catalog |
| SQL Query |
|
SQL statements that help generate fixes for the identified problem.
Fix Action #1
SELECT format('DROP VIEW %1$I.%2$I;', views.table_schema, views.table_name) AS statements
FROM information_schema.views
WHERE table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND view_definition ~*'[[:space:]]+for[[:space:]]+(update|no[[:space:]]+key[[:space:]]+update|share|key[[:space:]]+share)'
ORDER BY table_schema, table_name;Drop the view.
Fix Action #2
SELECT format('DROP MATERIALIZED VIEW %1$I.%2$I;', schemaname, matviewname) AS statements
FROM pg_catalog.pg_matviews
WHERE definition ~*'[[:space:]]+for[[:space:]]+(update|no[[:space:]]+key[[:space:]]+update|share|key[[:space:]]+share)'
ORDER BY schemaname, matviewname;Drop the materialized view.
| SQL Query to Generate Fix | Description |
|---|---|
| Drop the view. |
| Drop the materialized view. |
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.
Derived tables
Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer.
Performance
Queries of this category provide information about indexes in a database.
| Name | Description |
|---|---|
| Concurrency control | Queries of this category provide information about concurrency control. |
| Derived tables | Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer. |
| Performance | Queries of this category provide information about indexes in a database. |
Further reading and related materials: