I have not worked through all this but at first glance I suspect: select distinct b.* from b ... is distinct from ... constitutes a double negative. What happens if you eliminate the first distinct?
pgsql-general by date:
Соглашаюсь с условиями обработки персональных данных