Add the ability to self check the database.

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