Skip to content

Instantly share code, notes, and snippets.

@iver
Created August 6, 2015 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iver/5ca8be52858c2f24a9ba to your computer and use it in GitHub Desktop.
Save iver/5ca8be52858c2f24a9ba to your computer and use it in GitHub Desktop.
Update sequence query
SELECT
'select pg_catalog.setval(pg_get_serial_sequence('''||
pg_class.relname || ''', ''' ||
pg_attribute.attname || '''), (SELECT MAX(' || pg_attribute.attname || ') FROM ' ||
pg_class.relname || ') +1 ); '
FROM pg_index, pg_class, pg_attribute
WHERE
pg_class.oid = pg_class.relname::regclass AND
indrelid = pg_class.oid AND
pg_attribute.attrelid = pg_class.oid AND
pg_attribute.attnum = any(pg_index.indkey) AND
pg_class.relkind = 'r' and pg_class.relhaspkey
AND indisprimary
AND pg_class.relname LIKE '<table_name>'
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment