Last active
May 25, 2023 08:23
-
-
Save hipertracker/59417b0300fd81cda021b550c57f0ba0 to your computer and use it in GitHub Desktop.
Postgres query to update all sequences to proper next value:
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
with sequences as ( | |
select * | |
from ( | |
select table_schema, | |
table_name, | |
column_name, | |
pg_get_serial_sequence(format('%I.%I', table_schema, table_name), column_name) as col_sequence | |
from information_schema.columns | |
where table_schema not in ('pg_catalog', 'information_schema') | |
) t | |
where col_sequence is not null | |
), maxvals as ( | |
select table_schema, table_name, column_name, col_sequence, | |
(xpath('/row/max/text()', | |
query_to_xml(format('select max(%I) from %I.%I', column_name, table_schema, table_name), true, true, '')) | |
)[1]::text::bigint as max_val | |
from sequences | |
) | |
select table_schema, | |
table_name, | |
column_name, | |
col_sequence, | |
coalesce(max_val, 0) as max_val, | |
setval(col_sequence, coalesce(max_val, 1)) --<< this will change the sequence | |
from maxvals; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment