Skip to content

Instantly share code, notes, and snippets.

@sseletskyy
Created December 4, 2013 14:28
Show Gist options
  • Save sseletskyy/7788281 to your computer and use it in GitHub Desktop.
Save sseletskyy/7788281 to your computer and use it in GitHub Desktop.
UPDATE ALL SEQUENCES
--This is a handy PostreSQL script to fix sequences for all tables at once
SELECT 'SELECT SETVAL(' ||quote_literal(quote_ident(S.relname))|| ', MAX(' ||quote_ident(C.attname)|| ') ) FROM ' ||quote_ident(T.relname)|| ';'
FROM pg_class AS S, pg_depend AS D, pg_class AS T, pg_attribute AS C
WHERE S.relkind = 'S'
AND S.oid = D.objid
AND D.refobjid = T.oid
AND D.refobjid = C.attrelid
AND D.refobjsubid = C.attnum
ORDER BY S.relname;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment