11 December 2004
PostgreSQL is a good solid database. But if/when something goes wrong things quickly get scary. The official docs have this to say about recovering corrupt databases. Unofficially there's pgfsck which saved my skin once (thanks Martijn!). The root problem is that PostgreSQL stores its data in a binary file that's not humanly readable. MySQL is better in this regard, it seems to use a file format that's at least partially readable. As usual the winner is Moo which uses a plain text file.
But just because one can drag a Moo database into a text editor doesn't mean it is easy to find what one is looking for. Text and verbs are easy enough since there's a context to search for, but if you're hunting for a numeric value, you've got some forensics work to do. The usual approach is to fire up the database using the emergency mode, then poke around using evals. This is a fairly heavy-weight operation. I wanted a command-line tool that would quickly parse an offline Moo database and spit out the requested information.
Since Moo databases can be many megabytes in size, C was the only acceptable choice for this tool. I've only used C once or twice before, so the experience was educational, to say the least. Programming in C is dangerous. Heisenbugs lurk everywhere. Debuggers become indispensable. And slowly the world starts to make a bit more sense:
Of all the programmers I know, there are only five whose code I'd trust to execute as intended. The common factor? Not education (one's got a PhD, another is a high-school drop-out), not OSS politics, not background. The common factor is that all five are experienced C programmers. C is so filled with deadly pitfalls that it forces programmers to be constantly aware of multiple threats and unexpected inputs. When C programmers move on to other languages, these skills follow them. The result is solid code wherever they go.
It is ironic then that software environments designed for the educational market stress ease of use above all else (think Smalltalk, Pascal or Visual Whatever). These packages certainly make teaching easier and allows more students to complete the assignments. But it doesn't create paranoid programmers.
Oh yes, the Moo database browser works fine.