List tables with the largest number of columns in PostgreSQL database

Query that returns tables with number of columns, ordered from the ones that have the most.

Query

select tab.table_schema,
       tab.table_name,
       count(*) as columns
from information_schema.tables tab
inner join information_schema.columns col
           on tab.table_schema = col.table_schema
           and tab.table_name = col.table_name
where tab.table_schema not in ('information_schema', 'pg_catalog')
      and tab.table_type = 'BASE TABLE'
group by tab.table_schema, tab.table_name
order by count(*) desc;

Columns

  • table_schema - name of the schema
  • table_name - name of the table
  • columns - number of columns in table

Rows

  • One row represents one table in a database
  • Scope of rows: all tables in a database
  • Ordered by number of columns descending - from tables with the most columns

Sample results