Query goal: | Find updatable views that have WITH LOCAL CHECK OPTION constraint. The predicate of a view is the conjunction of the predicates of its (directly and indirectly) underlying tables (both base tables and derived tables) as well as the predicate of the view itself. In case of using WITH LOCAL CHECK OPTION constraint "New rows are only checked against the conditions defined directly in the view itself. Any conditions defined on underlying base views are not checked (unless they also specify the CHECK OPTION)." (PostgreSQL manual) Thus, use instead WITH CASCADED CHECK option to instruct the system to check new rows against the entire predicate of the view. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | High (Few or no false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Recreate the view with the WITH CHECK OPTION constraint. If the CHECK OPTION is specified, and neither LOCAL nor CASCADED is specified, then CASCADED is assumed. |
Data source: | INFORMATION_SCHEMA only |
SQL query: | Click on query to copy it
SELECT table_schema AS view_schema, table_name AS view_name FROM Information_schema.views WHERE check_option='LOCAL' 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) ORDER BY table_schema, table_name; |
SQL query | 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 check_option='LOCAL' 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) ORDER BY table_schema, table_name; | Recreate the view with the WITH CHECK OPTION constraint. |
Collection name | Collection 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 . |
Category name | Category description |
---|---|
Derived tables | Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer. |
Reference |
---|
http://www.postgresqltutorial.com/postgresql-views-with-check-option/ |