What if you could turn
thousands of lines of code into
simple queries?

Finding orphaned sequences

This time we’re having a database where sequences were used, but not systematically as a default value of a given column. It’s mainly an historic bad idea, but you know the usual excuse with bad ideas and bad code: the first 6 months it’s experimental, after that it’s historic. Not talking about genome orphaned sequences here, though Still, here’s a query for 8.4 that will allow you to list those sequences you have that are not used as a default value in any of your tables:


I wrote a book!


Now you know what piece of software is used to publish this blog. I really like it, the major mode makes it a great experience to be using this tool, and the fact that you produce the HTML and rsync it all from within Emacs ( C-c C-p then C-c C-r with some easy elisp code) is a big advantage as far as I’m concerned. No need to resort to shell and Makefile.


So, if you followed the previous blog entry, now you have a new database containing all the static tables encoded in UTF-8 rather than SQL_ASCII. Because if it was not yet the case, you now severely distrust this non-encoding.

Now is the time to have a look at properly encoding the live data, those stored in tables that continue to receive write traffic. The idea is to use the UPDATE facilities of PostgreSQL to tweak the data, and too fix the applications so as not to continue inserting badly encoded strings in there.


It happens that you have to manage databases designed by your predecessor, and it even happens that the team used to not have a DBA. Those histerical raisins can lead to having a SQL_ASCII database. The horror! What SQL_ASCII means, if you’re not already familiar with the consequences of such a choice, is that all the text and varchar data that you put in the database is accepted as-is. No checks.


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.

Dimitri Fontaine

PostgreSQL Major Contributor

Open Source Software Engineer

France