Skip to content

Instantly share code, notes, and snippets.

@nathanjsharpe
Created October 15, 2012 16:02
Show Gist options
  • Save nathanjsharpe/3893295 to your computer and use it in GitHub Desktop.
Save nathanjsharpe/3893295 to your computer and use it in GitHub Desktop.
Postgres Reset Sequence Function
CREATE OR REPLACE FUNCTION reset_sequence(tablename text) RETURNS "pg_catalog"."void" AS $$
DECLARE
BEGIN
RAISE NOTICE 'Resetting sequence for %...', tablename;
EXECUTE 'SELECT setval(''' || tablename || '_id_seq'', (SELECT MAX(id) FROM ' || tablename || '))';
RAISE NOTICE 'Sequence reset.';
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment