Add test coverage for the PostgreSQL storage backend

**WIP - DO NOT MERGE**

Add a test suite for the PostgreSQL storage backend and, as an added bonus, fix the bugs it has uncovered.
It doesn't run automatically, I catch up with it every few commits.