update database to latest upstream 2.2.1

I've let the upstream changes overwrite our own for the most part. Have
kept @rhafer's patch from 49b149e563f0054ad2eaef830e0df280d729a899 . The
changes to the postgresql provider are minimal. A lot of new
functionality was added which shouldn't break anything.