Skip to content

Instantly share code, notes, and snippets.

@dexalex84
Created November 19, 2016 14:52
Show Gist options
  • Save dexalex84/0d68e31050a4cfae5776b615ed954c2f to your computer and use it in GitHub Desktop.
Save dexalex84/0d68e31050a4cfae5776b615ed954c2f to your computer and use it in GitHub Desktop.
PostgreSQL Count of rows in tables in one schema by simple count and store in temp table
;
do
$$
DECLARE var_table_name character varying(1000);
var_cnt bigint;
begin
drop table if exists temp_tables;-- ( character varying(1000), bigint);
create temp table if not exists temp_tables (table_name character varying(1000), cnt bigint);
truncate table tt_tables;
for var_table_name in select table_name from information_schema.tables tt where tt.table_schema = 'tempdownload' loop
insert into temp_tables(table_name)
select var_table_name;
end loop;
for var_table_name in select table_name from temp_tables loop
execute 'select count(*) from tempdownload."'||var_table_name||'";'
into var_cnt;
raise notice 'processing table tempdownload."%"',var_table_name;
update temp_tables
set cnt = var_cnt
where
temp_tables.table_name = var_table_name;
end loop;
end;
$$
language 'plpgsql';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment