This query identifies automatically updatable views that lack the WITH CHECK OPTION clause. Without this constraint, it is possible to perform INSERT or UPDATE operations through the view that create rows which do not satisfy the view's defining predicate (the WHERE clause). This results in "phantom" modifications where the new or updated data is successfully committed to the base table but is immediately excluded from the view's result set. Enforcing WITH CHECK OPTION ensures that all data modifications performed through the view remain visible within the view.
Notes
The query excludes views that are inherently not updatable (e.g., have a join or grouping) but have a DO INSTEAD rule.
Type
Problem detection (Each row in the result could represent a flaw in the design)
Recreate the view with the WITH CHECK OPTION constraint.
Data Source
INFORMATION_SCHEMA only
SQL Query
SELECT table_schema AS view_schema, table_name AS view_name, is_updatable, is_insertable_into
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)'
AND view_definition!~*'(sum|count|avg|min|max|over)[[:space:]]{0,1}[(]')
ORDER BY table_schema, table_name;
SQL statements that help generate fixes for the identified problem.
SQL Query to Generate Fix
Description
SELECT format('CREATE OR REPLACE VIEW %1$I.%2$I WITH(security_barrier) AS %3$s WITH CHECK OPTION;', table_schema, table_name, rtrim(view_definition, ';')) AS statements
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)'
AND view_definition!~*'(sum|count|avg|min|max|over)[[:space:]]{0,1}[(]')
ORDER BY table_schema, table_name;
Recreate the view with the WITH CHECK OPTION constraint.
Collections
This query belongs to the following collections:
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:
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.
Security
Queries of this category provide information about the security measures.