Created
December 1, 2019 09:44
-
-
Save ImanMousavi/87698b2ed9732d614199556dd6f8d428 to your computer and use it in GitHub Desktop.
reset All sequence in postgresql
This file contains 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
SELECT | |
tablename, | |
reset_sequence ( C.tablename, 'id', C.tablename || '_id_seq' ) | |
FROM | |
pg_catalog.pg_tables AS "c" | |
WHERE | |
schemaname <> 'pg_catalog' | |
AND schemaname <> 'information_schema' | |
AND tablename NOT IN ( 'layer', 'spatial_ref', 'spatial_ref_sys', 'combination_has_province', 'statement', 'combination_has_category', 'combination_has_city', 'discount_has_category', 'value' ); | |
CREATE OR REPLACE FUNCTION "public"."reset_sequence" ( "tablename" TEXT, "columnname" TEXT, "sequence_name" TEXT ) RETURNS "pg_catalog"."void" AS $BODY$ DECLARE | |
BEGIN | |
IF | |
EXISTS ( SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_NAME = tablename AND COLUMN_NAME = columnname ) THEN | |
EXECUTE'SELECT setval( ''' || sequence_name || ''', ' || '(SELECT MAX(' || columnname || ') FROM ' || tablename || ')' || '+1)'; | |
END IF; | |
END; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment