Skip to content

[Resharding] Update sequences #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 14, 2025
Merged

[Resharding] Update sequences #327

merged 5 commits into from
Aug 14, 2025

Conversation

levkk
Copy link
Collaborator

@levkk levkk commented Aug 12, 2025

No description provided.

@vitabaks
Copy link

vitabaks commented Aug 12, 2025

@levkk maybe this will help you, this is how I usually copy all sequences:

# Copy sequences from source database
psql -h <source_dbhost> \
-U postgres \
-d <source_dbname>  \
-tAXc "
  select format(
    'select setval(%L, %s);',
    format('%I.%I', schemaname, sequencename),
    coalesce(last_value, 1) + 1000000
  )
  from pg_sequences
" > pg_sequences_increase.sql

# Apply new sequence values on target database
psql -U postgres -d <target_dbname> -f pg_sequences_increase.sql

Please note that here we copy the current value of all the sequences and add 1 million to them, usually this is enough, but in any case it is individual for each cluster because it depends on the load.

@vitabaks
Copy link

Although if you update a sequence only for a specific table, you won't need it.

@levkk
Copy link
Collaborator Author

levkk commented Aug 12, 2025

I'm using the primary schema dump as the source of truth and separating statements into 3 phases:

  • pre logical replication sync (tables, defaults, primary key index)
  • post logical replication sync (foreign keys, secondary indexes)
  • cutover sync (set sequence id to max(id) + 1)

@levkk levkk marked this pull request as ready for review August 14, 2025 15:53
@levkk levkk merged commit 99058e9 into main Aug 14, 2025
5 checks passed
@levkk levkk deleted the levkk-seq-update branch August 14, 2025 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants