Add the ability to self check the database.

Subclasses Postgres to prepare for write-wrapping for #1549
and to add self checks for #1180 and #1705.

Fixes #1180, #1705.