| Goal | This query identifies redundant rewrite rules within the database schema. It targets views that are inherently non-updatable (due to the presence of aggregates, joins, or set operations) but are nevertheless defined with a DO INSTEAD NOTHING rule. Since the PostgreSQL engine cannot perform DML operations on such views natively, the view is effectively read-only by definition. Consequently, the explicit rule serves no functional purpose in preventing data modification and represents superfluous schema metadata. |
|---|---|
| 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) |
| Fixing Suggestion | Drop the rules. |
| Data Source | INFORMATION_SCHEMA+system catalog |
| SQL Query |
|
SQL statements that help generate fixes for the identified problem.
Fix Action #1
WITH non_updatable_views AS (SELECT table_schema AS schema, table_name AS view
FROM Information_schema.views
WHERE (is_insertable_into='YES' OR is_updatable='YES') AND check_option='NONE'
AND 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~*'(JOIN|DISTINCT|GROUP|UNION|INTERSECT|EXCEPT|HAVING|WHERE.*[_[:alnum:]]+\.[_[:alnum:]]+[[:space:]]*=[[:space:]]*[_[:alnum:]]+\.[_[:alnum:]]+|LIMIT|OFFSET|FETCH[[:space:]]FIRST)'
OR view_definition~*'(sum|count|avg|min|max|over)[[:space:]]{0,1}[(]'))
SELECT format('DROP RULE %1$I ON %2$I.%3$I;', rulename, schemaname, tablename) AS statements
FROM pg_rules
WHERE definition~'ON[[:space:]](INSERT|UPDATE|DELETE).*DO[[:space:]]INSTEAD[[:space:]]NOTHING'
AND (schemaname, tablename) IN (SELECT schema, view
FROM non_updatable_views)
ORDER BY schemaname, tablename, rulename;Drop the rules.
| SQL Query to Generate Fix | Description |
|---|---|
| Drop the rules. |
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:
Derived tables
Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer.
Triggers and rules
Queries of this category provide information about triggers and rules in a database.
| Name | Description |
|---|---|
| Derived tables | Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer. |
| Triggers and rules | Queries of this category provide information about triggers and rules in a database. |