Query goal: | Identify disabled rules. These should be enabled or dropped, otherwise these are dead code. |
Notes about the query: | Rules are specific to PostgreSQL and thus it is not possible to get information about these from the INFORMATION_SCHEMA views. |
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: | Enable the rule or drop it. |
Data source: | system catalog only |
SQL query: | Click on query to copy it
WITH disabled_rules AS (SELECT n.nspname AS schemaname, c.relname AS tablename, r.rulename, CASE WHEN ev_type='1' THEN 'SELECT' WHEN ev_type='2' THEN 'UPDATE' WHEN ev_type='3' THEN 'INSERT' ELSE 'DELETE' END AS event, pg_get_ruledef(r.oid) AS definition FROM pg_rewrite r JOIN pg_class c ON c.oid = r.ev_class JOIN pg_namespace n ON n.oid = c.relnamespace JOIN pg_authid u ON n.nspowner = u.oid WHERE ev_enabled='D' AND (n.nspname = 'public' OR u.rolname <> 'postgres')) SELECT schemaname, tablename, rulename, event, definition FROM disabled_rules ORDER BY schemaname, tablename, rulename; |
SQL query | Description |
---|---|
WITH disabled_rules AS (SELECT n.nspname AS schemaname, c.relname AS tablename, r.rulename FROM pg_rewrite r JOIN pg_class c ON c.oid = r.ev_class LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE ev_enabled='D') SELECT format('ALTER TABLE %1$I.%2$I ENABLE RULE %3$I;', schemaname, tablename, rulename) AS statements FROM disabled_rules ORDER BY schemaname, tablename, rulename; | Enable the rule. |
WITH disabled_rules AS (SELECT n.nspname AS schemaname, c.relname AS tablename, r.rulename FROM pg_rewrite r JOIN pg_class c ON c.oid = r.ev_class LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE ev_enabled='D') SELECT format('DROP RULE %1$I ON %2$I.%3$I RESTRICT;', rulename, schemaname, tablename) AS statements FROM disabled_rules ORDER BY schemaname, tablename, rulename; | Drop the rule. |
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 |
---|---|
Triggers and rules | Queries of this category provide information about triggers and rules in a database. |
Unused implementation elements | Queries of this catergory provide information about the database objects that are not used. |
Reference |
---|
https://en.wikipedia.org/wiki/Dead_code |