-- Surrogate PKs check.
-- Use it to determine when you reach INT2/INT4/INT8 limits
do $$
  declare r record;
  begin
    create temp table seqs(name text, val int8);
    for r
      in select * from pg_class where relname ilike '%seq'
    loop
      execute 'do $b$ declare z int8; begin insert into seqs(name, val) select '''
        || r.relname::text || ''', last_value from '
        || r.relname::text || '; end; $b$ language plpgsql';
    end loop;
    for r in
      select * from seqs order by val asc
    loop
      raise info '%: %', r.name, r.val;
    end loop;
    drop table seqs;
  end;
$$ language plpgsql;