CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
SELECT table1.table_schema, table1.table_name, table2.column_name AS
polymorphic_column, table1.column_name AS classifier_column
FROM (SELECT table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.constraint_column_usage
WHERE 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) AND
(constraint_schema, constraint_name) IN
(SELECT constraint_schema, constraint_name
FROM INFORMATION_SCHEMA.check_constraints
WHERE check_clause~*'^.+=.*ANY.*[(].*ARRAY[[].+[])].*$')
UNION
SELECT cdu.table_schema, cdu.table_name, cdu.column_name
FROM INFORMATION_SCHEMA.column_domain_usage AS cdu INNER JOIN
INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name)
WHERE t.table_type='BASE TABLE' AND (domain_schema, domain_name) IN
(SELECT domain_schema, domain_name
FROM INFORMATION_SCHEMA.domain_constraints
WHERE (constraint_schema, constraint_name) IN
(SELECT constraint_schema, constraint_name
FROM INFORMATION_SCHEMA.check_constraints
WHERE check_clause~*'^.+=.*ANY.*[(].*ARRAY[[].+[])].*$'))) AS table1,
(SELECT c.table_schema, c.table_name, c.column_name
FROM INFORMATION_SCHEMA.columns AS c INNER JOIN INFORMATION_SCHEMA.tables AS t
USING (table_schema, table_name)
WHERE t.table_type='BASE TABLE') AS table2
WHERE table1.table_schema=table2.table_schema AND
table1.table_name=table2.table_name
AND table1.column_name<>table2.column_name AND
levenshtein(table1.column_name,table2.column_name)<=4 AND
(table2.table_schema, table2.table_name, table2.column_name) NOT IN (SELECT
kcu_dependent.table_schema, kcu_dependent.table_name, kcu_dependent.column_name
FROM INFORMATION_SCHEMA.key_column_usage AS kcu_dependent
INNER JOIN INFORMATION_SCHEMA.referential_constraints AS rc ON
(kcu_dependent.constraint_schema = rc.constraint_schema) AND
(kcu_dependent.constraint_name = rc.constraint_name))
ORDER BY table1.table_schema, table1.table_name;