Leklund fix distinct for pg and json fixes #506