Created
December 1, 2011 08:21
-
-
Save domguard/1414936 to your computer and use it in GitHub Desktop.
Remettre les séquences à zéro sur PostGreSQL
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
/usr/bin/psql --host localhost --port 5432 --username db_owner db_name | |
CREATE OR REPLACE FUNCTION "reset_sequence" (tablename text) RETURNS "pg_catalog"."void" AS | |
$body$ | |
DECLARE | |
BEGIN | |
EXECUTE 'SELECT setval( ''' | |
|| tablename | |
|| '_id_seq'', ' | |
|| '(SELECT id + 1 FROM "' | |
|| tablename | |
|| '" ORDER BY id DESC LIMIT 1), false)'; | |
END; | |
$body$ LANGUAGE 'plpgsql'; | |
select sequence_name, reset_sequence(split_part(sequence_name, '_id_seq',1)) from information_schema.sequences where sequence_schema='public'; | |
\q |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment