Rebased, debugged and fleshed out a tiny bit more, but still with
plenty of TODO notes and questions. I will talk about this idea at
PGCon, so I figured it'd help to have a patch that actually applies,
even if it doesn't work quite right yet. It's quite a large patch but
that's partly because it removes a lot of lines...