Skip to content
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

PostgreSQL sequences need to be reset after subsetter is completed #14

Open
binarydev opened this issue Oct 9, 2019 · 2 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@binarydev
Copy link

Hey Tonic devs,

I've been using your awesome tool for a couple of months now, and it's been so great to use! Something I noticed though is that after a subset has been generated, I need to run some SQL to reset DB sequences to their max value in the resulting table before I can generate a backup via pg_dump. Otherwise, the sequences are all reset to 1. Is this intentional on your part for some reason?

@acolombi
Copy link
Contributor

acolombi commented Oct 9, 2019

Good catch! No that's not intentional, and it's definitely a bug. As a work around, are you aware of the post_subset_sql configuration setting? You can stuff you sequence fixing SQL in there to make it a little easier for you, if you aren't already doing that already.

@acolombi acolombi self-assigned this Oct 9, 2019
@acolombi acolombi added the bug Something isn't working label Oct 9, 2019
@binarydev
Copy link
Author

binarydev commented Nov 20, 2019

Missed this comment from last month, but yes we do something similar we just don't use this configuration setting to do so. After the subsetting is completed, we use psql to run the following SQL against the targeted DB that has the subset imported. It catches all of our sequences and generates a SQL file with a reset per sequence that we pass to a 2nd psql command for execution:

-- Outputs a SQL command for each sequence in the schema, which should all be saved to a file and then executed
-- The generated SQL will reset each sequence to the maximum ID in each table + 1
SELECT 'SELECT SETVAL(' ||
       quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
       ', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
       quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
FROM pg_class AS S,
     pg_depend AS D,
     pg_class AS T,
     pg_attribute AS C,
     pg_tables AS PGT
WHERE S.relkind = 'S'
    AND S.oid = D.objid
    AND D.refobjid = T.oid
    AND D.refobjid = C.attrelid
    AND D.refobjsubid = C.attnum
    AND T.relname = PGT.tablename
ORDER BY S.relname;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants