Last active
June 7, 2017 18:23
-
-
Save SuperPaintman/4b4958a8ee8ddda7bc6474499eb84f78 to your computer and use it in GitHub Desktop.
PostgreSQL fixing sequences
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
#!/bin/bash | |
# | |
# Author: SuperPaintman <[email protected]> | |
# | |
database_name="my_awesome_database" | |
psql -Atq "$database_name" <<SQL | psql -Atq "$database_name" | |
SELECT 'SELECT ' || | |
quote_literal(T.relname || ' (' || C.attname || ') -> ') || | |
' || 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; | |
SQL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment