Last active
August 29, 2015 14:01
-
-
Save chanmix51/f3d26be648c47af52fe8 to your computer and use it in GitHub Desktop.
Restart sequences after a migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace function restart_sequence(seq_name varchar) returns void language plpgsql as $func$ | |
declare | |
maxint bigint; | |
table_name varchar; | |
begin | |
table_name := left(seq_name, -7); | |
execute 'select max(id) + 1 from '||table_name into maxint; | |
if maxint is not null then | |
execute 'alter sequence '||seq_name||' restart with '||cast(maxint as text); | |
end if; | |
end; | |
$func$ | |
; |
Author
chanmix51
commented
May 28, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment