Paste this into a new question or an answer at dba.stackexchange.com:
<!-- --> > create table t (Col1 text, Col2 text, Col3 text, Col4 text); > insert into t values > (null, 'xyz', 'pdq', null), > ('abc', 'def', 'ghj', null), > ('hsh', 'fff', 'oko',null); > > <pre> > ✓ > 3 rows affected > </pre> <!-- --> > do > $$ > declare > cols text; > > begin > > cols := string_agg('count(' || column_name::text || ') ' || column_name::text, ',') > from (select column_name > from information_schema.columns > where table_name = 't') c; > > execute format('create temp table counter as select %s from t;', cols); > > end; > $$; > > select * from counter; > > <pre> > ✓ > > col1 | col2 | col3 | col4 > ---: | ---: | ---: | ---: > 2 | 3 | 3 | 0 > </pre> *db<>fiddle [here](https://dbfiddle.uk/?rdbms=postgres_11&fiddle=fc9861154a99af50cc3ad4e21655a85b)*
back to fiddle