Rebased version of the patches of Mikko Tiihonen

This pull request replaces https://github.com/impossibl/pgjdbc-ng/pull/73 .

I've merged Mikko's changes in the latest devel branch.