Goal Find the number of rules in a database in different schemas, excluding the rules that are created to support views. Rules can be used to maintain data integrity in a database by causing rejection of incorrect insertions and updates. Therefore, the number of rules in a database gives an indication about the state of enforcing constraints at the database level.
Notes Rules are specific to PostgreSQL and thus it is not possible to get information about these from the INFORMATION_SCHEMA views. The query only returns data about schemas that have at least one user-defined rule.
Type Sofware measure Numeric values (software measures) about the database
License MIT (opens in new tab)
Data Source system catalog only
SQL Query
WITH 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
     JOIN pg_namespace n ON n.oid = c.relnamespace
     JOIN pg_authid u ON  n.nspowner = u.oid
WHERE r.rulename <> '_RETURN' AND
(n.nspname = 'public' OR u.rolname <> 'postgres'))
SELECT schemaname, Count(*) AS number_of_rules 
FROM rules
GROUP BY ROLLUP(schemaname)
ORDER BY schemaname;

Collections

This query belongs to the following collections:

Find problems by overview

Queries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .

Find quick numeric overview of the database

Queries that return numeric values showing mostly the number of different types of database objects in the database

NameDescription
Find problems by overviewQueries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .
Find quick numeric overview of the databaseQueries that return numeric values showing mostly the number of different types of database objects in the database

Categories

This query is classified under the following categories:

Triggers and rules

Queries of this category provide information about triggers and rules in a database.

NameDescription
Triggers and rulesQueries of this category provide information about triggers and rules in a database.