Goal Do not keep in your database elements that are not needed by anybody. These should be put in use or dropped, otherwise these are dead code.
Notes The query finds all the non-system schemas that do not contain any schema object.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability High (Few or no false-positive results)
License MIT (opens in new tab)
Fixing Suggestion Drop the schema or start to use it.
Data Source system catalog only
SQL Query
SELECT nspname AS schema_name
FROM pg_namespace AS n INNER JOIN pg_authid AS a ON n.nspowner=a.oid
WHERE (nspname='public' OR rolname<>'postgres')
AND NOT EXISTS (SELECT 1
FROM pg_class AS c
WHERE n.oid=c.relnamespace)
AND NOT EXISTS (SELECT 1
FROM pg_type AS pt
WHERE n.oid=pt.typnamespace)
AND NOT EXISTS (SELECT 1
FROM pg_proc AS pc
WHERE n.oid=pc.pronamespace)
ORDER BY nspname;

SQL statements that help generate fixes for the identified problem.

Fix Action #1
SELECT format('DROP SCHEMA %1$I;', nspname) AS statements
FROM pg_namespace AS n INNER JOIN pg_authid AS a ON n.nspowner=a.oid
WHERE (nspname='public' OR rolname<>'postgres')
AND NOT EXISTS (SELECT 1
FROM pg_class AS c
WHERE n.oid=c.relnamespace)
AND NOT EXISTS (SELECT 1
FROM pg_type AS pt
WHERE n.oid=pt.typnamespace)
AND NOT EXISTS (SELECT 1
FROM pg_proc AS pc
WHERE n.oid=pc.pronamespace)
ORDER BY nspname;

Drop the schema.

SQL Query to Generate FixDescription
SELECT format('DROP SCHEMA %1$I;', nspname) AS statements
FROM pg_namespace AS n INNER JOIN pg_authid AS a ON n.nspowner=a.oid
WHERE (nspname='public' OR rolname<>'postgres')
AND NOT EXISTS (SELECT 1
FROM pg_class AS c
WHERE n.oid=c.relnamespace)
AND NOT EXISTS (SELECT 1
FROM pg_type AS pt
WHERE n.oid=pt.typnamespace)
AND NOT EXISTS (SELECT 1
FROM pg_proc AS pc
WHERE n.oid=pc.pronamespace)
ORDER BY nspname;
Drop the schema.

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 .

NameDescription
Find problems automaticallyQueries, 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:

Comfortability of database evolution

Queries of this category provide information about the means that influence database evolution.

Unused implementation elements

Queries of this catergory provide information about the database objects that are not used.

NameDescription
Comfortability of database evolutionQueries of this category provide information about the means that influence database evolution.
Unused implementation elementsQueries of this catergory provide information about the database objects that are not used.

Further reading and related materials:

The corresponding code smells in case of cleaning code are "F4: Dead Function" and "G9: Dead Code". (Robert C. Martin, Clean Code)
Reference
https://en.wikipedia.org/wiki/Dead_code
The corresponding code smells in case of cleaning code are "F4: Dead Function" and "G9: Dead Code". (Robert C. Martin, Clean Code)