Skip to content

Instantly share code, notes, and snippets.

@dpirotte
Last active August 29, 2015 14:15
Show Gist options
  • Save dpirotte/c3dfc649d18e58f5cee6 to your computer and use it in GitHub Desktop.
Save dpirotte/c3dfc649d18e58f5cee6 to your computer and use it in GitHub Desktop.
Function to return all your postgres sequences and their last value
create or replace function sequence_positions(OUT sequence_name regclass, OUT last_value bigint) returns setof record as $$
declare
sequence regclass;
begin
FOR sequence IN SELECT format('%s.%s', n.nspname, c.relname) AS sequence FROM pg_class c JOIN pg_namespace n ON (c.relnamespace = n.oid) WHERE (c.relkind = 'S')
LOOP
return query execute format('select ''%s''::regclass, last_value from %s', sequence, sequence);
END LOOP;
end
$$ language plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment