Query goal: | Find pairs of names of different types of tables that are very similar or even equal. |
Notes about the query: | The query finds the pairs of table names where the Levenshtein distance between the names is less than three. The query uses a function from the fuzzystrmatch extension. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | Low (Many false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Make sure that the names are correct and there are no duplication or unused tables. |
Data source: | INFORMATION_SCHEMA+system catalog |
SQL query: | Click on query to copy it
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch; WITH tables AS (SELECT nspname AS table_schema, relname AS table_name, CASE WHEN relkind='r' THEN 'BASE TABLE' WHEN relkind='f' THEN 'FOREIGN TABLE' WHEN relkind='v' THEN 'VIEW' WHEN relkind='m' THEN 'MATERIALIZED VIEW' END AS table_type FROM pg_class INNER JOIN pg_namespace ON pg_class.relnamespace=pg_namespace.oid WHERE relkind IN ('r','m','v','f') AND nspname NOT IN (SELECT schema_name FROM INFORMATION_SCHEMA.schemata WHERE schema_name<>'public' AND schema_owner='postgres' AND schema_name IS NOT NULL)) SELECT t1.table_schema AS table1_schema, t1.table_name AS table1_name, t1.table_type AS table1_type, t2.table_schema AS table2_schema, t2.table_name AS table2_name, t2.table_type AS table2_type FROM tables AS t1, tables AS t2 WHERE NOT (t1.table_schema=t2.table_schema AND t1.table_name=t2.table_name) AND levenshtein(t1.table_name,t2.table_name)<=2 ORDER BY table1_name, table2_name; DROP EXTENSION IF EXISTS fuzzystrmatch; |
Category name | Category description |
---|---|
Duplication of implementation elements | Queries of this catergory provide information about the duplication of the database objects. |
Naming | Queries of this category provide information about the style of naming. |
Unused implementation elements | Queries of this catergory provide information about the database objects that are not used. |
Reference |
---|
https://www.postgresql.org/docs/current/fuzzystrmatch.html |
https://en.wikipedia.org/wiki/Levenshtein_distance |