osm2pgsql do this allready. (But it lacks support for relations composed of relations)