Resetting sequences. All of them, please!
So, after restoring a production dump with intermediate filtering, none of our sequences were set to the right value. I could have tried to review the process of filtering the dump here, but it's a one-shot action and you know what that sometimes mean. With some pressure you don't script enough of it and you just crawl more and more.
Still, I think how I solved it is worthy of a blog entry. Not that it's
about a super unusual clever trick, quite the contrary, because questions
involving this trick are often encountered on the support IRC.
The idea is to query the catalog for all sequences, and produce from there
the SQL command you will have to issue for each of them. Once you have this
query, it's quite easy to arrange from the psql prompt as if you had dynamic
scripting capabilities. Of course in 9.0 you will have inline anonymous DO
blocks.
#> \o /tmp/sequences.sql #> \t Showing only tuples. #> YOUR QUERY HERE #> \o #> \t Tuples only is off.
Once you have the /tmp/sequences.sql file, you can ask psql to execute its
command as you're used to, that's using \i in an explicit transaction block.
Now, the interresting part if you got here attracted by the blog entry title
is in fact the query itself. A nice way to start is to \set ECHO_HIDDEN then
describe some table, you now have a catalog example query to work with. Then
you tweak it somehow and get this:
SELECT 'select ' || trim(trailing ')' from replace(pg_get_expr(d.adbin, d.adrelid), 'nextval', 'setval')) || ', (select max( ' || a.attname || ') from only ' || nspname || '.' || relname || '));' FROM pg_class c JOIN pg_namespace n on n.oid = c.relnamespace JOIN pg_attribute a on a.attrelid = c.oid JOIN pg_attrdef d on d.adrelid = a.attrelid and d.adnum = a.attnum and a.atthasdef WHERE relkind = 'r' and a.attnum > 0 and pg_get_expr(d.adbin, d.adrelid) ~ '^nextval';
Coming next, a recode based script in order to get from SQL_ASCII to UTF-8,
and some strange looking queries too.
recode.sh [-npdf0TI] [-U user ] -s schema [-m mintable] pattern
Stay tuned!
Index of All Articles
- Synchronous Replication, Mon, 6 Sep 2010, 18:05
- Happy Numbers, Mon, 30 Aug 2010, 11:00
- Playing with bit strings, Thu, 26 Aug 2010, 17:45
- Editing constants in constraints, Mon, 9 Aug 2010, 14:45
- debian packaging PostgreSQL extensions, Fri, 6 Aug 2010, 13:00
- Querying the Catalog to plan an upgrade, Thu, 5 Aug 2010, 11:00
- Database Virtual Machines, Tue, 3 Aug 2010, 13:30
- Partitioning: relation size per “group”, Mon, 26 Jul 2010, 17:00
- Emacs and PostgreSQL, Thu, 22 Jul 2010, 9:30
- Background writers, Mon, 19 Jul 2010, 16:30
- Logs analysis, Tue, 13 Jul 2010, 14:15
- Using indexes as column store?, Thu, 8 Jul 2010, 11:15
- MVCC in the Cloud, Tue, 6 Jul 2010, 10:50
- Back from CHAR(10), Mon, 5 Jul 2010, 9:30
- Back from PgCon2010, Thu, 27 May 2010, 14:26
- Import fixed width data with pgloader, Tue, 27 Apr 2010, 12:01
- pgloader activity report, Tue, 6 Apr 2010, 9:10
- Finding orphaned sequences, Wed, 17 Mar 2010, 12:35
- Getting out of SQL_ASCII, part 2, Tue, 23 Feb 2010, 16:30
- Getting out of SQL_ASCII, part 1, Thu, 18 Feb 2010, 10:37
- Resetting sequences. All of them, please!, Tue, 16 Feb 2010, 15:23
- pg_staging's bird view, Tue, 8 Dec 2009, 11:04
- PGday.eu feedback, Tue, 1 Dec 2009, 15:45
- prefix 1.1.0, Mon, 30 Nov 2009, 11:10
- Yet Another PostgreSQL tool hits debian, Wed, 25 Nov 2009, 10:49
- PGDay.eu, Paris: it was awesome!, Mon, 9 Nov 2009, 8:50
- prefix 1.0.0, Tue, 6 Oct 2009, 15:56
- hstore-new & preprepare reach debian too, Tue, 18 Aug 2009, 9:14
- prefix 1.0~rc2 in debian testing, Mon, 3 Aug 2009, 14:50
- prefix 1.0~rc2-1, Thu, 9 Jul 2009, 12:48
- prefix extension reaches 1.0 (rc1), Tue, 23 Jun 2009, 10:53
- PgCon 2009, Wed, 27 May 2009, 14:30
- Prepared Statements and pgbouncer, Thu, 14 May 2009
- Skytools 3.0 reaches alpha1, Tue, 14 Apr 2009
- Prefix GiST index now in 8.1, Mon, 9 Feb 2009
- Importing XML content from file, Wed, 4 Feb 2009
- Asko Oja talks about Skype architecture, Tue, 3 Feb 2009
- Skytools ticker daemon and londiste, Mon, 2 Feb 2009
- Comparing Londiste and Slony, Fri, 30 Jan 2009
- Controling HOT usage in 8.3, Tue, 27 Jan 2009
- Londiste Trick, Tue, 20 Jan 2009

