Search code examples
postgresql

Get number of existing and previous columns for a table


My PostgreSQL installation has a max column count of 1600. It turns out this max value is a total for the lifetime of the table, so if I add and drop 1601 different columns, one at a time, then I have exceeded the max column count even if the table didn't have more than 1 column at any point.

I want to know if a given table is close to this max. Is there any way I can get the number of current and previous columns for a table?


Solution

  • The dropped columns are still visible in pg_attribute

    select count(*) filter (where not attisdropped) as active_columns, 
           count(*) filter (where attisdropped) as dropped_columns 
    from pg_attribute a
      join pg_class c on a.attrelid = c.oid
    where c.relname = 'your_table_name'  
      and attnum > 0; -- this filters out system columns