WITH name_cols AS (SELECT table_schema, table_name, column_name,
CASE WHEN character_maximum_length IS NOT NULL
THEN data_type || '(' || character_maximum_length || ')'
ELSE data_type END AS data_type, CASE WHEN is_nullable='NO' THEN 'NOT NULL' ELSE 'NULL' END AS not_null,
ordinal_position
FROM INFORMATION_SCHEMA.columns
WHERE column_name ~* '(comment|description|explanation|komment|selgitus|kirjeldus)'
AND (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE')
AND 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))
SELECT table_schema, table_name, Count(*) AS nr_of_columns, string_agg(column_name || ' ' || data_type || ' ' || not_null,';<br>' ORDER BY ordinal_position) AS columns
FROM name_cols
GROUP BY table_schema, table_name
HAVING Count(*)>1
ORDER BY Count(*) DESC, table_schema, table_name;