Created
November 4, 2020 07:38
-
-
Save MbuguaCaleb/79485cf98a703eb61240fc456b0a65e5 to your computer and use it in GitHub Desktop.
Resetting a Sequence
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
-- What is the result? | |
SELECT MAX(id) FROM your_table; | |
-- Then run... | |
-- This should be higher than the last result. | |
SELECT nextval('your_table_id_seq'); | |
-- If it's not higher... run this set the sequence last to your highest id. | |
-- (wise to run a quick pg_dump first...) | |
BEGIN; | |
-- protect against concurrent inserts while you update the counter | |
LOCK TABLE your_table IN EXCLUSIVE MODE; | |
-- Update the sequence | |
SELECT setval('your_table_id_seq', COALESCE((SELECT MAX(id)+1 FROM your_table), 1), false); | |
COMMIT; | |
SELECT MAX(id) FROM unittrust.um_counties; | |
SELECT nextval('unittrust.um_counties_id_seq'); | |
SELECT setval('unittrust.um_counties_id_seq', | |
COALESCE((SELECT MAX(id)+1 FROM unittrust.um_counties), 1), false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment