select * from
(
select ways.id as wayid,relations.id as relid,wlanduse.v::text as wlanduse,rlanduse.v::text as rlanduse,count(nodes) as nodes,source.v as source

from osm.current_ways ways

left join osm.current_way_nodes nodes on nodes.way_id=ways.id
left join osm.current_way_tags wlanduse on wlanduse.way_id=ways.id and wlanduse.k='landuse'
left join osm.current_way_tags source on source.way_id=ways.id and source.k='source'
left join osm.current_relation_members members on members.member_id=ways.id and members.member_role='outer'
left join osm.current_relations relations on relations.id=members.relation_id
left join osm.current_relation_tags rlanduse on rlanduse.relation_id=relations.id and rlanduse.k='landuse'
group by wayid,relid,wlanduse,rlanduse,source
) foo
where
relid is not NULL
    and (wlanduse = 'forest' or rlanduse = 'forest')
    -- and source='uhul:wms'
    and 'outer' not in (select member_role from osm.current_relation_members where member_type='Way' and member_role='outer' and member_id != foo.wayid and relation_id=foo.relid)
    and foo.wayid not in (select member_id from osm.current_relation_members where relation_id != foo.relid and member_type='Way' and member_id=foo.wayid)
    -- and relid not in (select relation_id from osm.current_relation_tags where relation_id=foo.relid and k != 'type' and v != 'multipolygon')
;
